Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is cmake file GLOB evil?

Tags:

glob

cmake

The CMake doc says about the command file GLOB:

We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.

Several discussion threads in the web second that globbing source files is evil.

However, to make the build system know that a source has been added or removed, it's sufficient to say

touch CMakeLists.txt 

Right?

Then that's less effort than editing CMakeLists.txt to insert or delete a source file name. Nor is it more difficult to remember. So I don't see any good reason to advise against file GLOB.

What's wrong with this argument?

like image 617
Joachim W Avatar asked Sep 05 '15 10:09

Joachim W


People also ask

What does GLOB do in CMake?

GLOB will generate a list of all files that match the globbing expressions and store it into the variable. Globbing expressions are similar to regular expressions, but much simpler. If RELATIVE flag is specified for an expression, the results will be returned as a relative path to the given path.

Why does CMake use txt?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.

What exactly does CMake do?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.


2 Answers

The problem is when you're not alone working on a project.

Let's say project has developer A and B.

A adds a new source file x.c. He doesn't changes CMakeLists.txt and commits after he's finished implementing x.c.

Now B does a git pull, and since there have been no modifications to the CMakeLists.txt, CMake isn't run again and B causes linker errors when compiling, because x.c has not been added to its source files list.

2020 Edit: CMake 3.12 introduces the CONFIGURE_DEPENDS argument to file(GLOB which makes globbing scan for new files: https://cmake.org/cmake/help/v3.12/command/file.html#filesystem

This is however not portable (as Visual Studio or Xcode solutions don't support the feature) so please only use that as a first approximation, else other people can have trouble building your CMake files under their IDE of choice!

like image 177
Jean-Michaël Celerier Avatar answered Sep 24 '22 19:09

Jean-Michaël Celerier


It's not inherently evil - it has advantanges and disadvantages, covered relatively well in this answer here on StackOverflow. But if you use it carelessly, you could end up ignoring dependency changes and requiring clean rebuilds of large parts of your codebase.

I'm personally in favor of using it - in smaller projects, or on certain subdirectories in larger ones - to avoid having to enter every file manually into the build files. Edit: My preference has changed and I currently tend to avoid it.

like image 44
einpoklum Avatar answered Sep 22 '22 19:09

einpoklum