Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - Check if a higher level directory exists

Tags:

cmake

I'm new to CMake and I tried to do this in my root 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()

Currently, CMake is just avoiding the instruction, leaving HAVE_MY_SDK in a OFF status. Is it possible to check an higher level directory with CMake ? Or maybe doing it with an indirect method

like image 831
JonOsterman Avatar asked Dec 06 '17 12:12

JonOsterman


People also ask

What is Cmake Strequal?

if(<variable|string> STREQUAL <variable|string>) True if the given string or variable's value is lexicographically equal to the string or variable on the right.

What does Add_subdirectory do in Cmake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

How do I change my path in Cmake?

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .


1 Answers

I think you're just missing a / after ${PROJECT_SOURCE_DIR}.

Out of completeness here is the code I use for this (note that if (EXISTS ...) needs full paths):

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

And here inside my extended add_subdirectory() version (including a "add once guard"):

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)
like image 60
Florian Avatar answered Sep 28 '22 08:09

Florian