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++.
3
u/Mikumiku_Dance Mar 28 '26
i think I'm using header units in gcc 15. I'm just experimenting in modules so its not production grade at all but it was useful to deal with libs that don't have modules of their own defined.
2
u/kronicum Mar 28 '26
Where can I find the source of your implementation to play with?
7
u/ravixp Mar 28 '26
I’m confused about why you brought Ninja into it. You’ve got code to enumerate the source files, you’ve got topological sort based on dependencies, you’ve got the ability to invoke the compiler, and that’s everything that Ninja is doing for you. It sounds like you could drop that dependency and do everything yourself!
(Or you could go the other direction and use Ninja for all that stuff, invoke it twice and make your tool into a higher-level build tool like CMake. But if you’re writing your own build system for fun, that sounds like the less fun path. :)
2
u/SilkHart Mar 30 '26
Ninja is definitely the way to go once you hit that 10k+ scale. Make starts to feel really sluggish at that point. once the codebase gets that massive, the local hardware becomes the biggest bottleneck anyway. We ended up using Incredibuild to spread those builds across the grid just to keep the devs from losing their minds. It is basically a necessity at that enterprise level to keep the feedback loop from falling off a cliff.
3
u/Crierlon Mar 28 '26
For my game, I built a internal build system and memory safety profiler for my C++ code.
Basically bringing Rust benefits into C++. My main advice is don't be opinionated. Some devs will be "that guy" who likes Scons instead of Cmake.
-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.-2
u/zerhud Mar 28 '26
There is a major issue in all build systems: the information about how to build is not in cpp, so we cannot use it inside code in compilation time.
For example we have two components, I want to write communication layer for them. The layer have to know is it a single process or two micro services for choice channel: just api, shared memory or network. The information exists in makefile, but cpp cannot get it, or we can describe it inside code, but a build system cannot get it for build.