# CMake presets: \`inherit\` makes it hard to include toolchain file

**URL:** https://discourse.cmake.org/t/cmake-presets-inherit-makes-it-hard-to-include-toolchain-file/7784
**Category:** Code
**Created:** [March 28, 2023, 6:43am UTC](https://discourse.cmake.org/t/cmake-presets-inherit-makes-it-hard-to-include-toolchain-file/7784 "2023-03-28T06:43:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![flodin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/flodin/32/3292_2.png) [@flodin](https://discourse.cmake.org/u/flodin)
#### Post date: [March 28, 2023, 6:43am UTC](https://discourse.cmake.org/t/cmake-presets-inherit-makes-it-hard-to-include-toolchain-file/7784/1 "2023-03-28T06:43:53Z")

</div>

Hi,

We have a CMake structure where there is a common platform which can be built by itself or included via different sub-components.

Since the platform is always used by the sub-components we thought it a good fit to specify a CMakePresets.json file in the platform and the sub-components would simply include it.

```auto
|- common/
| |- platform/
| | |- CMakeLists.txt
| | |- CMakePresets.json
|- sub-component1/
| |- CMakeLists.txt
| |- CMakePresets.json (includes the platforms CMakePreset.json)
|- sub-component2/
| | - CMakeLists.txt
| | - CMakePresets.json (includes the platforms CMakePreset.json)

```

The `configurePresets` in the platforms CMakePresets.json looks something like this:

```auto
 "configurePresets": [
    {
      "name": "linux",
      "description": "Configure options specific for Linux",
      "generator": "Ninja",
      "toolchainFile": "${fileDir}/cmake/toolchains/linux.cmake"
    },
    {
      "name": "linux-multi",
      "inherits": "linux",
      "description": "Multi-config linux setup",
      "generator": "Ninja Multi-Config"
    },
    {
      "name": "windows",
      "description": "Configure options specific for Windows",
      "generator": "Visual Studio 17 2022",
      "architecture": {
          "value": "x64"
        },
      "toolchainFile": "${fileDir}/cmake/toolchains/windows.cmake"
    }

```

This worked fine when the sub-components simply included the platforms `CMakePresets.json` from their own. However, when users started to specify their own presets and used `inherit` on the platforms presets the `toolchainFile` didn’t work any longer since the `${fileDir}` macro is no evaluated in the context of the platform, but the sub-components: “All macros are evaluated in the context of the preset being used, even if the macro is in a field that was inherited from another preset.” [https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#macro-expansion](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#macro-expansion)

Using the `CMAKE_TOOLCHAIN_FILE` is not an ideal solution for us (since it would require changing both the platform and all sub-components) since the sub-components typically include the platform after they’ve specified `project()` causing the toolchain file not to be included.

Given that we want to be able to build the platform by itself and via the sub-components, is it possible to use `inherit` in combination with the above `toolchainFile` inclusion?

---

<div class="post-metadata">

### Author: ![flodin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/flodin/32/3292_2.png) [@flodin](https://discourse.cmake.org/u/flodin)
#### Post date: [March 28, 2023, 12:23pm UTC](https://discourse.cmake.org/t/cmake-presets-inherit-makes-it-hard-to-include-toolchain-file/7784/2 "2023-03-28T12:23:54Z")

</div>

To clarify, adding the following CMakeUserPresets.json to `sub-component1`:

```cmake
{
    "version": 6,
    "configurePresets": [{
      "name": "default",
      "inherits": "linux-multi", // defined in platform
      "cacheVariables": {
        ...
      }
    }]
}

```

results in the following error:

```auto
 cmake --preset default .
Preset CMake variables:

  ...

  CMAKE_TOOLCHAIN_FILE:FILEPATH="/path/to/subcomponent1/cmake/toolchains/linux.cmake"

Using ccache /usr/bin/ccache
CMake Error at /usr/share/cmake-3.25/Modules/CMakeDetermineSystem.cmake:130 (message):
  Could not find toolchain file:
  /path/to/subcomponent1/cmake/toolchains/linux.cmake
Call Stack (most recent call first):
  CMakeLists.txt:42 (project)

CMake Error: CMake was unable to find a build program corresponding to "Ninja Multi-Config". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
-- Configuring incomplete, errors occurred!

```

---

<div class="post-metadata">

### Author: ![bjornskov](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/6bbea6/32.png) [@bjornskov](https://discourse.cmake.org/u/bjornskov)
#### Post date: [February 15, 2024, 10:40am UTC](https://discourse.cmake.org/t/cmake-presets-inherit-makes-it-hard-to-include-toolchain-file/7784/3 "2024-02-15T10:40:41Z")

</div>

Hi!  
I am facing the same issue. Did you find a workaround for this?  
For me it seems like a bug, as the fileDir should be the directory containing the preset, which should be this specific file. Otherwise there should be yet another macro defining this.

---

<div class="post-metadata">

### Author: ![flodin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/flodin/32/3292_2.png) [@flodin](https://discourse.cmake.org/u/flodin)
#### Post date: [February 16, 2024, 6:33am UTC](https://discourse.cmake.org/t/cmake-presets-inherit-makes-it-hard-to-include-toolchain-file/7784/4 "2024-02-16T06:33:43Z")

</div>

Unfortunately not. We ended up moving everything from the toolchain files into the CMakePreset.json of the platform component.

Please let me know if you find a solution 🙂

---

<div class="post-metadata">

### Author: ![thetic](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thetic/32/5632_2.png) [@thetic](https://discourse.cmake.org/u/thetic)
#### Post date: [June 21, 2025, 12:10am UTC](https://discourse.cmake.org/t/cmake-presets-inherit-makes-it-hard-to-include-toolchain-file/7784/5 "2025-06-21T00:10:49Z")

</div>

Has anyone found a workaround for this? It seems to prevent implementing any useful includes.

---

<div class="post-metadata">

### Author: ![Chardrazle](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chardrazle/32/4362_2.png) [@Chardrazle](https://discourse.cmake.org/u/Chardrazle)
#### Post date: [August 8, 2025, 11:16am UTC](https://discourse.cmake.org/t/cmake-presets-inherit-makes-it-hard-to-include-toolchain-file/7784/6 "2025-08-08T11:16:02Z")

</div>

We just ran into this issue. We “solved” it by using an environment variable _that is under control of the presets_.  
For example, in the “BasePresets.json”:

```auto
{
  "version": 8,
  "configurePresets": [
    {
      "name": "default",
      "hidden": true,
      "binaryDir": "$env{BASE_ROOT}/.build/${sourceDirName}",
      "toolchainFile": "$env{BASE_ROOT}/where_we_keep_common_stuff/toolchain.cmake",
      "environment": {
        "BASE_ROOT": "${fileDir}"
      }
    }
  ]
}

```

Then in the project-specific presets we specify “where” the root is from _this_ file by overriding the env-var.  
So if it is located one subdirectory away:

```auto
{
  "version": 8,
  "include": ["../BasePresets.json"],
  "configurePresets": [
    {
      "name": "default-inherited",
      "inherits": ["default"],
      "hidden": true,
      "environment": {
        "BASE_ROOT": "${fileDir}/.."
      }
    },
    {
      "name": "an-actual-preset-for-the-project",
      "inherits": ["default-inherited"]
    }
  ]
}

```
