# Generator expression based on user presets

**URL:** https://discourse.cmake.org/t/generator-expression-based-on-user-presets/13017
**Category:** Usage
**Created:** [November 22, 2024, 9:50am UTC](https://discourse.cmake.org/t/generator-expression-based-on-user-presets/13017 "2024-11-22T09:50:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![DavidRegan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/d07c76/32.png) [@DavidRegan](https://discourse.cmake.org/u/DavidRegan)
#### Post date: [November 22, 2024, 9:50am UTC](https://discourse.cmake.org/t/generator-expression-based-on-user-presets/13017/1 "2024-11-22T09:50:56Z")

</div>

I wish to control target\_compile\_definitions based on user presets so that local mods may be tested. (Want\_Config\_Dump)

In my user preset I’ve added

> ```
> "configurePresets": [
> 
> ```

```
{
  "name": "config-base-local",
 "hidden": true,
  "displayName": "base Configuration for local machine",
  "description": "Default build using Ninja generator",
  "inherits":"config-base",
  "cacheVariables": {
    "CW_STM32F746_LOCAL_REPO_PATH": "${sourceDir}/../../../",
    "STM32CUBEF7_LOCAL_REPO_PATH": "${sourceDir}/../../../",
    "CMAKE_BUILD_TYPE": "Debug",
    "ARM_TOOLCHAIN_BIN_DIR": "C:/ST/STM32CubeIDE_1.16.0/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.12.3.rel1.win32_1.0.200.202406191623/tools/bin",
    "GIT_BASH_CMD": "C:/Program Files/Git/usr/bin/bash",
    "CMAKE_EXPORT_COMPILE_COMMANDS": {
      "type": "BOOL",
      "value": "TRUE"
    },
    "Use_CW_Local_repo": true,
    "Use_ST_Local_repo": true,
    "Want_Config_Dump": false
  }
},

```

In my CMakeLists.txt I’ve added

> //Applicaton compile definitions  
> if (Want\_Config\_Dump)  
> set(CFG\_DUMP\_CTRL “-DWANT\_CFG\_DUMP=1”)  
> else()  
> set(CFG\_DUMP\_CTRL “-DWANT\_CFG\_DUMP=0”)  
> endif()  
> set(application\_compile\_definitions  
> $\<$CONFIG:DEBUG:-DDEBUG\>  
> -DUSE\_HAL\_DRIVER  
> -DSTM32F746xx  
> -DSTM32\_THREAD\_SAFE\_STRATEGY=5  
> -DUSES\_RTOS=1  
> -DconfigCWSTM32F746\_USE\_CODE\_TRACKPOINTS=0  
> -DUSE\_USB\_FS  
> -DconfigCWSTM32F746\_USE\_DBG\_TRIGGERS=15  
> -DWANT\_ATB\_PBS=1  
> -DWANT\_LWIP\_PLATFORM\_OVERRIDES  
> -DRELEASE=RELEASEB  
> // If WANT\_CFG\_DUMP is non-zero the application will not run the main system loop.  
> //This allows the configuration initialisation to be verified  
> ${CFG\_DUMP\_CTRL}  
> -DLOW\_LEVEL\_IO\_MESSAGES  
> -DWANT\_QUEUE\_REGISTRY=1  
> -DWANT\_STATMSG\_PRINTOUT=1  
> )

This works but I spent a while trying to do this this generator expressions based on Want\_Config\_Dump. Is this possible? I tried a few variations and all said it did not evaluate to a known 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: [November 23, 2024, 7:44pm UTC](https://discourse.cmake.org/t/generator-expression-based-on-user-presets/13017/2 "2024-11-23T19:44:04Z")

</div>

If you are passing these flags to commands like `target_compile_definitions()` and `add_compile_definitions()`, those commands do NOT want the leading `-D` to be included. Older commands like `add_definitions()` do want them, but you shouldn’t be using those old commands anyway. Check the docs of the command(s) you’re using to confirm whether they expect a `-D` or not. This is especially relevant if you’re using generator expressions because the presence of a generator expression bypasses the logic that checks for and removes a stray leading `-D` if an item has it but the command doesn’t want it.

And while I’m here, I’m guessing you probably tried to split your generator expression across multiple lines. See the _Generator Expressions_ section of my [Quoting In CMake](https://crascit.com/2022/01/25/quoting-in-cmake/#h-generator-expressions) article that explains how to do that safely, including examples of how to build up more complicated generator expressions in pieces.

---

<div class="post-metadata">

### Author: ![DavidRegan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/d07c76/32.png) [@DavidRegan](https://discourse.cmake.org/u/DavidRegan)
#### Post date: [December 9, 2024, 11:56am UTC](https://discourse.cmake.org/t/generator-expression-based-on-user-presets/13017/3 "2024-12-09T11:56:21Z")

</div>

Hi Craig  
Sorry for the late response - I got involved in something else. I’m using CMake to control a cross platform build for an embedded system. The -D switch is required in the command line.

I need to have on the command line -DWANT\_CFG\_DUMP=1 or -DWANT\_CFG\_DUMP=0 or nothing.

I think that I’m misunderstang generator expressions. In my user preset file I thought that I could add something like  
“Want\_Config\_Dump”: true, in the cacheVariables section  
and then evaluate it when generating in CMakeLists.txt with something like  
$\<Want\_Config\_Dump:-DWANT\_CFG\_DUMP=1\>

What am I doing wrong

---

<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: [December 9, 2024, 8:53pm UTC](https://discourse.cmake.org/t/generator-expression-based-on-user-presets/13017/4 "2024-12-09T20:53:39Z")

</div>

This should get you close:

```auto
$<$<BOOL:${Want_Config_Dump}>:-DWANT_CFG_DUMP=1>

```

The `$<xxx:...>` generator expression requires that `xxx` be exactly 0 or 1. No other value is valid. Your `Want_Config_Dump` variable is going to have neither of those values, but will be a boolean-ish string. Wrapping it in `$<BOOL:...>` converts it to exactly 0 or 1, thereby satisfying the constraint.

---

<div class="post-metadata">

### Author: ![DavidRegan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/d07c76/32.png) [@DavidRegan](https://discourse.cmake.org/u/DavidRegan)
#### Post date: [December 9, 2024, 9:26pm UTC](https://discourse.cmake.org/t/generator-expression-based-on-user-presets/13017/5 "2024-12-09T21:26:22Z")

</div>

Thank you Craig

Just what I wanted
