Debian Codegraph @

cmake/tribits/core/utils/SetDefaultAndFromEnv.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10
11 include(SetDefault)
12 include(PrintVar)
13
14
15 # @MACRO: set_default_and_from_env()
16 #
17 # Set a default value for a local variable and override from an environment
18 # variable of the same name if it is set.
19 #
20 # Usage::
21 #
22 # set_default_and_from_env(<varName> <defaultVal>)
23 #
24 # First calls ``set_default(<varName> <defaultVal>)`` and then looks for an
25 # environment variable named ``<varName>``, and if non-empty then overrides
26 # the value of the local variable ``<varName>``.
27 #
28 # This macro is primarily used in CTest code to provide a way to pass in the
29 # value of CMake variables. Older versions of ``ctest`` did not support the
30 # option ``-D <var>:<type>=<value>`` to allow variables to be set through the
31 # command-line like ``cmake`` always allowed.
32 #
33 macro(set_default_and_from_env VAR DEFAULT_VAL)
34
35 set_default(${VAR} "${DEFAULT_VAL}")
36
37 set(ENV_${VAR} $ENV{${VAR}})
38 if (NOT "${ENV_${VAR}}" STREQUAL "")
39 print_var(ENV_${VAR})
40 set(${VAR} ${ENV_${VAR}})
41 endif()
42
43 print_var(${VAR})
44
45 endmacro()