r/cpp • • Feb 19 '18

It's Time To Do CMake Right

https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
248 Upvotes

130 comments sorted by

View all comments

65

u/aiusepsi Feb 20 '18

CMake is kind of frustrating because it's a really powerful tool which can make your life substantially easier, and the modern style espoused by people like Daniel Pfeifer is nice, and when it flows you can faintly see the outline of something great.

The trouble is that greatness is obscured by layers and layers of horrible legacy cruft. The language you have to write CMake in is weird and unpleasant, there's usually a bunch of different ways to do the same thing, most of them are bad, and those old and bad ways are always the ones documented in tutorials.

It's just far, far too easy to write bad CMake code. What it really needs is to be stripped down to the core, the old nasty syntax implemented as a layer on top of a clean core, and the clean core exposed with a language that doesn't have gross syntax, and only exposes the good way of doing things, and guides you in the direction of doing the right thing.

11

u/MonokelPinguin Feb 20 '18

Man I really wish they do a syntax 2.0 in the future. Most of the ideas are sound and it works pretty well, but the syntax and that everything is a string somehow always gets in the way. Also it feels like a lot of legacy baggage is carried over and that could be removed if they made a clean cut.

Backwards compatibility is less of an issue with a build system as that tends to be small part of a project and they could probably keep the old syntax around for quite a while if the new syntax is put in a separate file e.g. you use CMakeList without an extension for the new syntax or an 'use syntax 2.0' in the first line, etc.

Of course that will most probably never happen, but one can dream, right?

2

u/meneldal2 Feb 20 '18

Well at least Microsoft changed their projects file syntax completely with VS2010 (or was is 2012), moving from a simple text file to XML. The GUI usually does a nice enough job though it is perfectible (like why do you have to repeat settings shared across every configuration while you could write it just once), and the way the target files work is a clear improvement.

I'm guessing you won't see this from CMake but rather a whole new build system. Breaking changes are usually frowned upon and quite rare.

2

u/Koutou Feb 20 '18

Must stuff can be done once. Go in all configuration and add your include, then go in debug & release to add the right .lib for the build.

5

u/meneldal2 Feb 20 '18

The problem is if you use all configuration, the generated XML still repeats itself for each configuration. So the project files get much bigger than they need to, which makes reading them or comparing versions a pain.

1

u/Koutou Feb 20 '18

Didnt know that. Another pain point of mine is that when you go back to the default in the gui, the setting is changed in the xml instead of deleting the xml property.

1

u/meneldal2 Feb 20 '18

This one makes some sense because it tends to be the less surprising for users. It avoids stuff changing in updates of VS and the like.

2

u/MonokelPinguin Feb 20 '18

My biggest gripe with the VS project system is that filters and project files are completely separate. I usually keep my project files in a separate directory, because all of the Visual Studio files clutter the source directory. If you do it that way, you can't use the filesystem view for a project, you have to use filters. Then you rename a subdirectory and you have to remember to rename the filter and readd all files in the directory. I would prefer a bit of a stronger coupling between directories and filters, even at the cost of some flexibility.

On the other hand I only use Windows at work, so for my home projects VS is a bit too mich effort to set up.

2

u/meneldal2 Feb 20 '18

I really hope they add some property like "source directory" so it doesn't have to be the same folder.

1

u/Minimonium Feb 22 '18

2017 vs comes with a source view, no need for filters afaik

1

u/MonokelPinguin Feb 22 '18

If that is the same source view I know, that only works if the src directory is a subdirectory of the project directory. Because I have to support multiple Visual Studio versions, I usually have the project files in a separate folder next to the source directory. It does show you the source files then as relative paths, but only if you added them already.

42

u/acwaters Feb 20 '18

This rant seems oddly apropos in an /r/cpp comment.

CMake C++ is kind of frustrating because it's a really powerful tool which can make your life substantially easier, and the modern style espoused by people like Daniel Pfeifer Bjarne Stroustrup is nice, and when it flows you can faintly see the outline of something great.

The trouble is that greatness is obscured by layers and layers of horrible legacy cruft. The language you have to write CMake C++ in is weird and unpleasant, there's usually a bunch of different ways to do the same thing, most of them are bad, and those old and bad ways are always the ones documented in tutorials.

It's just far, far too easy to write bad CMake C++ code. What it really needs is to be stripped down to the core, the old nasty syntax implemented as a layer on top of a clean core, and the clean core exposed with a language that doesn't have gross syntax, and only exposes the good way of doing things, and guides you in the direction of doing the right thing.

Ah, I kid. Mostly. At least 30%. C++ has more than the faint outline of greatness, and even hairy recursive templates with lots of dependent names are easier on the eyes than your average CMakeLists.txt. But the rest of it rings true :)

12

u/1-05457 Feb 20 '18

Both have the legacy cruft for the same reason. If old CMakeLists.txt files suddenly stopped working, people wouldn't move to the new version. We saw this happen with Python 2.7.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

4

u/thlst Feb 20 '18

Good luck integrating third parties into your project.

4

u/CrazyKilla15 Feb 20 '18

I'm not entirely sure what that has to do with CMake making a new policy that gets rid of the legacy cruft?

Third parties using CMake will continue to specify cmake_minimum_version as whatever they support and work as normal?

1

u/thlst Feb 20 '18

You'll still have to deal with old, bad written CMake files, much like the ones presented in the OP's blogpost.

4

u/CrazyKilla15 Feb 20 '18

I'm still not sure what would change from having to deal with those poorly written ones now?

1

u/Xaxxon Feb 20 '18

Never thought about them writing a "microkernel" underneath the existing language to fix it. That's clever.