# Escaping generator expressions in ExternalProject\_Add

**URL:** https://discourse.cmake.org/t/escaping-generator-expressions-in-externalproject-add/6855
**Category:** Code
**Tags:** os:windows, gen:ninja
**Created:** [November 12, 2022, 6:06pm UTC](https://discourse.cmake.org/t/escaping-generator-expressions-in-externalproject-add/6855 "2022-11-12T18:06:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mihe](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mihe/32/2920_2.png) [@mihe](https://discourse.cmake.org/u/mihe)
#### Post date: [November 12, 2022, 6:06pm UTC](https://discourse.cmake.org/t/escaping-generator-expressions-in-externalproject-add/6855/1 "2022-11-12T18:06:39Z")

</div>

As far as I can tell, you’re able to pass generator expressions to CMake through the command-line and have them be evaluated appropriately. This comes in handy when wanting to set `CMAKE_MSVC_RUNTIME_LIBRARY` for some third-party library while using a multi-config generator like `Ninja Multi-Config`:

```nohighlight
cmake -B ./build -G "Ninja Multi-Config" -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Foo,Bar>:Debug>
cmake --build ./build --config Foo

```

How do you achieve the same thing with `ExternalProject_Add` and its `CMAKE_ARGS` without the generator expression being evaluated in the calling project?

I’ve tried replacing every closing angle bracket with `$<ANGLE-R>` in hopes that it would somehow defer the evaluation of the generator expression, but without luck. My every attempt at trying to escape the generator expression has resulted in the generator expressions either still being evaluated in the calling project (i.e. not getting escaped) or seemingly just turned into a token soup that CMake can’t understand and it falls back to the default value of `MultiThreaded$<$<CONFIG:Debug>:Debug>DLL`.

---

<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: [November 12, 2022, 8:39pm UTC](https://discourse.cmake.org/t/escaping-generator-expressions-in-externalproject-add/6855/2 "2022-11-12T20:39:15Z")

</div>

Fighting with `ExternalProject` on escaping sucks. I’ve done it a lot, but not for genexes. It might be better to write it to a file and then use `-C path/to/file` as a cache initialization file.

```cmake
set(CMAKE_MSVC_RUNTIME_LIBRARY "…" CACHE STRING "")

```

---

<div class="post-metadata">

### Author: ![mihe](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mihe/32/2920_2.png) [@mihe](https://discourse.cmake.org/u/mihe)
#### Post date: [November 13, 2022, 11:01am UTC](https://discourse.cmake.org/t/escaping-generator-expressions-in-externalproject-add/6855/3 "2022-11-13T11:01:25Z")

</div>

Writing it to an initial cache file instead does seem to do the trick. It’s not ideal, but it’ll have to do. Thank you!
