如何从cmake`file(GLOB…)`模式中排除单个文件?


86

CMakeLists.txt包含以下行:

file(GLOB lib_srcs Half/half.cpp Iex/*.cpp IlmThread/*.cpp Imath/*.cpp IlmImf/*.cpp)

并且该IlmImf文件夹包含b44ExpLogTable.cpp,我需要从构建中排除该文件夹。

如何实现呢?

Answers:


100

您可以使用该list函数来操作列表,例如:

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

在您的情况下,也许这样的事情会起作用:

list(REMOVE_ITEM lib_srcs "IlmImf/b44ExpLogTable.cpp")

1
除了用lib_srcs代替lib_src和用IlmImf代替IlmThread之外,还成功了!万分感谢 !
2013年

20
注意:删除列表项时,请确保要搜索的值与列表中的值完全匹配。我在将$ {CMAKE_SOURCE_DIR} /src/file_to_remove.cpp与$ {CMAKE_CURRENT_SOURCE_DIR} /../ file_to_remove.cpp混合时遇到了一些麻烦。它指向相同的位置,但不是相同的字符串。message(“ $ {VARIABLE_NAME}”)可以帮助您调试那些内容。
hbobenicio

如果您使用CONFIGURE_DEPENDS并且需要排除由构建生成的文件,这将无济于事。file调用后列表上的过滤器仍会触发重新生成,对于我而言,我试图避免这种重新生成。
simon.watts

这是更好的在使用list(FILTER
hukeping

2
上面的解决方案不适用于版本3.10.2的camke,但是下面的解决方案:list(FILTER <list> <INCLUDE|EXCLUDE> REGEX <regular_expression>) 对我来说很好。
MH Yip

42

FILTER是另一种选择,在某些情况下可能更方便:

list(FILTER <list> <INCLUDE|EXCLUDE> REGEX <regular_expression>)

此行不包括以所需文件名结尾的所有项目:

list(FILTER lib_srcs EXCLUDE REGEX ".*b44ExpLogTable.cpp$")

这是cmake的Regex规范

The following characters have special meaning in regular expressions:

^         Matches at beginning of input
$         Matches at end of input
.         Matches any single character
[ ]       Matches any character(s) inside the brackets
[^ ]      Matches any character(s) not inside the brackets
 -        Inside brackets, specifies an inclusive range between
          characters on either side e.g. [a-f] is [abcdef]
          To match a literal - using brackets, make it the first
          or the last character e.g. [+*/-] matches basic
          mathematical operators.
*         Matches preceding pattern zero or more times
+         Matches preceding pattern one or more times
?         Matches preceding pattern zero or once only
|         Matches a pattern on either side of the |
()        Saves a matched subexpression, which can be referenced
          in the REGEX REPLACE operation. Additionally it is saved
          by all regular expression-related commands, including
          e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).

可以比绝对/相对路径更可靠
EFraim

1
list(REMOVE_ITEM
ceztko

1
请注意,这list(FILTER ...)是在cmake v3.6中引入的:stackoverflow.com/a/42167646/3476780
yano

这绝对是一个更好的答案,尽管如果您尝试使用它来排除整个目录,或者如果您的正则表达式混乱,则可能会排除您想要的更多内容。例如,.*test/.*如果您的项目在目录树中,其中一个父目录称为,则尝试排除可能会排除所有内容mytest
AnthonyD973 '19

1

试试这个 : CMakeLists.txt

install(DIRECTORY   ${CMAKE_SOURCE_DIR}/ 
            DESTINATION ${CMAKE_INSTALL_PREFIX}
            COMPONENT   copy-files
            PATTERN     ".git*"   EXCLUDE
            PATTERN     "*.in"    EXCLUDE
            PATTERN     "*/build" EXCLUDE)

add_custom_target(copy-files
            COMMAND ${CMAKE_COMMAND} -D COMPONENT=copy-files
            -P cmake_install.cmake)
$cmake <src_path> -DCMAKE_INSTALL_PREFIX=<install_path>
$cmake --build . --target copy-files
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.