# Generator expression question

**URL:** https://discourse.cmake.org/t/generator-expression-question/5570
**Category:** Usage
**Created:** [April 29, 2022, 8:55am UTC](https://discourse.cmake.org/t/generator-expression-question/5570 "2022-04-29T08:55:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [April 29, 2022, 8:55am UTC](https://discourse.cmake.org/t/generator-expression-question/5570/1 "2022-04-29T08:55:15Z")

</div>

Hi, I have a simple generator expression question. I want to add a source file conditionally based on a cache variable value. My code is:

```auto
set(ARM_PLATFORM "ModelSim" CACHE STRING "Target platform on which this executable will run (ModelSim or MPS2)")

set_property(CACHE ARM_PLATFORM PROPERTY STRINGS "ModelSim;MPS2")

add_executable(${EXECUTABLE}
               ${SRC_FILES}
               $<$<STREQUAL:${ARM_PLATFORM},"ModelSim">:
               # retarget.c is needed for ModelSim platform only
               src/arm_src/retarget.c
            >    
)

```

but if ARM\_PLATFORM equals “ModelSim” retarget.c is not getting compiled (I intend it should).

How is my syntax wrong?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [April 29, 2022, 9:22am UTC](https://discourse.cmake.org/t/generator-expression-question/5570/2 "2022-04-29T09:22:08Z")

</div>

I’m not sure this is a job for generator expressions at all.  
I’d rather do something along this:

```auto
if(ARM_PLATFORM STREQUAL "ModelSim")
    target_sources(${EXECUTABLE} PRIVATE retarget.c)
endif()

```

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [April 29, 2022, 9:45am UTC](https://discourse.cmake.org/t/generator-expression-question/5570/3 "2022-04-29T09:45:28Z")

</div>

@fenrir Thank you for your suggestion. I have changed my code to use target\_sources as you suggested and that fixed that problem. However, I have to use a similar generator expression in target\_compile\_definitions and it is not working there:

```auto
target_compile_definitions(${EXECUTABLE} PRIVATE
     _RTE_
     ARMCM4
     ELYSION_PLATFORM_IS_ARM=1
     $<$<STREQUAL:${ARM_PLATFORM},"ModelSim">:
        # For ModelSim only: define __FILE_INCOMPLETE (used in stdio.h) so that__FILE can be redefined in retarget.c
        __FILE_INCOMPLETE
     >    
    )

```

So I would still appreciate guidance on the correct syntax please.

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [April 29, 2022, 10:37am UTC](https://discourse.cmake.org/t/generator-expression-question/5570/4 "2022-04-29T10:37:43Z")

</div>

I’m not sure about the syntax but what I see is that you’re trying to use generator expressions where standard cmake-time constructs would suffice because they are not dependent on some properties known only at generation-time but on simple variables.  
Therefore I’d again suggest:

```auto
if(ARM_PLATFORM STREQUAL "ModelSim")
    target_compile_definitions(${EXECUTABLE} PRIVATE __FILE_INCOMPLETE)
endif()

```

The only thing that I can think of regarding the syntax is whitespace. I strongly suspect that your generator expression actually gets split into 3 arguments waaaay before it has a chance to get evaluated because of the whitespace you’ve put there.

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [April 29, 2022, 10:40am UTC](https://discourse.cmake.org/t/generator-expression-question/5570/5 "2022-04-29T10:40:12Z")

</div>

@fenrir Thanks again. I have done what you’ve suggested and it works fine. Much simpler!

---

<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 30, 2022, 12:45am UTC](https://discourse.cmake.org/t/generator-expression-question/5570/6 "2022-04-30T00:45:33Z")

</div>

> [@fenrir](#):
>
> The only thing that I can think of regarding the syntax is whitespace. I strongly suspect that your generator expression actually gets split into 3 arguments waaaay before it has a chance to get evaluated because of the whitespace you’ve put there.

Correct, that will have been the case. Also, the use of double-quotes around `"ModelSim"` inside the generator expression is also not likely to be what you want (I am pretty sure it makes the quotes part of the string to be matched). You may find the following article helpful in clarifying some of these behaviours:

> **[Quoting In CMake](https://crascit.com/2022/01/25/quoting-in-cmake/)**
>
> Get some clarity on CMake's quoting rules, as we highlight common pitfalls and offer guidelines for safer use.
