Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CMake GLOB_RECURSE to find directories

Tags:

glob

cmake

I have the following directory structure:

.
├── CMakeLists.txt
└── deps
    ├── eigen
    │   └── include
    │       ├── Eigen
    │       └── eigen.h
    └── osg
        ├── include
        │   └── Osg
        └── lib
            └── libosg.so

I am trying to get a maximum of the files I don't need to deploy my software, i.e. the libraries. I tried to create a globbing expression that matches the *.h files and the include directories:

file(GLOB_RECURSE
  FOUND_FILES
  LIST_DIRECTORIES true
  ${CMAKE_SOURCE_DIR}/deps/*/include
  ${CMAKE_SOURCE_DIR}/deps/*/*.h)
message(STATUS "Files are ${FOUND_FILES}")

However, for some reason, the variable FOUND_FILES contains deps/osg/lib. What did I not understand about the GLOB_RECURSE function?

- deps/eigen/include
- deps/eigen/include/eigen.h
- deps/osg/include
- deps/osg/lib
- deps/eigen/include
- deps/osg/include
- deps/osg/lib
like image 1000
Hugal31 Avatar asked Sep 18 '25 02:09

Hugal31


1 Answers

I think this could be a workaround:

file(GLOB_RECURSE
    FOUND_FILES
    LIST_DIRECTORIES true
    ${CMAKE_SOURCE_DIR}/deps/*/include
)
list(FILTER FOUND_FILES INCLUDE REGEX "^${CMAKE_SOURCE_DIR}/deps/.*/include$")
file(GLOB_RECURSE
    tmp
    ${CMAKE_BINARY_DIR}/deps/*/*.h
)
list(APPEND FOUND_FILES ${tmp})

I guess this is a bug when GLOB_RECURSE is used with LIST_DIRECTORIES true with an expression that has * not on the last entry in the path. Once a directory containing the matched entry is matched in cmake:Glob.cxx#L404, next directories will be added recursively to the output unconditionally at this add_file() in cmake Glob.cxx#L316. So once a ${CMAKE_SOURCE_DIR}/deps/* directory has a include directory or file inside it, all directories recursively from ${CMAKE_SOURCE_DIR}/deps/ are added to the output. Files are not added, as they are checked against the regex at cmake Glob.cxx:#L326.

like image 154
KamilCuk Avatar answered Sep 21 '25 12:09

KamilCuk