# CMake Preset Best Practices for Build Types

**URL:** https://discourse.cmake.org/t/cmake-preset-best-practices-for-build-types/6554
**Category:** Usage
**Tags:** os:linux
**Created:** [September 22, 2022, 6:51pm UTC](https://discourse.cmake.org/t/cmake-preset-best-practices-for-build-types/6554 "2022-09-22T18:51:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)
#### Post date: [September 22, 2022, 6:51pm UTC](https://discourse.cmake.org/t/cmake-preset-best-practices-for-build-types/6554/1 "2022-09-22T18:51:23Z")

</div>

I am trying to convert my project from using Visual Studio Code Extension specific `cmake-kits.json` to leveraging the CMake supported, CMakePresets. My project builds for a couple different toolchains, but shares some common cache options. A slightly stripped down version looks like this…

```auto
{
    "version": 3,
    "cmakeMinimumRequired": {
        "major": 3,
        "minor": 21,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "default",
            "description": "Default configuration",
            "hidden": true,
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/build/${presetName}",
            "cacheVariables": {
                "CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
                "OTHER_COMMON_OPTION": "ON"
            }
        },
        {
            "name": "linux",
            "inherits": "default",
            "toolchainFile": "${sourceDir}/tools/cmake/toolchains/linux.cmake"
        },
        {
            "name": "qnx",
            "inherits": "default",
            "toolchainFile": "${sourceDir}/tools/cmake/toolchains/qnx.cmake"
        }
    ]
}

```

For the most part, this works great. The only challenge or contrast I have to my previous configuration with the VS Code CMake Extension is the build directory structure. Previously this would’ve looked like this…

```auto
build
  - linux
    - Debug
    - Release
  - qnx
    - Debug
    - Release

```

With unique build directories for each platform _and_ build type. This is beneficial on my project where it’s common to switch between debug/release builds. In this situation, sharing the same build directory for different platforms or build types would require re-builds more often. In VS code, I could define a setting like `"cmake.buildDirectory": "${workspaceFolder}/build/${buildKit}/${buildType}"`. However, with CMakePresets `binaryDir` option, I’m not sure how to accomplish this. It’d be nice to be able to have an option in the base preset like `"binaryDir": "${sourceDir}/build/${presetName}/${buildType}"`, but I don’t think such an option exists. I understand part of the challenge is that build type is just a cache variable. Effectively any macro Presets like ${cache:CMAKE\_BUILD\_TYPE} might be difficult (if not impossible) to implement.

Is my only option to make different presets for each build type (linux-debug, linux-release, qnx-debug, qnx-release, qnx-relwithdeb, etc.)? I’m curious to hear how others have approached this challenge? How do others specify build type in their CMakePresets?

---

<div class="post-metadata">

### Author: ![kisielk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kisielk/32/2765_2.png) [@kisielk](https://discourse.cmake.org/u/kisielk)
#### Post date: [September 23, 2022, 8:11pm UTC](https://discourse.cmake.org/t/cmake-preset-best-practices-for-build-types/6554/2 "2022-09-23T20:11:11Z")

</div>

I use the “Ninja Multi-Config” generator so this is not an issue since all the build types come from the same binary directory.
