[CMake] include a file from the toolchain file

Craig Scott craig.scott at crascit.com
Thu Nov 1 16:22:11 EDT 2018


On Fri, Nov 2, 2018 at 3:25 AM Federico Kircheis <
federico.kircheis at gmail.com> wrote:

> As described on https://github.com/Kitware/CMake/ I'm writing here to
> check if the current behavior of cmake is really a bug.
>
> I'm having following issues with toolchains files.
>
> Here is my use case:
> I have a `toolchain.gcc.cmake` and a `toolchain.clang.cmake` toolchain
> files.
> Those files define variables for the gcc and clang compiler, and some
> settings (location of libraries, version numbers and so on) are common
> to both, and I would like to have a common `toolchain.cmake` toolchain
> file for that.
>
> If I write (uncomment on of the includes)
>
> -----
> # toolchain.gcc.cmake
>
> #include(common_settings.cmake)
> #include(./common_settings.cmake)
> #include(${CMAKE_CURRENT_SOURCE_DIR}/common_settings.cmake)
> #include(${CMAKE_SOURCE_DIR}/common_settings.cmake)
> #include(${CMAKE_HOME_DIRECTORY}/common_settings.cmake)
>
> set(CMAKE_C_COMPILER gcc)
> ----
>
> and execute (an empty CMakeLists.txt in the current directory is
> sufficient)
>
> ----
> rm -rf build; mkdir build && (cd build && cmake
> -DCMAKE_TOOLCHAIN_FILE=../toolchain.gcc.cmake ..);
> ----
>
> then cmake complains:
> ----
>    include could not find load file:
>
>
>
> /home/user/Workspace/toolchain/build/CMakeFiles/CMakeTmp/common_settings.cmake
> Call Stack (most recent call first):
>
> /home/user/Workspace/toolchain/build/CMakeFiles/3.12.3/CMakeSystem.cmake:6
> (include)
>
> /home/user/Workspace/toolchain/build/CMakeFiles/CMakeTmp/CMakeLists.txt:2
> (project)
>
>
> CMake Error at /usr/share/cmake-3.12/Modules/CMakeTestCCompiler.cmake:37
> (try_compile):
>    Failed to configure test project build system.
> Call Stack (most recent call first):
>    CMakeLists.txt:3 (project)
> ----
>
> As far as I've understood, cmake runs some internal test where, for
> example, `CMAKE_CURRENT_SOURCE_DIR` would not be the source directory of
> my code, and therefore
> `include(${CMAKE_CURRENT_SOURCE_DIR}/common_settings.cmake)` obviously
> fails.
> Same holds for other variables.
>
>
> The problem is that there is no way to set a path relative to the
> toolchain file, at least I was not able to find any.
>
> The only possible fix is setting an absolute path, but this is not
> really a solution since it makes the toolchain not portable.
>
> Is there anything else I could try?
> Is there already an open issue? I was not able to find any on
> https://gitlab.kitware.com/cmake/cmake/issues/.
>

You should be able to use CMAKE_CURRENT_LIST_DIR to specify a location
relative to the toolchain file itself.

-- 
Craig Scott
Melbourne, Australia
https://crascit.com

New book released: Professional CMake: A Practical Guide
<https://crascit.com/professional-cmake/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20181102/466e1e8f/attachment.html>


More information about the CMake mailing list