[CMake] Test if ENV${VAR} is set

Eric Clark eclark at ara.com
Fri Sep 14 16:39:45 EDT 2012


I do this all the time in my config files and I have found that best way to do it is to do this:

if(NOT ENV{FOO_HOME})
  message(FATAL_ERROR "Could not find FOO_HOME environment variable")
endif(NOT ENV{FOO_HOME}))

One thing to note is that if the environment variable points to a path on the system, you could also do something like this:

if(NOT EXISTS "$ENV{FOO_HOME}")
  message(FATAL_ERROR "Could not find FOO_HOME environment variable")
endif(NOT EXISTS "$ENV{FOO_HOME}"))

The above example tests for the existence of the path that is supposed to be provided by that environment variable. If the variable is not set, the string will be empty and will return true that it does not exist. This checks both the existence of the environment variable and the existence of the path it points to. You can save yourself a couple steps by doing this. There are two things to note about the above example:

  1. The quotes are required if there is even the slightest possibility of the path containing any spaces.
  2. If you are on Windows, there is a good chance the environment variable FOO_HOME is set using Windows path separators (\ not /). This will cause errors down the road if you don't convert the slashes to CMake slashes. The code below converts the slashes for you and sets a CMake variable FOO_HOME that stores the path found by $ENV{FOO_HOME}:

if(EXISTS "$ENV{FOO_HOME}")
  file(TO_CMAKE_PATH "$ENV{FOO_HOME}" FOO_HOME)
else()
  message(FATAL_ERROR "Could not find FOO_HOME environment variable")
endif(EXISTS "$ENV{FOO_HOME}")

Hope this helps!
Eric

> -----Original Message-----
> From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On
> Behalf Of David Cole
> Sent: Friday, September 14, 2012 3:26 PM
> To: Bogdan Cristea
> Cc: cmake at cmake.org
> Subject: Re: [CMake] Test if ENV${VAR} is set
> 
> On Fri, Sep 14, 2012 at 4:17 PM, Bogdan Cristea <cristeab at gmail.com> wrote:
> > On Friday 14 September 2012 16:13:16 Davis Ford wrote:
> >> if("$ENV{FOO_HOME}" MATCHES "")
> >>    message("You must set FOO_HOME")
> >>    return()
> >> endif()
> >
> > Try something like this
> >
> > if(NOT ENV{FOO_HOME})
> > message("etc")
> > endif()
> >
> > --
> > Bogdan
> > --
> >
> > Powered by www.kitware.com
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Please keep messages on-topic and check the CMake FAQ at:
> > http://www.cmake.org/Wiki/CMake_FAQ
> >
> > Follow this link to subscribe/unsubscribe:
> > http://www.cmake.org/mailman/listinfo/cmake
> 
> I would use the double quotes like you had originally, but use STREQUAL
> instead of MATCHES. Or use MATCHES "^$" to match ONLY the explicitly
> empty string, because MATCHES "" basically matches everything because
> everything "contains" the empty string...
> 
> HTH,
> David
> --
> 
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list