# Evaluate $\<JOIN:\> with another generator expression as the glue

**URL:** https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231
**Category:** Usage
**Created:** [April 28, 2021, 4:27pm UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231 "2021-04-28T16:27:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)
#### Post date: [April 28, 2021, 4:27pm UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231/1 "2021-04-28T16:27:44Z")

</div>

Consider the following project which tries to create a JOIN genex which uses $\<TARGET\_PROPERTY\> as glue

```
cmake_minimum_required(VERSION 3.16)

project(proj LANGUAGES CXX)

set(source "${CMAKE_CURRENT_BINARY_DIR}/source.cpp")
file(WRITE "${source}" "void foo() {}")
add_library(lib1 ${source})

set(source "${CMAKE_CURRENT_BINARY_DIR}/source2.cpp")
file(WRITE "${source}" "void foo() {}")
add_library(lib2 ${source})

set_target_properties(lib1 PROPERTIES _prop "plug1;plug2")
set_target_properties(lib2 PROPERTIES _prop "plug3;plug4")

set(pre_genex "$<TARGET_PROPERTY:")
set(post_genex ",_prop>")
set(glue "${post_genex}\n${pre_genex}")

set(join "${pre_genex}$<JOIN:lib1;lib2,${glue}>${post_genex}")

# What this is supposed to do is surround each lib with $<TARGET_PROPERTY:,_prop>
# to extract the _prop from each lib in the given list.
# The genex looks kinda like this
# $<TARGET_PROPERTY: $<JOIN:lib1;lib2, ,_prop> \n $<TARGET_PROPERTY: > ,_prop>

# Desired intermediate content
# $<TARGET_PROPERTY:lib1,_prop> \n $<TARGET_PROPERTY:lib2,_prop>

# Desired final content
# plug1;plug2
# plug3;plug4

message(">>>> join:\n${join}")
file(GENERATE
     OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/out.txt"
     CONTENT "join_content:\n${join}"
)

```

This fails generation with Error evaluating generator expression: $\<TARGET\_PROPERTY:\>  
I tried sprinkling escaping characters, bracket arguments, GENEX\_EVALs to no avail.  
Is something like this doable?

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [April 28, 2021, 7:07pm UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231/2 "2021-04-28T19:07:09Z")

</div>

Have you tried using [`string(JOIN ...)`](https://cmake.org/cmake/help/latest/command/string.html#join) instead of the `$<JOIN:...>` generator expression?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 29, 2021, 12:23am UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231/3 "2021-04-29T00:23:19Z")

</div>

The key is to prevent CMake from interpreting some things as generator expressions too early, then evaluating them later when you need them to be. This should give you the result you want:

```cmake
set(pre_genex "$$<1:<TARGET_PROPERTY:>")
set(post_genex "$<COMMA>_prop$<ANGLE-R>")
set(glue "${post_genex}\n${pre_genex}")

set(join "$<GENEX_EVAL:${pre_genex}$<JOIN:lib1;lib2,${glue}>${post_genex}>")

```

---

<div class="post-metadata">

### Author: ![jobor](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jobor/32/1230_2.png) [@jobor](https://discourse.cmake.org/u/jobor)
#### Post date: [April 29, 2021, 7:57am UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231/4 "2021-04-29T07:57:49Z")

</div>

Is $$ the canonical (documented) way to escape dollar?  
My hack looked like this: $\<1:$\>

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [April 29, 2021, 8:01am UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231/5 "2021-04-29T08:01:00Z")

</div>

I don’t think it’s really an escape. It’s just that the first `$` is not followed by `<` and so doesn’t get interpreted as a genex start during the first evaluation (the second `$` is, forming `$<1`). Then, at the second evaluation, the left-as-is `$` combines with the `<TARGET_PROPERTY:` produced by the `$<1:` genex, yielding `$<TARGET_PROPERTY:`, which is a normal genex start and thus treated as such.

---

<div class="post-metadata">

### Author: ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)
#### Post date: [April 29, 2021, 8:52am UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231/6 "2021-04-29T08:52:01Z")

</div>

> [@craig.scott](#):
>
> set(pre\_genex “$$\<1:\<TARGET\_PROPERTY:\>”)  
> set(post\_genex “$\_prop$”)  
> set(glue “${post\_genex}\n${pre\_genex}”)
> 
> set(join “$\<GENEX\_EVAL:${pre\_genex}$JOIN:lib1;lib2,${glue}${post\_genex}\>”)

Thanks, that works nicely!

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 29, 2021, 9:56am UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231/7 "2021-04-29T09:56:07Z")

</div>

> [@jobor](#):
>
> Is $$ the canonical (documented) way to escape dollar?  
> My hack looked like this: $\<1:$\>

As @Angew mentioned, it isn’t so much about escaping the $, it’s about preventing CMake from seeing `$<...` and treating it as the start of a generator expression. Your alternative of `$<1:$>` should also work.

---

<div class="post-metadata">

### Author: ![jobor](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jobor/32/1230_2.png) [@jobor](https://discourse.cmake.org/u/jobor)
#### Post date: [April 29, 2021, 10:05am UTC](https://discourse.cmake.org/t/evaluate-join-with-another-generator-expression-as-the-glue/3231/8 "2021-04-29T10:05:33Z")

</div>

Thanks for the explanation, I did read `$$<TARGET_PROPERTY` which is not what you wrote. 🙄
