make: precompile "git-compat-util.h"
cover make: precompile "git-compat-util.h"
Update our Makefile to make use of the recently introduced precompiled header to make builds faster (on my setup by about 35%).
SZEDER Gábor (4):
Makefile: remove XDIFF_OBJS initialization
cmake: remove any "$(*_OBJS)" variables when parsing Makefile for
sources
Makefile: reintroduce REFTABLE_OBJS
Makefile: precompile "git-compat-util.h"
.gitignore | 1 + Makefile | 59 +++++++++++++++++++---------- contrib/buildsystems/CMakeLists.txt | 6 ++- 3 files changed, 45 insertions(+), 21 deletions(-)
Signature · 1 line
2.55.0.1193.g1b994e35de
PATCH 1/4 Makefile: remove XDIFF_OBJS initialization
Object files under 'xlib/' used to be listed in the XDIFF_OBJS Makefile variable so we could build a static library from them. This static library was removed in cf680cdb95 (make: delete XDIFF_LIB, add xdiff to LIB_OBJS, 2025-10-02), along with filling XDIFF_OBJS with object files.
But the initial empty initalization of XDIFF_OBJS remained, so remove it now.
--- Makefile | 1 - 1 file changed, 1 deletion(-)
Makefile +0 -1
@@ -695,7 +695,6 @@ BUILTIN_OBJS = BUILT_INS = COMPAT_CFLAGS = COMPAT_OBJS = -XDIFF_OBJS = GENERATED_H = EXTRA_CPPFLAGS = FUZZ_OBJS =
Signature · 1 line
2.55.0.1193.g1b994e35de
PATCH 2/4 cmake: remove any "$(*_OBJS)" variables when parsing Makefile for sources
In reply to SZEDER GáborTo get various lists of files, CMake parses our Makefile looking for lines matching e.g. "list_var += ...". In case of LIB_OBJS this picks up the line "LIB_OBJS += $(COMPAT_OBJS)" as well, so the parsing macro has a specific instruction to remove "$(COMPAT_OBJS)" from the resulting list.
Currently this is the only such Makefile variable to be removed from the list, but the next patches will (re)introduce more variables containing lists of object files, so let's generalize that removing instruction to remove any "$(*_OBJS)" Makefile variable as well.
Note that we can't make the pattern matching the Makefile variable too general, e.g. to match any "$(VARIABLE)", because some lines of our Makefile do contain variables as directory prefixes, e.g. "UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o", and we must definitely keep those.
---
I rarely do CMake, so it's quite possible that this is not the most straightforward or idiomatic approach.
contrib/buildsystems/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/buildsystems/CMakeLists.txt +1 -1
@@ -102,7 +102,7 @@ project(git macro(parse_makefile_for_sources list_var makefile regex) file(STRINGS ${makefile} ${list_var} REGEX "^${regex} \\+=(.*)") string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}}) - string(REPLACE "$(COMPAT_OBJS)" "" ${list_var} ${${list_var}}) #remove "$(COMPAT_OBJS)" This is only for libgit. + string(REGEX REPLACE "\\$\\([^)]*_OBJS\\)" "" ${list_var} ${${list_var}}) # remove any "$(*_OBJS)" variables string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces string(REPLACE ".o" ".c;" ${list_var} ${${list_var}}) #change .o to .c, ; is for converting the string into a list list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
Signature · 1 line
2.55.0.1193.g1b994e35de
PATCH 3/4 Makefile: reintroduce REFTABLE_OBJS
In reply to SZEDER GáborObject files under "reftable/" used to be listed in the REFTABLE_OBJS Makefile variable so we could build a static library from them. This static library was removed in f3b4c89d59 (make: delete REFTABLE_LIB, add reftable to LIB_OBJS, 2025-10-02), along with filling REFTALBE_OBJS with object files.
However, the reftable source files are kind of special, because the reftable implementation is supposed to be easily includable in other projects. Therefore, the reftable source files don't include "git-compat-util.h", with the sole exception of the purposefully project-specific "reftable/system.c". Consequently, they shouldn't be compiled with our precompiled header, as it does include "git-compat-util.h".
Resurrect listing object files under "reftable/" in REFTABLE_OBJS (but not the static library), so in the next commit we'll be able to easily filter them out and keep building them the old way, without the precompiled header.
--- Makefile | 32 ++++++++++++++++------------- contrib/buildsystems/CMakeLists.txt | 4 ++++ 2 files changed, 22 insertions(+), 14 deletions(-)
Makefile +18 -14
@@ -707,6 +707,7 @@ OBJECTS = OTHER_PROGRAMS = PROGRAM_OBJS = PROGRAMS = +REFTABLE_OBJS = RUST_SOURCES = EXCLUDED_PROGRAMS = SCRIPT_PERL =
@@ -1284,20 +1285,6 @@ LIB_OBJS += refs/iterator.o LIB_OBJS += refs/packed-backend.o LIB_OBJS += refs/ref-cache.o LIB_OBJS += refspec.o -LIB_OBJS += reftable/basics.o -LIB_OBJS += reftable/block.o -LIB_OBJS += reftable/blocksource.o -LIB_OBJS += reftable/error.o -LIB_OBJS += reftable/fsck.o -LIB_OBJS += reftable/iter.o -LIB_OBJS += reftable/merged.o -LIB_OBJS += reftable/pq.o -LIB_OBJS += reftable/record.o -LIB_OBJS += reftable/stack.o -LIB_OBJS += reftable/system.o -LIB_OBJS += reftable/table.o -LIB_OBJS += reftable/tree.o -LIB_OBJS += reftable/writer.o LIB_OBJS += remote.o LIB_OBJS += repack.o LIB_OBJS += repack-cruft.o
@@ -1386,6 +1373,23 @@ LIB_OBJS += xdiff/xpatience.o LIB_OBJS += xdiff/xprepare.o LIB_OBJS += xdiff/xutils.o +REFTABLE_OBJS += reftable/basics.o +REFTABLE_OBJS += reftable/block.o +REFTABLE_OBJS += reftable/blocksource.o +REFTABLE_OBJS += reftable/error.o +REFTABLE_OBJS += reftable/fsck.o +REFTABLE_OBJS += reftable/iter.o +REFTABLE_OBJS += reftable/merged.o +REFTABLE_OBJS += reftable/pq.o +REFTABLE_OBJS += reftable/record.o +REFTABLE_OBJS += reftable/stack.o +REFTABLE_OBJS += reftable/system.o +REFTABLE_OBJS += reftable/table.o +REFTABLE_OBJS += reftable/tree.o +REFTABLE_OBJS += reftable/writer.o + +LIB_OBJS += $(REFTABLE_OBJS) + BUILTIN_OBJS += builtin/add.o BUILTIN_OBJS += builtin/am.o BUILTIN_OBJS += builtin/annotate.o
contrib/buildsystems/CMakeLists.txt +4 -0
@@ -668,6 +668,10 @@ include_directories(${CMAKE_BINARY_DIR}) #libgit parse_makefile_for_sources(libgit_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "LIB_OBJS") +#reftable +parse_makefile_for_sources(reftable_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "REFTABLE_OBJS") +list(APPEND libgit_SOURCES ${reftable_SOURCES}) + list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/") list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
Signature · 1 line
2.55.0.1193.g1b994e35de
Re: [PATCH 3/4] Makefile: reintroduce REFTABLE_OBJS
Quoted 6 lines · SZEDER Gábor
Object files under "reftable/" used to be listed in the
REFTABLE_OBJSMakefilevariable so we could build a static library from them. This static library was removed inf3b4c89d59(make: deleteREFTABLE_LIB, add reftable toLIB_OBJS, 2025-10-02), along with fillingREFTALBE_OBJSwith object files.
Quoted 7 lines
However, the reftable source files are kind of special, because the reftable implementation is supposed to be easily includable in other projects. Therefore, the reftable source files don't include "
git-compat-util.h", with the sole exception of the purposefully project-specific "reftable/system.c". Consequently, they shouldn't be compiled with our precompiled header, as it does include "git-compat-util.h".
This is the first mention of "our precompiled header" in this series, and the first hint that "our precompiled header" would include "git-compat-util.h". It may probably give us a better organization to state it upfront at the beginning of the proposed log message of this commit. It is of secondary importance that once in the past we used to have REFTABLE_OBJS Makefile variable that listed some files (but for completely different purposes). How about explaining it along this line...
The ultimate endgame of this series is to use the precompiled header facility to speed up compilation, and the plan is to have Git specific headers including git-compat-util.h precompiled.
The reftable sources are largely designed to be independent from the Git source proper, and except for reftable/system.c they do not include <git-compat-util.h>.
Move the object files in the subsystem from the LIB_OBJS to the REFTABLE_OBJS Makefile variable, so that we can use LIB_OBJS as list of files compiled with precompiled header files and others without
... or something like that, perhaps?
PATCH 4/4 Makefile: precompile "git-compat-util.h"
In reply to SZEDER GáborThis patch follows the idea of 671df48df8 (meson: precompile "git-compat-util.h", 2026-03-19) to make it faster to build Git using "make". The notable differences are the boilerplate needed to wire up the precompiled header with "make", and the selection of object files that are built using the precompiled header:
- Add a new rule to precompile "tools/precompiled.h" into "tools/precompiled.h.gch".
This rule and its dependencies are basically the same as our existing rule and its dependencies for compiling object files from our source files and, except that in this rule we don't use the EXTRA_CPPFLAGS target-specific variable. This is partly because any compiler flags in that target-specific variable can't possibly make sense for the project-wide "git-compat-util.h", and partly because it could run afoul of "make"'s quirks with target-specific variables and GCC's "-Winvalid-pch" compiler option [1].
Our Makefile always writes object files next to the source files they were compiled from. Since a precompiled header is in many ways similar to an object file, let's follow suit and write it next to the header file it was created from as well. 671df48df8 doesn't mention why "precompiled.h" was placed in "tools/".
Use the ".gch" suffix for the precompiled header file, because that's the only suffix GCC looks for when searching for a precompiled header. Clang apparently looks for both ".pch" and ".gch" suffixes. If we ever encounter a compiler which only supports a different precompiled header suffix, then we might make the suffix configurable via a Makefile knob; but until then it remains hard-coded for the sake of simplicity.
- Declare the precompiled header's dependencies, so it gets rebuilt when any of our header files it includes is modified.
When using computed header dependencies, then the compiler can generate the Makefile snippet containing the header files inlcuded by the precompiled header, just like when compiling a C source file into an object file.
When not using computed header dependencies, then make the precompiled header depend on the same header files that our object files depend on, i.e. those listed in LIB_H and GENERATED_H. This is not ideal, because the precompiled header will be rebuilt even when a header file it doesn't actually depend on changes; but in that case all object files will be rebuilt as well anyway.
- List the object files that are built using the precompiled header in the PRECOMPILED_HEADER_USERS variable:
- The precompiled header should not change what actually gets compiled. Therefore, use the precompiled header only when compiling source files that start with including "git-compat-util.h" (directly or indirectly, e.g. via "builtin.h"), or its inclusion is only preceeded by #define directives that don't influence "git-compat-util.h" between its include guards [2] (currently DISABLE_SIGN_COMPARE_WARNINGS, USE_THE_REPOSITORY_VARIABLE or GIT_TEST_PROGRESS_ONLY). [3]
Several (but not all) object or source files listed in the COMPAT_OBJS, REFTABLE_OBJS and THIRD_PARTY_SOURCES variables don't include "git-compat-util.h", therefore, for the sake of simplicity, none of the files listed in these variables are built with the precompiled header. [4]
Since 671df48df8 the Meson build uses the precompiled header even when compiling those reftable source files that don't include "git-compat-util.h" at all, although this change is not mentioned in the commit message.
- But other than that, use the precompiled header when compiling all other source files, including e.g. source files for standalone executables (e.g. "daemon.c" for "git-daemon"), or those in the directories "t/helper/" and "t/unit-tests/" as well.
The Meson build, however, only uses the precompiled header for compiling objects in "libgit_sources" and "builtin_sources", despite 671df48df8 giving the impression that the precompiled header is included in all compilation units.
- In short, PRECOMPILED_HEADER_USERS contains all object files listed in OBJECTS, except those that are listed in COMPAT_OBJS, REFTABLE_OBJS or THIRD_PARTY_SOURCES as well.
- Add a new rule to build object files listed in PRECOMPILED_HEADER_USERS with the precompiled header.
This rule and its dependencies are basically the same as our existing rule and its dependencies compiling object files from our source files, except:
- This rule depends on the precompiled header as well, to make sure that it's built before it is used.
- Use the "-include tools/precompiled.h" option to make the precompiled header the first header in the compilation unit, because only then is it used to speed things up.
- Use the "-Winvalid-pch" option to catch any issues when the precompiled header is present but can't be used for whatever reason. While being unable to use the precompiled header would normally only result in a slower build, it is probably better that developers are made aware that simething is not quite right. Meson uses this option as well when compiling a source file using the precompiled header.
Note, that in case of such an issue with "-Winvalid-pch" we would get a warning for each object file that is build using the precompiled header, but together with "-Werror" (e.g. with DEVELOPER=1) it would fail the build.
Object files not listed in PRECOMPILED_HEADER_USERS are built with the existing (and unchanged) rule for object files.
- This way the precompiled header is compiled only once during the whole build process.
The Meson build, however, currently compiles the precompiled header twice: once for "libgit_sources" and once for "builtin_sources", despite 671df48df8 giving the impression that it's compiled only once. And, by the looks of it, it would have to be compiled once more for each new library or executable where we declare the use of the precompiled header.
- Add the Makefile knob NO_PRECOMPILED_HEADER to make it possible to build without using the precompiled header for testing purposes, or for any compiler that might be out there that doesn't support the "-Winvalid-pch" option.
Note, that when this knob is set then we merely leave PRECOMPILED_HEADER_USERS empty: this way nothing is built with the precompiled header, but "make clean NO_PRECOMPILED_HEADER=1" would still remove a leftover precompiled header file.
With this patch series on top of v2.55.0 I got the following build time improvement:
Benchmark 1: make -j12 (rev = v2.55.0)
Time (mean ± σ): 29.414 s ± 0.031 s [User: 254.786 s, System: 47.447 s]
Range (min … max): 29.370 s … 29.470 s 10 runsBenchmark 2: make -j12 (rev = precompile)
Time (mean ± σ): 21.725 s ± 0.047 s [User: 186.047 s, System: 35.109 s]
Range (min … max): 21.643 s … 21.768 s 10 runs Summary
'make -j12 (rev = precompile)' ran
1.35 ± 0.00 times faster than 'make -j12 (rev = v2.55.0)'
[1] "make" applies target-specific variables to the first target's dependencies as well. So, if the rule creating the precompiled header included $(EXTRA_CPPFLAGS) as well, then we could get this:
$ make -s clean
$ make V=1 git
gcc -o tools/precompiled.h.gch -c -MF [...] -DSHELL_PATH='"/bin/sh"' '-DGIT_HTML_PATH="share/doc/git-doc"' '-DGIT_MAN_PATH="share/man"' '-DGIT_INFO_PATH="share/info"' tools/precompiled.h gcc -o git.o -c -include tools/precompiled.h -Winvalid-pch -MF [...] -DSHELL_PATH='"/bin/sh"' '-DGIT_HTML_PATH="share/doc/git-doc"' '-DGIT_MAN_PATH="share/man"' '-DGIT_INFO_PATH="share/info"' git.c gcc -o builtin/add.o -c -include tools/precompiled.h -Winvalid-pch -MF [...] -DSHELL_PATH='"/bin/sh"' builtin/add.c
cc1: error: ./tools/precompiled.h.gch: not used because `GIT_MAN_PATH' not defined [-Werror=invalid-pch]
cc1: all warnings being treated as errors
make: *** [Makefile:2940: builtin/add.o] Error 1
So in this case "git.o" is the first object file to be build, and since it has a target-specific EXTRA_CPPFLAGS and since it depends on the precompiled header, the precompiled header is built with the same EXTRA_CPPFLAGS as well. But then "builtin/add.o" is built with empty EXTRA_CPPFLAGS, and GCC's sanity checks for the -Winvalid-pch option complain. Clang apparently doesn't consider this to be an issue, and builds "git" just fine.
Note, however, that if the first object file to be built is not "git.o" (or one of the other object files with target-specific EXTRA_CPPFLAGS), which is usually the case with e.g. "make all", then the precompiled header is built with empty EXTRA_CPPFLAGS, and then all is well. Weird. We already had issues with "make" and target-specific variables in the past, see a673cfede6 (Makefile: Fix occasional GIT-CFLAGS breakage, 2010-03-19). The Meson build doesn't use target-specific variables, and the whole project needs to be rebuilt when e.g. "mandir" is changed, although this is not mentioned in 904339edbd (Introduce support for the Meson build system, 2024-12-06).
[2] See baa61e46da (git-compat-util.h: move warning infra to prepare
for PCHs, 2026-03-19).
[3] This can be verified by simply scanning through the output of:
{
printf '%s\n' 'print-%:'
printf '\t%s\n' '@printf "%s\n" $($*)'
} >/tmp/printvars.mak
for f in $(make -f /tmp/printvars.mak -f Makefile print-PRECOMPILED_HEADER_USERS)
do
f="${f%o}c"
sed -n -E -e "s%# *(define|include).*%& $f%p" -e '/# *include/q' "$f"
done | sort[4] "make"'s flexibility would allow us to separately list those object files that don't include "git-compat-util.h", but then on my (I assume fairly typical) Linux box the number of object files built using the precompiled header increases only by 9 (from 536 to 545), which reduces the build time by about 1% (0.2s). Therefore, I don't think it's worth the churn.
--- .gitignore | 1 + Makefile | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-)
.gitignore +1 -0
@@ -233,6 +233,7 @@ /.vscode/ /tags /TAGS +/tools/precompiled.h.gch /cscope* /compile_commands.json /.cache/
Makefile +21 -5
@@ -321,6 +321,9 @@ include shared.mak # dependency rules. The default is "auto", which means to use computed header # dependencies if your compiler is detected to support it. # +# Define NO_PRECOMPILED_HEADER if you want to build Git without precompiling +# "git-compat-util.h". +# # Define NATIVE_CRLF if your platform uses CRLF for line endings. # # Define GIT_USER_AGENT if you want to change how git identifies itself during
@@ -2905,8 +2908,15 @@ endif .PHONY: objects objects: $(OBJECTS) -dep_files := $(foreach f,$(OBJECTS),$(dir $f).depend/$(notdir $f).d) -dep_dirs := $(addsuffix .depend,$(sort $(dir $(OBJECTS)))) +PRECOMPILED_HEADER := tools/precompiled.h +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)) +endif + +dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d) +dep_dirs := $(addsuffix .depend,$(sort $(dir $(OBJECTS) $(PRECOMPILED_HEADER_GCH)))) ifeq ($(uname_S),Darwin) dep_dirs += $(addsuffix .depend,$(sort $(dir contrib/credential/osxkeychain/git-credential-osxkeychain.o)))
@@ -2940,7 +2950,13 @@ missing_compdb_dir = compdb_args = endif -$(OBJECTS): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) +$(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $@ -c $(dep_args) $(compdb_args) $(ALL_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) $< + +$(filter-out $(PRECOMPILED_HEADER_USERS),$(OBJECTS)): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< %.s: %.c GIT-CFLAGS FORCE
@@ -2954,7 +2970,7 @@ ifneq ($(dep_files_present),) include $(dep_files_present) endif else -$(OBJECTS): $(LIB_H) $(GENERATED_H) +$(OBJECTS) $(PRECOMPILED_HEADER_GCH): $(LIB_H) $(GENERATED_H) endif ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
@@ -3908,7 +3924,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) GIT-TEST-SUITES $(RM) po/git.pot po/git-core.pot $(RM) git.rc git.res - $(RM) $(OBJECTS) + $(RM) $(OBJECTS) $(PRECOMPILED_HEADER_GCH) $(RM) headless-git.o $(RM) $(LIB_FILE) $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS)
Signature · 1 line
2.55.0.1193.g1b994e35de
Re: [PATCH 4/4] Makefile: precompile "git-compat-util.h"
Quoted 22 lines · SZEDER Gábor
- List the object files that are built using the precompiled header in the
PRECOMPILED_HEADER_USERSvariable:- The precompiled header should not change what actually gets compiled. Therefore, use the precompiled header only when compiling source files that start with including "
git-compat-util.h" (directly or indirectly, e.g. via "builtin.h"), or its inclusion is only preceeded by #define directives that don't influence "git-compat-util.h" between its include guards [2] (currentlyDISABLE_SIGN_COMPARE_WARNINGS,USE_THE_REPOSITORY_VARIABLEorGIT_TEST_PROGRESS_ONLY). [3]Several (but not all) object or source files listed in the
COMPAT_OBJS,REFTABLE_OBJSandTHIRD_PARTY_SOURCESvariables don't include "git-compat-util.h", therefore, for the sake of simplicity, none of the files listed in these variables are built with the precompiled header. [4]- In short,
PRECOMPILED_HEADER_USERScontains all object files listed in OBJECTS, except those that are listed inCOMPAT_OBJS,REFTABLE_OBJSorTHIRD_PARTY_SOURCESas well.
Quoted 6 lines
[4] "make"'s flexibility would allow us to separately list those object files that don't include "
git-compat-util.h", but then on my (I assume fairly typical) Linux box the number of object files built using the precompiled header increases only by 9 (from 536 to 545), which reduces the build time by about 1% (0.2s). Therefore, I don't think it's worth the churn.
For reference, a change to do that would look like that patch below, but, as mentioned above, the benefit is rather small:
Benchmark 1: make -j12 (rev = precompile)
Time (mean ± σ): 21.747 s ± 0.048 s [User: 186.120 s, System: 35.032 s]
Range (min … max): 21.650 s … 21.802 s 10 runsBenchmark 2: make -j12 (rev = precompile-all)
Time (mean ± σ): 21.556 s ± 0.062 s [User: 184.584 s, System: 35.016 s]
Range (min … max): 21.477 s … 21.675 s 10 runs Summary
'make -j12 (rev = precompile-all)' ran
1.01 ± 0.00 times faster than 'make -j12 (rev = precompile)'
--- >8 ---
Makefile +34 -26
@@ -698,6 +698,7 @@ BUILTIN_OBJS = BUILT_INS = COMPAT_CFLAGS = COMPAT_OBJS = +COMPAT_NOPCH_OBJS = GENERATED_H = EXTRA_CPPFLAGS = FUZZ_OBJS =
@@ -711,6 +712,7 @@ OTHER_PROGRAMS = PROGRAM_OBJS = PROGRAMS = REFTABLE_OBJS = +REFTABLE_NOPCH_OBJS = RUST_SOURCES = EXCLUDED_PROGRAMS = SCRIPT_PERL =
@@ -721,6 +723,7 @@ TEST_BUILTINS_OBJS = TEST_OBJS = TEST_PROGRAMS_NEED_X = THIRD_PARTY_SOURCES = +THIRD_PARTY_NOPCH_SOURCES = UNIT_TEST_PROGRAMS = UNIT_TEST_DIR = t/unit-tests UNIT_TEST_BIN = $(UNIT_TEST_DIR)/bin
@@ -1376,20 +1379,22 @@ LIB_OBJS += xdiff/xpatience.o LIB_OBJS += xdiff/xprepare.o LIB_OBJS += xdiff/xutils.o -REFTABLE_OBJS += reftable/basics.o -REFTABLE_OBJS += reftable/block.o -REFTABLE_OBJS += reftable/blocksource.o -REFTABLE_OBJS += reftable/error.o -REFTABLE_OBJS += reftable/fsck.o -REFTABLE_OBJS += reftable/iter.o -REFTABLE_OBJS += reftable/merged.o -REFTABLE_OBJS += reftable/pq.o -REFTABLE_OBJS += reftable/record.o -REFTABLE_OBJS += reftable/stack.o REFTABLE_OBJS += reftable/system.o -REFTABLE_OBJS += reftable/table.o -REFTABLE_OBJS += reftable/tree.o -REFTABLE_OBJS += reftable/writer.o +REFTABLE_NOPCH_OBJS += reftable/basics.o +REFTABLE_NOPCH_OBJS += reftable/block.o +REFTABLE_NOPCH_OBJS += reftable/blocksource.o +REFTABLE_NOPCH_OBJS += reftable/error.o +REFTABLE_NOPCH_OBJS += reftable/fsck.o +REFTABLE_NOPCH_OBJS += reftable/iter.o +REFTABLE_NOPCH_OBJS += reftable/merged.o +REFTABLE_NOPCH_OBJS += reftable/pq.o +REFTABLE_NOPCH_OBJS += reftable/record.o +REFTABLE_NOPCH_OBJS += reftable/stack.o +REFTABLE_NOPCH_OBJS += reftable/table.o +REFTABLE_NOPCH_OBJS += reftable/tree.o +REFTABLE_NOPCH_OBJS += reftable/writer.o + +REFTABLE_OBJS += $(REFTABLE_NOPCH_OBJS) LIB_OBJS += $(REFTABLE_OBJS)
@@ -1535,11 +1540,13 @@ THIRD_PARTY_SOURCES += compat/inet_ntop.c THIRD_PARTY_SOURCES += compat/inet_pton.c THIRD_PARTY_SOURCES += compat/obstack.% THIRD_PARTY_SOURCES += compat/poll/% -THIRD_PARTY_SOURCES += compat/regex/% -THIRD_PARTY_SOURCES += sha1collisiondetection/% -THIRD_PARTY_SOURCES += sha1dc/% -THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/% -THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% +THIRD_PARTY_NOPCH_SOURCES += compat/regex/% +THIRD_PARTY_NOPCH_SOURCES += sha1collisiondetection/% +THIRD_PARTY_NOPCH_SOURCES += sha1dc/% +THIRD_PARTY_NOPCH_SOURCES += $(UNIT_TEST_DIR)/clar/% +THIRD_PARTY_NOPCH_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% + +THIRD_PARTY_SOURCES += $(THIRD_PARTY_NOPCH_SOURCES) CLAR_TEST_SUITES += u-ctype CLAR_TEST_SUITES += u-dir
@@ -1959,7 +1966,7 @@ ifdef SNPRINTF_RETURNS_BOGUS endif ifdef FREAD_READS_DIRECTORIES COMPAT_CFLAGS += -DFREAD_READS_DIRECTORIES - COMPAT_OBJS += compat/fopen.o + COMPAT_NOPCH_OBJS += compat/fopen.o endif ifdef OPEN_RETURNS_EINTR COMPAT_CFLAGS += -DOPEN_RETURNS_EINTR
@@ -2063,7 +2070,7 @@ ifdef NO_TRUSTABLE_FILEMODE endif ifdef NEEDS_MODE_TRANSLATION COMPAT_CFLAGS += -DNEEDS_MODE_TRANSLATION - COMPAT_OBJS += compat/stat.o + COMPAT_NOPCH_OBJS += compat/stat.o endif ifdef NO_IPV6 BASIC_CFLAGS += -DNO_IPV6
@@ -2229,7 +2236,7 @@ ifdef SHA1_MAX_BLOCK_SIZE endif ifdef NO_HSTRERROR COMPAT_CFLAGS += -DNO_HSTRERROR - COMPAT_OBJS += compat/hstrerror.o + COMPAT_NOPCH_OBJS += compat/hstrerror.o endif ifdef NO_MEMMEM COMPAT_CFLAGS += -DNO_MEMMEM
@@ -2282,7 +2289,7 @@ ifdef UNRELIABLE_FSTAT endif ifdef NO_REGEX COMPAT_CFLAGS += -Icompat/regex - COMPAT_OBJS += compat/regex/regex.o + COMPAT_NOPCH_OBJS += compat/regex/regex.o else ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS COMPAT_CFLAGS += -DUSE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
@@ -2387,17 +2394,17 @@ endif ifdef FILENO_IS_A_MACRO COMPAT_CFLAGS += -DFILENO_IS_A_MACRO - COMPAT_OBJS += compat/fileno.o + COMPAT_NOPCH_OBJS += compat/fileno.o endif ifdef NEED_ACCESS_ROOT_HANDLER COMPAT_CFLAGS += -DNEED_ACCESS_ROOT_HANDLER - COMPAT_OBJS += compat/access.o + COMPAT_NOPCH_OBJS += compat/access.o endif ifdef FSMONITOR_DAEMON_BACKEND COMPAT_CFLAGS += -DHAVE_FSMONITOR_DAEMON_BACKEND - COMPAT_OBJS += compat/fsmonitor/fsm-listen-$(FSMONITOR_DAEMON_BACKEND).o + COMPAT_NOPCH_OBJS += compat/fsmonitor/fsm-listen-$(FSMONITOR_DAEMON_BACKEND).o COMPAT_OBJS += compat/fsmonitor/fsm-health-$(FSMONITOR_DAEMON_BACKEND).o endif
@@ -2531,6 +2538,7 @@ endif LIBS = $(filter-out %.o, $(GITLIBS)) $(EXTLIBS) BASIC_CFLAGS += $(COMPAT_CFLAGS) +COMPAT_OBJS += $(COMPAT_NOPCH_OBJS) LIB_OBJS += $(COMPAT_OBJS) # Quote for C
@@ -2912,7 +2920,7 @@ PRECOMPILED_HEADER := tools/precompiled.h 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)) +PRECOMPILED_HEADER_USERS := $(filter-out $(COMPAT_NOPCH_OBJS) $(REFTABLE_NOPCH_OBJS) $(patsubst %.c,%.o,$(THIRD_PARTY_NOPCH_SOURCES)),$(OBJECTS)) endif dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d)
config.mak.uname +5 -3
@@ -152,7 +152,7 @@ ifeq ($(uname_S),Darwin) NO_MEMMEM = YesPlease USE_ST_TIMESPEC = YesPlease HAVE_DEV_TTY = YesPlease - COMPAT_OBJS += compat/precompose_utf8.o + COMPAT_NOPCH_OBJS += compat/precompose_utf8.o BASIC_CFLAGS += -DPRECOMPOSE_UNICODE BASIC_CFLAGS += -DPROTECT_HFS_DEFAULT=1 HAVE_BSD_SYSCTL = YesPlease
@@ -535,7 +535,8 @@ endif AR = compat/vcbuild/scripts/lib.pl CFLAGS = BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE - COMPAT_OBJS = compat/msvc.o compat/winansi.o \ + COMPAT_NOPCH_OBJS = compat/winansi.o + COMPAT_OBJS = compat/msvc.o \ compat/win32/flush.o \ compat/win32/path-utils.o \ compat/win32/pthread.o compat/win32/syslog.o \
@@ -735,7 +736,8 @@ ifeq ($(uname_S),MINGW) BASIC_LDFLAGS += -municode COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32 COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" - COMPAT_OBJS += compat/mingw.o compat/winansi.o \ + COMPAT_NOPCH_OBJS += compat/winansi.o + COMPAT_OBJS += compat/mingw.o \ compat/win32/trace2_win32_process_info.o \ compat/win32/flush.o \ compat/win32/path-utils.o \
contrib/buildsystems/CMakeLists.txt +2 -2
@@ -101,7 +101,7 @@ project(git #macros for parsing the Makefile for sources and scripts macro(parse_makefile_for_sources list_var makefile regex) file(STRINGS ${makefile} ${list_var} REGEX "^${regex} \\+=(.*)") - string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}}) + string(REGEX REPLACE "${regex} \\+=" "" ${list_var} ${${list_var}}) string(REGEX REPLACE "\\$\\([^)]*_OBJS\\)" "" ${list_var} ${${list_var}}) # remove any "$(*_OBJS)" variables string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces string(REPLACE ".o" ".c;" ${list_var} ${${list_var}}) #change .o to .c, ; is for converting the string into a list
@@ -669,7 +669,7 @@ include_directories(${CMAKE_BINARY_DIR}) parse_makefile_for_sources(libgit_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "LIB_OBJS") #reftable -parse_makefile_for_sources(reftable_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "REFTABLE_OBJS") +parse_makefile_for_sources(reftable_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "REFTABLE_(|NOPCH_)OBJS") list(APPEND libgit_SOURCES ${reftable_SOURCES}) list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
Signature · 1 line
2.55.0.1193.g1b994e35de
v2 cover make: precompile "git-compat-util.h"
In reply to SZEDER GáborUpdate our Makefile to make use of the recently introduced precompiled header to make builds faster (on my setup by about 35%).
Changes since v1:
- Update the commit message of patch 3/4 per Junio's suggestion.
SZEDER Gábor (4):
Makefile: remove XDIFF_OBJS initialization
cmake: remove any "$(*_OBJS)" variables when parsing Makefile for
sources
Makefile: reintroduce REFTABLE_OBJS
Makefile: precompile "git-compat-util.h"
.gitignore | 1 + Makefile | 59 +++++++++++++++++++---------- contrib/buildsystems/CMakeLists.txt | 6 ++- 3 files changed, 45 insertions(+), 21 deletions(-)
Range-diff against v1:
1: d52a1476d1 = 1: d52a1476d1 Makefile: remove XDIFF_OBJS initialization 2: 5d60181efd = 2: 5d60181efd cmake: remove any "$(*_OBJS)" variables when parsing Makefile for sources 3: 0296c6cffc ! 3: cfc77c0b2c Makefile: reintroduce REFTABLE_OBJS
@@ Metadata
## Commit message ##
Makefile: reintroduce REFTABLE_OBJS
- Object files under "reftable/" used to be listed in the REFTABLE_OBJS
- Makefile variable so we could build a static library from them. This
- static library was removed in f3b4c89d59 (make: delete REFTABLE_LIB,
- add reftable to LIB_OBJS, 2025-10-02), along with filling
- REFTALBE_OBJS with object files.
+ In the next commit we are about to precompile "git-compat-util.h" with
+ "make" to reduce build times. But using the precompiled header should
+ not change what actually gets compiled, therefore a source file can
+ only be compiled using the precompiled header if the first included
+ header file is "git-compat-util.h".
- However, the reftable source files are kind of special, because the
- reftable implementation is supposed to be easily includable in other
- projects. Therefore, the reftable source files don't include
+ The reftable source files are kind of special, because the reftable
+ implementation is supposed to be easily includable in other projects.
+ Therefore, the reftable source files don't include
"git-compat-util.h", with the sole exception of the purposefully project-specific "reftable/system.c". Consequently, they shouldn't be
- compiled with our precompiled header, as it does include
- "git-compat-util.h".
+ compiled with our precompiled header.
+
+ List object files under "reftable" in the REFTABLE_OBJS Makefile
+ variable, so in the next commit we'll be able to easily filter them
+ out and keep building them the old way, without the precompiled
+ header.
- Resurrect listing object files under "reftable/" in REFTABLE_OBJS (but
- not the static library), so in the next commit we'll be able to easily
- filter them out and keep building them the old way, without the
- precompiled header.
+ Note that object files under "reftable/" used to be listed in
+ REFTABLE_OBJS so we could build a static library from them. This
+ static library was removed in f3b4c89d59 (make: delete REFTABLE_LIB,
+ add reftable to LIB_OBJS, 2025-10-02), along with filling
+ REFTALBE_OBJS with object files. This change essentially reverts the
+ removal of REFTABLE_OBJS, but not the building of that static library.
4: ce9fd91413 = 4: a933be0664 Makefile: precompile "git-compat-util.h"
Signature · 1 line
2.56.0.rc0.467.ge054dd0dd6
PATCH v2 1/4 Makefile: remove XDIFF_OBJS initialization
Object files under 'xlib/' used to be listed in the XDIFF_OBJS Makefile variable so we could build a static library from them. This static library was removed in cf680cdb95 (make: delete XDIFF_LIB, add xdiff to LIB_OBJS, 2025-10-02), along with filling XDIFF_OBJS with object files.
But the initial empty initalization of XDIFF_OBJS remained, so remove it now.
--- Makefile | 1 - 1 file changed, 1 deletion(-)
Makefile +0 -1
@@ -695,7 +695,6 @@ BUILTIN_OBJS = BUILT_INS = COMPAT_CFLAGS = COMPAT_OBJS = -XDIFF_OBJS = GENERATED_H = EXTRA_CPPFLAGS = FUZZ_OBJS =
Signature · 1 line
2.56.0.rc0.467.ge054dd0dd6
PATCH v2 2/4 cmake: remove any "$(*_OBJS)" variables when parsing Makefile for sources
In reply to SZEDER GáborTo get various lists of files, CMake parses our Makefile looking for lines matching e.g. "list_var += ...". In case of LIB_OBJS this picks up the line "LIB_OBJS += $(COMPAT_OBJS)" as well, so the parsing macro has a specific instruction to remove "$(COMPAT_OBJS)" from the resulting list.
Currently this is the only such Makefile variable to be removed from the list, but the next patches will (re)introduce more variables containing lists of object files, so let's generalize that removing instruction to remove any "$(*_OBJS)" Makefile variable as well.
Note that we can't make the pattern matching the Makefile variable too general, e.g. to match any "$(VARIABLE)", because some lines of our Makefile do contain variables as directory prefixes, e.g. "UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o", and we must definitely keep those.
--- contrib/buildsystems/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/buildsystems/CMakeLists.txt +1 -1
@@ -102,7 +102,7 @@ project(git macro(parse_makefile_for_sources list_var makefile regex) file(STRINGS ${makefile} ${list_var} REGEX "^${regex} \\+=(.*)") string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}}) - string(REPLACE "$(COMPAT_OBJS)" "" ${list_var} ${${list_var}}) #remove "$(COMPAT_OBJS)" This is only for libgit. + string(REGEX REPLACE "\\$\\([^)]*_OBJS\\)" "" ${list_var} ${${list_var}}) # remove any "$(*_OBJS)" variables string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces string(REPLACE ".o" ".c;" ${list_var} ${${list_var}}) #change .o to .c, ; is for converting the string into a list list(TRANSFORM ${list_var} STRIP) #remove trailing/leading whitespaces for each element in list
Signature · 1 line
2.56.0.rc0.467.ge054dd0dd6
PATCH v2 3/4 Makefile: reintroduce REFTABLE_OBJS
In reply to SZEDER GáborIn the next commit we are about to precompile "git-compat-util.h" with "make" to reduce build times. But using the precompiled header should not change what actually gets compiled, therefore a source file can only be compiled using the precompiled header if the first included header file is "git-compat-util.h".
The reftable source files are kind of special, because the reftable implementation is supposed to be easily includable in other projects. Therefore, the reftable source files don't include "git-compat-util.h", with the sole exception of the purposefully project-specific "reftable/system.c". Consequently, they shouldn't be compiled with our precompiled header.
List object files under "reftable" in the REFTABLE_OBJS Makefile variable, so in the next commit we'll be able to easily filter them out and keep building them the old way, without the precompiled header.
Note that object files under "reftable/" used to be listed in REFTABLE_OBJS so we could build a static library from them. This static library was removed in f3b4c89d59 (make: delete REFTABLE_LIB, add reftable to LIB_OBJS, 2025-10-02), along with filling REFTALBE_OBJS with object files. This change essentially reverts the removal of REFTABLE_OBJS, but not the building of that static library.
--- Makefile | 32 ++++++++++++++++------------- contrib/buildsystems/CMakeLists.txt | 4 ++++ 2 files changed, 22 insertions(+), 14 deletions(-)
Makefile +18 -14
@@ -707,6 +707,7 @@ OBJECTS = OTHER_PROGRAMS = PROGRAM_OBJS = PROGRAMS = +REFTABLE_OBJS = RUST_SOURCES = EXCLUDED_PROGRAMS = SCRIPT_PERL =
@@ -1284,20 +1285,6 @@ LIB_OBJS += refs/iterator.o LIB_OBJS += refs/packed-backend.o LIB_OBJS += refs/ref-cache.o LIB_OBJS += refspec.o -LIB_OBJS += reftable/basics.o -LIB_OBJS += reftable/block.o -LIB_OBJS += reftable/blocksource.o -LIB_OBJS += reftable/error.o -LIB_OBJS += reftable/fsck.o -LIB_OBJS += reftable/iter.o -LIB_OBJS += reftable/merged.o -LIB_OBJS += reftable/pq.o -LIB_OBJS += reftable/record.o -LIB_OBJS += reftable/stack.o -LIB_OBJS += reftable/system.o -LIB_OBJS += reftable/table.o -LIB_OBJS += reftable/tree.o -LIB_OBJS += reftable/writer.o LIB_OBJS += remote.o LIB_OBJS += repack.o LIB_OBJS += repack-cruft.o
@@ -1386,6 +1373,23 @@ LIB_OBJS += xdiff/xpatience.o LIB_OBJS += xdiff/xprepare.o LIB_OBJS += xdiff/xutils.o +REFTABLE_OBJS += reftable/basics.o +REFTABLE_OBJS += reftable/block.o +REFTABLE_OBJS += reftable/blocksource.o +REFTABLE_OBJS += reftable/error.o +REFTABLE_OBJS += reftable/fsck.o +REFTABLE_OBJS += reftable/iter.o +REFTABLE_OBJS += reftable/merged.o +REFTABLE_OBJS += reftable/pq.o +REFTABLE_OBJS += reftable/record.o +REFTABLE_OBJS += reftable/stack.o +REFTABLE_OBJS += reftable/system.o +REFTABLE_OBJS += reftable/table.o +REFTABLE_OBJS += reftable/tree.o +REFTABLE_OBJS += reftable/writer.o + +LIB_OBJS += $(REFTABLE_OBJS) + BUILTIN_OBJS += builtin/add.o BUILTIN_OBJS += builtin/am.o BUILTIN_OBJS += builtin/annotate.o
contrib/buildsystems/CMakeLists.txt +4 -0
@@ -668,6 +668,10 @@ include_directories(${CMAKE_BINARY_DIR}) #libgit parse_makefile_for_sources(libgit_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "LIB_OBJS") +#reftable +parse_makefile_for_sources(reftable_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "REFTABLE_OBJS") +list(APPEND libgit_SOURCES ${reftable_SOURCES}) + list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/") list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
Signature · 1 line
2.56.0.rc0.467.ge054dd0dd6
PATCH v2 4/4 Makefile: precompile "git-compat-util.h"
In reply to SZEDER GáborThis patch follows the idea of 671df48df8 (meson: precompile "git-compat-util.h", 2026-03-19) to make it faster to build Git using "make". The notable differences are the boilerplate needed to wire up the precompiled header with "make", and the selection of object files that are built using the precompiled header:
- Add a new rule to precompile "tools/precompiled.h" into "tools/precompiled.h.gch".
This rule and its dependencies are basically the same as our existing rule and its dependencies for compiling object files from our source files and, except that in this rule we don't use the EXTRA_CPPFLAGS target-specific variable. This is partly because any compiler flags in that target-specific variable can't possibly make sense for the project-wide "git-compat-util.h", and partly because it could run afoul of "make"'s quirks with target-specific variables and GCC's "-Winvalid-pch" compiler option [1].
Our Makefile always writes object files next to the source files they were compiled from. Since a precompiled header is in many ways similar to an object file, let's follow suit and write it next to the header file it was created from as well. 671df48df8 doesn't mention why "precompiled.h" was placed in "tools/".
Use the ".gch" suffix for the precompiled header file, because that's the only suffix GCC looks for when searching for a precompiled header. Clang apparently looks for both ".pch" and ".gch" suffixes. If we ever encounter a compiler which only supports a different precompiled header suffix, then we might make the suffix configurable via a Makefile knob; but until then it remains hard-coded for the sake of simplicity.
- Declare the precompiled header's dependencies, so it gets rebuilt when any of our header files it includes is modified.
When using computed header dependencies, then the compiler can generate the Makefile snippet containing the header files inlcuded by the precompiled header, just like when compiling a C source file into an object file.
When not using computed header dependencies, then make the precompiled header depend on the same header files that our object files depend on, i.e. those listed in LIB_H and GENERATED_H. This is not ideal, because the precompiled header will be rebuilt even when a header file it doesn't actually depend on changes; but in that case all object files will be rebuilt as well anyway.
- List the object files that are built using the precompiled header in the PRECOMPILED_HEADER_USERS variable:
- The precompiled header should not change what actually gets compiled. Therefore, use the precompiled header only when compiling source files that start with including "git-compat-util.h" (directly or indirectly, e.g. via "builtin.h"), or its inclusion is only preceeded by #define directives that don't influence "git-compat-util.h" between its include guards [2] (currently DISABLE_SIGN_COMPARE_WARNINGS, USE_THE_REPOSITORY_VARIABLE or GIT_TEST_PROGRESS_ONLY). [3]
Several (but not all) object or source files listed in the COMPAT_OBJS, REFTABLE_OBJS and THIRD_PARTY_SOURCES variables don't include "git-compat-util.h", therefore, for the sake of simplicity, none of the files listed in these variables are built with the precompiled header. [4]
Since 671df48df8 the Meson build uses the precompiled header even when compiling those reftable source files that don't include "git-compat-util.h" at all, although this change is not mentioned in the commit message.
- But other than that, use the precompiled header when compiling all other source files, including e.g. source files for standalone executables (e.g. "daemon.c" for "git-daemon"), or those in the directories "t/helper/" and "t/unit-tests/" as well.
The Meson build, however, only uses the precompiled header for compiling objects in "libgit_sources" and "builtin_sources", despite 671df48df8 giving the impression that the precompiled header is included in all compilation units.
- In short, PRECOMPILED_HEADER_USERS contains all object files listed in OBJECTS, except those that are listed in COMPAT_OBJS, REFTABLE_OBJS or THIRD_PARTY_SOURCES as well.
- Add a new rule to build object files listed in PRECOMPILED_HEADER_USERS with the precompiled header.
This rule and its dependencies are basically the same as our existing rule and its dependencies compiling object files from our source files, except:
- This rule depends on the precompiled header as well, to make sure that it's built before it is used.
- Use the "-include tools/precompiled.h" option to make the precompiled header the first header in the compilation unit, because only then is it used to speed things up.
- Use the "-Winvalid-pch" option to catch any issues when the precompiled header is present but can't be used for whatever reason. While being unable to use the precompiled header would normally only result in a slower build, it is probably better that developers are made aware that simething is not quite right. Meson uses this option as well when compiling a source file using the precompiled header.
Note, that in case of such an issue with "-Winvalid-pch" we would get a warning for each object file that is build using the precompiled header, but together with "-Werror" (e.g. with DEVELOPER=1) it would fail the build.
Object files not listed in PRECOMPILED_HEADER_USERS are built with the existing (and unchanged) rule for object files.
- This way the precompiled header is compiled only once during the whole build process.
The Meson build, however, currently compiles the precompiled header twice: once for "libgit_sources" and once for "builtin_sources", despite 671df48df8 giving the impression that it's compiled only once. And, by the looks of it, it would have to be compiled once more for each new library or executable where we declare the use of the precompiled header.
- Add the Makefile knob NO_PRECOMPILED_HEADER to make it possible to build without using the precompiled header for testing purposes, or for any compiler that might be out there that doesn't support the "-Winvalid-pch" option.
Note, that when this knob is set then we merely leave PRECOMPILED_HEADER_USERS empty: this way nothing is built with the precompiled header, but "make clean NO_PRECOMPILED_HEADER=1" would still remove a leftover precompiled header file.
With this patch series on top of v2.55.0 I got the following build time improvement:
Benchmark 1: make -j12 (rev = v2.55.0)
Time (mean ± σ): 29.414 s ± 0.031 s [User: 254.786 s, System: 47.447 s]
Range (min … max): 29.370 s … 29.470 s 10 runsBenchmark 2: make -j12 (rev = precompile)
Time (mean ± σ): 21.725 s ± 0.047 s [User: 186.047 s, System: 35.109 s]
Range (min … max): 21.643 s … 21.768 s 10 runs Summary
'make -j12 (rev = precompile)' ran
1.35 ± 0.00 times faster than 'make -j12 (rev = v2.55.0)'
[1] "make" applies target-specific variables to the first target's dependencies as well. So, if the rule creating the precompiled header included $(EXTRA_CPPFLAGS) as well, then we could get this:
$ make -s clean
$ make V=1 git
gcc -o tools/precompiled.h.gch -c -MF [...] -DSHELL_PATH='"/bin/sh"' '-DGIT_HTML_PATH="share/doc/git-doc"' '-DGIT_MAN_PATH="share/man"' '-DGIT_INFO_PATH="share/info"' tools/precompiled.h gcc -o git.o -c -include tools/precompiled.h -Winvalid-pch -MF [...] -DSHELL_PATH='"/bin/sh"' '-DGIT_HTML_PATH="share/doc/git-doc"' '-DGIT_MAN_PATH="share/man"' '-DGIT_INFO_PATH="share/info"' git.c gcc -o builtin/add.o -c -include tools/precompiled.h -Winvalid-pch -MF [...] -DSHELL_PATH='"/bin/sh"' builtin/add.c
cc1: error: ./tools/precompiled.h.gch: not used because `GIT_MAN_PATH' not defined [-Werror=invalid-pch]
cc1: all warnings being treated as errors
make: *** [Makefile:2940: builtin/add.o] Error 1
So in this case "git.o" is the first object file to be build, and since it has a target-specific EXTRA_CPPFLAGS and since it depends on the precompiled header, the precompiled header is built with the same EXTRA_CPPFLAGS as well. But then "builtin/add.o" is built with empty EXTRA_CPPFLAGS, and GCC's sanity checks for the -Winvalid-pch option complain. Clang apparently doesn't consider this to be an issue, and builds "git" just fine.
Note, however, that if the first object file to be built is not "git.o" (or one of the other object files with target-specific EXTRA_CPPFLAGS), which is usually the case with e.g. "make all", then the precompiled header is built with empty EXTRA_CPPFLAGS, and then all is well. Weird. We already had issues with "make" and target-specific variables in the past, see a673cfede6 (Makefile: Fix occasional GIT-CFLAGS breakage, 2010-03-19). The Meson build doesn't use target-specific variables, and the whole project needs to be rebuilt when e.g. "mandir" is changed, although this is not mentioned in 904339edbd (Introduce support for the Meson build system, 2024-12-06).
[2] See baa61e46da (git-compat-util.h: move warning infra to prepare
for PCHs, 2026-03-19).
[3] This can be verified by simply scanning through the output of:
{
printf '%s\n' 'print-%:'
printf '\t%s\n' '@printf "%s\n" $($*)'
} >/tmp/printvars.mak
for f in $(make -f /tmp/printvars.mak -f Makefile print-PRECOMPILED_HEADER_USERS)
do
f="${f%o}c"
sed -n -E -e "s%# *(define|include).*%& $f%p" -e '/# *include/q' "$f"
done | sort[4] "make"'s flexibility would allow us to separately list those object files that don't include "git-compat-util.h", but then on my (I assume fairly typical) Linux box the number of object files built using the precompiled header increases only by 9 (from 536 to 545), which reduces the build time by about 1% (0.2s). Therefore, I don't think it's worth the churn.
--- .gitignore | 1 + Makefile | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-)
.gitignore +1 -0
@@ -233,6 +233,7 @@ /.vscode/ /tags /TAGS +/tools/precompiled.h.gch /cscope* /compile_commands.json /.cache/
Makefile +21 -5
@@ -321,6 +321,9 @@ include shared.mak # dependency rules. The default is "auto", which means to use computed header # dependencies if your compiler is detected to support it. # +# Define NO_PRECOMPILED_HEADER if you want to build Git without precompiling +# "git-compat-util.h". +# # Define NATIVE_CRLF if your platform uses CRLF for line endings. # # Define GIT_USER_AGENT if you want to change how git identifies itself during
@@ -2905,8 +2908,15 @@ endif .PHONY: objects objects: $(OBJECTS) -dep_files := $(foreach f,$(OBJECTS),$(dir $f).depend/$(notdir $f).d) -dep_dirs := $(addsuffix .depend,$(sort $(dir $(OBJECTS)))) +PRECOMPILED_HEADER := tools/precompiled.h +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)) +endif + +dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d) +dep_dirs := $(addsuffix .depend,$(sort $(dir $(OBJECTS) $(PRECOMPILED_HEADER_GCH)))) ifeq ($(uname_S),Darwin) dep_dirs += $(addsuffix .depend,$(sort $(dir contrib/credential/osxkeychain/git-credential-osxkeychain.o)))
@@ -2940,7 +2950,13 @@ missing_compdb_dir = compdb_args = endif -$(OBJECTS): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) +$(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $@ -c $(dep_args) $(compdb_args) $(ALL_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) $< + +$(filter-out $(PRECOMPILED_HEADER_USERS),$(OBJECTS)): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< %.s: %.c GIT-CFLAGS FORCE
@@ -2954,7 +2970,7 @@ ifneq ($(dep_files_present),) include $(dep_files_present) endif else -$(OBJECTS): $(LIB_H) $(GENERATED_H) +$(OBJECTS) $(PRECOMPILED_HEADER_GCH): $(LIB_H) $(GENERATED_H) endif ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
@@ -3908,7 +3924,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) GIT-TEST-SUITES $(RM) po/git.pot po/git-core.pot $(RM) git.rc git.res - $(RM) $(OBJECTS) + $(RM) $(OBJECTS) $(PRECOMPILED_HEADER_GCH) $(RM) headless-git.o $(RM) $(LIB_FILE) $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS)
Signature · 1 line
2.56.0.rc0.467.ge054dd0dd6
Re: [PATCH v2 4/4] Makefile: precompile "git-compat-util.h"
Quoted 5 lines · SZEDER Gábor
This patch follows the idea of
671df48df8(meson: precompile "git-compat-util.h", 2026-03-19) to make it faster to build Git using "make". The notable differences are the boilerplate needed to wire up the precompiled header with "make", and the selection of object files that are built using the precompiled header:
I got an interesting error message from this today:
$ make imap-send.o
* new build flags
CC tools/precompiled.h.gch
CC imap-send.o
cc1: warning: ./tools/precompiled.h.gch: not used because ‘NO_OPENSSL’ is defined [-Winvalid-pch]You won't see it with:
make NO_OPENSSL=1 imap-send.o
The culprit is that I have this in my config.mak:
imap-send.o: EXTRA_CPPFLAGS += -DNO_OPENSSL
so the build options for the precompiled header and imap-send.c are not the same. So now of course you are asking why I would have such a weird line in my config.mak.
The answer is that I want to disable openssl for old builds, because I am often building historical versions which use openssl constructs that are deprecated or removed. So naturally you are now asking why it does not just say:
NO_OPENSSL = BrokenOnOldVersions
or similar. But that breaks _some_ old versions which really do need openssl for various things.
The good-ish news is that it's mostly cosmetic for me. I also loosen -Werror for old builds, for obvious reasons. So it's not breaking any build.
I don't know if my use case is too crazy to care about, but I thought I'd mention it in case there are other less-crazy related cases we might run into.
And yes, obviously old versions will not have the precompiled header, either, but my logic for "loosen compilation" is mostly "we are not on a branch nor rebasing", so a sight-seeing trip to "git checkout origin/seen" puts me in the same mode. And eventually it _will_ be old, too. ;)
-Peff
Re: [PATCH v2 4/4] Makefile: precompile "git-compat-util.h"
Quoted 18 lines · Jeff King
Quoted 5 lines · SZEDER Gábor
This patch follows the idea of
671df48df8(meson: precompile "git-compat-util.h", 2026-03-19) to make it faster to build Git using "make". The notable differences are the boilerplate needed to wire up the precompiled header with "make", and the selection of object files that are built using the precompiled header:I got an interesting error message from this today:
$ make imap-send.o * new build flags CC tools/precompiled.h.gch CC imap-send.o cc1: warning: ./tools/precompiled.h.gch: not used because ‘NO_OPENSSL’ is defined [-Winvalid-pch]You won't see it with:
Quoted 8 lines
make NO_OPENSSL=1 imap-send.o
The culprit is that I have this in my config.mak:
imap-send.o:
EXTRA_CPPFLAGS+= -DNO_OPENSSLso the build options for the precompiled header and
imap-send.care not the same.
Hrm. I've run into this with 'make git.o' and the other object files for which we set EXTRA_CPPFLAGS in our Makefile, and wrote about it at length in the commit message. I thought omitting EXTRA_CPPFLAGS from the command building the precompiled header solved this issue, and was puzzled at first why your use case still causes problems... The reason for the difference is that none of the EXTRA_CPPFLAGS we set in our Makefile affect 'git-compat-util.h', but -DNO_OPENSSL does.
If you set such a custom EXTRA_CPPFLAGS, then you might as well append '-Wno-invalid-pch' to it to silence that warning. The rule building object files using the precompiled header has '-Winvalid-pch' near the beginning while EXTRA_CPPFLAGS are near the end, so we can override it from EXTRA_CPPFLAGS. I didn't find a way to override '-include precompiled.h'.
(Btw, can you do something like this with Meson? :) Without resorting to creating yet another static library, of course.)
Quoted 6 lines
So now of course you are asking why I would have such a weird line in my config.mak.
The answer is that I want to disable openssl for old builds, because I am often building historical versions which use openssl constructs that are deprecated or removed.
Well, for the same reason I have the following in my config.mak:
# Build knobs to build older versions:
# 1ed2c7b115 (imap-send: use HMAC() function provided by OpenSSL, 2016-04-09)
ifeq ($(shell git merge-base --is-ancestor 1ed2c7b11570f5d16bdc70d151fa78c3dccf6d38 HEAD 2>/dev/null; echo $$?),1)
$(warning Setting NO_OPENSSL for old revisions)
NO_OPENSSL = UnfortunatelyYes
endifQuoted 7 lines
So naturally you are now asking why it does
not just say:
NO_OPENSSL= BrokenOnOldVersionsor similar. But that breaks _some_ old versions which really do need openssl for various things.
I haven't run into any such breakages with disabling OPENSSL for the whole build... but maybe I just haven't built old enough versions?! Anyway, will adapt it to your EXTRA_CPPFLAGS trick, thanks.
Quoted 5 lines
The good-ish news is that it's mostly cosmetic for me. I also loosen -Werror for old builds, for obvious reasons. So it's not breaking any build.
I don't know if my use case is too crazy to care about
I would say so, yes ;)
Quoted 3 lines
but I thought
I'd mention it in case there are other less-crazy related cases we might run into.
Not sure what those less crazy use cases might be, but I'm inclined to say that "If you deliberately set a custom EXTRA_CPPFLAGS that affects 'git-compat-util.h', then you should also add '-Wno-invalid-pch' as well".
Quoted 5 lines
And yes, obviously old versions will not have the precompiled header, either, but my logic for "loosen compilation" is mostly "we are not on a branch nor rebasing", so a sight-seeing trip to "git checkout origin/seen" puts me in the same mode. And eventually it _will_ be old, too. ;)
I'm not sure about loosening compilation for 'seen', especially when it comes to DEVELOPER=1, because it's best to catch any issues with DEVELOPER=1 while the commit is still only in 'seen'. My config.mak doesn't set DEVELOPER=1 when bisecting or when building a revision reachable from a tagged release.
Re: [PATCH v2 4/4] Makefile: precompile "git-compat-util.h"
In reply to SZEDER GáborQuoted 8 lines · SZEDER Gábor
- The precompiled header should not change what actually gets compiled. Therefore, use the precompiled header only when compiling source files that start with including "
git-compat-util.h" (directly or indirectly, e.g. via "builtin.h"), or its inclusion is only preceeded by #define directives that don't influence "git-compat-util.h" between its include guards [2] (currentlyDISABLE_SIGN_COMPARE_WARNINGS,USE_THE_REPOSITORY_VARIABLEorGIT_TEST_PROGRESS_ONLY). [3]
Well, it turns out the precompiled header does change what gets compiled, and it causes a visible behavior difference, though I think it's really minor.
The crux of the issue is that without the precompiled header the compiler processes "git-compat-util.h", but with it, for some reason, it processes "./git-compat-util.h".
This is visible when expanding __FILE__ in "git-compat-util.h", e.g. in the assert macro in regexec_buf(). When the assertion is triggered, e.g. with this diff:
common-main.c +3 -0
@@ -5,6 +5,9 @@ int main(int argc, const char **argv) {int result; +/* Intentionally bogus regexec_buf() call to trigger its assert() */ +regexec_buf (NULL ,NULL ,0 ,0 ,NULL ,0 ); +init_git (argv); result =cmd_main (argc, argv);
Then without the precompiled header we get:
$ ./git git: git-compat-util.h:1002: regexec_buf: Assertion `nmatch > 0 && pmatch' failed. Aborted (core dumped)
But with the precompiled header:
$ ./git git: ./git-compat-util.h:1002: regexec_buf: Assertion `nmatch > 0 && pmatch' failed. Aborted (core dumped)
Similar could happen when the ALLOC_GROW_BY() macro is invoked with bogus parameters to trigger a BUG().
(Sidenote: While this assert does prevent us from invoking regexec() with nonsense, the source file name and line number in the resulting error message are not as useful as they could be, it would be better to show the caller's filename and line number.)
Since in "git-compat-util.h" __FILE__ is only expanded in error messages that should basically never happen (BUG() and assert()), I think this is acceptable.
BTW, this is also visible in compiler error messages:
git-compat-util.h +2 -0
@@ -1,6 +1,8 @@#ifndef GIT_COMPAT_UTIL_H #define GIT_COMPAT_UTIL_H +trigger_compiler_error +#if __STDC_VERSION__ -0 <199901L /* * Git is in a testing period for mandatory C99 support in the compiler. If
Without precompiled header:
CC daemon.o
In file included from daemon.c:3:
git-compat-util.h:4:23: error: expected ‘;’ before ‘typedef’
4 | trigger_compiler_error
| ^
| ;With precompiled header:
CC tools/precompiled.h.gch
In file included from tools/precompiled.h:1:
./git-compat-util.h:4:23: error: expected ‘;’ before ‘typedef’
4 | trigger_compiler_error
| ^
| ;