# Presets - conditional config

**URL:** <https://discourse.cmake.org/t/presets-conditional-config/3864>\
**Category:** Usage\
**Created:** [August 2, 2021, 3:27pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864 "2021-08-02T15:27:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbailey](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rbailey](https://discourse.cmake.org/u/rbailey)\
**Post date:** [August 2, 2021, 3:27pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/1 "2021-08-02T15:27:59Z")

</div>

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.

```auto
"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)

```auto
 {
      "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?

---

<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:** [August 2, 2021, 4:48pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/2 "2021-08-02T16:48:17Z")

</div>

Cc: @kyle.edwards

---

<div class="post-metadata">

**Author:** ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)\
**Post date:** [August 3, 2021, 1:37pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/3 "2021-08-03T13:37:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [August 11, 2021, 1:16pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/4 "2021-08-11T13:16:29Z")

</div>

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`).

---

<div class="post-metadata">

**Author:** ![trmagma](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/74df32/32.png) [@trmagma](https://discourse.cmake.org/u/trmagma)\
**Post date:** [August 17, 2021, 10:09am UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/5 "2021-08-17T10:09:32Z")

</div>

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

```auto
"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/…

---

<div class="post-metadata">

**Author:** ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)\
**Post date:** [July 5, 2022, 1:00pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/6 "2022-07-05T13:00:06Z")

</div>

Any news on this topics ? I would like to set a cache variable differently on different OSs

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [February 1, 2024, 3:10pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/7 "2024-02-01T15:10:28Z")

</div>

IMHO: It would be time to get a response ?

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [February 1, 2024, 8:50pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/8 "2024-02-01T20:50:45Z")

</div>

Zombie thread reactivate! I’m hoping gurus like @scivision or @ben.boeckel might have a fix for me as I try to do this.

Namely, I want to build with presets differently on one machine versus others (due to how disks are set up, etc.). I have:

```json
  "configurePresets": [
    {
      "name": "base-configure-nccs",
      "hidden": true,
      "displayName": "Base Configure Settings",
      "description": "Sets build and install directories",
      "binaryDir": "$penv{CMAKE_BUILD_LOCATIONS}/${sourceDirName}/build-${presetName}",
      "installDir": "$penv{CMAKE_INSTALL_LOCATIONS}/${sourceDirName}/install-${presetName}",
      "condition": { "lhs": "$penv{CMAKE_SITE_NAME}", "type": "equals", "rhs": "NCCS" }
    },
    {
      "name": "base-configure-default",
      "hidden": true,
      "displayName": "Base Configure Settings",
      "description": "Sets build and install directories",
      "binaryDir": "${sourceDir}/build-${presetName}",
      "installDir": "${sourceDir}/install-${presetName}",
      "condition": { "lhs": "$penv{CMAKE_SITE_NAME}", "type": "notEquals", "rhs": "NCCS" } },
    {
      "name": "base-configure",
      "hidden": true,
      "inherits": [
         "base-configure-nccs",
         "base-configure-default"
         ],
      "displayName": "Base Configure Settings",
      "description": "Sets build and install directories"
    },

```

So if I have `CMAKE_SITE_NAME=NCCS` in my environment, we build and install to directories external to the `sourceDir`. But otherwise, I’ll just keep the build and install dirs in `sourceDir`.

Good news is that it works on the NCCS machine. But anywhere else:

```bash
❯ cmake --preset Debug
CMake Error: Could not use disabled preset "Debug"

```

Is there an obvious mistake I made here?

---

<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:** [February 4, 2024, 1:22pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/9 "2024-02-04T13:22:29Z")

</div>

Alas, my knowledge of presets is about the same as it was back then. The new issue you have filed is probably the most useful next step.

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [February 4, 2024, 3:14pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/10 "2024-02-04T15:14:40Z")

</div>

I did figure out…something for my case that seems to work, though I’m still testing. I’m trying:

```json
  "include": [
    "presets/CMake$penv{CMAKE_PRESET_NAME}Presets.json"
  ],

```

where I’m using an environment variable to control which file has my “base” configure class. Thanks to v7 this is doable. I’m still trying to figure out how little I need to include in my `include`.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 18, 2024, 2:39pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/11 "2024-06-18T14:39:24Z")

</div>

with cmake v3.30 this works now:

```json
{
    "version": 9,
    "include": [
        "CMake${hostSystemName}Presets.json"
    ],
    "buildPresets": [
        {
            "name": "default",
            "configurePreset": "default"
        }
    ],
    "testPresets": [
        {
            "name": "default",
            "configurePreset": "default",
            "output": {
                "outputOnFailure": true
            },
            "execution": {
                "noTestsAction": "error",
                "stopOnFailure": true
            }
        }
    ],
    "packagePresets": [
        {
            "name": "default",
            "configurePreset": "default",
            "generators": [
                "TGZ"
            ]
        }
    ],
    "workflowPresets": [
        {
            "name": "default",
            "steps": [
                {
                    "type": "configure",
                    "name": "default"
                },
                {
                    "type": "build",
                    "name": "default"
                },
                {
                    "type": "test",
                    "name": "default"
                },
                {
                    "type": "package",
                    "name": "default"
                }
            ]
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [June 18, 2024, 3:51pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/12 "2024-06-18T15:51:35Z")

</div>

Hmm. What was added in version 9 of presets? (I usually don’t look for the changes in presets until my current setup breaks. 🙂 )

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 18, 2024, 3:54pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/13 "2024-06-18T15:54:39Z")

</div>

- [`cmake-presets(7)`](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#manual:cmake-presets(7)) files now support schema version `9`. `include` fields now expand all macros except `$env{}`and preset-specific macros, i.e., those derived from the fields inside a preset’s definition.

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [June 18, 2024, 4:10pm UTC](https://discourse.cmake.org/t/presets-conditional-config/3864/14 "2024-06-18T16:10:57Z")

</div>

Ohhhhhh! You have:

```json
        "CMake${hostSystemName}Presets.json"

```

where there is no `$penv{}`!

I kept staring at your code trying to figure out what was different in there. 🙂
