How to detect if current scope has a parent in CMake?

前端 未结 2 920
天涯浪人
天涯浪人 2020-12-01 14:25

Is there any way to detect if the current scope has a parent?

I have a project that can either be a standalone project or a sub-project of another. To allow the sub

相关标签:
2条回答
  • 2020-12-01 14:48

    I think the most robust approach is to use the PARENT_DIRECTORY directory property.

    This will yield the correct answer regardless of whether it's called before or after the project command, and regardless of whether the parent and child both have the same project name.

    get_directory_property(hasParent PARENT_DIRECTORY)
    if(hasParent)
      message(STATUS "Has a parent scope.")
    else()
      message(STATUS "Doesn't have a parent scope.")
    endif()
    
    0 讨论(0)
  • 2020-12-01 14:56

    Expanding a bit on @ruslo idea I would not take the PROJECT_SOURCE_DIR but the CMAKE_PROJECT_NAME variable (contains the name of the first defined project) and the PROJECT_NAME (contains the name of the current project), so you could do something like this in the CMakeLists.txt of the Subproject:

    project(bar)
    
    if(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
     #do stuff
    else()
     #do other stuff
    end(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
    
    0 讨论(0)
提交回复
热议问题