I have a source code layout that looks like this:
TopDir/
CMakeLists.txt
A.cpp
A.hpp
...
File/
F1.cpp
F1.hpp
...
Section/
S1.cpp
S1.hpp
...
Test/
CMakeLists.txt
TestF1S1.cpp
TestF2S2.cpp
...
I want to capture all the .cpp files as source files (ENDF6_SRC), so in my TopDir/CMakeLists.txt file, I have a line that looks like this:
file(GLOB_RECURSE ENDF6_SRC ${PROJECT_SOURCE_DIR} *.cpp)
This grabs all the .cpp files in TopDir/, File/, Section/ as expected, but also grabs all the .cpp files in Test/ as well.
How can I create my ENDF6_SRC variable without adding the .cpp files from the Test directory? I don't want a CMakeLists.txt file in File/ or Section/.
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").
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With