Issue
I have two questions relative to CMake
Assume that we have a variable
${MY_CURRENT_DIR}
that contains the path of a directory that contains several subdirectories : mydir1, mydir2 and mydir3. I want to detect these subdirectories and put their names into${SUBDIRS}
(not the complete path of these directories, only their name). How to do that automatically ?Assume that
${SUBDIRS}
contains "mydir1 mydir2 mydir3". How to replaceADD_SUBDIRECTORY(mydir1) ADD_SUBDIRECTORY(mydir2) ADD_SUBDIRECTORY(mydir3)
by a loop over ${SUBDIRS}
?
Solution
Use this macro:
MACRO(SUBDIRLIST result curdir) FILE(GLOB children RELATIVE ${curdir} ${curdir}/*) SET(dirlist "") FOREACH(child ${children}) IF(IS_DIRECTORY ${curdir}/${child}) LIST(APPEND dirlist ${child}) ENDIF() ENDFOREACH() SET(${result} ${dirlist}) ENDMACRO()
Example:
SUBDIRLIST(SUBDIRS ${MY_CURRENT_DIR})
Use
foreach
:FOREACH(subdir ${SUBDIRS}) ADD_SUBDIRECTORY(${subdir}) ENDFOREACH()
Answered By - refaim