From: Harald Nordgren <hara…@gmail.com> Since 8d3a673147 (Makefile: precompile "git-compat-util.h", 2026-09-15), rebuilding Git with ccache is almost as slow as building it without. ccache refuses to cache a compilation that uses a precompiled header unless its "pch_defines" and "time_macros" sloppiness is enabled, so it runs the compiler for nearly every object file, even after a "make clean" in a tree it has already built. Both settings are safe for our build. The precompiled header is only pulled in with "-include" ahead of the source file, so no "#define" in a source file can come before it, and none of our sources use __DATE__, __TIME__ or __TIMESTAMP__. Enable them whenever we build with the precompiled header. On my machine, "make clean && make -j8" with a warm cache goes from about 100 seconds to under 30. Clang also records the modification times of the headers in the precompiled header. After switching to a branch that changes one of them and back again, ccache then misses for the precompiled header and for every object built with it. Build the precompiled header with "-fno-pch-timestamp" when the compiler accepts it. Clang uses the modification times to reject a precompiled header that is out of date, and make rebuilds it in that case anyway. On my machine, "make -j8" after such a round trip goes from about 140 seconds to under 20. Signed-off-by: Harald Nordgren <hara…@gmail.com> --- Makefile: let ccache cache objects built with the precompiled header With ccache, building after switching between two feature branches gets about 6 times faster. The first build of each branch gets about 25% slower while ccache fills its cache. Changes in v3: * Build the precompiled header with -fno-pch-timestamp when the compiler accepts it, so Clang builds also hit the cache after switching branches and back. Changes in v2: * Move the CCACHE_SLOPPINESS export from the precompiled header targets into the ifndef NO_PRECOMPILED_HEADER block. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2464%2FHaraldNordgren%2Fmakefile-ccache-pch-v3 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2464/HaraldNordgren/makefile-ccache-pch-v3 Pull-Request: https://github.com/git/git/pull/2464 Range-diff vs v2: 1: e61cca8cca ! 1: 2190ea49ec Makefile: let ccache cache objects built with the precompiled header @@ Commit message On my machine, "make clean && make -j8" with a warm cache goes from about 100 seconds to under 30. - With Clang, switching to a branch that changes one of the headers the - precompiled header includes and back again still misses the cache, - because Clang records the modification times of those headers in the - precompiled header. + Clang also records the modification times of the headers in the + precompiled header. After switching to a branch that changes one of + them and back again, ccache then misses for the precompiled header and + for every object built with it. Build the precompiled header with + "-fno-pch-timestamp" when the compiler accepts it. Clang uses the + modification times to reject a precompiled header that is out of date, + and make rebuilds it in that case anyway. On my machine, "make -j8" + after such a round trip goes from about 140 seconds to under 20. Signed-off-by: Harald Nordgren <hara…@gmail.com> @@ Makefile: PRECOMPILED_HEADER_GCH := $(addsuffix .gch,$(PRECOMPILED_HEADER)) ifndef NO_PRECOMPILED_HEADER PRECOMPILED_HEADER_USERS := $(filter-out $(COMPAT_OBJS) $(REFTABLE_OBJS) $(patsubst %.c,%.o,$(THIRD_PARTY_SOURCES)),$(OBJECTS)) +export CCACHE_SLOPPINESS += pch_defines,time_macros ++no_pch_timestamp_check = $(shell $(CC) -Xclang -fno-pch-timestamp \ ++ -c -x c /dev/null -o /dev/null 2>&1; \ ++ echo $$?) ++ifeq ($(no_pch_timestamp_check),0) ++PRECOMPILED_HEADER_CFLAGS = -Xclang -fno-pch-timestamp ++endif endif dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d) +@@ Makefile: compdb_args = + endif + + $(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) +- $(QUIET_CC)$(CC) -o $@ -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $< ++ $(QUIET_CC)$(CC) -o $@ -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(PRECOMPILED_HEADER_CFLAGS) $< + + $(PRECOMPILED_HEADER_USERS): %.o: %.c $(PRECOMPILED_HEADER_GCH) GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $*.o -c -include $(PRECOMPILED_HEADER) -Winvalid-pch $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< Makefile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a96be506b5..96ddecaaaa 100644 --- a/Makefile +++ b/Makefile @@ -2903,6 +2903,13 @@ PRECOMPILED_HEADER_GCH := $(addsuffix .gch,$(PRECOMPILED_HEADER)) ifndef NO_PRECOMPILED_HEADER PRECOMPILED_HEADER_USERS := $(filter-out $(COMPAT_OBJS) $(REFTABLE_OBJS) $(patsubst %.c,%.o,$(THIRD_PARTY_SOURCES)),$(OBJECTS)) +export CCACHE_SLOPPINESS += pch_defines,time_macros +no_pch_timestamp_check = $(shell $(CC) -Xclang -fno-pch-timestamp \ + -c -x c /dev/null -o /dev/null 2>&1; \ + echo $$?) +ifeq ($(no_pch_timestamp_check),0) +PRECOMPILED_HEADER_CFLAGS = -Xclang -fno-pch-timestamp +endif endif dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d) @@ -2941,7 +2948,7 @@ compdb_args = endif $(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) - $(QUIET_CC)$(CC) -o $@ -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $< + $(QUIET_CC)$(CC) -o $@ -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(PRECOMPILED_HEADER_CFLAGS) $< $(PRECOMPILED_HEADER_USERS): %.o: %.c $(PRECOMPILED_HEADER_GCH) GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) $(QUIET_CC)$(CC) -o $*.o -c -include $(PRECOMPILED_HEADER) -Winvalid-pch $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< base-commit: 37b3475fb530c14109211faca33716a173db61ed
-- gitgitgadget