Example from cmake-generator-expressions is not robust
From https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#whitespace-and-quoting
# The $<BOOL:...> check prevents adding anything if the property is empty,
# assuming the property value cannot be one of CMake's false constants.
set(prop "$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>")
add_custom_target(run_some_tool
COMMAND some_tool "$<$<BOOL:${prop}>:-I$<JOIN:${prop},;-I>>"
COMMAND_EXPAND_LISTS
VERBATIM
)
Unfortunately, this is not robust. A trivial example is set(prop ";")
, which is evaluated to true by the BOOL
check.
A less artificial example which fails this check are apparently generator expressions. The latest nlohman_json has the following COMPILE_DEFINITIONS:
INTERFACE_COMPILE_DEFINITIONS "\$<\$<NOT:\$<BOOL:ON>>:JSON_USE_GLOBAL_UDLS=0>;\$<\$<NOT:\$<BOOL:ON>>:JSON_USE_IMPLICIT_CONVERSIONS=0>;\$<\$<BOOL:OFF>:JSON_DISABLE_ENUM_SERIALIZATION=1>;\$<\$<BOOL:OFF>:JSON_DIAGNOSTICS=1>;\$<\$<BOOL:OFF>:JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON=1>"
This may expand to ;;;;;
, which is expanded to -D
with the code from the example.
Changing $<BOOL:${prop}>:...
to $<BOOL:$<JOIN:${prop},;>>:...
avoids this error.
set(prop ";")
add_custom_target(genexdebug}
COMMAND ${CMAKE_COMMAND} -E echo "T_Err: $<$<BOOL:${prop}>:-I$<JOIN:${prop},;-I>>"
COMMAND ${CMAKE_COMMAND} -E echo "T_OK: $<$<BOOL:$<JOIN:${prop},;>>:-I$<JOIN:${prop},;-I>>"
COMMAND_EXPAND_LISTS
VERBATIM
)
I think #20951 (closed) was meant to fix this, but there are obviously cases which are not handled correctly (CMake 3.24.0).