CMake, How to create a list of directories (1 level deep not reqursive) inside…
- User
CMake, How to create a list of directories (1 level deep not reqursive) inside CMAKE_SOURCE_DIR that contain a CMakeLists.txt
Assistant
Creating a List of Directories with CMakeLists.txt in CMake
To create a list of directories (1 level deep, non-recursive) inside
CMAKE_SOURCE_DIRthat contain aCMakeLists.txtfile, you can use the following CMake code:cmake# Get all immediate subdirectories of CMAKE_SOURCE_DIR file(GLOB children RELATIVE ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/*) # Filter to only directories set(dirs) foreach(child ${children}) if(IS_DIRECTORY ${CMAKE_SOURCE_DIR}/${child}) list(APPEND dirs ${child}) endif() endforeach() # Filter to only directories that contain CMakeLists.txt set(cmake_dirs) foreach(dir ${dirs}) if(EXISTS ${CMAKE_SOURCE_DIR}/${dir}/CMakeLists.txt) list(APPEND cmake_dirs ${dir}) endif() endforeach() # Now cmake_dirs contains the list you want message(STATUS "Directories with CMakeLists.txt: ${cmake_dirs}")Alternative (More Concise) Version
Here's a more concise version that accomplishes the same thing:
cmakefile(GLOB children LIST_DIRECTORIES true ${CMAKE_SOURCE_DIR}/*) set(cmake_dirs) foreach(child ${children}) if(IS_DIRECTORY ${child} AND EXISTS ${child}/CMakeLists.txt) file(RELATIVE_PATH rel_path ${CMAKE_SOURCE_DIR} ${child}) list(APPEND cmake_dirs ${rel_path}) endif() endforeach()Usage Example
You can then use this list to add subdirectories:
cmakeforeach(dir ${cmake_dirs}) add_subdirectory(${dir}) endforeach()Notes
- This is non-recursive - it only checks immediate subdirectories of
CMAKE_SOURCE_DIR - The paths in the resulting list are relative to
CMAKE_SOURCE_DIR - If you need absolute paths, you can modify the code to store those instead
- This is non-recursive - it only checks immediate subdirectories of
- Usercode
file(GLOB SUBDIR_CMAKELISTS CONFIGURE_DEPENDS RELATIVE ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/*/CMakeLists.txt") foreach(path ${SUBDIR_CMAKELISTS}) get_filename_component(dir ${path} DIRECTORY) list(APPEND AVAIL_BENCHMARK_TARGETS "${dir}") endforeach() if (NOT BENCHMARK_TARGETS) set(BENCHMARK_TARGETS "noop;abseil;boost;fstlog;fstlog-fast;g3log;log4cxx;p7;poco;quill;quill-bounded;spdlog" CACHE STRING "The Benchmarks to build." FORCE) endif() message(STATUS "Benchmark targets built: ${BENCHMARK_TARGETS}.") message(STATUS "Benchmark targets not built: ${AVAIL_BENCHMARK_TARGETS}.") Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.