Git development
 help / color / mirror / Atom feed
* [PATCH] Makefile: let ccache cache objects built with the precompiled header
@ 2026-10-27 12:40 Harald Nordgren via GitGitGadget
  2026-10-27 15:58 ` Ben Knoble
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Harald Nordgren via GitGitGadget @ 2026-10-27 12:40 UTC (permalink / raw)
  To: git; +Cc: Harald Nordgren, Harald Nordgren

From: Harald Nordgren <haraldnordgren@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 for the precompiled header and the objects built with it.
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.

Signed-off-by: Harald Nordgren <haraldnordgren@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.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2464%2FHaraldNordgren%2Fmakefile-ccache-pch-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2464/HaraldNordgren/makefile-ccache-pch-v1
Pull-Request: https://github.com/git/git/pull/2464

 Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Makefile b/Makefile
index a96be506b5..43ea5e0c40 100644
--- a/Makefile
+++ b/Makefile
@@ -2946,6 +2946,8 @@ $(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_com
 $(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) $<
 
+$(PRECOMPILED_HEADER_GCH) $(PRECOMPILED_HEADER_USERS): export CCACHE_SLOPPINESS += pch_defines,time_macros
+
 $(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) $<
 

base-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] Makefile: let ccache cache objects built with the precompiled header
  2026-10-27 12:40 [PATCH] Makefile: let ccache cache objects built with the precompiled header Harald Nordgren via GitGitGadget
@ 2026-10-27 15:58 ` Ben Knoble
  2026-10-27 20:00   ` Harald Nordgren
  2026-10-29  8:33 ` [PATCH v2] " Harald Nordgren via GitGitGadget
  2026-12-04  8:45 ` [PATCH v3] " Harald Nordgren via GitGitGadget
  2 siblings, 1 reply; 11+ messages in thread
From: Ben Knoble @ 2026-10-27 15:58 UTC (permalink / raw)
  To: Harald Nordgren via GitGitGadget; +Cc: git, Harald Nordgren


> Le 27 oct. 2026 à 08:49, Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com> a écrit :
> 
> From: Harald Nordgren <haraldnordgren@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 for the precompiled header and the objects built with it.
> 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.
> 
> Signed-off-by: Harald Nordgren <haraldnordgren@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.
> 
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2464%2FHaraldNordgren%2Fmakefile-ccache-pch-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2464/HaraldNordgren/makefile-ccache-pch-v1
> Pull-Request: https://github.com/git/git/pull/2464

I can’t comment on the above, not being a ccache user myself.

> Makefile | 2 ++
> 1 file changed, 2 insertions(+)
> 
> diff --git a/Makefile b/Makefile
> index a96be506b5..43ea5e0c40 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -2946,6 +2946,8 @@ $(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_com
> $(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) $<
> 
> +$(PRECOMPILED_HEADER_GCH) $(PRECOMPILED_HEADER_USERS): export CCACHE_SLOPPINESS += pch_defines,time_macros
> +

This is a little curious to me. I’m familiar with the GNU Make-ism 
to set variables for particular targets; how does the export work?
Is it the same but with a propagation to the recipe processes?

Would it be better (for some definition…) to put the export in the 2
pre-compiled header recipes directly?

And, what about Meson?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Makefile: let ccache cache objects built with the precompiled header
  2026-10-27 15:58 ` Ben Knoble
@ 2026-10-27 20:00   ` Harald Nordgren
  2026-10-27 21:57     ` D. Ben Knoble
  0 siblings, 1 reply; 11+ messages in thread
From: Harald Nordgren @ 2026-10-27 20:00 UTC (permalink / raw)
  To: Ben Knoble; +Cc: Harald Nordgren via GitGitGadget, git

> > @@ -2946,6 +2946,8 @@ $(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_com
> > $(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) $<
> >
> > +$(PRECOMPILED_HEADER_GCH) $(PRECOMPILED_HEADER_USERS): export CCACHE_SLOPPINESS += pch_defines,time_macros
> > +
>
> This is a little curious to me. I’m familiar with the GNU Make-ism
> to set variables for particular targets; how does the export work?
> Is it the same but with a propagation to the recipe processes?
>
> Would it be better (for some definition…) to put the export in the 2
> pre-compiled header recipes directly?

Yeah, that could be a good idea.

> And, what about Meson?

I think it's not touched by this? My goal is just to speed up my local builds.


Harald

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Makefile: let ccache cache objects built with the precompiled header
  2026-10-27 20:00   ` Harald Nordgren
@ 2026-10-27 21:57     ` D. Ben Knoble
  2026-10-28  7:34       ` Patrick Steinhardt
  0 siblings, 1 reply; 11+ messages in thread
From: D. Ben Knoble @ 2026-10-27 21:57 UTC (permalink / raw)
  To: Harald Nordgren; +Cc: Harald Nordgren via GitGitGadget, git

On Tue, Oct 27, 2026 at 4:00 PM Harald Nordgren
<haraldnordgren@gmail.com> wrote:
>
> > And, what about Meson?
>
> I think it's not touched by this? My goal is just to speed up my local builds.

Yeah, it'd generally be nice to keep the 2 on par though ;)

I don't know enough about our pch infra, though, so I'll bow out from
here. Just thought maybe we should make Meson do the same?

-- 
D. Ben Knoble

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Makefile: let ccache cache objects built with the precompiled header
  2026-10-27 21:57     ` D. Ben Knoble
@ 2026-10-28  7:34       ` Patrick Steinhardt
  2026-10-28  9:58         ` Harald Nordgren
  0 siblings, 1 reply; 11+ messages in thread
From: Patrick Steinhardt @ 2026-10-28  7:34 UTC (permalink / raw)
  To: D. Ben Knoble; +Cc: Harald Nordgren, Harald Nordgren via GitGitGadget, git

On Tue, Oct 27, 2026 at 05:57:38PM -0400, D. Ben Knoble wrote:
> On Tue, Oct 27, 2026 at 4:00 PM Harald Nordgren
> <haraldnordgren@gmail.com> wrote:
> >
> > > And, what about Meson?
> >
> > I think it's not touched by this? My goal is just to speed up my local builds.
> 
> Yeah, it'd generally be nice to keep the 2 on par though ;)
> 
> I don't know enough about our pch infra, though, so I'll bow out from
> here. Just thought maybe we should make Meson do the same?

As Meson auto-detects ccache I'd think that stuff like this should
rather be handled in Meson itself. I e.g. found [1], which feels like
the right direction to me.

But if somebody wants to implement this in our instructions meanwhile I
won't complain.

Patrick

[1]: https://github.com/mesonbuild/meson/issues/14662

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Makefile: let ccache cache objects built with the precompiled header
  2026-10-28  7:34       ` Patrick Steinhardt
@ 2026-10-28  9:58         ` Harald Nordgren
  2026-10-30 22:26           ` Harald Nordgren
  0 siblings, 1 reply; 11+ messages in thread
From: Harald Nordgren @ 2026-10-28  9:58 UTC (permalink / raw)
  To: Patrick Steinhardt; +Cc: D. Ben Knoble, Harald Nordgren via GitGitGadget, git

> > > > And, what about Meson?
> > >
> > > I think it's not touched by this? My goal is just to speed up my local builds.
> >
> > Yeah, it'd generally be nice to keep the 2 on par though ;)
> >
> > I don't know enough about our pch infra, though, so I'll bow out from
> > here. Just thought maybe we should make Meson do the same?
>
> As Meson auto-detects ccache I'd think that stuff like this should
> rather be handled in Meson itself. I e.g. found [1], which feels like
> the right direction to me.
>
> But if somebody wants to implement this in our instructions meanwhile I
> won't complain.
>
> Patrick
>
> [1]: https://github.com/mesonbuild/meson/issues/14662

Ok, so I'll leave Meson out of this. Maybe I'll look into opening a PR
there instead.


Harald

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2] Makefile: let ccache cache objects built with the precompiled header
  2026-10-27 12:40 [PATCH] Makefile: let ccache cache objects built with the precompiled header Harald Nordgren via GitGitGadget
  2026-10-27 15:58 ` Ben Knoble
@ 2026-10-29  8:33 ` Harald Nordgren via GitGitGadget
  2026-12-03 11:04   ` Phillip Wood
  2026-12-04  8:45 ` [PATCH v3] " Harald Nordgren via GitGitGadget
  2 siblings, 1 reply; 11+ messages in thread
From: Harald Nordgren via GitGitGadget @ 2026-10-29  8:33 UTC (permalink / raw)
  To: git; +Cc: Ben Knoble, Patrick Steinhardt, Harald Nordgren, Harald Nordgren

From: Harald Nordgren <haraldnordgren@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.

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.

Signed-off-by: Harald Nordgren <haraldnordgren@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 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-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2464/HaraldNordgren/makefile-ccache-pch-v2
Pull-Request: https://github.com/git/git/pull/2464

Range-diff vs v1:

 1:  1984b13246 ! 1:  e61cca8cca Makefile: let ccache cache objects built with the precompiled header
     @@ Commit message
          a source file can come before it, and none of our sources use
          __DATE__, __TIME__ or __TIMESTAMP__.
      
     -    Enable them for the precompiled header and the objects built with it.
     +    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.
      
     @@ Commit message
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## Makefile ##
     -@@ Makefile: $(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_com
     - $(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: PRECOMPILED_HEADER_GCH := $(addsuffix .gch,$(PRECOMPILED_HEADER))
       
     -+$(PRECOMPILED_HEADER_GCH) $(PRECOMPILED_HEADER_USERS): export CCACHE_SLOPPINESS += pch_defines,time_macros
     -+
     - $(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) $<
     + 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
     + endif
       
     + dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d)


 Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Makefile b/Makefile
index a96be506b5..80653bb22c 100644
--- a/Makefile
+++ b/Makefile
@@ -2903,6 +2903,7 @@ 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
 endif
 
 dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d)

base-commit: 37b3475fb530c14109211faca33716a173db61ed
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] Makefile: let ccache cache objects built with the precompiled header
  2026-10-28  9:58         ` Harald Nordgren
@ 2026-10-30 22:26           ` Harald Nordgren
  0 siblings, 0 replies; 11+ messages in thread
From: Harald Nordgren @ 2026-10-30 22:26 UTC (permalink / raw)
  To: Patrick Steinhardt; +Cc: D. Ben Knoble, Harald Nordgren via GitGitGadget, git

> > > > > And, what about Meson?
> > > >
> > > > I think it's not touched by this? My goal is just to speed up my local builds.
> > >
> > > Yeah, it'd generally be nice to keep the 2 on par though ;)
> > >
> > > I don't know enough about our pch infra, though, so I'll bow out from
> > > here. Just thought maybe we should make Meson do the same?
> >
> > As Meson auto-detects ccache I'd think that stuff like this should
> > rather be handled in Meson itself. I e.g. found [1], which feels like
> > the right direction to me.
> >
> > But if somebody wants to implement this in our instructions meanwhile I
> > won't complain.
> >
> > Patrick
> >
> > [1]: https://github.com/mesonbuild/meson/issues/14662
>
> Ok, so I'll leave Meson out of this. Maybe I'll look into opening a PR
> there instead.

This has now been merged into Meson:
https://github.com/mesonbuild/meson/pull/16317


Harald

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] Makefile: let ccache cache objects built with the precompiled header
  2026-10-29  8:33 ` [PATCH v2] " Harald Nordgren via GitGitGadget
@ 2026-12-03 11:04   ` Phillip Wood
  2026-12-03 11:45     ` Harald Nordgren
  0 siblings, 1 reply; 11+ messages in thread
From: Phillip Wood @ 2026-12-03 11:04 UTC (permalink / raw)
  To: Harald Nordgren via GitGitGadget, git
  Cc: Ben Knoble, Patrick Steinhardt, Harald Nordgren

Hi Harald

On 29/10/2026 08:33, Harald Nordgren via GitGitGadget wrote:
> From: Harald Nordgren <haraldnordgren@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.

While I use ccache, I've always just used the builtin defaults, so I was 
hoping someone with more experience would review this but that hasn't 
happened. What you've written agrees with the ccache man page. One thing 
I don't quite understand is that the manual also says "without proper 
setup, ccache falls back to the non-precompiled header (if available)." 
yet we're seeing it rebuild everything instead.

> 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.

I tested this patch with gcc and see the same. The cache is also used 
when switching to a different branch and back again.

> 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.

That's a pain, the ccache manual mentions using -fno-pch-timestamp - 
should we be adding that as well?

Thanks for working on this

Phillip

> Signed-off-by: Harald Nordgren <haraldnordgren@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 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-v2
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2464/HaraldNordgren/makefile-ccache-pch-v2
> Pull-Request: https://github.com/git/git/pull/2464
> 
> Range-diff vs v1:
> 
>   1:  1984b13246 ! 1:  e61cca8cca Makefile: let ccache cache objects built with the precompiled header
>       @@ Commit message
>            a source file can come before it, and none of our sources use
>            __DATE__, __TIME__ or __TIMESTAMP__.
>        
>       -    Enable them for the precompiled header and the objects built with it.
>       +    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.
>        
>       @@ Commit message
>            Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
>        
>         ## Makefile ##
>       -@@ Makefile: $(PRECOMPILED_HEADER_GCH): %.gch: % GIT-CFLAGS $(missing_dep_dirs) $(missing_com
>       - $(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: PRECOMPILED_HEADER_GCH := $(addsuffix .gch,$(PRECOMPILED_HEADER))
>         
>       -+$(PRECOMPILED_HEADER_GCH) $(PRECOMPILED_HEADER_USERS): export CCACHE_SLOPPINESS += pch_defines,time_macros
>       -+
>       - $(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) $<
>       + 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
>       + endif
>         
>       + dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d)
> 
> 
>   Makefile | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/Makefile b/Makefile
> index a96be506b5..80653bb22c 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -2903,6 +2903,7 @@ 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
>   endif
>   
>   dep_files := $(foreach f,$(OBJECTS) $(PRECOMPILED_HEADER_GCH),$(dir $f).depend/$(notdir $f).d)
> 
> base-commit: 37b3475fb530c14109211faca33716a173db61ed


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] Makefile: let ccache cache objects built with the precompiled header
  2026-12-03 11:04   ` Phillip Wood
@ 2026-12-03 11:45     ` Harald Nordgren
  0 siblings, 0 replies; 11+ messages in thread
From: Harald Nordgren @ 2026-12-03 11:45 UTC (permalink / raw)
  To: phillip.wood
  Cc: Harald Nordgren via GitGitGadget, git, Ben Knoble,
	Patrick Steinhardt

> > 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.
>
> That's a pain, the ccache manual mentions using -fno-pch-timestamp -
> should we be adding that as well?

Interesting point, let me take a look at this.

> Thanks for working on this

Thank you for the review!


Harald

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3] Makefile: let ccache cache objects built with the precompiled header
  2026-10-27 12:40 [PATCH] Makefile: let ccache cache objects built with the precompiled header Harald Nordgren via GitGitGadget
  2026-10-27 15:58 ` Ben Knoble
  2026-10-29  8:33 ` [PATCH v2] " Harald Nordgren via GitGitGadget
@ 2026-12-04  8:45 ` Harald Nordgren via GitGitGadget
  2 siblings, 0 replies; 11+ messages in thread
From: Harald Nordgren via GitGitGadget @ 2026-12-04  8:45 UTC (permalink / raw)
  To: git
  Cc: Ben Knoble, Patrick Steinhardt, Phillip Wood, Harald Nordgren,
	Harald Nordgren

From: Harald Nordgren <haraldnordgren@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 <haraldnordgren@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 <haraldnordgren@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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-12-04  8:45 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-27 12:40 [PATCH] Makefile: let ccache cache objects built with the precompiled header Harald Nordgren via GitGitGadget
2026-10-27 15:58 ` Ben Knoble
2026-10-27 20:00   ` Harald Nordgren
2026-10-27 21:57     ` D. Ben Knoble
2026-10-28  7:34       ` Patrick Steinhardt
2026-10-28  9:58         ` Harald Nordgren
2026-10-30 22:26           ` Harald Nordgren
2026-10-29  8:33 ` [PATCH v2] " Harald Nordgren via GitGitGadget
2026-12-03 11:04   ` Phillip Wood
2026-12-03 11:45     ` Harald Nordgren
2026-12-04  8:45 ` [PATCH v3] " Harald Nordgren via GitGitGadget

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox