Check target exists with generator expressions
It would be nice to be able to check if a target exists in a generator expression. Something like $<TARGET_EXISTS:...>
that return 1 if the target exists. This way I could link in optional dependencies with generator expressions:
target_link_libraries(foo $<$<TARGET_EXISTS:bar>:bar>)
target_compile_definitions(foo PUBLIC $<$<TARGET_EXISTS:bar>:HAS_FOO=1>)
This way it will optionally link in bar if its available from another add_subdirectory
.
There is also this related stackoverflow question about this same scenario: