Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake how to exclude ._ files in macos in FILE(GLOB ) directive

Tags:

regex

cmake

In my cmake C++ project, I am adding source files to target by

file(GLOB HEADERS *.h)
file(GLOB SOURCES *.cpp)
add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADERS})

In macOS this is including files like ._Source.cpp and ._Header.h I tried the REGEX

list(FILTER HEADERS REGEX "^[^\.].+" output_variable HEADERS)
list(FILTER SOURCES REGEX "^[^\.].+" output_variable SOURCES)

but this is not working.

like image 362
Necktwi Avatar asked Oct 17 '25 05:10

Necktwi


1 Answers

Turning my comments into an answer

file(GLOB HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.h") 
file(GLOB SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.cpp") 

list(FILTER HEADERS EXCLUDE REGEX "^\\..+") 
list(FILTER SOURCES EXCLUDE REGEX "^\\..+"
  • The list(FILTER ...) needs INCLUDE or EXCLUDE keyword
  • The file(GLOB ...) by default will return full paths, so you need to add the RELATIVE keyword
  • The regex needs double backslashs, because CMake evaluates escape sequences first
  • You don't need the [] (any-of-expression) because you only check for a single character

Reference

  • CMake: how to get the backslash literal in Regexp replace?
like image 118
Florian Avatar answered Oct 19 '25 01:10

Florian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!