Presets - conditional config

Is there a way for a single preset to apply different settings based on a condition?

For example, I would like cmake --preset default to choose the appropriate vcpkg triplet for the current OS.

"configurePresets": [
    {
      "name": "windows",
      "hidden": true,
      "cacheVariables": {
        "VCPKG_TARGET_TRIPLET": "x64-windows"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      }
    },
    {
      "name": "macos",
      "hidden": true,
      "cacheVariables": {
        "VCPKG_TARGET_TRIPLET": "x64-macos"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Darwin"
      }
    },
    {
      "name": "default",
      "binaryDir": "${sourceDir}/build/default",
      "generator": "Ninja",
      "inherits": [
        "windows",
        "macos"
      ]
    }
]

This does not work as ‘default’ winds up with conflicting condition objects and just picks the first (I think)

 {
      "name": "default",
      "binaryDir": "${sourceDir}/build/default",
      "generator": "Ninja",
      "cacheVariables": {
        "VCPKG_TARGET_TRIPLET": "x64-windows"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      }
    },
    {
      "name": "default",
      "binaryDir": "${sourceDir}/build/default",
      "generator": "Ninja",
      "cacheVariables": {
        "VCPKG_TARGET_TRIPLET": "x64-macos"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Darwin"
      }
    }

This does not work as you may not have two presets with the same name, even if one is disabled.

Is this something that is currently possible, or planned for the future?

2 Likes

Cc: @kyle.edwards

I have also noticed this limitation, and it would be nice to be able to have a conditional preset of the same name.

Ah. I was about to make a topic but the search found this. Indeed, I have need for something like this myself. I would like to set a cache variable differently on different OSs (say, ON for Linux, but OFF for Darwin`).

This is neither an answer nor a question, but a report of way to achieve a similar goal in a different way. It also failed, but perhaps it helps to find the right way.

Tried to inherit from multiple platform specific presets, which are enabled/disabled according to the platform. According to the 3.21 documentation a condition can have a value of “null”, so that (as I understood, but I might be wrong) the according preset is enabled (i.e. does not cause a inherited presets to fail), but the inheriting presets do not inherit from the preset (cache variables/environment/…). But this also failed, cause if the condition is “false” the inherited configurations can not be used.

What I would need, is a object, so that the value of the condition field becomes null (not false).

"configurePresets": [

        {
            "name": "windows-only",
            "hidden": true,
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Windows"
            },
            "environment": {
                "PLATFORM": "WINDOWS64",
            }
        },
        {
            "name": "linux-only",
            "hidden": true,
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Windows"
            },
            "environment": {
                "PLATFORM": "LINUX64",
            }
        },
        {
            "name": "default",
            "inherits": [
                "linux-only",
                "windows-only"
            ],
...

In the big picture, I tried to have the platform to be a property (different build/install folders, environments, cache settings, …) but not, that I need to double/tripple/… (for 2/3/… platforms) each preset (for e.g. Debug/Release/NightlyBuild/DeploymentBuild/CI/…