CMake expand env variable with default value for custom file

冷暖自知 提交于 2020-02-06 09:24:26

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/53723221/cmake-expand-env-variable-with-default-value-for-custom-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!