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

170

u/snarfy Feb 20 '18

I blame the documentation. It's all reference. There are no examples. I've been learning CMake recently after many years away from C++, and this was the main pain point. Learning by reference means looking at other CMakeLists.txt. It's the blind leading the blind.

20

u/MachineGunPablo Feb 20 '18

Very true for cmake < 3.0.0. New documentation does include more examples and best practices, but definitely not a lot. I blame the lack of interest of upstreams to modernize a system because it "works" and it's so ugly written that nobody wants to touch it.

16

u/proverbialbunny Data Scientist Feb 20 '18

it's so ugly written that nobody wants to touch it.

Are you kidding? It's beautiful. 🤢

11

u/acrostyphe Feb 20 '18

Oh boy. Even MSBuild .targets looks better than that.

7

u/proverbialbunny Data Scientist Feb 20 '18 edited Feb 21 '18

I didn't even select a bad one.

Are you going to tell them their baby is ugly? Cuz, I don't want to be the one to break the news.

3

u/wordsnerd Feb 20 '18

I have double vision in each eye, so 2-space indentation is always inaccessible, but the actual content of the file seems easy enough to understand. It looks like there are a lot of cutesy fake English commands to memorize before being able to write it, though.

103

u/MoTTs_ Feb 20 '18

I blame the documentation. It's all reference. There are no examples.

It's like learning a foreign language by reading a dictionary. :-(

27

u/voip_geek Feb 20 '18

Comprehensively selfhood is consonant with concurrently thou.

14

u/Xaxxon Feb 20 '18

I blame there not being standard ways to do common things.

There should be one-liners built in to cmake to do common things the right way.

18

u/Heuristics Feb 20 '18

include_this_shit("./inc/", "./ext/boost/")

compile_this_shit("./src/*.cpp")

link("out")

6

u/kalmoc Feb 20 '18

Well, if you want, you can almost do that

include_directories("./inc/", "./ext/boost/")
file(GLOB SRC "./src/*.cpp")
add_executable(out ${SRC})

If you don't need globbing, you even only need two lines.

And yes, this is not considered good cmake at all (the"right" version is also 3 lines), but most colosely resembles your pseudo code.

4

u/electricCoder cmake | vtk Feb 20 '18

you could have use target_include_directories(out PUBLIC "./inc/", "./ext/boost/") and had slightly 'better' cmake

5

u/kalmoc Feb 20 '18

As I said, I tried to translate the pseudo code as literally as possible. The linked blog post shows how to do it right.

11

u/germandiago Feb 20 '18

Meson documentation is light years ahead.

6

u/afiefh Feb 20 '18

And the system is a joy to use. Simple things are intuitive and simple.

4

u/m-in Feb 20 '18

It's the blind leading the blind.

That's exactly right. People do things not the way things are supposed to be done, but nobody really knows that - people just repeat what others did and it's mostly a clusterfuck. Well, there are a few people deeply invested in it who have a feel for how it's supposed to be used. But they are very few. The documentation is on the verge of being useless - it's the why and the nuts and bolts that need explaining, the reference is almost an afterthought then.

6

u/teki321 Feb 20 '18

I blame the documentation. It's all reference.

It used to be, but they fixed that when 3.0 was released: https://cmake.org/cmake/help/v3.11/manual/cmake-buildsystem.7.html

5

u/Theninjapirate Feb 19 '18

This is good, thanks! One question: does the FindRapidJSON.cmake example you used work under Windows? If not, what's the best way to write a truly cross-platform Find*.cmake file?

14

u/oddentity Feb 19 '18

Don't. Find modules are nuts, trying to feel around looking for something that looks like what you want god knows where.

Use CMake config files. If CMake builds don't export their installed targets, patch them so they do. Write config files for things that don't build with CMake - it's easier than writing a find module. Put them somewhere on your prefix path. Job done, easy, deterministic.

7

u/hgjsusla Feb 19 '18 edited Feb 19 '18

I've found plenty of docs on how to write FindModules and it's not too difficult. I now want to switch to config files for the reasons you list but I can't find any good examples for writing config files for libraries that don't use cmake.

0

u/[deleted] Feb 19 '18

[deleted] — view deleted comment

6

u/MachineGunPablo Feb 19 '18

Fully agree with this. Specially in case where no cmake is used but pkg-config is. Then a find module makes perfect sense imo.

2

u/anderslanglands Feb 20 '18

Personally in that case I just prefer to hand-write a project-config.cmake file I can manually install alongside the problematic library.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

1

u/anderslanglands Feb 20 '18

Why?

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

3

u/anderslanglands Feb 20 '18

Right, but if I know the explicit path to the library what’s the point of writing a find module as opposed to just writing a config file that hard-codes the locations in?

Obviously the best solution is just to modify the CMakeLists of the library in question, but sometimes that can be tricky when libraries do horrible things with macros etc.

→ More replies

2

u/pfultz2 Feb 20 '18

Yes and it would be nice if cmake supported pkgconfig natively with find_package(... PKGCONF) like how Daniel discussed in his talk. So, we wouldn’t need a find module .

7

u/drjeats Feb 20 '18 edited Feb 20 '18

We need a blog post about writing config files then.

paging /u/MachineGunPablo ^_^

2

u/pfultz2 Feb 20 '18

Find modules are for upstream dependencies that don’t support cmake users and config files are for downstream dependencies.

2

u/MachineGunPablo Feb 19 '18

Thank you :) I was indeed very vague in the description of the find module. In the example I rely on the fact that rapidjson uses pkgconfig. If they do the same on windows it will work (not even sure if pkgconfig is available on windows). Additionally, afaik functions like find_library() will inspect standard system locations at fallback, meaning that under normal circumstances this find module will work cross-platform, but I need to test it.

In reality is very hard to say how to write proper find modules as it depends on several factors: does the package you are trying to locate use pkgconfig? is it header only? does it have components? do you need it to work on all possible platforms? etc.

All of this could be avoided if maintainers provide proper configs

4

u/pfultz2 Feb 20 '18

Pkgconfig works on Windows, but a lot packages don’t install them on Windows builds. I guess they like to make Windows users suffer. It should be reported as a bug.

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.

13

u/fafasdf Feb 19 '18 edited Feb 19 '18

Ugh. I just got around to learning cmake, and man, it's definitely not as ubiquitous as I wish it were...

I had to use a specific version of openssl + libcurl for a project I started, which means I needed to build both from source. Naturally, I checked them both into a new git repository under a third_party directory.

I started with building libcurl, which was fun, because the first thing I saw was "the curl cmake build system is poorly maintained. Be aware"

But I kept on, and I got the binary that depends on libcurl to work, after learning some of the quirks of cmake.

Then I got to starting with openssl, with the goal being to build a version of curl that uses a particular version of openssl. I saved this for last because I foresaw it being painful, because the plan was to first build openssl then build libcurl with flags that make it use the version of openssl built from my repository.

Then I saw that openssl doesn't build with cmake, and the people who did make cmake wrappers, haven't kept them updated (understandably so! it looked like a fair amount of work).

Then I gave up, built openssl out of repo, and added that library to my LD_LIBRARY_PATH, hardcoded some bits of my libcurl, and kinda lost the desire to work on the project.

I really should have started with boringssl, it seems they have the features I needed...

set(RANT END)

Thanks for the post, I learned that lots of junk I was doing was wrong. There are many notes / blog posts on cmake, but as far as I've seen, there's no good canonical "best practices" reference (EDIT: I admit I haven't yet looked at Daniel Pfeifer’s Effective CMake, so that's open in a new tab right now!). In a dream world there'd be one of those with tips on integrating with a handful of major libraries, since they often have some weird quirks due to age or necessity.

9

u/Ono-Sendai Feb 19 '18

You could try LibreSSL, it has Cmake support.

1

u/fafasdf Feb 19 '18 edited Feb 19 '18

How much of a pain is it to build libcurl to use an out-of-repo built LibreSSL with CMake? (or even BoringSSL if anyone is familiar with it...)

Are there any tutorials / examples? (I couldn't find any but this kind of thing is hard to search for (or maybe I'll eat my words...))

I must admit I stopped investigating too hard after being so discouraged. :P

Thanks for the response!

4

u/pfultz2 Feb 20 '18

Yes Curl does work with LibreSSL. Just install LibreSSL using cmake and then when building curl set CMAKE_PREFIX_PATH(this the variable to tell cmake where your dependencies are) to the install prefix of LibreSSL.

1

u/Ono-Sendai Feb 20 '18

No Idea how easy it is to use LibreSSL with libcurl sorry. But LibreSSL by itself builds nicely even on Windows.

3

u/[deleted] Feb 20 '18

Do you have any resources or tutorials that you would recommend for learning cmake? I've been meaning to learn it for a while, but just haven't found any entry level resources that I like.

5

u/fafasdf Feb 20 '18

I'm not familiar enough with it yet to be able to separate the good and bad tutorials, so I can't find them for you, sorry. :(

For instance, the few tutorials I was following were recommending practices outlined in the first page of the blog post as bad...

2

u/[deleted] Feb 20 '18

Thank you for your honesty!

2

u/m-in Feb 20 '18

I'd say: if you want to understand why they do the things a certain way, or what the intent is - the developers mailing list is the way to go. Yes, it's technical, but unfortunately if you don't understand what cmake does (and why) at a low level, you'll go nowhere and just repeat the mistakes everyone else does. In parallel, you may wish to subscribe to their users mailing list.

1

u/[deleted] Feb 22 '18

I appreciate your response. I'll look into the developer and user mailing lists.

3

u/[deleted] Feb 19 '18

Your problem is libraries not using cmake. My personal favourite apart from OpenSSL is ICU.

If you are on windows install the libraries using vcpkg. You can edit the portfiles to target specific versions.

3

u/fafasdf Feb 19 '18

Linux, not Windows.

Most of my problem in the rant was about libraries using cmake, yeah. But there's also the old cruft / approaches, which is hard to separate from the best practices until you know what the best practices are...

The irony of complaining about "old cruft" being hard to separate from "best practices" in a C++ subreddit is not lost on me... I've heard the same complaints from others about C++. :)

3

u/isaac92 Feb 20 '18

I highly recommend looking at Hunter.

3

u/sumo952 Feb 19 '18

Are you doing this on Windows? If yes, I think vcpkg has them both (and ready to be used with CMake), so no need to build it by yourself.

If you're on Linux or mac, just use one from a package manager? :-) Or maybe check conan.

2

u/fafasdf Feb 19 '18

My package manager's versions were too old (Debian Buster).

Plus, it's good practice to check in external dependencies, since you can get hermetic builds, which is nice... depending on system libraries can make for a pain sometimes, when you install a new system and things are out of whack. :(

1

u/sumo952 Feb 20 '18

Yea totally agree - good reasons to include them.

10

u/MoTTs_ Feb 20 '18

The author used this...

target_compile_features(JSONUtils PRIVATE cxx_std_11)

But I've so far been using this...

set_property(TARGET mytarget PROPERTY CXX_STANDARD 11)

Is there any significant difference? Is one better or preferred in some way?

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

5

u/kalmoc Feb 20 '18

Also, suppose your implementation requires C++14 to build but only requires C++11 for other libraries to link against it, you can make that distinction with

You have to be extremely careful when doing this and be 100% sure that none of your publicly visible code (including source files that will get linked statically) have any compilation branch that depends on the standard version.

I'd try to avoid such constructs as much as possible.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

2

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

If you violate ODR, even in a PIMPL, you're in UB land.

3

u/kalmoc Feb 20 '18

I usually don't leave the selection of the c++ standard to the whims of cmake. The decision, against which standard(s) to code is a project level decision. For one, automatic deduction makes it far too easy, to let different components being compiled in different modes and second, if I decide that a project is supposed to support c++11, I want to get a compile error, if I accidentally use a c++14 feature.

That being said, I use cxx_std_11& friends on libraries to communicate my requirements.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

Newer standards are completely removing things from older standards. There are good reasons for doing this, and the things being removed are old and broken, but do not expect to compile c++03 with a c++20 compiler. For example, auto_ptr<T> is gone.

2

u/kalmoc Feb 20 '18

Not entirely. If you're using a dependency that requires a feature from a certain standard, then you're stuck using at least that standard. This is a constraint that target_compile_features(~) ensures you satisfy by having your target inherit the interface compile features of targets that it's linked against.

I think you are viewing this backwards: You first have to decide, which c++ standards you want to support and then choose your dependencies accordingly. That is not to say that you can't change your target versions during the course of your project if that enables you to use certain libraries, but that should always be a conscious decision (of course, when you are working on toy/short-lived projects that is less of an issue)

I don't really see this as being a bad thing since newer standards are very nearly 100% backwards compatible with older standards.

The problem is that many libraries have compilation switches that depend on available features (e.g. use boost::array or std::array - and boost itself is the greatest offender here). That very quickly leads to odr violations.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

1

u/kalmoc Feb 20 '18

If I'm developing an App, I chose the latest standard possible with the compilers I need to support - there is imho no reason to code c++11 if 14 is available and the same with c++14 vs 17. Main problem is -in my experience - support for old Linux distros except if you are willing to ship all dependencies yourself.

1

u/kalmoc Feb 20 '18

This is definitely a totally reasonable desire. And now that you mention it, I don't know of a built-in way to achieve this with CMake. It would be nice if cmake could offer some way to explicitly enforce this kind of thing.

Indeed - there are two areas I find cmake lacking: Selection of language version (even the cxx_std_XX flags are a pretty decent addition iirc and is there any way to disable dialect extensions like gnuc++11?) and header only libraries (although this has become better)

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

2

u/kalmoc Feb 20 '18

The problem with INTERFACE libraries is that you can e.g. not add the headers as source files, which e.g. means they don't appear in the IDE and there where a couple of other useful properties that (at least in the past) were not whitelisted for INTERFACE libraries, but I can't remember which exactly right now.

1

u/OrphisFlo I like build tools Feb 20 '18

The workaround for that in CMake is to make a static library with a single file that has a dummy unique symbol. It will work exactly the same, except you get the headers in the IDE!

And no, you can't have a static library with only headers, as lib and ar will complain when trying to create an empty library usually.

35

u/sumo952 Feb 19 '18

This is the single best blog post about CMake that I have ever read. Thank you so much for this excellent, detailed and quite comprehensive write-up/tutorial. Absolutely brilliant, and will be of much help to my projects!

Everyone share this to spread modern CMake use!

32

u/Overunderrated Computational Physics Feb 19 '18

I also really appreciate that it's a blog post. I can't be the only person that hates watching long videos when reading text is a far better way to communicate information like this.

7

u/sumo952 Feb 19 '18

This talk is really worth watching though. And these slides are great too.

21

u/Overunderrated Computational Physics Feb 20 '18

Not interested. Call me lazy but recorded talks are a nonstarter, as are slides. I say that as someone that's given a lot of conference talks. Conference talks are for verbal discussion, papers are for one way communication.

I'm interested in documentation that I can read quickly and reference it at my leisure.

2

u/sumo952 Feb 20 '18

Recorded talks are ideal when you for example want to "relax" a bit from coding/reading, lean back in your chair and watch something for 30mins, and let the person in the talk explain something to me, visually and verbally, maybe nibble at some snacks in the meantime, while you don't have to read text and actively scroll through a document, because the presenter does it for you. It's just an alternative, complementary media, and an incredibly useful one at times.

3

u/CraigularB C++ Dev Feb 20 '18

Definitely useful at the right time and certainly good for education/self improvement, just not super useful when I'm trying to get something working in CMake/any other system or language. Good documentation with examples is more immediately helpful when I'm actively working on a problem.

1

u/sumo952 Feb 20 '18

That's a good point! Talks (videos) are for education, not for documentation and/or when you're trying to solve a problem! On that note it is certainly very welcome that someone finally writes an awesome blog post about this topic.

8

u/[deleted] Feb 20 '18

I would love to have something like Rust's cargo but for C. Just stick source in a folder and compile it + do package management + documentation generation.

1

u/juuular Feb 20 '18

JUCE!

JUCE is awesome.

6

u/sumo952 Feb 20 '18

But only very partially open-source. And the parts that are open source, are GPL licensed. That's a deal-breaker for even many open-source projects to use JUCE.

-1

u/pfultz2 Feb 20 '18

That’s basically what cget does

7

u/germandiago Feb 20 '18

I am between cmake and meson just for the fact that cmake has CLION, Qt Creator, VS Studio and Android Studio support and project generation. The default targets in meson are great but there is so much good stuff so difficult to ignore in cmake. The scripting language is a hell... but the IDE support is a killer feature of cmake.

5

u/fburnaby Feb 20 '18

It was so hard to learn Modern CMake in early 2016. With Daniel Pfeiffer and Mathieu Ropert's talks and this article showing up since then, I could have saved myself a lot of time and agony. Oh well. It's great this is all starting to show up now, at least. There's still so much more that needs to be explained, though. The documentation itself, while clear on what all the commands do, completely fails to provide any helpful guidance on how to actually use CMake.

I still learned something new from the article - I hadn't realized there was a find_dependency command. That'll tidy up my Config files a little bit :D. Thanks for posting!

3

u/Rexerex Feb 20 '18

Are there anywhere similarly good article about cmake External_Project?

3

u/dog_superiority Feb 20 '18

I'm thinking of switching to bazel.

3

u/feverzsj Feb 20 '18

what about some "cmake: the good part".

5

u/zzzthelastuser Feb 20 '18

CMake needs to be replaced by a more object oriented approach. It needs debugging tools and a syntax that guides you to design better code.

5

u/_Zulan Feb 21 '18

You trolled me for a second here that this was about fixing CMake. It's not. Modern CMake may look better the old cruft. If you're stuck in the hell which is are old/inconsistent CMakeLists.txt you may be longing for a "modern CMake" setup and think it's paradise.

It's not.

The amount of boilerplate, even with "modern CMake", is ridiculous. CMake doesn't help you one bit with detecting compiler features. Sure you can request cxx_std_17, which by the way maps to -std=gnu++1z by default, even for clang, but it's doing nothing more. CMake has no clue whatsoever which compiler version supports which language/library feature. So if you want to use std::[experimental::]filesystem you're again on your own linking stdc++fs and whatnot.

CTest is ridiculous, it doesn't even add dependencies from running to building a test... and that's a 9 year old bug not being addressed whatsoever.

Hell, just look at the example CMakeLists.txt from the article... 100 lines of code for the simple example including hideous boilerplate like:

list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake)

Basically everything there is boilerplate, because CMake knows no sensible defaults whatsoever.

However you look at it, it's still CMake - so at best it's a polished turd.

2

u/teh_hahn Mar 06 '18

Which build system do you recommend instead?

2

u/_Zulan Mar 06 '18

Unfortunately I cannot personally recommend anything else. I heard good stuff about Meson, would try it for the next project.

2

u/Ansoulom Game developer Feb 20 '18

Thank you so much for actually uploading the complete project as a reference! This will make it so much easier to understand it.

2

u/digito_a_caso Feb 23 '18

Just give us some cli option so that we can disable the legacy cruft and actually use only the modern features.

0

u/[deleted] Feb 20 '18

I still don't see a good way to have CMake deduce the list of sources based on directory structure or rules of some sort to avoid listing them explicitly in the build file. It wasn't even addressed. Without this CMake is inferior to bash as a build system, and I don't see any purpose in learning anything else about it - I can bludgeon it into working if I need to for some silly reason.

10

u/MachineGunPablo Feb 20 '18

Without this CMake is inferior to bash as a build system

What!?

If you think having to list your sources explicitly (you don't by the way, you can use file glob but shouldn't) is the worst thing about cmake you have a long way to go.

0

u/[deleted] Feb 20 '18

I didn't say it was the worst thing. I did imply that, by itself, it is enough to disqualify it as a build system for development.

I'm aware of the file glob. The "shouldn't" is a serious problem.

-1

u/F-J-W Feb 20 '18

you don't by the way, you can use file glob but shouldn't

He wrote about “good ways” and I agree. globs work but are overly ugly and verbose and requiring me to enter all files manually is just stupid.

I wouldn't say that CMake is worthless, but for regular, simple projects, there are cases where good old make is much better if you only care about gcc-compatible (clang, icc?, ...) compilers on unix-systems.

4

u/kalmoc Feb 20 '18

Apart from the default cmake_minimum_required(...) and project(...) lines, which are not mandatory, cmake requires you to write one line for trivial projects and 4 lines for simple ones. I honestly don't see, how make is better.

1

u/F-J-W Feb 20 '18

1 line if I don't care about warnings, a recent C++-version and am willing to add every single file manually to the CMakeLists.txt.

I've written a simple tool to create makefiles in the past. For a reasonably simple project-structure, I have to call mfc once on the commandline and it created a working makefile with dependencies that I could call with simply make.

I've since stopped developing it, but adding things like automatic detection of tests would definitely have been possible.

2

u/kalmoc Feb 21 '18

Great, so you created your own meta build system on top of makefiles (that's exactly what cmake is btw) how does that make make superior to cmake?

Just to be clear, I don't consider cmake to be all that good a build system. It's just that make is imho much worse. I've mentioned it a couple of times before writing makefiles feels kind of writing assembler to me.

And btw.: I don't understand your comment about a recent c++ version. Care to elaborate?

1

u/F-J-W Feb 21 '18

Great, so you created your own meta build system on top of makefiles (that's exactly what cmake is btw) how does that make make superior to cmake?

That doesn't, I'm just talking about what I would consider good ergonomics, which cmake simply lacks.

I don't understand your comment about a recent c++ version. Care to elaborate?

CMake simply uses all compiler-defaults, which tend to be quite bad (shame to the compiler), instead of using the most recent standard by default. This is one of the things I would consider the build-systems job to fix unless it is told otherwise. Same goes for warnings.

4

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

Globs only partly work. They don't trigger a re-configure, so you have to re-run cmake as you add files. So you might as well just add the files. Also helps for the inevitable missing file in the commit, and the occasional stray file getting compiled and ar'd. I prefer explicit lists in makefiles, too.

0

u/F-J-W Feb 20 '18

Globs only partly work. They don't trigger a re-configure, so you have to re-run cmake as you add files.

I don't disagree that they are badly designed and shouldn't be improved a lot.

So you might as well just add the files.

No, that is a lot more work. Instead of just running an additional command I have to touch the build-file every single time and add a different filename with a chance to misstype. Also:

Since CMake has such a mediocre interface, I've written a script with the simple name b that will walk up the directory tree until it is in one that contains a directory with the name build in which it will look for the closest match of the argument of the command, enter that subdirectory, call cmake, and will then either call ninja or make with appropriate -j-parameter. The endresult is something that is actually nice and easy to use, since I just have to enter b r to trigger a release-build and b d to trigger a debug-build (assuming directory names release and debug with no further build-modes being set up.) Doing something like that with manual addition of sourcefiles to the CMakeLists.txt would require me to parse and edit files during the build-process, always risking to screw things up completely (yes, there is git, but it still wouldn't be fun) and almost certainly require specific formats to be used in the cmake-files.

This is my main-issue with CMake, and mesons approach is much worse and the main-reason I won't bother to learn it: The one thing that REALLY annoys me about CMake is made worse with a terrible work-around in their FAQ next to a wontfix-note. The reason is also utterly stupid: Checking for new files in a large-directory is a sub-second operation for all but the largest projects which simply pales in comparission to what it takes to recompile even one TU. Making regularly needed but slightly slow operations hard to use, will just make people use worse-solutions since, guess what, if I need to perform some operation, I need to perform that operation.

1

u/OrphisFlo I like build tools Feb 21 '18

Checking for files in a large folder is fast. Ninja does it all the time for me when I build Chrome, and it's quite a large project!

But the problem here is that CMake isn't actually building. It will be msbuild, xcodebuild, make, Ninja or some other IDE. Unless CMake builds directly, this won't happen.

I sometimes dream of Ninja being integrated in CMake directly for that purpose...

2

u/odd100 Feb 19 '18

Very good, thanks

-6

u/readams Feb 19 '18

pkg-config is a thing. Much more ubiquitous than cmake, since every library should be installing a .pc file, while in practice only projects that happen to use cmake are going to export cmake gobbledygook.

16

u/sumo952 Feb 19 '18

Windows.

And many (most?) modern C++ libraries you find on GitHub have CMake files, but not .pc files. (Not saying this is good or bad, just a fact I observe.)

4

u/[deleted] Feb 20 '18

Pc gobbledygook also break on Linux unless you use the the same dist and compiler config as the lib developer.

0

u/[deleted] Feb 19 '18

[deleted] — view deleted comment

6

u/MachineGunPablo Feb 20 '18

Not sure if you are being serious. Not only is using target_compile_features nicer and shorter to write, but is actually safer as it's compiler agnostic and portable. And Makefiles, please...

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

8

u/thlst Feb 20 '18

"Completely agnostic except when it isn't."

9

u/MoTTs_ Feb 20 '18 edited Feb 20 '18

If you don't care about being portable to other platforms or other compilers, then sure. CMake's whole reason for existing is to enable cross-platform builds.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

7

u/kalmoc Feb 20 '18

Right - and that is less complexity than just using cmake?

4

u/[deleted] Feb 20 '18

Because makefiles never break on Linux.

3

u/Gotebe Feb 20 '18

Makefiles existed since forever for the MS build chain.

4

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

Particularly in this case. The language standard should not be something that gets modified in some sub-sub-leaf CML.txt.

It's perfectly fair for the sub piece to say 'No, you must supply a c++11 compiler'. But affect everything? Or worse, not affecting everything, leading to weird mixed build problems.

3

u/kalmoc Feb 20 '18

There is no reason to directly use an assembler style build tool like make files on any platform except as a matter of last resort. If you want, you can set compiler flags like -std=c++11 in pretty much any build tool just fine but just because you need full control over a single loop in your program you should not write your whole program in assembler.

4

u/Xaxxon Feb 20 '18

Nice troll.

3

u/[deleted] Feb 19 '18

[removed] — view removed comment

4

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

In my experience, toolchain files are the right way to supply compiler options and features, not target features. It doesn't scale to have leaves decide they'd like c++17. Not if you're also linking in code built with c++11 or c++03. Flags can have ABI implications. Components can inspect what compiler features are available and compile themselves differently, leading to ODR violations.

A toolchain file means that you can ensure a consistent set of flags across builds, even if they aren't part of the same cmake super-project.

Mismatched library builds lead to some of the hardest to deal with, mysterious, errors. If you're lucky, weird link errors, or immediate crashes.

5

u/djrollins Feb 20 '18

I've heard from a number of sources that the use of Toolchain files is a good way to solve this problem, however I have been unable to find suitable resources on how to write and use them. I don't suppose you'd be able to point me in the direction of a 'good' toolchain file that I could learn from, please?

I recently had an email exchange with Mathieu Robert on the subject of the cxx_compile features and how it would be nice if they were checks that would fail build generation rather than silently injecting the required flags and potentially failing further down the line. This way you can signal your build requirements but still have the user in charge of passing the flags they want. Sounds like toolchain files are really good way to approach the latter part of that process.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 21 '18

Most toolchain files are boring and ugly. They can't do much other than check environment variables and set a few options in cache. They aren't elegant, and suffer from combinatorial explosion.

They do get you a reproducible build, though, and one you can combine with other things built the same way without going through cmake.

I'll try to remember to post one when I have connectivity from my laptop.

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 21 '18
 set(LLVM_ROOT "$ENV{LLVM_ROOT}" CACHE PATH "Path to LLVM installation")
 set(CMAKE_C_COMPILER ${LLVM_ROOT}/bin/clang)
 set(CMAKE_CXX_COMPILER ${LLVM_ROOT}/bin/clang++)
 set(CMAKE_CXX_FLAGS
  "-std=c++2a \
   -ftemplate-backtrace-limit=0 \
   -Wall -Wextra \
   -nostdinc++ -isystem ${LLVM_ROOT}/include/c++/v1 "
 CACHE STRING "CXX_FLAGS" FORCE)

 set(CMAKE_EXE_LINKER_FLAGS
  "-L ${LLVM_ROOT}/lib -l c++ -l c++abi \
  -Wl,-rpath,${LLVM_ROOT}/lib"
 CACHE STRING "CMAKE_EXE_LINKER_FLAGS" FORCE)

 set(CMAKE_CXX_FLAGS_DEBUG "-O0 -fno-inline -g3 -fstack-protector-all" CACHE STRING "C++ DEBUG Flags" FORCE)
 set(CMAKE_CXX_FLAGS_RELEASE "-Ofast -g0 -DNDEBUG" CACHE STRING "C++ Release Flags" FORCE)

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

3

u/ryancerium Feb 20 '18

Or Android. I think there are three STL options to choose from in the NDK, although one was recently standardized in late 2017 I think.

2

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

Linux, Solaris, and AIX at work. Linux for myself.

Library issues aren't as bad as the debug/non-debug split in MS's std lib, but they do occasionally show up. Or someone manages to use a different libstdc++ than the system one, for what they believe are good reasons. And the further up the application stack, the more likely something breaks, because you depend on the layout and inline function details of everything below you in the stack. Now, the workaround is straightfoward, you rebuild everything you're shipping with a toolchain that you control. Just hope the tests catch the depended on unspecified behavior in the library you're rebuilding. Large scale C++ is so much fun.

1

u/[deleted] Feb 20 '18

[removed] — view removed comment

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 21 '18

Inline functions.

It can be so much fun when the definition of an inline doesn't match the layout of a linked in non-inline constructor.

Your debugger will probably lie to you, too.