How to use cmake GLOB_RECURSE for only some subdirectories

You can also exclude Test dir by filtering the globbed list :

file(GLOB_RECURSE ENDF6_SRC ${PROJECT_SOURCE_DIR} *.cpp)
list(FILTER ENDF6_SRC EXCLUDE REGEX "${PROJECT_SOURCE_DIR}/Test/.*" )

If you don't have subdirectories inside "TopDir/File" or "TopDir/Section", you can do:

file(GLOB ENDF6_SRC
       ${PROJECT_SOURCE_DIR}/*.cpp
       ${PROJECT_SOURCE_DIR}/File/*.cpp
       ${PROJECT_SOURCE_DIR}/Section/*.cpp)

If you do have subdirectories there, you'll need more than one call:

file(GLOB ENDF6_SRC_TOP
       ${PROJECT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE ENDF6_SRC_NESTED
       ${PROJECT_SOURCE_DIR}/File/*.cpp
       ${PROJECT_SOURCE_DIR}/Section/*.cpp)
set(ENDF6_SRC ${ENDF6_SRC_TOP} ${ENDF6_SRC_NESTED})

By the way, doing file(GLOB_RECURSE ...) in your top-level directory will likely pick up unwanted cpp files from the build folder too in the case of an in-source build (i.e. where the build root is inside "TopDir").

Tags:

Cmake