Reputation: 8197
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?
Upvotes: 56
Views: 34001
Reputation: 132128
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.
More generally, and more philosophically, using globs is an(other) obstacle in the way of ensuring reproducible builds.
I used to be 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. But over time, and having been burnt by incorrect rebuilds, my preference has changed and I currently avoid it.
Upvotes: 16
Reputation: 7936
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!
Upvotes: 53
Reputation: 1664
On top of the reasons other people here posted, imho the worst issue with glob is that it can yield DIFFERENT file lists on different platforms. As I see it, that's a bug. In OSX glob ignores case sensitivity and in a ubuntu box it doesn't.
Upvotes: 10
Reputation: 384
Globbing breaks all code inspection in things like CLion that otherwise understand limited subsets of CMakeLists.txt and do not and never will support globbing as it is unsafe.
Write script to dump the globbed list and paste it in, its very simple, and then CLion can actually find the referenced files and infer them as useful. Maybe even put such script into the tree so that the other devs can either run it without being a moron OR set git hooks to make it happen.
In no case should some random file dropped into some directory ever get automatically linked that's how trojans happen.
Also CLion without context jumping to known definitions and what not, is like hiking barefoot /// why bother.
Upvotes: 2