[CMake] if (DEFINED $ENV{VAR}) doesn't work as expected

Glenn Coombs glenn.coombs at gmail.com
Tue Oct 11 12:02:13 EDT 2011


Hi,

I've just had a CMakeLists.txt fail to work as expected because somebody was
testing to see whether an environment variable was set with the syntax: if
(DEFINED $ENV{VAR}).  This short example shows the problem:

cmake_minimum_required(VERSION 2.8)

project(foo)

message("HOME: $ENV{HOME}")
if (DEFINED $ENV{HOME})
    message("HOME is defined")
else()
    message("HOME is NOT defined")
endif()

if (DEFINED FOO)
    message("At 1: FOO is defined")
else()
    message("At 1: FOO is NOT defined")
endif()

set(FOO "foo")

if (DEFINED FOO)
    message("At 2:FOO is defined")
else()
    message("At 2:FOO is NOT defined")
endif()

When run it prints this:

HOME: /user/grc
HOME is NOT defined
At 1: FOO is NOT defined
At 2:FOO is defined

So the test for if a variable is defined works for cmake variables but not
for environment variables.  I can work around this by testing if the
environment variable is the empty string I guess.  Is the current behaviour
what is wanted, or is this a bug ?

--
Glenn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20111011/7a2a2a92/attachment.htm>


More information about the CMake mailing list