---
name: scaffold-cmake-presets
description: Generates a CMakePresets.json file to standardize CMake configuration and build commands. Use whenever a project builds with CMake.
---

# CMake Presets Scaffolding Skill

This skill instructs the agent on how to scaffold a `CMakePresets.json` file to establish a standardized, hallucination-free build process.

## Requirements

When the user specifies CMake as their Build System, the agent MUST generate a `CMakePresets.json` file in the project root.

## Instructions for `CMakePresets.json`

The file MUST be a valid JSON matching the CMake Presets version 3 (or higher) specification.

### 1. Configure Presets

Define your configuration presets.

- **Hidden Base Preset**: Create a hidden preset (e.g., `default-base` with `"hidden": true`) that defines common properties:
  - Generator (default to `"Ninja"` unless requested otherwise).
  - Common cache variables (e.g., `"CMAKE_EXPORT_COMPILE_COMMANDS": "YES"`).
- **User-Facing Presets**: Create user-facing presets (e.g., `dev` and `release`) that inherit from the base preset.
  - Set `"binaryDir"` on each user-facing preset (not the hidden base). Use `"${sourceDir}/build/Debug"` / `"${sourceDir}/build/Release"` to stay consistent with the naming convention used by Conan-generated presets.
  - Set `"CMAKE_BUILD_TYPE"` appropriately (Debug for dev, Release for release).

### 2. Conan Integration (CRITICAL)

If the project uses **Conan 2.x** for dependency management:

- Do NOT add an `"include": ["CMakeUserPresets.json"]` entry to your own `CMakePresets.json`, and do NOT make your user-facing presets (e.g. `dev`) `inherits` a Conan-generated preset (e.g. `"conan-debug"`) by name. Both fail at configure time (`Cyclic include among preset files` / `Inherited preset "<name>" is unreachable from preset's file`).
- Instead, set `"toolchainFile"` directly on each user-facing preset, pointing at the toolchain file Conan generates:
  ```json
  {
    "name": "dev",
    "inherits": "default-base",
    "binaryDir": "${sourceDir}/build/Debug",
    "toolchainFile": "${sourceDir}/build/Debug/generators/conan_toolchain.cmake",
    "cacheVariables": {
      "CMAKE_BUILD_TYPE": "Debug"
    }
  }
  ```
- `CMakeUserPresets.json` is still generated by Conan as a side effect of `conan install` and should still be added to `.gitignore`.
- **Order of Operations**: Run `conan install .` (often with build profile arguments) **before** `cmake --preset dev` — `conan_toolchain.cmake` doesn't exist until that step runs.

### 3. vcpkg Integration

If the project uses **vcpkg** (and not Conan), explicitly define the `CMAKE_TOOLCHAIN_FILE` in the base preset's cache variables. E.g.:

- `"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"`

### 4. Multi-Package Manager Support (Conan AND vcpkg)

If a project supports _both_ package managers to give developers a choice, do NOT attempt to chain them together. Instead, generate separate, distinct user-facing presets for each:

- **`dev-conan`**: Inherits from the base preset and sets `"toolchainFile"` to the Conan-generated toolchain (see section 2):
  ```json
  {
    "name": "dev-conan",
    "inherits": "default-base",
    "binaryDir": "${sourceDir}/build/Debug",
    "toolchainFile": "${sourceDir}/build/Debug/generators/conan_toolchain.cmake",
    "cacheVariables": {
      "CMAKE_BUILD_TYPE": "Debug"
    }
  }
  ```
- **`dev-vcpkg`**: Inherits from the base preset but explicitly defines the vcpkg toolchain variable:
  ```json
  "cacheVariables": {
    "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
  }
  ```

### 5. Build and Test Presets

- Create `buildPresets` that correspond to your configure presets (e.g., a `dev` build preset that references the `dev` configure preset).
- If a testing framework is used, create `testPresets` that run CTest against the configured build directory.

## Build Skills Generation

If CMake Presets are scaffolded, update the project's build instructions to use the simple preset commands:

```bash
cmake --preset dev
cmake --build --preset dev
```

## Example `CMakePresets.json` Structure

```json
{
  "version": 3,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 23,
    "patch": 0
  },
  "configurePresets": [
    {
      "name": "default-base",
      "hidden": true,
      "generator": "Ninja",
      "cacheVariables": {
        "CMAKE_EXPORT_COMPILE_COMMANDS": "YES"
      }
    },
    {
      "name": "dev",
      "inherits": "default-base",
      "binaryDir": "${sourceDir}/build/Debug",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug"
      }
    },
    {
      "name": "release",
      "inherits": "default-base",
      "binaryDir": "${sourceDir}/build/Release",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Release"
      }
    }
  ],
  "buildPresets": [
    {
      "name": "dev",
      "configurePreset": "dev"
    },
    {
      "name": "release",
      "configurePreset": "release"
    }
  ]
}
```

**Note on `binaryDir` and Conan**: Conan installs dependencies into `build/Debug` / `build/Release` by convention. Set `binaryDir` explicitly to the same path on each user-facing preset, since these presets don't inherit from Conan's generated presets.
