Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake : Set environment variables from a script

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.

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


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. I found this tutorial, which explains how to manually set for cross-compilation, but not how to use the script, which sets everything
  3. 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
like image 549
BЈовић Avatar asked Jan 10 '14 14:01

BЈовић


People also ask

Does CMake set environment variables?

in which case the environment variable will be set. In CMake there are two types of variables: normal variables and cache variables. Normal variables are meant for the internal use of the script (just like variables in most programming languages); they are not persisted across CMake runs.

How do you set a variable in Cmakelists txt?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake .

How do I use environment variables in CMake?

Use the syntax $ENV{VAR} to read environment variable VAR . To test whether an environment variable is defined, use the signature if(DEFINED ENV{<name>}) of the if() command. For general information on environment variables, see the Environment Variables section in the cmake-language(7) manual.

How do I set environment variables in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.

How to set the current process environment of CMake?

Set the current process environment <variable> to the given value. It influences the current process environment that is created when CMake is started from the shell. It is not the environment of the shell itself. Prefix your command with cmake -E env XXX=YYY. To test, use cmake -E env XXX=YYY cmake -E environment.

How do I set environment variables in a batch file?

Setting environment variables in a batch file called using execute_process. Say I have a batch file that looks like this. I want my CMakeLists.txt file to make use of this environment variable with the syntax provided by CMake. Before that can be done, I need to run the batch script to set that environment variable.

What are environment variables and how do they work?

Environment variables can be global for the project or specific to an individual configuration (selected with the configuration dropdown menu). These environment variables will be passed to everything, including CMake builds, custom tasks, and debug targets.

How to do cross-compilation with CMake?

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. 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.


1 Answers

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

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:

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://stackoverflow.com/a/15053460/684229

like image 131
Tomas Avatar answered Sep 22 '22 06:09

Tomas