get_target_property() Returning VAR-NOTFOUND for an inherited property
Code to reproduce:
define_property(TARGET PROPERTY SOME_PROPERTY INHERITED)
add_custom_target(someTarget)
get_target_property(MY_PROPERTY someTarget SOME_PROPERTY)
message(FATAL_ERROR "MY_PROPERTY : ${MY_PROPERTY}")
Expected outcome:
CMake Error at CMakeLists.txt:505 (message):
MY_PROPERTY :
Actual Outcome:
CMake Error at CMakeLists.txt:505 (message):
MY_PROPERTY : MY_PROPERTY-NOTFOUND
The documentation for define_property()
says
https://cmake.org/cmake/help/latest/command/define_property.html
If the INHERITED option is given, 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.
and
Note that this scope chaining behavior only applies to calls to get_property(), get_directory_property(), get_target_property(), get_source_file_property() and get_test_property().
and I am using get_target_property()
The documentation for get_target_property()
says
https://cmake.org/cmake/help/latest/command/get_target_property.html
Non-inherited properties will set to -NOTFOUND, whereas inherited properties will search the relevant parent scope as described for the define_property() command and if still unable to find the property, will be set to an empty string.
So I've defined an inherited property at the "TARGET" scope, defined a target, and queried for that property.
Since I did not set that inherited target property to a value manually at either the target or directory scope, the result of get_target_property()
should be that the variable MY_PROPERTY
is set to empty string.
I needed to provide an initial value variable which I set to "" to get an empty string to be returned:
set(DEFAULT_SOME_PROPERTY "")
define_property(TARGET PROPERTY SOME_PROPERTY INITIALIZE_FROM_VARIABLE DEFAULT_SOME_PROPERTY)
add_custom_target(someTarget)
get_target_property(MY_PROPERTY someTarget SOME_PROPERTY)
message(FATAL_ERROR "MY_PROPERTY : ${MY_PROPERTY}")
Keeping or removing the INHERITED parameter seems to make no difference
Setting the DEFAULT_SOME_PROPERTY
like set(DEFAULT_SOME_PROPERTY)
without the empty quotes doesn't seem to work.