Generation expression based on dependee?

Is it possible to write a generator expression at any point that varies the output based on a property coming from being linked to something?

Such that in the following

add_library(widgets ...)
add_library(mouse ...)
...
add_library(input ...)
target_link_library(input PUBLIC $<IF:$<LINKAGE_INCLUDES:mouse>,widgets>)

add_executable(app ...)
target_link_libraries(app PUBLIC input mouse)

automatically links ‘input’ against ‘widgets’ because ‘mouse’ is present?

And is it possible to write a condition based on a property that is being gained as above by inheritance (so I don’t know the target name, its the name of the thing that asked to link me against it)

if (NOT HEADLESS)
  set_property(TARGET some_other_target NEEDS_WIDGETS ON)
endif ()
...
target_link_libraries(mouse PUBLIC $<IF:$<INHERITED_PROPERTY:NEEDS_WIDGETS>,widgets>)

such that mouse implies widgets but only when ‘some_other_target’ is in the link chain?

— Edit

Clarification: What I’m trying to achieve is a library that pulls in different dependencies based on later linkage context, e.g linking readline vs x11 based on whether the command-line-arguments parser is being linked to us; linking the appropriate scripting library depending on whether the executable we end up linked to has USE_PYTHON or USE_JAVASCRIPT.

This is what I call “intersectional usage requirements” (or “I use A and B therefore C” kind of logic). CMake doesn’t have support for this. What you can do is something like:

target_link_libraries(input
  INTERFACE
    "$<$<BOOL:$<TARGET_PROPERTY:USES_MOUSE>>:widgets>")

add_executable(app)
target_link_libraries(app PUBLIC input mouse)
set_property(TARGET app PROPERTY USES_MOUSE 1)

There’s also no support for $<INHERITED_PROPERTY> because it can cause undecidable builds (a “later” target could “set” a property that changes what an “earlier” target would have done which, if followed, makes the “later” target not necessary: basically a way to say “this sentence is false”).

1 Like