CMake: generator expression to check if target has a property

Viewed 140

How can I check in a generator expression if a target has a given property?

I am looking for somethig like

$<TARGET_HAS_PROPERTY:tgt,property>
1 Answers

Since a non-existing property yields the empty string, you could always do a string comparison to the empty string. This requires the property to be set to a value other than the empty string, if it's set of course so specific cases this approach may not work:

$<STREQUAL:$<TARGET_PROPERTY:tgt,property>,>

yields the negation, so

$<NOT:$<STREQUAL:$<TARGET_PROPERTY:tgt,property>,>>

yields the bool value you're looking for.

Example

add_custom_target(test_print COMMAND ${CMAKE_COMMAND} -E echo $<IF:$<STREQUAL:$<TARGET_PROPERTY:MyTarget,CXX_STANDARD>,>,"CXX_STANDARD not set for target MyTarget","CXX_STANDARD set for target MyTarget">)
Related