cmake:从脚本中设置环境变量

时间:2021-08-11 23:20:46

I have a script that sets all variables needed for the cross-compilation. Here is just part of it :

我有一个脚本,用于设置交叉编译所需的所有变量。这只是其中的一部分:

export CONFIG_SITE=~/workspace/eldk-5.4/powerpc/site-config-powerpc-linux
export CC="powerpc-linux-gcc  -m32 -mhard-float --sysroot=~/workspace/eldk-5.4/powerpc/sysroots/powerpc-linux"
export CXX="powerpc-linux-g++  -m32 -mhard-float --sysroot=~/workspace/eldk-5.4/powerpc/sysroots/powerpc-linux"
export CPP="powerpc-linux-gcc -E  -m32 -mhard-float --sysroot=~/workspace/eldk-5.4/powerpc/sysroots/powerpc-linux"
export AS="powerpc-linux-as "
export LD="powerpc-linux-ld  --sysroot=~/workspace/eldk-5.4/powerpc/sysroots/powerpc-linux"
export GDB=powerpc-linux-gdb

If I do source environment-setup-powerpc-linux, all environment variables are imported into the current shell session, and I can compile my example.

如果我做源代码环境-setup-powerpc-linux,所有的环境变量都被导入到当前shell会话中,我可以编译我的示例。

Is it possible to import these variables in cmake? If yes, how?

是否可以在cmake中导入这些变量?如果是,如何?


A bit more details :

更多细节:

  1. I am using ELDK v 5.4, and it's install script generates a script which sets all environment variables
  2. 我正在使用ELDK v5.4,它的安装脚本生成一个脚本,该脚本设置所有的环境变量
  3. I found this tutorial, which explains how to manually set for cross-compilation, but not how to use the script, which sets everything
  4. 我找到了本教程,它解释了如何手动设置交叉编译,而不是如何使用脚本设置所有内容
  5. if I call the script before setting cmake, all works fine, and I can cross-compile, but I'd like that cmake calls the script
  6. 如果我在设置cmake之前调用这个脚本,一切都没问题,我可以交叉编译,但是我希望cmake调用这个脚本

2 个解决方案

#1


52  

Reading through the cmake quick start, you can specify variable on a command line:

通过阅读cmake快速启动,您可以在命令行上指定变量:

cmake -DVARIABLE1=value1 -DVARIABLE2=value2 ...

Otherwise, set command in the cmake script is probably what you want, see the reference manual. To set the environment variable PATH, do:

否则,您可能需要在cmake脚本中设置命令,请参阅参考手册。要设置环境变量路径,请执行:

set(ENV{PATH} "/home/martink")

To set normal variable, do:

要设置正常变量,请执行:

set(variable "value")

Not sure which ones you have to set, probably the environment ones.

不确定哪些是你必须设置的,可能是环境问题。

That said, setting environment variable prior to calling cmake is often the easiest solution to solve the problem, as in this case: https://*.com/a/15053460/684229

也就是说,在调用cmake之前设置环境变量通常是解决问题的最简单的解决方案,如在本例中:https://*.com/a/15053460/684229。

#2


4  

The only way to set a compiler and flags to do cross-compilation reliably with CMake is with a toolchain-file as done in the tutorial you have found.

要设置编译器和标志以便与CMake进行可靠的交叉编译,惟一的方法是使用工具链文件,如您在本教程中所发现的那样。

When we faced the same issue you have (a toolkit which produces a script so set the compile-environment) we changed the toolkit in a way that it produces a toolchain-file along with the script.

当我们遇到您遇到的相同问题(生成脚本从而设置编译环境的工具包)时,我们对工具包进行了修改,使其与脚本一起生成一个工具链文件。

In reality a cmake-toolchain-file does not change that often. The basic flags used for the target are fixed quite early in a project - normally. And with CMake's CMAKE_BUILD_TYPE you can switch between Debug and Release compilations without changing the toolchain-file.

实际上,cmake工具链文件不会经常改变。用于目标的基本标志在项目中很早就被修复了——通常是这样。使用CMake的CMAKE_BUILD_TYPE,您可以在调试和发布编译之间进行切换,而无需更改工具链文件。

If you have different targets to support, create different toolchain and use the out-of-source-build with CMake.

如果您需要支持不同的目标,可以创建不同的工具链,并使用CMake的源代码外构建。

EDIT: One thing you could do is to invoke cmake with the -D-argument setting the variables you want to and having sourced your script before:

编辑:您可以做的一件事是使用- d参数调用cmake来设置您想要的变量,并在此之前找到您的脚本:

source environment-setup-powerpc-linux
cmake -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX etc

The result will be identical as to having used a toolchain-file.

结果将与使用工具链文件相同。

#1


52  

Reading through the cmake quick start, you can specify variable on a command line:

通过阅读cmake快速启动,您可以在命令行上指定变量:

cmake -DVARIABLE1=value1 -DVARIABLE2=value2 ...

Otherwise, set command in the cmake script is probably what you want, see the reference manual. To set the environment variable PATH, do:

否则,您可能需要在cmake脚本中设置命令,请参阅参考手册。要设置环境变量路径,请执行:

set(ENV{PATH} "/home/martink")

To set normal variable, do:

要设置正常变量,请执行:

set(variable "value")

Not sure which ones you have to set, probably the environment ones.

不确定哪些是你必须设置的,可能是环境问题。

That said, setting environment variable prior to calling cmake is often the easiest solution to solve the problem, as in this case: https://*.com/a/15053460/684229

也就是说,在调用cmake之前设置环境变量通常是解决问题的最简单的解决方案,如在本例中:https://*.com/a/15053460/684229。

#2


4  

The only way to set a compiler and flags to do cross-compilation reliably with CMake is with a toolchain-file as done in the tutorial you have found.

要设置编译器和标志以便与CMake进行可靠的交叉编译,惟一的方法是使用工具链文件,如您在本教程中所发现的那样。

When we faced the same issue you have (a toolkit which produces a script so set the compile-environment) we changed the toolkit in a way that it produces a toolchain-file along with the script.

当我们遇到您遇到的相同问题(生成脚本从而设置编译环境的工具包)时,我们对工具包进行了修改,使其与脚本一起生成一个工具链文件。

In reality a cmake-toolchain-file does not change that often. The basic flags used for the target are fixed quite early in a project - normally. And with CMake's CMAKE_BUILD_TYPE you can switch between Debug and Release compilations without changing the toolchain-file.

实际上,cmake工具链文件不会经常改变。用于目标的基本标志在项目中很早就被修复了——通常是这样。使用CMake的CMAKE_BUILD_TYPE,您可以在调试和发布编译之间进行切换,而无需更改工具链文件。

If you have different targets to support, create different toolchain and use the out-of-source-build with CMake.

如果您需要支持不同的目标,可以创建不同的工具链,并使用CMake的源代码外构建。

EDIT: One thing you could do is to invoke cmake with the -D-argument setting the variables you want to and having sourced your script before:

编辑:您可以做的一件事是使用- d参数调用cmake来设置您想要的变量,并在此之前找到您的脚本:

source environment-setup-powerpc-linux
cmake -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX etc

The result will be identical as to having used a toolchain-file.

结果将与使用工具链文件相同。