FR: allow cache variables to be initialized by an environment variable
In my CMake code, I have noticed myself repeating this pattern:
if(DEFINED ENV{SOME_VAR})
set (somevar_init "$ENV{SOME_VAR}")
else()
set (somevar_init "default value")
endif()
set (SOME_VAR "${somevar_init}" CACHE STRING "description")
I propose adding a new argument to the set(CACHE)
command:
set (<name> <default_value> CACHE <type> "description" [ENV [<varName>]])
If the ENV
keyword is specified, the command should check if the environment variable is defined, and if so, use its value as the cache variable's initial value. If the environment variable is not defined, the <default_value>
should be used.
I further propose that specifying the environment variable name be optional - if only ENV
is present, the command should look for an environment variable with the same name as the cache variable.