Add feature to define custom variable-initialized properties like (CMAKE_AUTOMOC)
Initially asked on the forum, will post description here.
To disable some compile flags based on a target property, consumers of my library have to do something like the following:
find_package(Mylib)
add_executable(app_target)
target_link_libraries(app_target INTERFACE mylib::mylib)
set_target_properties(app_target PROPERTIES DISABLE_SPECIAL_FLAG 1)
and my library when created has something like the following
add_library(SHARED mylib)
set(genex_condition "$<NOT:$<BOOL:$<TARGET_PROPERTY:DISABLE_SPECIAL_FLAG>>>")
set(flags "$<${genex_condition}:-my-nice-flag>")
target_compile_options("mylib" INTERFACE "${flags}")
The above API forces users to explicitly set the DISABLE_SPECIAL_FLAG
property on each target that is directly linking against mylib (I’m aware that users can work around it by linking publicly against their own interface library that sets it only once).
It would be nice if users could define something like CMAKE_AUTOMOC
, where if the CMAKE_AUTOMOC
variable is set in a scope, all targets created below that variable get their AUTOMOC
property initialized to whatever is set in CMAKE_AUTOMOC
.
So I’d like the user to set a GLOBAL_DISABLE_SPECIAL_FLAG
variable to 1, and then the DISABLE_SPECIAL_FLAG
property is automatically populated with 1 for all targets created below that statement.
Perhaps it could be done via a command call rather than a variable.
Something like: add_properties(PROPERTY_NAME "value")
, and all targets created within this scope and later would have that property set.