Friday, October 07, 2011

Add All Subdirectories in CMake

有時候,某個目錄下有那些子目錄要用add_subdirectory加到build system裡是動態的(也就是不能寫死),像是我們用android的repo工具,projects目錄下要掛什麼sub-project完全就是看default.xml,那麼這個時候cmake的add_subdirectory就整個很沒用,所以就只好用regex硬幹...如果有人碰到一樣的問題可以用以下的方法:



file(GLOB all_valid_subdirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*/CMakeLists.txt")

foreach(dir ${all_valid_subdirs})
message(STATUS "path = ${dir}")
if(${dir} MATCHES "^([^/]*)//CMakeLists.txt")
string(REGEX REPLACE "^([^/]*)//CMakeLists.txt" "\\1" dir_trimmed ${dir})
add_subdirectory(${dir_trimmed})
endif()
endforeach(dir)

0 Comments: