# How to force that CMAKE\_TOOLCHAIN\_FILE is an absolute file path?

**URL:** https://discourse.cmake.org/t/how-to-force-that-cmake-toolchain-file-is-an-absolute-file-path/15670
**Category:** Code
**Created:** [May 27, 2026, 9:19am UTC](https://discourse.cmake.org/t/how-to-force-that-cmake-toolchain-file-is-an-absolute-file-path/15670 "2026-05-27T09:19:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [May 27, 2026, 9:19am UTC](https://discourse.cmake.org/t/how-to-force-that-cmake-toolchain-file-is-an-absolute-file-path/15670/1 "2026-05-27T09:19:58Z")

</div>

I use this code to check if the installed cmake config package is usable:

```bash
if(BEMAN_SCOPE_INSTALL_CONFIG_FILE_PACKAGE)
    # test if the targets are usable from the install directory
    add_test(
        NAME install-to-stagedir
        COMMAND
            ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR} --prefix
            ${CMAKE_BINARY_DIR}/stagedir --config $<CONFIG>
    )
    add_test(
        NAME find-package-test
        COMMAND
            ${CMAKE_CTEST_COMMAND} # --verbose
            --output-on-failure -C $<CONFIG> --build-and-test
            "${CMAKE_SOURCE_DIR}/examples"
            "${CMAKE_CURRENT_BINARY_DIR}/find-package-test" --build-generator
            ${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM}
            --build-options #
            "-D BEMAN_SCOPE_IMPORT_STD=${BEMAN_SCOPE_IMPORT_STD}"
            "-D BEMAN_SCOPE_USE_DANIELA_ADVICE=${BEMAN_SCOPE_USE_DANIELA_ADVICE}"
            "-D BEMAN_USE_STD_MODULE=${BEMAN_USE_STD_MODULE}"
            "-D CMAKE_BUILD_TYPE=$<CONFIG>"
            "-D CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
            "-D CMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}"
            "-D CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
            "-D CMAKE_PREFIX_PATH=${CMAKE_BINARY_DIR}/stagedir"
            "-D CMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}"
    )
endif()

```

but sometimes the `CMAKE_TOOLCHAIN_FILE` is not an absolute file path?

---

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [May 27, 2026, 1:56pm UTC](https://discourse.cmake.org/t/how-to-force-that-cmake-toolchain-file-is-an-absolute-file-path/15670/2 "2026-05-27T13:56:49Z")

</div>

For CMake ≥ 3.21:

See [https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-toolchain](https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-toolchain)

and use the command [cmake\_path](https://cmake.org/cmake/help/latest/command/cmake_path.html) (if needed):

```cmake
cmake_path(
    ABSOLUTE_PATH <relative_path>
    BASE_DIRECTORY <assumed_prefix>
    OUTPUT_VARIABLE <result_absolute_path>)

```

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [May 27, 2026, 5:05pm UTC](https://discourse.cmake.org/t/how-to-force-that-cmake-toolchain-file-is-an-absolute-file-path/15670/3 "2026-05-27T17:05:56Z")

</div>

On **CI** it was a relative path, no idea why?

I am using `CMake v4.3.3` both on **CI** and local.

But this changes helps:

```diff
diff --git a/CMakePresets.json b/CMakePresets.json
index b8ca3cc..085ddc9 100644
--- a/CMakePresets.json
+++ b/CMakePresets.json
@@ -16,7 +16,7 @@
         "CMAKE_EXPORT_COMPILE_COMMANDS": true,
         "CMAKE_INSTALL_MESSAGE": "LAZY",
         "CMAKE_SKIP_TEST_ALL_DEPENDENCY": false,
- "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "infra/cmake/use-fetch-content.cmake"
+ "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "${sourceDir}/infra/cmake/use-fetch-content.cmake"
       }
     },
     {
@@ -53,7 +53,7 @@
         "_debug-base"
       ],
       "cacheVariables": {
- "CMAKE_TOOLCHAIN_FILE": "infra/cmake/gnu-toolchain.cmake"
+ "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/infra/cmake/gnu-toolchain.cmake"
       }
     },
     {

```

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 27, 2026, 7:50pm UTC](https://discourse.cmake.org/t/how-to-force-that-cmake-toolchain-file-is-an-absolute-file-path/15670/4 "2026-05-27T19:50:38Z")

</div>

Note that a relative `CMAKE_TOOLCHAIN_FILE` is valid and sometimes useful. In fact, I have that exact situation with a number of consulting clients. A relative path is first checked against the build directory. If not found there, CMake then checks against the source directory (or it might be the current working directory, but I think it’s the source directory).
