IF Generator expression evaluates both branches eagerly
When writing an IF
generator expression, both branches of the IF
are evaluated eagerly, which leads to problems if the non-selected branch evaluates another generator expression that is invalid in that context. For example, consider:
set(link_command "$<IF:$<TARGET_EXISTS:${target}>,-l$<TARGET_PROPERTY:${target},OUTPUT_NAME>,-l${target}>")
The goal here is to evaluate the $<TARGET_PROPERTY:${target},OUTPUT_NAME>
only when the $<TARGET_EXISTS:${target}>
condition is true, because trying to do otherwise is obviously wrong. However, CMake appears to evaluate both branches regardless of which one is selected, and this yields an error when ${target}
does not exist.