Directory properties and subdirectories

2019-02-23 18:56发布

The CMake manual for set_directory_properties claims:

Set a property for the current directory and subdirectories.

To me this suggests that properties set in a parent directory should also be inherited to all subdirectories. But this does not seem to be the case. Consider:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(foo CXX)

set_property(DIRECTORY . PROPERTY narf "zort")

add_subdirectory(a)

get_property(res DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY narf)
message("Property read from root: " ${res})

a/CMakeLists.txt

get_property(res DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY narf)
message("Property for a read from a: " ${res})
get_property(res DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY narf)
message("Property for root directory root read from a: " ${res})

This prints:

Property for a read from a: 
Property for root directory root read from a: zort
Property read from root: zort

So the property can only be retrieved from the directory on which it was set, not the subdirectories. The same is true when using the set_directory_properties/get_directory_properties to deal with the properties.

Did I misinterpret the respective section in the set_directory_properties manual? Or is it simply outdated/wrong?

标签: cmake
1条回答
我命由我不由天
2楼-- · 2019-02-23 19:36

Turning my comment into an answer

If I look at CMake's source code this depends on the chained member of cmPropertyDefinition to be true.

So you can achieve this for your own directory property by using the INHERITED keyword with define_property():

define_property(
    DIRECTORY 
    PROPERTY narf 
    INHERITED 
    BRIEF_DOCS "Brief Doc" 
    FULL_DOCS "Full Doc"
)

Even if the INHERITED documentation says only:

If the INHERITED option then the get_property() command will chain up to the next higher scope when the requested property is not set in the scope given to the command. DIRECTORY scope chains to GLOBAL. TARGET, SOURCE, and TEST chain to DIRECTORY.

查看更多
登录 后发表回答