# target\_compile\_options: SHELL prefix not working in generator expression

**URL:** https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351
**Category:** Code
**Created:** [October 27, 2021, 1:41pm UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351 "2021-10-27T13:41:20Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![PlatonCM](https://discourse.cmake.org/user_avatar/discourse.cmake.org/platoncm/32/1910_2.png) [@PlatonCM](https://discourse.cmake.org/u/PlatonCM)
#### Post date: [October 27, 2021, 1:41pm UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/1 "2021-10-27T13:41:20Z")

</div>

Hi, I’m setting up my first project with CMake and am struggling with compile options specific to the armasm.exe we’re using in the company.

The goal is to get the following command line (stripped down) for assembly files:  
`armasm.exe --pd "__UVISION_VERSION SETA 536" --pd "STM32F427xx SETA 1" (etc.)`

I tried various approaches but either it works for ASM files or C files.

**1. SHELL: in generator expression**

```auto
target_compile_options(${thisTarget}
    PUBLIC
        $<$<COMPILE_LANGUAGE:ASM>:"SHELL:--pd \"__UVISION_VERSION SETA 536\"">
        $<$<COMPILE_LANGUAGE:ASM>:"SHELL:--pd \"STM32F427xx SETA 1\"">
)

```

yields

```auto
[build] C:\Keil_v5\ARM\ARMCC\bin\armasm.exe "\"SHELL:--pd \"__UVISION_VERSION SETA 536\"\"" "\"SHELL:--pd \"STM32F427xx SETA 1\"\"" -o CMakeFiles\AmuTarget.dir\Debug\RTE\Device\STM32F427ZITx\startup_stm32f427xx.o D:\Projects\Misc\BuildSystem\Source\RTE\Device\STM32F427ZITx\startup_stm32f427xx.s

```

and armasm fails.

**2. Without generator expression**

```auto
target_compile_options(${thisTarget}
    PUBLIC
        "SHELL:--pd \"__UVISION_VERSION SETA 536\""
        "SHELL:--pd \"STM32F427xx SETA 1\""
)

```

which works for armasm but obviously not for armcc (C/C++):

```auto
[build] C:\Keil_v5\ARM\ARMCC\bin\armcc.exe -DSTM32F427xx -D_RTE_ -D __UVISION_VERSION=\"536\" -DCMAKE_INTDIR=\"Debug\" --pd "__ UVISION_VERSION SETA 536" --pd "STM32F427xx SETA 1" --depend=CMakeFiles\AmuTarget.dir\Debug\MyClass.o.d --depend_single_line --no_depend_system_headers -o CMakeFiles\AmuTarget.dir\Debug\MyClass.o -c D:\Projects\Misc\BuildSystem\Source\MyClass.cpp

```

**3. With brackets**

```auto
target_compile_options(${thisTarget}
    PUBLIC
        $<$<COMPILE_LANGUAGE:ASM>:[=[--p "__UVISION_VERSION SETA 536" --pd "STM32F427xx SETA 1"]=]>
)

```

where armasm interprets “[=[…]=]” as a filename:

```auto
[build] C:\Keil_v5\ARM\ARMCC\bin\armasm.exe "[=[--p;__UVISION_VERSION SETA 536;--pd;STM32F427xx SETA 1;]=]" -o CMakeFiles\AmuTarget.dir\Debug\RTE\Device\STM32F427ZITx\startup_stm32f427xx.o D:\Projects\Misc\BuildSystem\Source\RTE\Device\STM32F427ZITx\startup_stm32f427xx.s

```

❓  
What am I missing?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [October 27, 2021, 2:56pm UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/2 "2021-10-27T14:56:29Z")

</div>

For the correct interpretation of the `genex` and `SHELL:` prefix, specify the whole genex inside quotes. Try this version:

```cmake
target_compile_options(${thisTarget}
    PUBLIC
        "$<$<COMPILE_LANGUAGE:ASM>:SHELL:--pd \"__UVISION_VERSION SETA 536\">"
        "$<$<COMPILE_LANGUAGE:ASM>:SHELL:--pd \"STM32F427xx SETA 1\">"
)

```

---

<div class="post-metadata">

### Author: ![PlatonCM](https://discourse.cmake.org/user_avatar/discourse.cmake.org/platoncm/32/1910_2.png) [@PlatonCM](https://discourse.cmake.org/u/PlatonCM)
#### Post date: [October 27, 2021, 3:07pm UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/3 "2021-10-27T15:07:33Z")

</div>

Spot on, thank you very much.  
Are the quotes required because of the SHELL: prefix? I added other options (without prefixes) with unquoted genex, and those work fine.

---

<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: [October 27, 2021, 3:09pm UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/4 "2021-10-27T15:09:24Z")

</div>

The quotes are required due to the spaces in the value. Without the overall quoting, there will be `$<GENEX:SHELL:--pd` and `\"value\">` arguments to `target_compile_options` (essentially injecting a `;` into the value).

---

<div class="post-metadata">

### Author: ![PlatonCM](https://discourse.cmake.org/user_avatar/discourse.cmake.org/platoncm/32/1910_2.png) [@PlatonCM](https://discourse.cmake.org/u/PlatonCM)
#### Post date: [October 27, 2021, 3:17pm UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/5 "2021-10-27T15:17:52Z")

</div>

Thanks for pointing that out. Then  
`$<$<OR:$<COMPILE_LANGUAGE:CXX>,$<COMPILE_LANGUAGE:C>>:--asm -c --interleave>`  
only works without quotes because the individual options do not take any additional values.

---

<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: [October 27, 2021, 4:25pm UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/6 "2021-10-27T16:25:01Z")

</div>

I’d still quote that myself, but yeah, that seems plausible.

I’ll also note that `$<COMPILE_LANGUAGE:CXX,C>` will make the `$<OR>` unnecessary.

---

<div class="post-metadata">

### Author: ![PlatonCM](https://discourse.cmake.org/user_avatar/discourse.cmake.org/platoncm/32/1910_2.png) [@PlatonCM](https://discourse.cmake.org/u/PlatonCM)
#### Post date: [October 28, 2021, 6:06am UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/7 "2021-10-28T06:06:41Z")

</div>

Nice, to get rid of that OR.  
However, quoting breaks the function in this case, interestingly:

```auto
$<$<COMPILE_LANGUAGE:CXX,C>:--signed_chars --split_sections --no_exceptions --no_rtti>
"$<$<COMPILE_LANGUAGE:CXX,C>:--asm -c --interleave>"

```

⬇  
**[build] Fatal error: C3900U: Unrecognized option ‘–asm -c --interleave’.**

```auto
[build] C:\Keil_v5\ARM\ARMCC\bin\armcc.exe --signed_chars --split_sections --no_exceptions --no_rtti "--asm -c --interleave" --depend=CMakeFiles\AmuTarget.dir\Debug\MyClass.o.d --depend_single_line --no_depend_system_headers -o CMakeFiles\AmuTarget.dir\Debug\MyClass.o -c D:\Projects\Misc\BuildSystem\Source\MyClass.cpp

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [October 28, 2021, 10:11am UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/8 "2021-10-28T10:11:03Z")

</div>

I am surprised that `$<$<COMPILE_LANGUAGE:CXX,C>:--signed_chars --split_sections --no_exceptions --no_rtti>` works as expected because it will be seen as the following list:

```auto
$<$<COMPILE_LANGUAGE:CXX,C>:--signed_chars
--split_sections
--no_exceptions
--no_rtti>

```

So, the `genex` is malformed and cannot be evaluated…

For the second ex[pression, this is because you don’t use correctly the `genex`. What is expected by the `genex` is a `CMake` list, and you specify a string. Correct expression is:

```cmake
"$<$<COMPILE_LANGUAGE:CXX,C>:--asm;-c;--interleave>"

```

---

<div class="post-metadata">

### Author: ![PlatonCM](https://discourse.cmake.org/user_avatar/discourse.cmake.org/platoncm/32/1910_2.png) [@PlatonCM](https://discourse.cmake.org/u/PlatonCM)
#### Post date: [October 28, 2021, 11:23am UTC](https://discourse.cmake.org/t/target-compile-options-shell-prefix-not-working-in-generator-expression/4351/9 "2021-10-28T11:23:47Z")

</div>

The malformed genex indeed creates a working result as I can see in _compile\_commands.json_.  
I changed it to a list, and of course, it works like a charm now.  
Thank you
