我有一组用于构建大量项目的cmake文件。目前,我的cmake代码不支持嵌套的子项目。在我的构建中,每个项目都将其库安装到名为“devel_artifacts”的顶级目录中。由于所有项目目前处于同一级别,因此每个项目都是这样的:
get_filename_component(DEVEL_INSTALL_PATH_PREFIX_ABSOLUTE“../ level_artifacts”ABSOLUTE)
将相对的../devel_artifacts转换为绝对路径。为了支持嵌套的子项目,我想编写一个cmake函数,在当前目录中查找devel_artifacts,如果找不到它,则从路径中删除最终目录并再次尝试...并继续这样做直到它要么找到devel_artifacts的路径,要么失败(在这种情况下它可以简单地终止)。在cmake中这可能吗?
答案 0 :(得分:2)
您可以从当前源目录开始构建搜索路径列表,并迭代添加每个父项,然后将该列表传递给find_file
:
set(SearchPath "${CMAKE_CURRENT_SOURCE_DIR}")
while(NOT "${SearchPath}" STREQUAL "${PreviousSearchPath}")
list(APPEND SearchPaths "${SearchPath}")
set(PreviousSearchPath "${SearchPath}")
# use legacy 'PATH' arg instead of 'DIRECTORY' for CMake version <= 2.8.11
get_filename_component(SearchPath "${SearchPath}" DIRECTORY)
endwhile()
find_file(DevelArtifacts
NAMES devel_artifacts
PATHS ${SearchPaths}
DOC "Path to devel_artifacts found by iterating up through dirs from ${CMAKE_CURRENT_SOURCE_DIR}"
NO_DEFAULT_PATH)