Config preset in CMakeUserPresets.json doesn't inherit cacheVariables from parent in CMakePresets.json

My CMakePresets.json:

{
    "version": 3,
    "configurePresets": [
        {
            "name": "base",
            "hidden": true,
            "generator": "Ninja",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "clang-cl",
                "CMAKE_CXX_COMPILER": "clang-cl",
                "CMAKE_C_FLAGS": "-fuse-ld=lld /EHsc",
                "CMAKE_CXX_FLAGS": "-fuse-ld=lld /EHsc"
            },
            "condition": {
                "type":"inList",
                "string": "${hostSystemName}",
                "list": ["Linux", "Windows"]
            }
        },
        {
            "name": "x64-debug",
            "displayName": "x64 Debug",
            "inherits": "base",
            "binaryDir": "${sourceDir}/build/x64/Debug",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "x64-debug",
            "configurePreset": "x64-debug"
        }
    ]
}

My CMakeUserPresets.json:

{
    "version": 3,
    "configurePresets": [
        {
            "name": "x64-debug-special",
            "inherits": "x64-debug",
            "cacheVariables": {
                "CMAKE_C_FLAGS": "/winsdkdir /path",
                "CMAKE_CXX_FLAGS": "/winsdkdir /path"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "x64-debug-special",
            "configurePreset": "x64-debug-special"
        }
    ]
}

When I run cmake --debug-output --preset x64-debug-special, the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS aren’t the combined flags from base and x64-debug-special as I expect following from the documentation:

Cache variables are inherited through the inherits field, and the preset’s variables will be the union of its own cacheVariables and the cacheVariables from all its parents. If multiple presets in this union define the same variable, the standard rules of inherits are applied. Setting a variable to null causes it to not be set, even if a value was inherited from another preset.

$ cmake --debug-output --preset x64-debug-special
Running with debug output on.
Preset CMake variables:

  CMAKE_BUILD_TYPE="Debug"
  CMAKE_CXX_COMPILER="clang-cl"
  CMAKE_CXX_FLAGS="/winsdkdir /path"
  CMAKE_C_COMPILER="clang-cl"
  CMAKE_C_FLAGS="/winsdkdir /path"

If I remove cacheVariables from the x64-debug-special preset, I can see that the x64-debug config preset’s cacheVariables are inherited.

Cc: @kyle.edwards

The part of the docs you quoted does not mean that values for the same cache variable are merged, only that the set of cache variables will be merged. If a preset sets a cache variable that one of the presets it inherits from also sets, the value from the inherited preset is not used.

To make it more concrete, CMAKE_C_FLAGS and CMAKE_CXX_FLAGS from x64-debug-special will prevent the ones from base being used. You do not get both concatenated. The “merging” in this context is that you do get CMAKE_C_COMPILER and CMAKE_CXX_COMPILER from base, along with CMAKE_C_FLAGS and CMAKE_CXX_FLAGS from x64-debug-special, and CMAKE_BUILD_TYPE from x64-debug.

1 Like