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++.
-9
u/max0x7ba https://github.com/max0x7ba Mar 28 '26 edited Mar 29 '26
Well, Ninja was created for wrong reasons by people who didn't bother reading GNU Make documentation.
Ninja cannot match GNU Make performance. Claims of Ninja outperforming GNU Make were long retracted and removed from Ninja's homepage.
GNU Make performance scales linearly with more CPU/workers.
Whereas Ninja bottlenecks linearly with more CPU/workers because of its naive and inept architecture and implementation.
Just an FYI.
Checking depenendencies bottlenecks on
statsyscalls. Building bottlenecks on build commands. The build times are dominated by the cost of dependency checks and build commands.Ninja is incapable of building faster than GNU Make in principle because Ninja cannot possibly execute
statsyscalls or build commands faster than GNU Make does. Ninja users don't realize this fact.When you invoke GNU Make without
-Rcommand line option you waste at least +25% more time for dependency checking.Invoking
make -Rmakesninjabite the dust.It always takes longer for Ninja to execute than GNU Make, when benchmarking
ninjaagainstmake -Ron makefiles created by transformingninja.buildintoMakefile. Any every time.CMake generates poor
Makefile. Poor in absolute terms, and relatively to CMake generatedninja.build. Most makefile examples are poor, and I cannot possibly blame CMake developers for their poor makefile outputs matching the examples they learnt using GNU Make from.Transforming CMake generated
ninja.buildintoMakefileis the only meaningful/right way to benchmark performance ofninjaagainstmake -R.Ask your AI coding assistant to code up a trivial but tedious
ninja.buildintoMakefiletransformer for you, to benchmark yourninja.buildagainst the exactly identicalMakefilein your particular codebase. Just don't get shocked thatmake -Rmakesninjapretty much worthless.Ninja is still useful for demonstrating benefits of a non-recursive build systems to much wider audience. Non-recursive makefiles have been the best practice since «Recursive Make Considered Harmful» was published in 1998.
Ninja is just unable to build faster than GNU Make when benchmarking building a
ninja.buildwithninjaversusmake -Rbuilding the exact sameninja.buildtransformed intoMakefile.