Using CMake I'm trying to expand system environment variable values in custom file.
I do the following command:
configure_file(config.cnf.in config.cnf)
config.cnf.in content:
[options]
some_value1 = $ENV{SYSTEM_ENV_VAR}
The question is:
Is it possible to set the default value for SYSTEM_ENV_VAR variable, if it is not defined?
I tried to do this:
some_value1 = $ENV{SYSTEM_ENV_VAR:-defaultValue}
and got an cmake error:
Invalid character (':') in a variable name: 'SYSTEM_ENV_VAR'
I didn't find the answer in the docs:
https://cmake.org/cmake/help/v3.2/command/configure_file.html
You cannot set a default value for the variable, but you can store the environment variable inside a normal cmake variable, and if it was not set define a default value.
I tested this:
set(EXIST $ENV{HOME})
set(NOT_EXIST $ENV{NOT_EXIST})
if(EXIST)
message("Variable EXIST exist")
else()
message("Variable EXIST DOES NOT exist, setting default value")
set(EXIST "Default value")
endif()
if(NOT_EXIST)
message("Variable NOT_EXIST exist")
else()
message("Variable NOT_EXIST DOES NOT exist, setting default value")
set(NOT_EXIST "Default value")
endif()
message ("EXIST: ${EXIST}")
message ("NOT_EXIST: ${NOT_EXIST}")
My output for this is
Variable EXIST exist
Variable NOT_EXIST DOES NOT exist, setting default value
EXIST: /home/user
NOT_EXIST: Default value