[CMake] shell level access to values of cmake variables

Michael Hertling mhertling at online.de
Thu Nov 17 08:41:29 EST 2011


On 11/10/2011 06:48 PM, vagn scott wrote:
> 
> 
> in my CMakeLists.txt file I have a statement
> 
> project(hello_foo_baz)
> 
> This defines PROJECT_NAME among other things.
> 
> is there something like
> 
>          cmake --dump-var PROJECT_NAME
> 
> that would output the string
> 
>          hello_foo_baz
> 
> TIA,
> 
> vagn

PROJECT_NAME is a CMake variable, i.e. it exists while the CMake
process is running, and it's visible in the CMakeLists.txt files
by the usual ${} dereferencer. It is *not* a member of the CMake
process' environment, although it can be written to the latter:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(ENVVAR NONE)
MESSAGE("BEFORE:")
EXECUTE_PROCESS(COMMAND env COMMAND grep PROJECT_NAME)
SET(ENV{PROJECT_NAME} ${PROJECT_NAME})
MESSAGE("AFTER:")
EXECUTE_PROCESS(COMMAND env COMMAND grep PROJECT_NAME)

In no case it is written to the environment of the CMake process'
parent, e.g. the shell, i.e. you can not access a PROJECT_NAME
environment variable after the CMake process has terminated.
If you need to access PROJECT_NAME after the configuration
has finished, the CMakeLists.txt must preserve it, e.g.

CONFIGURE_FILE(project_name.txt.in project_name.txt @ONLY)

with a project_name.txt.in like "@PROJECT_NAME@".

Regards,

Michael


More information about the CMake mailing list