CMake - 检查是否存在更高级别的目录

时间:2017-12-06 12:59:50

标签: cmake

我是CMake的新手,我试图在我的根CMakeLists.txt中执行此操作:

set(HAVE_MY_SDK   OFF)

if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}../Libs/A")
    if(EXISTS "${PROJECT_SOURCE_DIR}../Libs/A/CMakeLists.txt")
        set (HAVE_MY_SDK   ON)
    endif()
endif()

目前,CMake正在避开该指令,使HAVE_MY_SDK处于OFF状态。是否可以使用CMake检查更高级别的目录?或者也许用间接方法

1 个答案:

答案 0 :(得分:1)

我认为你在/之后错过${PROJECT_SOURCE_DIR}

这里的完整性是我用于此的代码(注意if (EXISTS ...)需要完整路径):

get_filename_component(_fullpath "${_dir}" REALPATH)
if (EXISTS "${_fullpath}" AND EXISTS "${_fullpath}/CMakeLists.txt")
    ...

在我的扩展add_subdirectory()版本中(包括“添加一次警卫”):

function(my_add_subdirectory _dir)
    get_filename_component(_fullpath "${_dir}" REALPATH)
    if (EXISTS "${_fullpath}" AND EXISTS "${_fullpath}/CMakeLists.txt")
        get_property(_included_dirs GLOBAL PROPERTY GlobalAddSubdirectoryOnceIncluded)
        list(FIND _included_dirs "${_fullpath}" _used_index)
        if (${_used_index} EQUAL -1)
            set_property(GLOBAL APPEND PROPERTY GlobalAddSubdirectoryOnceIncluded "${_fullpath}")
            add_subdirectory(${_dir} ${ARGN})
        endif()
    else()
        message(WARNING "my_add_subdirectory: Can't find ${_fullpath}/CMakeLists.txt")
    endif()
endfunction(my_add_subdirectory)