转载自:https://www.codeleading.com/article/36054217046/
# 获取目录下的子目录名
# output 子目录名称列表, 填充时不能加${}
# target 目标路径, 全路径
macro(list_subdir output target)
file(GLOB children ${target}/*)
set(dir_list "")
foreach(child ${children})
file(RELATIVE_PATH child_name ${target} ${child})
if(IS_DIRECTORY ${target}/${child_name})
list(APPEND dir_list ${child_name})
endif()
endforeach()
list(APPEND ${output} ${dir_list})
endmacro()
# 判断目录下是否有 CMakeLists
# output 是否含有 CMakeLists.txt, 填充时不能加 ${}
# target 目标路径, 全路径
macro(check_cmakelists output target)
set(${output} FALSE)
if(IS_DIRECTORY ${target})
if(EXISTS ${target}/CMakeLists.txt)
set(${output} TRUE)
endif()
else()
message(FATAL_ERROR "Invalid dir para: ${target}")
endif()
endmacro()
# 为含有 CMakeList 的子目录添加 add_subdirectory 调用
macro(add_all_subdirs)
set(subdir_list "")
list_subdir(subdir_list ${CMAKE_CURRENT_SOURCE_DIR})
foreach(subdir ${subdir_list})
set(has_cmakelist FALSE)
check_cmakelists(has_cmakelist ${CMAKE_CURRENT_SOURCE_DIR}/${subdir})
if(${has_cmakelist})
add_subdirectory(${subdir})
endif()
endforeach()
endmacro()
标签:set,target,子目录,list,添加,自动,subdir,child,output
From: https://www.cnblogs.com/lonelyraft/p/add_all_subdirs.html