CMake, How to create a list of directories (1 level deep not reqursive) inside CMAKE_SOURCE_DIR that contain a CMakeLists.txt
Assistant
Here’s a small, self-contained recipe you can drop into the top-level CMakeLists.txt.
It scans only the first level under ${CMAKE_SOURCE_DIR}, keeps the sub-directories that own their own CMakeLists.txt, and then (optionally) adds them with .
User
code
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.
#
# 1. Collect *immediate* children of CMAKE_SOURCE_DIR
#
file(GLOB CHILD_DIRS
LIST_DIRECTORIES true # we want directories, not files
CONFIGURE_DEPENDS # re-run CMake when the tree changes (≥3.12)
RELATIVE ${CMAKE_SOURCE_DIR} # nicer, relative paths
"${CMAKE_SOURCE_DIR}/*") # one “/*” → only 1 level deep
#
# 2. Filter: only keep those that really are dirs + have a CMakeLists.txt
#
set(DIRS_WITH_CMAKELISTS "")
foreach(dir ${CHILD_DIRS})
if(IS_DIRECTORY "${CMAKE_SOURCE_DIR}/${dir}" AND
EXISTS "${CMAKE_SOURCE_DIR}/${dir}/CMakeLists.txt")
list(APPEND DIRS_WITH_CMAKELISTS "${dir}")
endif()
endforeach()
message(STATUS "1-level sub-dirs that contain CMakeLists.txt: ${DIRS_WITH_CMAKELISTS}")
#
# 3. (Optional) Automatically add them
#
foreach(dir ${DIRS_WITH_CMAKELISTS})
add_subdirectory(${dir})
endforeach()
Why this works
• file(GLOB … "${CMAKE_SOURCE_DIR}/*")
• A single * never crosses a /, so only the first depth level is matched.
• LIST_DIRECTORIES true ensures we receive the directory names, not the files inside them.
• CONFIGURE_DEPENDS (CMake 3.12+) tells CMake to reconfigure automatically if you add/remove directories.
• The foreach loop makes sure the candidate is indeed a directory and has its own CMakeLists.txt.
Older CMake (<3.12)
Omit CONFIGURE_DEPENDS; you’ll just have to re-run CMake manually if the directory layout changes.
Alternative one-liner (when you only need the list, not the add_subdirectory):
cmake
file(GLOB SUBDIR_CMAKELISTS RELATIVE ${CMAKE_SOURCE_DIR}
"${CMAKE_SOURCE_DIR}/*/CMakeLists.txt")
# SUBDIR_CMAKELISTS now holds paths like "demo/CMakeLists.txt"
# Get just the directory part:
foreach(path ${SUBDIR_CMAKELISTS})
get_filename_component(dir ${path} DIRECTORY)
list(APPEND DIRS_WITH_CMAKELISTS "${dir}")
endforeach()
Both approaches stay strictly one level deep and avoid recursive scans.
Below is a tidy, drop-in snippet that does what you seem to be aiming for:
• find every first-level sub-directory that owns a CMakeLists.txt
• offer/keep a cache variable called BENCHMARK_TARGETS (with a default list only the very first time)
• tell you which of the available benchmarks will be built and which are skipped
cmake
#
# 1. Collect one-level-deep dirs that have a CMakeLists.txt
#
file(GLOB _bench_files
CONFIGURE_DEPENDS
RELATIVE ${CMAKE_SOURCE_DIR}
"${CMAKE_SOURCE_DIR}/*/CMakeLists.txt")
set(AVAIL_BENCHMARK_TARGETS "")
foreach(f ${_bench_files})
get_filename_component(dir "${f}" DIRECTORY) # strip the trailing /CMakeLists.txt
list(APPEND AVAIL_BENCHMARK_TARGETS "${dir}")
endforeach()
list(REMOVE_DUPLICATES AVAIL_BENCHMARK_TARGETS)
list(SORT AVAIL_BENCHMARK_TARGETS)
#
# 2. Provide a cache variable with sensible defaults (only once)
#
if(NOT DEFINED BENCHMARK_TARGETS OR BENCHMARK_TARGETS STREQUAL "")
set(_default "noop;abseil;boost;fstlog;fstlog-fast;g3log;log4cxx;p7;poco;quill;quill-bounded;spdlog")
set(BENCHMARK_TARGETS "${_default}"
CACHE STRING "Semicolon-separated list of benchmarks to build")
endif()
#
# 3. Work out which ones are actually going to be built / skipped
#
set(BENCHMARKS_BUILT "")
set(BENCHMARKS_SKIPPED "")
foreach(dir ${AVAIL_BENCHMARK_TARGETS})
list(FIND BENCHMARK_TARGETS ${dir} _idx)
if(_idx EQUAL -1)
list(APPEND BENCHMARKS_SKIPPED "${dir}")
else()
list(APPEND BENCHMARKS_BUILT "${dir}")
endif()
endforeach()
#
# 4. Nicely report the outcome
#
message(STATUS "")
message(STATUS "┌─ Benchmark discovery ──────────────────────────────")
message(STATUS "│ Available : ${AVAIL_BENCHMARK_TARGETS}")
message(STATUS "│ To build : ${BENCHMARKS_BUILT}")
message(STATUS "│ Skipped : ${BENCHMARKS_SKIPPED}")
message(STATUS "└────────────────────────────────────────────────────")
# (Optional) Actually add the chosen sub-directories
foreach(dir ${BENCHMARKS_BUILT})
add_subdirectory(${dir})
endforeach()
What changed vs. your original code?
CONFIGURE_DEPENDS + a single star (*) gives you a non-recursive, one-level scan that will auto-re-configure when new dirs pop up.
Use NOT DEFINED BENCHMARK_TARGETS instead of NOT BENCHMARK_TARGETS; this way an empty user value (-DBENCHMARK_TARGETS=) is respected.
We now compute two new lists (BENCHMARKS_BUILT, BENCHMARKS_SKIPPED) so the final messages really say what they claim.
Removed FORCE from the set(... CACHE ...) call—there’s usually no need to overwrite what the user explicitly passed in.
Copy-paste the block into your top-level CMakeLists.txt, configure, and you should see something like: