Kenny
Kenny

Reputation: 675

CMake: Managing a list of source files

The problem I'm having at the moment is that I simply wish to manage my list of source files by grabbing everything and removing the few odds and ends that I do not need. I was hoping that Cmake provided nice built-in tools for this.

So I might start with:

file(GLOB A "Application/*.cpp")

I feel like I want to create another list of files to be removed and I want to tell CMake: Remove from list A items that are in list B.

If this were Python I might do something like:

C = [f for f in A if f not in B]

I may have screwed that syntax up but I'm wondering if there is built-in support for managing these lists of files in a more elegant way?

Even if I could do something like my Python example, A is list of absolute paths so constructing B is clunky.

And why absolute paths anyway? It seems like this will break your build as soon as you relocate the source.

Upvotes: 4

Views: 7434

Answers (2)

Leonardo
Leonardo

Reputation: 1844

You can do that by using the list command with the REMOVE_ITEM option:

list(REMOVE_ITEM <list> <value> [<value> ...])

Have a look:

file(GLOB FOO *)
set (ITEMS_TO_REMOVE "item;item2;item3")
message(STATUS "FOO is ${FOO}")
list(REMOVE_ITEM FOO ${ITEMS_TO_REMOVE})
message(STATUS "FOO is now ${FOO}")

Keep in mind that the paths returned by file(GLOB) are absolute, you might want to build your list of items to remove by prepending ${CMAKE_CURRENT_LIST_DIR} to each one of them:

set (ITEMS_TO_REMOVE "${CMAKE_CURRENT_LIST_DIR}/item;
                      ${CMAKE_CURRENT_LIST_DIR}/item2;
                      ${CMAKE_CURRENT_LIST_DIR}/item3")

Upvotes: 8

Peter Petrik
Peter Petrik

Reputation: 10165

If you like Python, you can generate your list of source files with execute_process. There is also possiblity to work with lists.

But I would recommend you to "hardcode" your list of source files. File command documentation states:

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.

Upvotes: 4

Related Questions