Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cmake GLOB_RECURSE for only some subdirectories

Tags:

cmake

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/.

like image 907
jlconlin Avatar asked Jan 16 '15 20:01

jlconlin


2 Answers

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").

like image 53
Fraser Avatar answered Oct 18 '22 10:10

Fraser


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/.*" )
like image 24
Serge Avatar answered Oct 18 '22 10:10

Serge