Reputation: 2491
Following is a part of my CMakeLists.txt file.
file(GLOB SOURCES "xyz/*.cpp")
message("${SOURCES}")
list(REMOVE_ITEM SOURCES "src1.cpp")
message("${SOURCES}")
Here in file "xyz/*.cpp"
is a relative path.
Content of ${SOURCES}
is the same before and after REMOVE_ITEM
.
Why is list(REMOVE_ITEM)
not working in my case? Any help would be invaluable.
Upvotes: 19
Views: 22849
Reputation: 5475
In addition to accepted answer you can use the following function to filter lists using regexp:
# Filter values through regex
# filter_regex({INCLUDE | EXCLUDE} <regex> <listname> [items...])
# Element will included into result list if
# INCLUDE is specified and it matches with regex or
# EXCLUDE is specified and it doesn't match with regex.
# Example:
# filter_regex(INCLUDE "(a|c)" LISTOUT a b c d) => a c
# filter_regex(EXCLUDE "(a|c)" LISTOUT a b c d) => b d
function(filter_regex _action _regex _listname)
# check an action
if("${_action}" STREQUAL "INCLUDE")
set(has_include TRUE)
elseif("${_action}" STREQUAL "EXCLUDE")
set(has_include FALSE)
else()
message(FATAL_ERROR "Incorrect value for ACTION: ${_action}")
endif()
set(${_listname})
foreach(element ${ARGN})
string(REGEX MATCH ${_regex} result ${element})
if(result)
if(has_include)
list(APPEND ${_listname} ${element})
endif()
else()
if(NOT has_include)
list(APPEND ${_listname} ${element})
endif()
endif()
endforeach()
# put result in parent scope variable
set(${_listname} ${${_listname}} PARENT_SCOPE)
endfunction()
For example in question it could look as the following:
file(GLOB SOURCES "xyz/*.cpp")
message("${SOURCES}")
filter_regex(EXCLUDE "src1\\.cpp" SOURCES ${SOURCES})
message("${SOURCES}")
(Because regexp is used then .
is replaced with \\.
)
Upvotes: 4
Reputation: 1418
I got a solution for your problem. The idea behind my solution is, to get the full path of the special cpp file, because I saw, that the command file(GLOB SOURCES "src/*.cpp")
gives me a list of full paths. After getting the full path of the special file, I could do a remove from the list. Here is a small example
cmake_minimum_required(VERSION 3.4)
project(list_remove_item_ex)
file(GLOB SOURCES "src/*.cpp")
# this is the file I want to exclude / remove from the list
get_filename_component(full_path_test_cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/test.cpp ABSOLUTE)
message("${full_path_test_cpp}")
list(REMOVE_ITEM SOURCES "${full_path_test_cpp}")
message("${SOURCES}")
Upvotes: 29