Generator expression based on user presets

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

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 article that explains how to do that safely, including examples of how to build up more complicated generator expressions in pieces.

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

This should get you close:

$<$<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.

Thank you Craig

Just what I wanted