我是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检查更高级别的目录?或者也许用间接方法
答案 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)