Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Managing a list of source files

Tags:

cmake

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.

like image 321
Kenny Avatar asked Dec 15 '22 23:12

Kenny


2 Answers

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")
like image 60
Leonardo Avatar answered Dec 24 '22 15:12

Leonardo


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.

like image 23
Peter Avatar answered Dec 24 '22 14:12

Peter