TExcelTAD
Home › Modern CMake (with CTest & CPack) › CMakePresets.json

CMakePresets.json

A real project's configure command grows long: generator, build type, toolchain file, a few options, a cache directory. Everyone on the team types a slightly different version, and CI has its own. Presets put those command lines into a JSON file in the repository, so cmake --preset release means the same thing on every machine, in every IDE (VS Code, CLion and Visual Studio read the file) and in CI.

There are two files:

File Committed? Purpose
CMakePresets.json yes the project's shared presets
CMakeUserPresets.json no (add it to .gitignore) a developer's personal presets; may inherit from the shared ones

Kinds of preset

Kind Command Describes
configurePresets cmake --preset <name> generator, binaryDir, cache variables, toolchain, environment
buildPresets cmake --build --preset <name> which configure preset, targets, configuration, jobs
testPresets ctest --preset <name> output options, filters, parallelism
packagePresets cpack --preset <name> generators, configurations
workflowPresets cmake --workflow --preset <name> a sequence: configure, then build, test and package steps

A complete example

bash · try it yourself
mkdir -p calc && cd calc
cat > CMakePresets.json <<'JSON'
{
  "version": 6,
  "cmakeMinimumRequired": { "major": 3, "minor": 25, "patch": 0 },
  "configurePresets": [
    {
      "name": "base",
      "hidden": true,
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build/${presetName}",
      "cacheVariables": { "CALC_WARNINGS_AS_ERRORS": "ON" }
    },
    {
      "name": "debug",
      "displayName": "Debug (GCC)",
      "inherits": "base",
      "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" }
    },
    {
      "name": "release",
      "displayName": "Release (GCC)",
      "inherits": "base",
      "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" }
    }
  ],
  "buildPresets": [
    { "name": "debug", "configurePreset": "debug" },
    { "name": "release", "configurePreset": "release" }
  ],
  "testPresets": [
    {
      "name": "release",
      "configurePreset": "release",
      "output": { "outputOnFailure": true },
      "execution": { "noTestsAction": "error" }
    }
  ],
  "workflowPresets": [
    {
      "name": "release",
      "steps": [
        { "type": "configure", "name": "release" },
        { "type": "build", "name": "release" },
        { "type": "test", "name": "release" }
      ]
    }
  ]
}
JSON
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Calc LANGUAGES CXX)
option(CALC_WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
add_executable(calc main.cpp)
target_compile_options(calc PRIVATE -Wall 
lt;
lt;BOOL:${CALC_WARNINGS_AS_ERRORS}>:-Werror>) enable_testing() add_test(NAME calc_runs COMMAND calc) message("build type ${CMAKE_BUILD_TYPE}, warnings as errors ${CALC_WARNINGS_AS_ERRORS}") CM cat > main.cpp <<'SRC' #include <cstdio> int main() { std::puts("calc ok"); } SRC cmake --list-presets echo "=== the workflow" cmake --workflow --preset release echo "=== debug, step by step" cmake --preset debug --log-level=NOTICE cmake --build --preset debug ls build
Available configure presets:

  "debug"   - Debug (GCC)
  "release" - Release (GCC)
=== the workflow
Executing workflow step 1 of 3: configure preset "release"

-- The CXX compiler identification is GNU 13.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
build type Release, warnings as errors ON
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/calc/build/release

Executing workflow step 2 of 3: build preset "release"

[1/2] Building CXX object CMakeFiles/calc.dir/main.cpp.o
[2/2] Linking CXX executable calc

Executing workflow step 3 of 3: test preset "release"

Test project /home/learner/calc/build/release
    Start 1: calc_runs
1/1 Test #1: calc_runs ........................   Passed    0.00 sec

100% tests passed out of 1

Total Test time (real) =   0.00 sec
=== debug, step by step
build type Debug, warnings as errors ON
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/calc/build/debug
[1/2] Building CXX object CMakeFiles/calc.dir/main.cpp.o
[2/2] Linking CXX executable calc
debug
release

What the workflow did, in one command: configured with the release configure preset (into build/release, from binaryDir), built with the release build preset, and ran the tests with the release test preset. That one line is what a CI job should run: the CI configuration then contains no CMake flags at all, and developers can reproduce CI locally with the same command.

Things worth knowing

  • version selects the schema: 6 (CMake 3.25) added package and workflow presets; 10 (CMake 3.31) is the newest widely available. Use the lowest version that has what you need.
  • Macros in strings: ${sourceDir}, ${presetName}, ${hostSystemName}, $env{HOME}, $penv{PATH} (the parent environment).
  • inherits can take a list; later entries are overridden by earlier ones, and the preset's own fields override everything inherited. Hidden presets ("hidden": true) exist only to be inherited.
  • condition enables a preset only on some hosts, e.g. {"type": "equals", "lhs": "${hostSystemName}", "rhs": "Windows"}.
  • toolchainFile (version 3+) sets CMAKE_TOOLCHAIN_FILE, which is how presets select a toolchain or vcpkg.
  • Command-line -D options still work on top of a preset (cmake --preset debug -DCALC_WARNINGS_AS_ERRORS=OFF).
  • Multi-config generators: build presets select the configuration with "configuration": "Release".

A user preset

A developer who wants Clang locally adds an untracked CMakeUserPresets.json:

json
{
  "version": 6,
  "configurePresets": [
    {
      "name": "my-clang",
      "inherits": "debug",
      "environment": { "CC": "clang", "CXX": "clang++" },
      "cacheVariables": { "CMAKE_EXPORT_COMPILE_COMMANDS": "ON" }
    }
  ]
}

User presets can see and inherit the project's presets, but not the other way around.

Test yourself

Which file should NOT be committed to version control?

  1. CMakePresets.json
  2. CMakeUserPresets.json
  3. CMakeLists.txt
  4. cmake/*.cmake

What does cmake --workflow --preset release do?

  1. Only configures
  2. Runs the workflow's steps in order: e.g. configure, build, test (and package)
  3. Uploads to CDash
  4. Creates a release on GitHub

What is a hidden preset for?

  1. Secret credentials
  2. A base that other presets inherit from; it can't be used directly
  3. Presets only shown in CLion
  4. Disabled presets