# Add CMakePresets.json to a CMake Project

Yes. An agent writes a CMakePresets.json that captures your project's configure, build and test setups and moves the CI matrix onto cmake --preset, in a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-cmake-presets/cmake/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the project is configured today, in CMake and in CI. In a header-only library like jarro2783/cxxopts, `CMakeLists.txt` declares `cmake_minimum_required(VERSION 3.5...3.19)`, reads the version from the `CXXOPTS__VERSION_*` defines in `include/cxxopts.hpp` through `cmake/cxxopts.cmake`, and exposes five options and one variable: `CXXOPTS_BUILD_EXAMPLES`, `CXXOPTS_BUILD_TESTS`, `CXXOPTS_ENABLE_INSTALL` and `CXXOPTS_ENABLE_WARNINGS` default to ON only when cxxopts is the top-level project, `CXXOPTS_USE_UNICODE_HELP` defaults to OFF and needs ICU through pkg-config, and the `CXXOPTS_CXX_STANDARD` variable, read in `cmake/cxxopts.cmake`, overrides the default of C++11 with extensions off. The warnings macro adds `-Wall -Werror -Wextra -Wshadow -Weffc++` and more on GCC and Clang, so a configuration with warnings on is a stricter build than one without.

`test/CMakeLists.txt` is where the configurations multiply: `options_test` and `options_test_noregex`, two more `noexceptions` variants built with `-fno-exceptions` only on Clang and GCC, UBSan flags on the parser tests on Clang and GCC off Windows, a `find-package-test`, an `add-subdirectory-test` and three `pkg-config-*-test` runs that each re-invoke `ctest --build-and-test` and pass `${CMAKE_GENERATOR}` and `${CMAKE_BUILD_TYPE}` through, plus a `fuzzer` target on Clang on Linux. `.github/workflows/cmake.yml` spells those configurations out by hand: an Ubuntu matrix over `ubuntu-22.04` and `ubuntu-24.04` with `g++-9`, `g++-10` and `clang++`, each configuring with `-DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=...`; a macOS job that adds `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`; and a Windows job that configures with `-T "ClangCl"` and then runs `./link_test && ./options_test` by hand instead of `ctest`. `build.yml` is a second, older workflow that does `mkdir build; cmake ..; make; ctest`. `.travis.yml` and `.tipi/` are leftovers from earlier CI, and `meson.build` with `meson.yml` is a separate build that presets do not touch.

From that the agent writes `CMakePresets.json` at `version` 3, which needs CMake 3.21 and is satisfied by every runner in the matrix, with a hidden `base` configure preset (`binaryDir` of `${sourceDir}/build/${presetName}`, `CXXOPTS_BUILD_TESTS` ON, `CMAKE_EXPORT_COMPILE_COMMANDS` ON) and concrete presets that inherit it: `default` and `debug` for the two build types, `gcc-9`, `gcc-10` and `clang` that set `CMAKE_CXX_COMPILER`, `unicode` that turns on `CXXOPTS_USE_UNICODE_HELP`, `cxx17` that sets `CXXOPTS_CXX_STANDARD` to 17 to match what `src/CMakeLists.txt` already requires of the example, and `windows-clangcl` with the Visual Studio generator, `toolset` `ClangCl` and a `condition` on `hostSystemName`. Each configure preset gets a build preset and a test preset, the test presets with `outputOnFailure` and `noTestsAction` set to `error`, so a preset that accidentally builds no tests fails instead of passing on zero. `CMakeUserPresets.json` goes into `.gitignore` beside `build/` and `build-*/`.

Then it moves CI onto the presets: the matrix in `cmake.yml` becomes a list of preset names, and each job's three steps become `cmake --preset`, `cmake --build --preset` and `ctest --preset`, which puts the Windows job on `ctest` like the others instead of running two binaries by hand. `build.yml` is reduced to the `default` preset so it stops duplicating the matrix with a different generator. The agent checks that the generator each preset names is on the runner image it runs on, and before opening anything it runs `cmake --preset default`, builds, and runs `ctest --preset default` in its sandbox, then the `clang` and `cxx17` presets, reading the test list to confirm the `--build-and-test` sub-tests still receive the generator and build type they expect. `INSTALL` gets a short presets section next to its existing `-D CXXOPTS_BUILD_TESTS=On` instructions, and the README a pointer to it.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add CMake presets to this project. Read CMakeLists.txt, cmake/cxxopts.cmake, include/CMakeLists.txt, src/CMakeLists.txt, test/CMakeLists.txt, .github/workflows/cmake.yml, .github/workflows/build.yml, INSTALL, README.md and .gitignore first. Write CMakePresets.json at version 3 with a hidden base configure preset that sets binaryDir under build/, enables the tests and exports compile commands, and concrete presets for release and debug, each compiler the CI matrix uses, the unicode option, a C++17 standard, and a Windows ClangCl toolset preset guarded by a hostSystemName condition. Give every configure preset a build preset and a test preset with outputOnFailure and noTestsAction set to error. Add CMakeUserPresets.json to .gitignore. Rewrite the CI workflows to a matrix of preset names using cmake --preset, cmake --build --preset and ctest --preset, including the Windows job, and check that each preset's generator exists on its runner image. Configure, build and run ctest with at least the default, clang and C++17 presets in the sandbox before opening a pull request, document the presets in INSTALL and the README, and leave the Meson build and the Bazel files alone.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds `CMakePresets.json`, changes `.github/workflows/cmake.yml` and `.github/workflows/build.yml` to drive their jobs from preset names, adds one line to `.gitignore`, and adds a presets section to `INSTALL` and the README. `CMakeLists.txt`, `cmake/cxxopts.cmake` and the test CMake files are unchanged: the presets only name values those files already accept, so no option, target or test is added or renamed.

The description lists each preset with the options it sets and which CI job it replaces, quotes the `ctest --preset` summaries from the sandbox for the presets it ran, and names the generator each preset uses on each runner. The configure and build commands with their output are in the session transcript at `GET /o/{org}/sessions/{id}/messages`.

Before merging, a reviewer compares the preset list with the old matrix to confirm nothing dropped out, checks the Windows preset against the `-T ClangCl` configure it replaces, and watches the pull request's own checks, which now run through the presets, go green on all three operating systems. A follow-up message to the same session can add a `version` 6 workflow preset that chains configure, build and test in one `cmake --workflow` call once the oldest runner's CMake allows it.

## Run it on every terminal hand-off

A maintainer who keeps typing the same `-DCMAKE_CXX_COMPILER=` and `-DCXXOPTS_BUILD_TESTS=On` flags starts this from the terminal with this body and goes back to the parser bug. The run reads the real options and the real CI matrix, so the presets match what the project builds today rather than a generic template.

Send a `request_id`, a UUID, with the body so a retried start returns the run it already began. When the run is done, its `GET /o/{org}/sessions/{id}` answer names the pull request.

## Why run it on Rush?

A presets file is easy to get subtly wrong: a preset that forgets the tests option passes with zero tests, a toolset that only exists on Windows breaks the macOS job, a generator the runner lacks fails at configure. The agent configures, builds and runs ctest for each preset it can in a sandbox with GCC and Clang installed, reads the test count and the sub-test output, and only then rewrites the workflows.

The session keeps every `cmake --preset` and `ctest --preset` invocation with its output, so a reviewer can see the configurations were exercised rather than assumed. A team with several CMake repositories can send this request for each at once and get one pull request per project, each describing that project's own options and CI matrix.

## Frequently asked questions

### Does adding presets raise the minimum CMake version of the project?

No. `cmake_minimum_required(VERSION 3.5...3.19)` stays as it is, and a consumer that uses `add_subdirectory` or `find_package` never reads the presets file. Only someone who runs `cmake --preset` needs CMake 3.21 or later, and the CI runners already have it.

### Why is the Windows job moved to ctest?

Today it runs `./link_test && ./options_test` from `build/test/Release` by hand, which skips the `noregex` test and every `--build-and-test` sub-test. With a test preset the Windows job runs the same `ctest` the other platforms run. If the extra tests fail on Windows, that is a real finding and the pull request reports it rather than hiding it.

### What about Meson and Bazel?

They are left alone. `meson.build`, `meson_options.txt` and `meson.yml` are a separate build with its own CI, and `BUILD.bazel` and `WORKSPACE` are untouched. CMake presets describe only the CMake build.

### Can I add my own presets without committing them?

Yes. `CMakeUserPresets.json` is read beside `CMakePresets.json` and can inherit any preset in it, which is why the agent adds it to `.gitignore`. A developer can keep a preset that points at a local ICU or a sanitizer build there.

## More CMake jobs

- [Add CodeQL Scanning to a CMake C++ Project](https://getrush.ai/run/add-codeql-scanning/cmake/)
- [Add Conan 2 Packaging to a CMake C++ Library](https://getrush.ai/run/add-conan-package/cmake/)
- [Add Test Coverage Reporting to a CMake Project's CI](https://getrush.ai/run/add-coverage-to-ci/cmake/)

## More jobs like this: Hand off a long task

- [Add a Custom Code Block Renderer to an Obsidian Plugin](https://getrush.ai/run/add-code-block-processor/obsidian-plugin/)
- [Add Route-Based Code Splitting to a React App](https://getrush.ai/run/add-code-splitting/react/)
- [Check sqlc Generated Code in CI With sqlc diff and sqlc vet](https://getrush.ai/run/add-codegen-check-to-ci/sqlc/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
