Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of all added subdirectories in CMake

Tags:

cmake

In CMake Subdirectories can be added using add_subdirectory(). How can I get the list of all subdirectories that have been added in CMake?

like image 241
Amani Avatar asked Sep 17 '25 23:09

Amani


2 Answers

CMake 3.7 has a SUBDIRECTORIES property on the directories. For example, to get all the subdirectories of DIRNAME:

get_directory_property(subdir_list DIRECTORY ${DIRNAME} SUBDIRECTORIES)
foreach(subdir ${subdir_list})
  message(STATUS "${subdir}")
endforeach()

Using this approach there is no need to define a custom function for addig subdirectories.

like image 51
Csq Avatar answered Sep 21 '25 11:09

Csq


Replace every call with a call to your own function, e. g. add_and_store_subdirectory() which adds the directory to a global variable.

like image 45
Andreas Haferburg Avatar answered Sep 21 '25 11:09

Andreas Haferburg