Directory properties and subdirectories
Asked Answered
A

1

5

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?

Assuage answered 3/7, 2017 at 12:44 Comment(2)
If I look at CMake's source code this depends on a chained member of cmPropertyDefinition to be true. So I haven't checked if you can set this from the outside, but have you tried to call define_property(DIRECTORY PROPERTY narf INHERITED BRIEF_DOCS "" FULL_DOCS"")? Probably the INHERITED keyword does the trick (even if the INHERITED documentation says it's used for something else).Cathcart
@Cathcart wow, you're right! I'd never have guessed that. The INHERITED documentation only refers to chaining scope types, not parent scopes of the same type. You should make this an answer, it is important.Lanky
C
4

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.

Cathcart answered 3/7, 2017 at 14:48 Comment(2)
See the change here which updates the docs to clarify the existing behavior. Hopefully it makes things a bit clearer.Stacistacia
I feel though while valuable, this does not really answers the question. The contradiction between the way set_directory_properties works and the way it is documented seem to remain.Matthieu

© 2022 - 2024 — McMap. All rights reserved.