CMake: How do I get the project's directory from the toolchain file?

Viewed 2145

I have a project which is structured like this

project:
-- CMakeLists.txt
-- toolchain.cmake
-- source
-- includes
-- tools
   -- arm-toolchain
      -- arm-gnueabihf-gcc
      -- ...
-- libraries
   -- lib1
      -- ...
      -- CMakeLists.txt
   -- lib2
      -- ...
      -- CMakeLists.txt

I would like to set the compiler for another architecture inside the toolchain.cmake like this:

...
set (CMAKE_C_COMPILER "${CMAKE_SOURCE_DIR}/tools/arm-toolchain/arm-gnueabihf-gcc")
...

Unfortunately the variable ${CMAKE_SOURCE_DIR} does change while compiling the project, at first it is:

CMAKE_SOURCE_DIR=/home/username/project

That's exactly what I expected. Unfortunately, it changes then to

CMAKE_SOURCE_DIR=/home/username/project/build/CMakeFiles/CMakeTmp

Therefore I cannot use ${CMAKE_SOURCE_DIR} as the root of my project-folder, is there another variable which does not change? Or, can I store the first value somehow? The problem is that the toolchain is invoked more than once, therefore a variable in which I store the initial value of ${CMAKE_SOURCE_DIR} is overwritten on the second run.

1 Answers
Related