r/cpp • u/TheRavagerSw • 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++.
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
-ror-Rarguments.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 -rormake -Rdisable 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
makedoesn't re-build anything, try the following commands (my outputs come from a small propriatary codebase using modern non-recursive makefiles):From the above output:
Disabling GNU Make's built-in rules and variables with
-Rcommand line option speeds-up dependency checking by +33% -- the elapsed time. The system time is the cost ofstatsyscalls used for dependency checking.-dcommand line option also logs GNU Make's dependency checks. Specifying-Rcommand line option halves the number of log lines.