[cmake-developers] I'm sure this is often asked

Eric Noulard eric.noulard at gmail.com
Fri Jul 29 02:47:45 EDT 2011


2011/7/29 J Decker <d3ck0r at gmail.com>:
> Any reason that environment variables can't be used as cmake
> variables?  Like of course any cmake variable in a script would
> override the environment, but if it's not otherwise found, checking in
> the environemnt would make scripts prettier.
>
>
> set BUILD_TYPE=debug
> set BUILD_MONOLITHIC=1
> cmake /path/to/source

I think you could build this feature into your CMakeLists.txt already
did you try something like:

if(NOT DEFINED BUILD_TYPE)
  if("$ENV{BUILD_TYPE}" STRGREATER "")
    message("BUILD_TYPE set from environment: $ENV{BUILD_TYPE}")
    set(BUILD_TYPE $ENV{BUILD_TYPE})
  else()
    message("BUILD_TYPE is not set")
  endif()
else()
   message("BUILD_TYPE is user defined: ${BUILD_TYPE}")
endif()

beware that you may need to:
export BUILD_TYPE=Debug
and not
set BUILD_TYPE=Debug

because is forked by the shell and not a shell script, thus
it lose unexported var.

It should even be doable to write a macro that would conditionnally
setup a list of varname from the environment if the CMake var is not defined.

If you do that send it here of put it on the Wiki, it may be useful for others.

> instead of having to pass -DBUILD_TYPE=debug -DBUILD_MONOLITHIC which
> gets strung out all on one line usually.  some longer varaibles might
> be -DSACK_INTERSHELL_SDK_ROOT_PATH=$SACK_INTERSHELL_SDK_ROOT_PATH

The good thing with the macro trick is that it would work for user
variable as well.

-- 
Erk
Membre de l'April - « promouvoir et défendre le logiciel libre » -
http://www.april.org



More information about the cmake-developers mailing list