FR - presets: specify default preset based on OS

In my CI scripts, the steps are typically the same on all OSes. Usually the only difference is the CMake preset being used, so I typically end up with something like this (assuming that the configure, build, test, and package presets all have the same name):

- name: configure
  run: cmake --preset $CMAKE_PRESET
- name: build
  run: cmake --build --preset $CMAKE_PRESET
- name: test
  run: ctest --preset $CMAKE_PRESET
- name: package
  run: cpack --preset $CMAKE_PRESET

I would love a way to encode in the CMakePresets.json some logic for selecting what CMake preset to use. I have several ideas for how this could work:

  1. A simple map with a list of OS names matched to preset names, ie:
{
  "configurePresets": [ 
    { "name": "msvc", ... },
    { "name": "xcode", ... },
    { "name": "gcc", ... }
  ],
  "defaultPresets": {
    "configure": {
      "Windows": "msvc",
      "Darwin": "xcode",
      "Linux": "gcc"
    }
  }
}
  1. A list of preset names paired with conditions, and the first condition that is satisfied will be selected. The last item in the list would not have a condition object, and would be considered the fallback default - ie:
{
  "defaultPresets": {
    "configure": [
      {
        "name": "msvc",
        "condition": {
          "lhs": "${hostSystemName}",
          "rhs": "Windows",
          "type": "equals"
        }
      },
      {
        "name": "xcode",
        "condition": {
          "lhs": "Darwin",
          "rhs": "${hostSystemName}",
          "type": "equals"
        }
      },
      { "name": "gcc" }
    ]
  }
}

As for command line usage, I’m thinking either:

  1. call cmake with the special token default as the preset name
  2. call cmake --preset with no name specified

It would also be great if cmake --list-presets could indicate which preset is the default for your current platform.

I wanted to post this here before making an official FR on the issue tracker, to see what the community feedback is.

1 Like

There are elements of this that sound familiar from comments in the CMake gitlab issue tracker. You might want to go searching in there for where defaults for presets have already been discussed, I think in some detail. Not saying it shouldn’t be revisited, only that there’s prior material on this.

1 Like