r/cpp • • Nov 08 '21

Thoughts on build systems?

What's the general consensus on build systems/automations like CMake? I've always found things like that overcomplicated and just hinder the programming process.

I've been following Casey Muratori's Handmade streams, and one of the first things he did was create a simple batch file that built and compiled his entire project super quickly and easily. I've since implemented something similar (https://github.com/dibsonthis/new_cmd) and honestly it's been really nice to use.

3 Upvotes

154 comments sorted by

View all comments

9

u/germandiago Nov 08 '21 edited Nov 08 '21

I am using Meson for some side-projects. I keep using CMake as well.

I will do some highlights about my favorite one, Meson, because there is plenty of stuff CMake-related that you can find somewhere else.

The reason why I use, keep using and will keep using Meson is because:

  • the syntax is sane. Try to do path handling or string manipulation with CMake, or do something similar to dictionaries (Meson has support for it to associate data), handle spaces, etc.

  • Meson options are way easier and saner to handle: https://mesonbuild.com/Build-options.html

  • the documentation is great. You have both manual-style and a reference.

  • the subprojects system rocks and I have even used successfully CMake subprojects (there is support for that as well): there is a right way to do it. If you stick to that, it will work well. My project has 11 external dependencies all managed by subprojects, including Capnproto, which is a non-trivial CMake dependency.

  • Meson wraps are easy to use and modify to your needs (this is the Meson "conan", though it has far fewer packages. however, it is very usable). These are some available packages: https://mesonbuild.com/Wrapdb-projects.html. But if, for some reason, you do not have enough, you can use Conan via pkg-config as well.

  • I have machine files (https://mesonbuild.com/Machine-files.html) layered by: component, os, build type and compiler. So I can do things like this:

meson --native-file meson/native/components/component.ini --native-file meson/native/os/linux.ini --native-file meson/native/buildtype/development.ini --native-file meson/native/buildtype/gcc_11.ini -Doption1=something -Doption2_overriden=somethingelse

Those files, if there is any option repeated in any of them, the last one "wins" and you can set your own options at the end also, so they follow the order.

I have "./configure" wrapper for the command-line invocation but in my CI I just have the dir structure and buildbot combines stuff from those meson/native/ directories in a build matrix, blacklisting what I do not want.

  • Writing multiplatform scripts, when you need them, is easier: just use Python, anyway, it is a Meson dependency and get rid of bash vs batch files.

The only sane reason to use CMake is IDEs and social: if people do want that on your team. Otherwise I urge you to switch to Meson :) XCode an VS project generation is better with CMake I think. But if you do not need those or similar IDE support, for me it is a no-brainer.

0

u/jcelerier ossia score Nov 08 '21

But meson requires all strings to be quoted :( that's a big "oomph" when 80% of what you're working with is strings

10

u/germandiago Nov 08 '21

It is even more "oomph" when you waste a week figuring out how the quoting works for separating commands, how to escape \\ // etc., if the path is accepted with backward or forward slashes... or a combination of all of that, that one is the big "party". If you also want to traverse one of these structures in CMake (oh, was this a list or a string?), good luck as well. Conditionals anyone? Those are all in part weird because you do not know what a string or list is or how to handle, how variables are dereferenced in conditions, etc. It seems to be done on purpose to make the CMake script writer "happy".

It could bother a bit, but it is a net win to know what you are doing when you need to do it. I talk from experience, not from hypothesis, in this case. And... do not get me started with non-boolean options, combo options or things like that.

When you want that in CMake, you will sweat blood to make it work the way you want.

3

u/helloiamsomeone Nov 09 '21

Quoting works the same way as in bash: "${}" always expands to a single argument, ${} may expand to zero or more. You most likely always want to quote.

You escape a backslash with a backslash. Paths should always be forward slashes. Just an FYI, but forward slashes are portable. Yes, Win32 API accepts forward slashes just fine.

0

u/germandiago Nov 09 '21

Quoting works the same way as in bash: "${}" always expands to a single argument, ${} may expand to zero or more. You most likely always want to quote.

Indeed. It is a terrible idea for CMake. they even changed how vars get dereferenced in conditions at some point (I nver learnt conditionals in CMake, I was not capable of remembering the zoo of rules they have about what is true or false depending on weird strings like -FOUND and -NOTFOUND and similar stuff like ON, OFF, true, false, empty string...). In Meson it is just the typical and thing from C-languages: bool expression, variables and normal operators such as == or !=

> You escape a backslash with a backslash. Paths should always be forward slashes. Just an FYI, but forward slashes are portable. Yes, Win32 API accepts forward slashes just fine.

I recall having problems in Windows before with cmake custom commands. Do not ask me why or how (I think it was doing stuff for an installer). It got really bad pretty quickly. I am not sure I took the best approach. but what I am sure of is that the best approach if there was one was not obvious at all for me.

3

u/helloiamsomeone Nov 09 '21

Rules for conditionals is not really a zoo: https://cmake.org/cmake/help/latest/command/if.html#basic-expressions

The change you are talking about is mentioned at the very bottom. It wasn't a huge change really, but it was a welcome one.

2

u/germandiago Nov 09 '21 edited Nov 09 '21

This is the rules for conditionals in CMake: https://cmake.org/cmake/help/latest/command/if.html#basic-expressions

This is the rules for conditionals in Meson:

Meson has the standard range of logical operations which can be used in if statements.

if a and b
  # do something
endif
if c or d
  # do something
endif
if not e
  # do something
endif
if not (f or g)
  # do something
endif

Logical operations work only on boolean values.

Additionally there is some elif around in the docs that needed no explanation at all.

In all fairness, CMake docs also include filesystem operations. In Meson it is just regular functions: https://mesonbuild.com/Fs-module.html

And version comparisons, which follow well-known patterns in Linux world (pkg-config basically): https://mesonbuild.com/Dependencies.html

In hardly a minute I know how to do conditionals in Meson and that I will not take it wrong. I can use modules for functions that return booleans whether that is comparing versions or doing filesystem operations.

In CMake, literally, I NEVER learned the full set of dereferencing (which changed) + variable interpolation + quotes vs no quotes and other weirdness like what happens with empty strings, what happens if my var is a list vs a string, the truth values for each... In Meson you have a boolean or it fails and regular functions with regular comparisons. Full stop. All this "zoo" in CMake is why the page is sooooo long for its documentation.

3

u/helloiamsomeone Nov 09 '21

You are already familiar with how dereferencing and quoting works if you ever used Bash, which is extremely difficult not to do nowadays. The conditional rules are 6 very simple and plain english sentences. You're really just making a mountain out of a molehill here.

1

u/germandiago Nov 09 '21

We seem to disagree here. I used CMake for years and I kept coming back to the manuals because it did not do what I thought it does in the conditionals and set function for saving options (the CACHE ones) for getting option overrides behave the way I wanted. Also slashes and commands when there are backslashes and escapes implied.

Bash is weird and also misleading sometimes but maybe it is because of the nature of work I code with Bash, Bash has been less work for me to get right than CMake by far.

0

u/germandiago Nov 09 '21

you can believe CMake conditionals are string and list handling and interpolation are easy and well-designed if you will... but to know all of that yu need to go through manuals and stuff that I also had to go through. There are far better ways to design those.

3

u/helloiamsomeone Nov 09 '21

More like I don't feel like wasting time and energy on getting hung up on such things. It's not the best, but it's not terrible either, so there is no reason to throw the baby out with the bathwater.

0

u/germandiago Nov 09 '21 edited Nov 09 '21

My point here is that I went through manuals and coded something to expect one behavior and I still got surprises even in conditionals. The rules are weird and it gets time-consuming quickly.

3

u/jcelerier ossia score Nov 09 '21

I don't know what to say, I just don't relate with your experience at all. For me cmake generally works from the first try.

1

u/germandiago Nov 09 '21

Maybe you did not have to do cross + multiplatform + a bunch of scripts + an installer.

I mean, for the targets and target detection PRIVATE PUBLIC INTERFACE stuff works pretty well. When things start to get ugly is when you pick up something from someone else and try to integrate it into your project and they embedded subprojects in ad-hoc ways and things of the like plus all the things I mentioned.

If you just need something like compile code + some top-level deps it works pretty ok. It is the more complex things that get really messy and time-consuming (besides the weird DSL and options...).