Necktwi
Necktwi

Reputation: 2625

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

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.

Upvotes: 3

Views: 11121

Answers (2)

ceztko
ceztko

Reputation: 15237

@Florian answer is correct, but another (more restricting) solution is to work on the glob expression, which is not a full regex but it is enough expressive to restrict globbing on file not starting with a dot.

Example:

file(GLOB SOURCE_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "[^.]*.h" "[^.]*.cpp")

Another example:

file(GLOB_RECURSE SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/[^.]*")

Upvotes: 0

Florian
Florian

Reputation: 43058

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

Upvotes: 10

Related Questions