Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collect source files with CMake without globbing?

Tags:

build

cmake

The CMake documentation explicitly states that file(GLOB ...) is not recommended to collect source files for a build, but it doesn't mention what the recommended method actually is.

Specifying every source file manually sounds a little bit too manually to me. So, what is the right method to collect source files, if not file(GLOB ...)?

like image 485
pmr Avatar asked Jun 06 '12 12:06

pmr


People also ask

Should I use GLOB CMake?

The creators of CMake themselves advise not to use globbing. (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.)

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.

What files does CMake generate?

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.

What does CMake generate do?

CMake can generate a native build environment that will compile source code, create libraries, generate wrappers and build executables in arbitrary combinations. CMake supports in-place and out-of-place builds, and can therefore support multiple builds from a single source tree.


2 Answers

Manual is indeed the recommended method. By recommending against using GLOB, the documentation is simply warning against a build system that depends on files present. For example, you want to add a test executable, so you create mytest.cpp. Oops. Now your library compilation breaks. The documentation for AUX_SOURCE_DIRECTORY (similar purpose as globbing for for source files) gives the following warning:

It is tempting to use this command to avoid writing the list of source files for a library or executable target. While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added. Normally the generated build system knows when it needs to rerun CMake because the CMakeLists.txt file is modified to add a new source. When the source is just added to the directory without modifying this file, one would have to manually rerun CMake to generate a build system incorporating the new file.

If you're certain that you want all the contents of a directory, and don't plan on adding new ones, then by all means use a GLOB.

Also, don't forget listing files manually doesn't have to involve typing all the filenames. You could do, for example, ls *.cpp >> CMakeLists.txt, then use your editor to move the list of files to the correct place in the file.

like image 179
Peter Avatar answered Sep 20 '22 21:09

Peter


I use GLOB for exactly that and every time I add a file I run

touch ../src/CMakeLists.txt 

The next make command will re-scan the directories.

"There is no way for CMake to generate a build system that knows when a new source file has been added" Really? Okay, so tell it!

It's not 100% automatic but a damn sight easier than adding files manually.

like image 41
spraff Avatar answered Sep 24 '22 21:09

spraff