r/cpp • • Feb 07 '21

Yet another CMake tutorial

https://www.youtube.com/watch?v=mKZ-i-UfGgQ
0 Upvotes

59 comments sorted by

View all comments

32

u/AlexReinkingYale Feb 07 '21 edited Feb 07 '21

Yet another CMake tutorial written by someone who has no idea how to use CMake.

They glob for sources which is bad enough, but then they glob recursively and without setting CONFIGURE_DEPENDS, which is outright incorrect and won't notice additions or removals of files without rerunning CMake (not just the build tool) manually.

The minimum version is 3.10, which is FAR from modern, while 3.16 is available everywhere and 3.20 is around the corner.

Skipped ahead to the "how to use libraries" section. The code doesn't use imported targets. So, again, not modern. Also, find_library doesn't have a REQUIRED argument until 3.18, so that code will just outright not work on the advertised version. Edit: worse, the video uses SFML in an unsupported way; the variables they expand were removed in 2018 in favor of imported targets. The example code doesn't even work on Ubuntu 20.04 LTS.

Skip this.

-2

u/codevion Feb 07 '21

Thanks for the feedback. I’ll add some warnings around the rest of it. But can you explain what the alternative to glob_recurse is?

8

u/AlexReinkingYale Feb 07 '21 edited Feb 08 '21

You list the source files manually. There are a lot of reasons for this.

  1. The devs tell you not to. This is huge. It means that when something goes wrong, your bug reports get rejected.

  2. CONFIGURE_DEPENDS is not guaranteed to work on all generators. Edit: a colleague tells me it's broken before Ninja 1.10.2.

  3. When things go wrong, like during a git bisect, it's hard to figure out what went wrong and which file was mistakenly added or ignored.

  4. It gets slower with more files since globbing is slow. It slows down your incremental builds too since it has to re-glob every time (with CONFIGURE_DEPENDS). This is particularly an issue on Windows.

The CONFIGURE_DEPENDS flag makes it so the glob is re-run on every build (eg. Make or Ninja run) so that CMake can run again if the glob result changes. Without it, the build is utterly broken. With it, you still run into issues. List your source files.

3

u/codevion Feb 07 '21

Listing files manually is a nonstarter for me and for a lot of other people I talk to. Thank you for mentioning configure_depends though! I’ll add a warning about that.

7

u/AlexReinkingYale Feb 07 '21

Too bad. Then don't use CMake. You're basically saying "writing bug-free code is a non-starter for me".

3

u/codevion Feb 07 '21

I think we keep using it so long as it keeps working. I’ll find an alternative when they remove support for it

2

u/qv51 Feb 07 '21 edited Feb 07 '21

Don't listen to him. There was a talk from a packager for vcpkg who basically begs one to glob. It will make packaging much easier, and forces you to organize your library better for yourself. Edit: link https://m.youtube.com/watch?v=_5weX5mx8hc

4

u/AlexReinkingYale Feb 07 '21

So we shouldn't listen to the devs either? I asked this same question back in March and got this response from Ben Boeckel, one of the CMake maintainers:

I still highly discourage globbing for the reason that files may appear in your source tree that you do not intend to build. The main case I’ve run into is that during conflict resolution in git, the other versions of the file(s) in conflict are named ${base}_${origin}_${pid}.${ext}, so if you try to build in the middle of a conflict, you’re going to glob up these files.

Another reason is that now the addition/removal of a file is not present in your build system diff, so tracking down "what did you change?" in debugging reported problems can be harder since there’s no evidence of accidentally added/removed files in a normal ${vcs} diff output.

1

u/angry_cpp Feb 08 '21

So we shouldn't listen to the devs either?

IMO in this case - yes, we should not listen to them.

There is basically no good reason to manually add files to CMakeLists.txt.

files may appear in your source tree that you do not intend to build

If files can "appear" in your source directory without your knowledge then you have greater problem to solve than globbing :) How about using version control...

The main case I’ve run into is that during conflict resolution in git,

... never mind :) "using version control right".

try to build in the middle of a conflict

In my 5+ years of globbing source files in large (not as large as Chromium) projects in CMake there were exactly 0 cases of this. And the number of accidentally compiling "suddenly appeared" files is also 0.

Does anyone even encountered this problem in the real life?

On the other hand when my colleagues and I were using explicit file lists in CMakeLists.txt there were "Oh, I forgot to add file to CMakeLists.txt" from someone in the office room almost every couple of days.

Another reason is that now the addition/removal of a file is not present in your build system diff,

That makes no sense at all. When you use globbing your build system doesn't have diff about files at all. And then you can look for adding and removing of files directly in your version control system of choice.

without setting CONFIGURE_DEPENDS, which is outright incorrect and won't notice additions or removals of files without rerunning CMake (not just the build tool) manually.

I don't buy it. What is more simple to use:

  1. run your CMake after adding/removing source files.

  2. manually add copied file name and path to appropriate list in your CMakeLists.txt. Then on next build your CMake files will be regenerated "automatically".

I put "automatically" in quotes because it is hardly automatic when you need to edit your CMakeLists.txt file for this to even work.

IMO it is obvious what is the right answer.

Maybe I missing something?

1

u/AlexReinkingYale Feb 08 '21

Does anyone even encountered this problem in the real life?

Yes, some merge tools create temp files with globbable names. Git doesn't do it by default, but I Ben's tool must. There are some advantages to using a merge tool that uses temp files. It's easier to build and test your merge when you don't have sources with merge markers in them.

"Oh, I forgot to add file to CMakeLists.txt" ... I put "automatically" in quotes because it is hardly automatic when you need to edit your CMakeLists.txt file for this to even work.

You can completely automate this with a tiny presubmit script that just checks that every .cpp and .h file is mentioned in a CMakeLists.txt. Adding one line in a lists file is not some insane burden.

I don't buy it. What is more simple to use:

I'm confused, are you objecting to my statement that glob without CONFIGURE_DEPENDS is incorrect? Because that's not really debatable at all. If you run an incremental build on the generated build system it ought to pick up on globs. With C-D you at least get CMake to reconfigure automatically when the glob results change.

→ More replies (0)