Environment variables can be used in if
command:
if ($ENV{FOO})
message(STATUS “bar”)
endif ()
This works as expected but produces a warning if cmake is executed with --warn-uninitialized and if FOO is not defined. Using DEFINED produces a syntax error because $ENV{FOO} is evaluated to nothing:
if (DEFINED ENV{FOO} AND $ENV{FOO})
message(STATUS “bar”)
endif ()
Adding quotes makes the example work as expected but a warning about uninitialized variable is produced because $ENV{FOO} is evaluated before the if
command even receives the arguments:
if (DEFINED ENV{FOO} AND “$ENV{FOO}”)
message(STATUS “bar”)
endif ()
This works as expected and doesn’t produce a warning about uninitialized variable:
if (DEFINED ENV{FOO})
if ($ENV{FOO})
message(STATUS “bar”)
endif ()
endif ()
But is there a way to use an environment variable in a single if
command without producing warning about uninitialized variable?