Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if an environment variable is set in cmake

I set an environment variable in my bash profile, so I can see it in the terminal just fine . .

blah/builds$ echo $THING

thingy

How do I display it in a cmake message and check if it is set? I have tried the following, but it just displays thing as blank and skips the body of the if statement

message("THING:" $ENV{THING})
if(DEFINED ENV{THING})
    message(STATUS "THING environment variable defined")
    # some more commands
endif()
like image 574
learnvst Avatar asked Apr 17 '15 00:04

learnvst


People also ask

Does CMake set environment variables?

In this case <variable> is set to a semicolon separated list of values. in which case the environment variable will be set. In CMake there are two types of variables: normal variables and cache variables.

How are CMake variables set?

Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.

How do you read an environment variable in C++?

The getenv() function in C++ returns a pointer to a C string containing the value of the environment variable passed as argument. If the environment variable passed to the getenv() function is not in the environment list, it returns a null pointer.

Where are CMake variables stored?

The type of the variable is used by the cmake-gui to control how that variable is set and displayed, but the value is always stored in the cache file as a string.


3 Answers

Knowing perfectly well what export does and how environment works in general, I've still got a mouthful of WTFs with this form:

IF(DEFINED $ENV{THING})

but it worked fine in this form:

IF(DEFINED ENV{THING})

Notice the $ omission.


N.B. you can quickly test this using cmake -P:

[~] cat > test-env.cmake << 'EOF'
IF(DEFINED ENV{FOOBAR})
    MESSAGE(STATUS "FOOBAR env seen: --[$ENV{FOOBAR}]--")
ELSE()
    MESSAGE(STATUS "WTF")
ENDIF()
EOF
[~]
[~] FOOBAR=test cmake -P test-env.cmake
-- FOOBAR env seen: --[test]--

The reason it behaves so weirdly, is legacy, as usual. IF used to do insane stuff in older CMake; they sort-of fixed this in CMake 3.1 — in a backward-compatible manner — with CMP0054, which you have to enable explicitly:

CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
PROJECT(...)

CMAKE_POLICY(SET CMP0054 NEW) #-- fixes IF() with quoted args
CMAKE_POLICY(SET CMP0057 NEW) #-- enables IF(.. IN_LIST ..)
like image 177
ulidtko Avatar answered Oct 24 '22 09:10

ulidtko


You CMake code is correct. The problem is most likely that you only set the environment variable in your shell but did not export it. Run the following before invoking cmake:

export THING
like image 13
kynan Avatar answered Oct 24 '22 10:10

kynan


Just to be clear: as https://cmake.org/cmake/help/latest/command/if.html says:

if (DEFINED ENV{THING})
  # do stuff
endif()

is the right syntax, no $ anywhere.

like image 7
Jan Wilmans Avatar answered Oct 24 '22 11:10

Jan Wilmans