[CMake] is it possbile to export environment varibles from cmake scripts?

Timenkov Yuri ytimenkov at parallels.com
Mon Jul 7 08:22:46 EDT 2008


On Monday 07 July 2008 16:07:54 Alexander Bubnov wrote:
> It is clear. I just would like to know a similiar command of cmake.
> ...it is a pity there is no one.
You can export variables for child processes, but not for parent. Look for 
pkg-config: rather than exporting something, it just prints it to stdout. If 
you need something similar, you can dump such variables to file and load them 
later with "source" bash command (on UNIX).

For child processes you can try:

cmake_minimum_required(VERSION 2.6)

set(ENV{MYVAR} "SOME VALUE")
message(STATUS "the value of MYVAR: " $ENV{MYVAR})

execute_process(COMMAND "/usr/bin/env" OUTPUT_VARIABLE myEnvOut)

#message(STATUS "Local Environment: ${myEnvOut}")

string(REGEX MATCH "MYVAR=[^\n]*" MyVarEnv "${myEnvOut}")

message(STATUS "Found MYVAR=${MyVarEnv}")

>
> Thanks.
>
> 2008/7/7, Timenkov Yuri <ytimenkov at parallels.com>:
> >  On Monday 07 July 2008 15:53:00 Alexander Bubnov wrote:
> >  > Hello!
> >  >
> >  > Below simple example to clarify the problem.
> >  >
> >  > My CMakeLists.txt:
> >  >
> >  > cmake_minimum_required(VERSION 2.6)
> >  >
> >  > set(ENV{MYVAR} "SOME VALUE")
> >  > message("the value of MYVAR: " $ENV{MYVAR})
> >  >
> >  > the output:
> >  >
> >  > the value of MYVAR: SOME VALUE
> >  > -- Configuring done
> >  > -- Generating done
> >  > -- Build files have been written to:
> >  > /home/alek/trial/cmake/CMakeExample
> >  >
> >  >
> >  > after that I am typing: echo $MYVAR
> >  > MYVAR: Undefined variable.
> >  >
> >  > Is it possible to export MYVAR to shell environment?
> >
> > I don't think it's possible with any tool. Environment variables are
> >  propagated from parent process to child when the former creates start-up
> >  parameters (for exec* or CreateProcess calls) for the latter (as CMake
> > and any other program (including shell) does when executes external
> > utilities).
> >
> >  The "export" or "set" are keywords of command line interpreter itself
> > which tell it to set appropriate environment variables for child
> > processes.
> >
> >  As for CMake, you can try to execute script from CMakeLists.txt to see
> > if it exports variables properly.
> >
> >  > Thanks in advance.



More information about the CMake mailing list