From ffdfd0fd0b0966b65e08e94e59f6d6e6cf685e30 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 3 Oct 2024 11:11:32 +1300
Subject: [PATCH 1/2] ci: Use ccache for headerscheck/cpluspluscheck.

Using stable pathnames for the generated files and turning on ccache
shaves about a minute and a half off typical runtime.
---
 .cirrus.tasks.yml                |  8 +++-----
 src/tools/pginclude/headerscheck | 16 +++++++++++++---
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/.cirrus.tasks.yml b/.cirrus.tasks.yml
index 90cb95c8681..1f67065816b 100644
--- a/.cirrus.tasks.yml
+++ b/.cirrus.tasks.yml
@@ -775,8 +775,6 @@ task:
   ###
   # Verify headerscheck / cpluspluscheck succeed
   #
-  # - Don't use ccache, the files are uncacheable, polluting ccache's
-  #   cache
   # - Use -fmax-errors, as particularly cpluspluscheck can be very verbose
   # - XXX have to disable ICU to avoid errors:
   #   https://postgr.es/m/20220323002024.f2g6tivduzrktgfa%40alap3.anarazel.de
@@ -787,11 +785,11 @@ task:
         ${LINUX_CONFIGURE_FEATURES} \
         --without-icu \
         --quiet \
-        CC="gcc" CXX"=g++" CLANG="clang-16"
+        CC="ccache gcc" CXX="ccache g++" CLANG="ccache clang-16"
       make -s -j${BUILD_JOBS} clean
-      time make -s headerscheck EXTRAFLAGS='-fmax-errors=10'
+      time make -s headerscheck EXTRAFLAGS='-fmax-errors=10' TMPDIR="headerscheck_tmp"
     headers_cpluspluscheck_script: |
-      time make -s cpluspluscheck EXTRAFLAGS='-fmax-errors=10'
+      time make -s cpluspluscheck EXTRAFLAGS='-fmax-errors=10' TMPDIR="cpluspluscheck_tmp"
 
   always:
     upload_caches: ccache
diff --git a/src/tools/pginclude/headerscheck b/src/tools/pginclude/headerscheck
index 3fc737d2cc1..b53a8648506 100755
--- a/src/tools/pginclude/headerscheck
+++ b/src/tools/pginclude/headerscheck
@@ -72,7 +72,14 @@ else
 fi
 
 # Create temp directory.
-tmp=`mktemp -d /tmp/$me.XXXXXX`
+if [ -z "$TMPDIR" ] ; then
+	tmp=`mktemp -d /tmp/$me.XXXXXX`
+else
+	# Use a directory name provided.  It will be removed at end.  A stable name
+	# is needed for ccache to be effective.
+	tmp="$TMPDIR"
+	mkdir -p "$tmp"
+fi
 
 trap "ret=$?; rm -rf $tmp; exit $ret" 0 1 2 3 15
 
@@ -81,6 +88,9 @@ exit_status=0
 # Scan all of src/ and contrib/ for header files.
 for f in `cd "$srcdir" && find src contrib -name '*.h' -print`
 do
+	# Help ccache by using a stable name.  Remove slashes and dots.
+	name=$(echo $f | tr "\/." "__")
+
 	# Ignore files that are unportable or intentionally not standalone.
 
 	# These files are platform-specific, and c.h will include the
@@ -227,7 +237,7 @@ do
 	    esac
 	    echo "#include \"$f\""
 	    $cplusplus && echo '};'
-	} >$tmp/test.$ext
+	} >$tmp/$name.$ext
 
 	# Some subdirectories need extra -I switches.
 	case "$f" in
@@ -249,7 +259,7 @@ do
 	if ! $COMPILER $COMPILER_FLAGS -I $builddir -I $srcdir \
 		-I $builddir/src/include -I $srcdir/src/include \
 		-I $builddir/src/interfaces/libpq -I $srcdir/src/interfaces/libpq \
-		$EXTRAINCLUDES $EXTRAFLAGS -c $tmp/test.$ext -o $tmp/test.o
+		$EXTRAINCLUDES $EXTRAFLAGS -c $tmp/$name.$ext -o $tmp/$name.o
 	then
 		exit_status=1
 	fi
-- 
2.46.1

