sergej
sergej

Reputation: 18009

How to pass a variable from command line to a CMake Toolhain file?

Is there a way to pass a variable to a toolchain file when invoking cmake?

For example, I have the following toolchain file:

message("FOO = ${FOO}")

I have tried the following, but it did not work. The variable is not set in the toolchain file.

cmake <src-dir> -DCMAKE_TOOLCHAIN_FILE=<toolchain-file> -DFOO="bar" -B <build-dir>

What I am actually trying to achieve is passing a path to the toolchain file. And I can't modify the main CMakeLists.txt in .

Upvotes: 5

Views: 11618

Answers (1)

Thomas Sablik
Thomas Sablik

Reputation: 16448

You have to set the path to your toolchain file with -DCMAKE_TOOLCHAIN_FILE. The following works for me (cmake3 version 3.12.0):

ToolChain.cmake:

message("Toolchain file loaded with path: ${FOO}")

CMakeLists.txt empty

cmake -DCMAKE_TOOLCHAIN_FILE=./ToolChain.cmake . -DFOO=/path

Output is:

Toolchain file loaded with path: /path

The order of <src-path> and -DCMAKE_TOOLCHAIN_FILE=./ToolChain.cmake is important.

cmake . -DCMAKE_TOOLCHAIN_FILE=./ToolChain.cmake -DFOO=/path

doesn't work.

Upvotes: 8

Related Questions