r/cpp • • Mar 28 '26

My experience writing build system for C++

So, I was writing a build system last month, it was a very good learning experience.

I want explain the general procedure for anyone interested.

1 - You extract build information from some file/script whatever

2 - You parse that information to a graph, run a topo sort, write a ninja file and save build info to a cache.

3 - You generate all your sources

4 - Ninja calls some cli tools for p1689 generation and to scan all your c++ sources and write rsp files and a global dyndep file.

5- You use rsp files to compile sources and dyndep file to manage build order

Some notes:

Header units aren’t supported, by anything. Clang driver for them is buggy and scan deps for them don't work. I think they must be removed from the standard because after 6 years no one really adopted them.

Static typed scripting is awesome, I used umka for my build system and the api just matches what I write in C++.

20 Upvotes

55 comments sorted by

View all comments

Show parent comment

2

u/max0x7ba https://github.com/max0x7ba Mar 28 '26 edited Mar 29 '26

I rarely see invocations of GNU Make with -r or -R arguments.

GNU Make built-in rules also match all C and C++ source files and their auto-generated header dependencies .d files. These rules try finding their prerequisites to re-make the source files and .d files automatically for you. Being long obsolete, they aren't disabled by default for legacy compatibility reasons. But their probing of prerequisite files burns at least +25% extra CPU cycles for Make's dependency checking.

These rules can be disabled from within a Makefile, but not completely. make -r or make -R disable the built-in rules completely, and there is no justification to ever invoke make without these options in the 21st century.

Benchmarks of Make vs Ninja universally report Ninja shorter times relative to GNU Make in this particular use-case and for clean parallel builds. Which are the exact symptoms and outcomes of GNU Make's legacy built-in rules not being disabled in benchmark command lines timing GNU Make.

https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html


For example, having built with your makefiles, when invoking make doesn't re-build anything, try the following commands (my outputs come from a small propriatary codebase using modern non-recursive makefiles):

$ export TIME='Elapsed time %E, user %Us, system %Ss, CPU %P, RSS %MkB, exit-status %x.\n'

$ /bin/time make -d > /tmp/make.with-default-rules.log
Elapsed time 0:00.09, user 0.06s, system 0.03s, CPU 100%, RSS 4608kB, exit-status 0.

$ /bin/time make -R -d > /tmp/make.without-default-rules.log
Elapsed time 0:00.06, user 0.03s, system 0.02s, CPU 100%, RSS 4608kB, exit-status 0.

$ wc -l --total=never /tmp/make.with{,out}-default-rules.log
  38615 /tmp/make.with-default-rules.log
  19814 /tmp/make.without-default-rules.log

From the above output:

  • Disabling GNU Make's built-in rules and variables with -R command line option speeds-up dependency checking by +33% -- the elapsed time. The system time is the cost of stat syscalls used for dependency checking.

  • -d command line option also logs GNU Make's dependency checks. Specifying -R command line option halves the number of log lines.

4

u/ts826848 Mar 28 '26

Hrm, interesting. I'll have to try to remember to experiment with -r/-R next time I have the opportunity.

Benchmarks of Make vs Ninja universally report Ninja shorter times relative to GNU Make in this particular use-case and for clean parallel builds.

For what it's worth I have a different recollection than you do with respect to the bolded part. The benchmarks I remember seeing included both incremental and clean builds.

As a quick sample, a cursory search turned up this 2016 article, this 2015 article and this article from the Meson devs, all of which include both clean and incremental builds. The 2016 article used make -rR as well.

You seem to be more plugged into this topic, though, so I wouldn't be surprised if you're aware of more benchmarks than I am.

1

u/max0x7ba https://github.com/max0x7ba Mar 29 '26

For what it's worth I have a different recollection than you do with respect to the bolded [and for clean parallel builds] part. The benchmarks I remember seeing included both incremental and clean builds.

That https://david.rothlis.net/ninja-benchmark/ you cite benchmarks make -rR -- kudos to the benchmark author for being literate. (Option -r is just a lesser -R, though, -R implies -r; just -R is adequate and sufficient.)

In that benchmark, clean build times are identical. That demonstrates that ninja is unable to execute the exact same build commands faster than make -R, as expected.

Clean builds do not require examining any auto-generated header dependencies because a non-existent target file has to be built unconditionally.

In that benchmark, the no-op build times are the times it takes to execute dependency check stat syscalls. make -R taking orders of magnitude longer time for dependency checking, which the author correctly attributes to processing auto-generated header dependency .d files, is caused by make -R examining orders of magnitude more dependencies than ninja does.

Next, the author just gives up with «I don’t know if this could explain the difference in average build times, perhaps due to higher resource usage by the linker leading to less effective parallelisation – but then why was the build just as fast as Ninja that one time?»

And that's the pattern of most benchmarks of Ninja vs GNU Make: ninja is faster than make, i don't know why, the end.

«Make’s output tends to be a bit noisier than Ninja’s, so the performance measurements may be unduly impacted by IO to the terminal» implies that the author uses a terminal with poor I/O performance, such as gnome-terminal or konsole -- they both choke on voluminous outputs, block the output producers in write syscalls. That's the reason I had to test drive other terminal app and switch to Alacritty because Alacritty doesn't choke at all and burns at least 3× fewer CPU cycles than konsole when running htop.

Benchmarks authors universally fail to uncover and identify the exact root cause of make -R taking orders of magnitude longer time for dependency checks.

The root cause is makefiles checking more dependencies than ninja does. make -R -d logs dependency checks and uncovers the unexpected accidental dependency checks. Explicitly cutting dependency chains and their unnecessary checks with with no-recipe rules, e.g. %.d: ;, %.cc: ; %.mk: ;, and generating header dependencies with -MP option, is often all it takes to make ninja uncompetitive relative to make -R.

2

u/ts826848 Mar 29 '26

Next, the author just gives up with «I don’t know if this could explain the difference in average build times, perhaps due to higher resource usage by the linker leading to less effective parallelisation – but then why was the build just as fast as Ninja that one time?

And that's the pattern of most benchmarks of Ninja vs GNU Make: ninja is faster than make, i don't know why, the end.

I think this is a slight misread. The "giv[ing] up" is with respect to trying to explain the observed difference in average clean build times and takes place before discussion of the no-op build results. Since dependency checks don't matter for a clean build, as you said, and times should be dominated by whatever the compiler is doing, it's less clear where the difference/variation comes from in this case.

«Make’s output tends to be a bit noisier than Ninja’s, so the performance measurements may be unduly impacted by IO to the terminal» implies that the author uses a terminal with poor I/O performance, such as gnome-terminal or konsole

You might be interested in the very next sentence:

So for fresh builds I have redirected all output to /dev/null, except where otherwise noted.

This is also reflected in the charts where the labels indicate the presence/absence of redirection.

Benchmarks authors universally fail to uncover and identify the exact root cause of make -R taking orders of magnitude longer time for dependency checks.

The author comes to a similar conclusion as you, I think?

On the largest project (where a no-op build with Make is taking 70 to 100 seconds) Make is spending 98% of its time processing the 100,000 “.d” files with the header dependencies.

Unfortunately they don't create a Makefile that tries your proposed solution.


Interestingly, it looks like the benchmarks are relatively easy to reproduce. Most of the file generation is handled by gen-test-case.pl in the tup repo, and the script that produces the exact Makefile/ninja.build is provided in the blog post.

A small snippet of the generated Makefile for your viewing pleasure:

%.o: %.c
        gcc -MMD -I. -c $< -o $@
./prog: ./0.o ./1.o ./2.o ./3.o ./4.o ./5.o ./6.o ./7.o ./8.o ./9.o
        gcc -o $@ $^
-include ./0.d
-include ./1.d
-include ./2.d
-include ./3.d
-include ./4.d
-include ./5.d
-include ./6.d
-include ./7.d
-include ./8.d
-include ./9.d
linux/prog: linux/30.o linux/31.o linux/32.o linux/33.o linux/34.o linux/35.o linux/36.o linux/37.o linux/38.o linux/39.o
        gcc -o $@ $^
-include linux/30.d
-include linux/31.d
-include linux/32.d
-include linux/33.d
-include linux/34.d
-include linux/35.d
-include linux/36.d
-include linux/37.d
-include linux/38.d
-include linux/39.d
# etc...

Presumably one could modify the script in the blog post to produce a "proper" Makefile but I don't think I'm the right person to do that :/