# Generation expression based on dependee?

**URL:** https://discourse.cmake.org/t/generation-expression-based-on-dependee/7220
**Category:** Usage
**Created:** [January 9, 2023, 7:56pm UTC](https://discourse.cmake.org/t/generation-expression-based-on-dependee/7220 "2023-01-09T19:56:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![kfsone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kfsone/32/1880_2.png) [@kfsone](https://discourse.cmake.org/u/kfsone)
#### Post date: [January 9, 2023, 7:56pm UTC](https://discourse.cmake.org/t/generation-expression-based-on-dependee/7220/1 "2023-01-09T19:56:10Z")

</div>

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

```auto
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)

```auto
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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 9, 2023, 10:59pm UTC](https://discourse.cmake.org/t/generation-expression-based-on-dependee/7220/2 "2023-01-09T22:59:38Z")

</div>

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:

```cmake
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”).
