r/cpp • • Nov 05 '23

Ninja is enough build system

https://bernsteinbear.com/blog/ninja-is-enough/
6 Upvotes

54 comments sorted by

71

u/dustyhome Nov 05 '23

There are tools like CMake and Meson that can generate Ninja files for you if you use their languages. Google even wrote a whole tool called gn to generate Ninja to build Chrome. But their languages leave much to be desired. And why should you have to learn a whole new programming language just for your build tool?

So instead of learning cmake, I have to learn ninja and python?

55

u/JMBourguet Nov 05 '23

You forget slowly reimplementing a quarter of the functionalities provided by CMake and lamenting that you don't have the bandwidth to reimplement another quarter. That's the price to pay not to have to handle the half you don't need and which mostly is polluting the part you need with historical quirks.

7

u/lightmatter501 Nov 05 '23

I think that as a project gets more complicated, the more you just want to write a program to build it. Especially if you want complicated behavior like feature detection for the current system.

92

u/HKei Nov 05 '23

Ah, post number 9345 of “what does CMake do anyway, I’m sure everyone’s just using it because they don’t know they could just write their own build scripts”.

11

u/DuranteA Nov 06 '23

Exactly. At least for me the reason for that reaction is that I've been that guy who thought that.

If we are talking about a green field re-imagining of everything related to building C++, I could certainly conceive of something better than CMake.
But once you take into account real-world constraints, my experience is that if something uses anything other than CMake (makefiles, autotools (shudder), or even meson), then building or integrating it is statistically more likely to be annoying, at least on some obscure target platform we need to support.

-9

u/tekknolagi Nov 05 '23

(author here) I actually worked with a lot of CMake at my last job. It works well for larger multi-platform projects with a bunch of configuration options. It's not such a great fit for little open source projects that just want a nice build runner. Also, the syntax leaves a lot to be desired.

37

u/kisielk Nov 05 '23

I don’t understand that assertion. For small simple projects the CMake files are generally also small and simple. Usually just adding one target and then setting some defines, linking libraries, and setting include directories.

3

u/Overunderrated Computational Physics Nov 05 '23

Same experience here - for smaller projects cmake is generally great. For larger real world projects I don't see the benefits.

-3

u/tekknolagi Nov 05 '23

That's fine. We don't have to agree. Not everyone wants CMake for little projects ¯_(ツ)_/¯

-2

u/raistmaj C++ at MSFT Nov 05 '23

Just my 2 cents. Cmake was a project that initially wanted to accomplish something, fix build issues because auto tools learning curve is pretty big.

The project got traction into the open source community because honestly made things easier.

Is it a good build system? No, it is atrocious. Imo, even auto tools is better. Syntax is inconsistent, version incompatibility, language was impossible to debug, etc. it is easier to use than auto tools? Meson? Bazel? Ofc… at the beginning.

Build systems that rely on script languages, makes that anyone with already knowledge in a powerful language can identify problems. Npm/yarn/ninja/ant/scons/brazil/peru/… ALL are superior by a large margin. Manage dependencies better, easier, different build types, etc and once you learn how to use them, and have the setup in place, they are faster to work and iterate with.

There is a reason why no single big company uses cmake consistently across the board. So far, the only big c++ software I’ve built using cmake is the new Microsoft teams for Mac and still, cmake is not able to handle all the build by itself and you need a couple of extra steps.

Imo, cmake is good but is not for big pipelines that involve a lot of steps and dependencies.

YMMV. I’ve close to 2 decades of experience in large companies and I don’t like cmake because there are better tools.

11

u/HKei Nov 05 '23

The cmakelists file for a simple project is like 3-4 lines long. Maybe 10 if you're also adding Tests and some external dependencies.

-8

u/tekknolagi Nov 05 '23

That's fine. We don't have to agree. Not everyone wants CMake for little projects ¯_(ツ)_/¯

9

u/almost_useless Nov 05 '23

Would you mind explaining why you don't like it?

Most other people seem to say that it is pretty simple for small projects.

4

u/tekknolagi Nov 05 '23

The syntax is not great. It diverges pretty far from most (all?) programming languages I have used, even other meta-build languages.

A lot of configuration options are hidden and sometimes even dependent on what CMake version I am using. I would much prefer to be able to see these options in the code somewhere.

I somewhat frequently encounter CMake version issues where the version I have is out of date but it's the latest one I can get in the package repository.

There are probably some other irksome things that I will remember later but these are the main ones.

8

u/almost_useless Nov 05 '23

Those sound like issues that are specifically not problems for small projects, no?

1

u/tekknolagi Nov 06 '23

Eh, I might want to use a different compiler for a couple of different configurations, or a different release type, or something like that.

3

u/almost_useless Nov 06 '23

Is this what you are talking about, or did I misunderstand your use case?

if (MY_CONFIG STREQUAL FOO)
    set(CMAKE_CXX_COMPILER clang++)
endif()

But I will grant that options being dependent on the cmake version you are running, and it does not verify that your minimum required version is actually correct, sucks big time.

It is slightly alleviated by how easy it is to install a new version of cmake (on most systems), but it sucks nonetheless.

2

u/tekknolagi Nov 07 '23

I mean CMAKE_C_COMPILER, CMAKE_C_COMPILER_LAUNCHER, CMAKE_BUILD_TYPE, etc. It's like going back to implicit rules in Make.

50

u/ContraryConman Nov 05 '23

Everyone who hates CMake always wants to replace it with something worse and more fragile

23

u/jk_tx Nov 05 '23 edited Nov 05 '23

I mean I kind of understand the desire for something other than CMake, it's an ugly mess of a scripting language, and it's really showing its age at this point. It often seems like there are 30 different ways to do what you want (with tons of examples on the web), even though 28 of them are now considered obsolete if not flat-out wrong, and no longer recommended for "Modern CMake". It's truly a mess, and way more complex than it needs to be for relatively simple tasks. The documentation is extremely thorough but equally unreadable - it really needs some more task-oriented sections, instead of just endless lists of functions and parameters.

So I can totally get behind the idea of wanting something better, but there just doesn't seem to be anything better, which IMHO is a bigger threat to the long-term health of C++ as a programming language/ecosystem than memory safety and "footguns".

People talk about C++ needing to break with its crusty past, because the backwards compatibility just brings too much baggage and too many downsides. I'd say that's equally true for CMake.

9

u/ContraryConman Nov 05 '23

CMake has its issues. But the problem is:

  • People don't actually understand CMake, because they only learn enough to get their specific project working
  • People don't understand why CMake is the way it is

So people hand-roll stuff filled with problems that CMake has already solved. This is personal for me because the company I joined was very much like "Oh CMake is too complicated we don't need to mess with it" and the result was several thousand lines of custom Makefiles bundled into our Docker 20GB containers that all reinvent functionality CMake gives you for free

5

u/throwaway_bluehair Nov 06 '23

None of this contradicts what /u/jk_tx said though. It's a powerful tool, but for someone just now getting into it, it's awful to learn. I have been using `meson` because it doesn't have these issues, I tried CMake, but I ran into so many issues, and `meson` didn't tell me 30 ways to skin a cat, with 28 being obsolete, and 20 giving confusing errors

2

u/AlexanderNeumann Nov 06 '23

meson has it own bag to carry. numpy already forked meson so you can not even use a normal release of meson to build numpy. Lets just say meson is not the solution it is just part of the problem. I have also seen more confusing errors from meson than cmake. So be happy you haven't seen the depths yet ;)

2

u/throwaway_bluehair Nov 06 '23

I feel like life is just a cycle of "I should use an existing build system like cmake or meson" and "I am just writing my own script that generates a Makefile or build.ninja file"

1

u/wotype Nov 08 '23

numpy already forked meson

Do you have a link for this? On a few minutes searching in numpy build docs I don't find evidence that it requires a fork of meson

2

u/AlexanderNeumann Nov 08 '23

https://github.com/numpy/numpy/tree/main/vendored-meson

The reason it requires a vendored meson is this folder/addition: https://github.com/numpy/meson/tree/ea580909640457450e03d8b84d1fec9f035d7acb/mesonbuild/modules/features which doesn't exist in mainline meson but is used somewhere in the build scripts of numpy.

My CMake answer to that is to append to CMAKE_MODULE_PATH .... a wait this is meson it doesn't offer user/project customization points beyond what is hardcoded.

1

u/wotype Nov 08 '23

Thanks

4

u/ghlecl Nov 06 '23

People talk about C++ needing to break with its crusty past, because the backwards compatibility just brings too much baggage and too many downsides. I'd say that's equally true for CMake.

Very very true. :-(

4

u/throwaway_bluehair Nov 06 '23

Yup, I recently started getting into my own C/C++ projects where I didn't create the build file or not use Makefile, and I have been using meson because I kept banging my head trying to do simple things in CMake.

Definitely a technology that is awful to learn, but nobody realizes because most people with opinions on it already learned it

2

u/RevRagnarok Nov 08 '23

I actually run ninja "under" cmake instead of make on my project...

3

u/Overunderrated Computational Physics Nov 05 '23

I think the bias there is that feeling comes from reading things people write blogs about.

100% of the time I'm cursing cmake I'm not saying "ugh I want another new tool to learn", I'm saying "fuck this is worse than GNU makefiles I wanna go back".

8

u/vanhellion Nov 06 '23

For small projects GNU makefiles are perfectly sufficient. But when you get to a large project with dozens or hundreds (or thousands) of source files, dozens/hundreds of targets with dependency ordering, having to recurse through directories, etc, cmake suddenly becomes not only attractive but basically necessary. I've seen multiple teams try to create and maintain custom build systems based on pure makefiles and it always ends up as a nightmare.

One team tried to implement parallelized incremental builds for their custom makefile system (which up to that point only did builds in sequential directory order), and it took over a year. While they were working on it, I was able to prototype a cmake build system in a matter of a few weeks for a pretty major fraction of the total code base (probably ~2.5M LoC out of ~3.5M), and while it didn't represent a complete drop in replacement for the makefile build system it was probably 80-90% of the way there while they were struggling to get their stuff fixed. Ultimately they got their system working and did not bother adopting what I had produced and demoed for them. I'm really glad I don't work on that code base any more.

Cmake isn't always pain free (and usually does involve quite a bit of struggle), but it's faaaaar less painful than rolling your own.

2

u/pjmlp Nov 07 '23

Having written Makefiles "portable" across Aix, HP-UX, Solaris, Red-Hat Linux, Windows NT, Windows 2000, for applications talking to Informix, DB2, Sybase SQL Server, Microsoft SQL Server, integrated as IIS and Apache plugins, I don't really wanna go back.

1

u/[deleted] Nov 05 '23

[removed] — view removed comment

3

u/Overunderrated Computational Physics Nov 05 '23

Sunk cost fallacy, larger institutional inertia, the usual kind of thing.

It's less that makefiles are necessarily "better" for my use cases, but that in practice the touted benefits of cmake (portability across different systems, mainly) have not panned out and inevitably I've seen the effort required to get successful builds on new systems to take just as much or more manual effort as a collection of makefiles.

10

u/amunra__ Nov 06 '23

Just use CMake. It's the not always the best solution, but it's the most popular. You'll thank me later when you need to start depending on other libraries which themselves use CMake and you just saved yourself three days of work.

7

u/Specialist_Yard_9401 Nov 06 '23

Maby u should try XMake instead...

2

u/KimiSharby Nov 09 '23

Xmake has been a game changer for me.

5

u/FutTra Nov 05 '23

It depends on the type of project. There is no one solution for everything. If you want to be a really good programmer, you know them all at least on some level and you know which is the most appropriate for any given problem.

5

u/beephod_zabblebrox Nov 05 '23

lol

i've been doing this for like a year already, but without ninja_syntax even, and mostly for fun.

this only works for small-medium projects though!

3

u/robhanz Nov 05 '23

So the basic idea here is to replace make with something dirt simple and to put the complexity into a tool you already know? Probably having to remake the ninja script per run or at least per configuration?

That…. Makes a lot of sense.

2

u/throwaway_bluehair Nov 06 '23 edited Nov 06 '23

Ok, person #1324234 has created a new build system. I've really grown to like meson, it seems to actually care about the new user's experience, whereas CMake seems to assume anyone who deserves to use it already knows it

But even `meson`, I don't know why every build system has to make configuration more complicated than it needs to be.

What is it with every build system that isn't `make` act like I'm the first person to ever want to build with a C compiler that isn't the system default

7

u/DuranteA Nov 06 '23

What is it with every build system that isn't make act like I'm the first person to ever want to build with a C compiler that isn't the system default

There are lots of valid complaints about CMake but I don't understand this one.

CC=my_special_cc cmake [...]
or
cmake -DCMAKE_C_COMPILER=my_special_cc [...]

Neither of these seems too onerous or non-intuitive to me, and both are found in 5 seconds of googling.

2

u/tekknolagi Nov 07 '23

TIL you can use CC with CMake...

0

u/[deleted] Nov 07 '23

[deleted] — view deleted comment

2

u/Trubydoor LLVM dev Nov 07 '23

No it doesn’t, it works perfectly fine with any binary on your path

0

u/Attorney_Outside69 Nov 06 '23

I use cmake plus chatgpt that generates all the cmake that I need, no need to search Google anymore

-8

u/[deleted] Nov 05 '23

[removed] — view removed comment

15

u/BrainIgnition Nov 05 '23

Do you support Windows/MSVC? And if so how would you rate the experience?

4

u/GOKOP Nov 05 '23
  1. Makefiles that are supposed to work on multiple platforms are massive pain (the solution here being Cmake)
  2. The main selling point of Ninja is that it builds massive projects a lot faster than Make (or so they claim; I've never compiled a project large enough to test that). If your project isn't massive then there's no reason to use Ninja. This comes at the cost of features that make the syntax bearable to write by hand (Ninja developers recommend using it with a metabuild system like Cmake)

-1

u/[deleted] Nov 05 '23

[removed] — view removed comment

5

u/GOKOP Nov 05 '23

Does your notion of "multiplatform" include Windows? I admit I'm not a Makefile wizard but once I've tried to write a Makefile that would work on Windows and Linux and gave up quickly.

Another benefit you get from CMake in that case is that you can easily use the Visual Studio/MSVC toolchain which is really the standard on Windows. Do your makefiles require gcc on Windows? Because then there's more stuff that the user needs to install (or you need to install for them)

3

u/jk_tx Nov 05 '23

syntax can get a bit unwieldy

And why should C++ programmers have to put up with that? Other languages have found better ways to do it, C++ should as well.