# cannot use CMAKE\_SOURCE\_DIR in toolchain file

**URL:** https://discourse.cmake.org/t/cannot-use-cmake-source-dir-in-toolchain-file/546
**Category:** Code
**Created:** [January 28, 2020, 11:21am UTC](https://discourse.cmake.org/t/cannot-use-cmake-source-dir-in-toolchain-file/546 "2020-01-28T11:21:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Tokra](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/b487fb/32.png) [@Tokra](https://discourse.cmake.org/u/Tokra)
#### Post date: [January 28, 2020, 11:21am UTC](https://discourse.cmake.org/t/cannot-use-cmake-source-dir-in-toolchain-file/546/1 "2020-01-28T11:21:04Z")

</div>

When trying to include something from CMAKE\_SOURCE\_DIR in a toolchain-file, cmake seems to make a flat copy of the file for compiler-tests, then /CMakeFiles/CMakeTmp/CMakeLists.txt:3 cannot find the include-file in /CMakeFiles/CMakeTmp/

Example: we are using conan and our compiler is provided as a conan-package. Conan will generate a conanbuildinfo.cmake which will provide path-variables for all tools. toolchain-file:

````
```
include(${CMAKE_SOURCE_DIR}/conanbuildinfo.cmake)
set(CMAKE_C_COMPILER ${CONAN_BIN_DIRS_CLANG}/clang-cl.exe)
set(CMAKE_CXX_COMPILER ${CONAN_BIN_DIRS_CLANG}/clang-cl.exe)
set(CMAKE_LINKER ${CONAN_BIN_DIRS_CLANG}/lld-link.exe)
```

````

output:

````
```
-- The C compiler identification is Clang 9.0.0 with MSVC-like command-line
-- The CXX compiler identification is Clang 9.0.0 with MSVC-like command-line
-- Check for working C compiler: user/.conan/data/clang/9.0.0/tools/stable/package/9d74748ca8155c54bee5ded2267de5f3e409cf81/bin/clang-cl.exe
CMake Error at C:/src/Example/tool-clang-windows-x86.cmake:8 (include):
  include could not find load file:

    C:/src/Example/CMakeFiles/CMakeTmp/conanbuildinfo.cmake
Call Stack (most recent call first):
  C:/src/Example/CMakeFiles/3.15.4/CMakeSystem.cmake:6 (include)
  C:/src/Example/CMakeFiles/CMakeTmp/CMakeLists.txt:3 (project)
```
````

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [January 28, 2020, 1:13pm UTC](https://discourse.cmake.org/t/cannot-use-cmake-source-dir-in-toolchain-file/546/2 "2020-01-28T13:13:28Z")

</div>

The toolchain file is `C:/src/Example/tool-clang-windows-x86.cmake`, but where is `conanbuildinfo.cmake` exactly? If it is a sibling of the toolchain file, then I would use

```cmake
include("${CMAKE_CURRENT_LIST_DIR}/conanbuildinfo.cmake")

```

---

<div class="post-metadata">

### Author: ![eriols](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eriols/32/945_2.png) [@eriols](https://discourse.cmake.org/u/eriols)
#### Post date: [November 1, 2020, 8:11pm UTC](https://discourse.cmake.org/t/cannot-use-cmake-source-dir-in-toolchain-file/546/3 "2020-11-01T20:11:34Z")

</div>

Sorry for reviving an old topic, but I think it makes sense.

I have the same issue, only in my case the included file is not a sibling but a few directories above - meaning I can’t use `CMAKE_CURRENT_LIST_DIR` as starting point. I’d really want to specify it from `CMAKE_SOURCE_DIR` instead.  
Is this possible? If not, why not?

Edit: I can do something like `${CMAKE_CURRENT_LIST_DIR}../../../myfile.cmake` so I guess the question “why” I can’t base this off off `CMAKE_SOURCE_DIR` still stands?

---

<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: [November 1, 2020, 9:35pm UTC](https://discourse.cmake.org/t/cannot-use-cmake-source-dir-in-toolchain-file/546/4 "2020-11-01T21:35:02Z")

</div>

Toolchain files are used not just for the main project, but also for `try_compile()` calls which create their own separate mini-projects to implement the compile test. For these separate mini-projects, the `CMAKE_SOURCE_DIR` would be a different directory to the value reported by the main project’s `CMAKE_SOURCE_DIR`. You should consider the only reliable variables to be those that are relative to the location of the toolchain file itself (e.g. `CMAKE_CURRENT_LIST_DIR`).

---

<div class="post-metadata">

### Author: ![natersoz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/natersoz/32/3017_2.png) [@natersoz](https://discourse.cmake.org/u/natersoz)
#### Post date: [December 31, 2022, 6:48pm UTC](https://discourse.cmake.org/t/cannot-use-cmake-source-dir-in-toolchain-file/546/5 "2022-12-31T18:48:58Z")

</div>

> [@craig.scott](#):
>
> `CMAKE_CURRENT_LIST_DIR`

Thank you. Very helpful.  
Is there a link about CMake variables and their initialization readiness?

---

<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: [January 1, 2023, 1:02am UTC](https://discourse.cmake.org/t/cannot-use-cmake-source-dir-in-toolchain-file/546/6 "2023-01-01T01:02:07Z")

</div>

> [@natersoz](#):
>
> Is there a link about CMake variables and their initialization readiness?

That’s a pretty specific topic. The problem isn’t to do with initialization though, it’s about the context in which code executes. People don’t tend to think about (perhaps aren’t aware of) the different contexts in which a toolchain file might be used. The best way to shield yourself from that is to make your toolchain files completely independent of the project they are used with. If your toolchain file is tied to the project, that’s a bit of a warning that something probably isn’t quite right.
