Debian Codegraph @

cmake/tribits/core/package_arch/TribitsAddOptionAndDefine.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 include(TribitsPkgExportCacheVars)
11 include(GlobalSet)
12
13
14 # @FUNCTION: tribits_add_option_and_define()
15 #
16 # Add an option and an optional macro define variable in one shot.
17 #
18 # Usage::
19 #
20 # tribits_add_option_and_define( <userOptionName> <macroDefineName>
21 # "<docStr>" <defaultValue> [NONCACHE])
22 #
23 # This macro sets the user cache ``BOOL`` variable ``<userOptionName>`` and if
24 # it is true, then sets the global (internal cache) macro define variable
25 # ``<macroDefineName>`` to ``ON``, and otherwise sets it to ``OFF``. If
26 # ``NONCACHE`` is passed in, then ``<macroDefineName>`` is set as a non-cache
27 # local variable instead of a cache variable.
28 #
29 # This is designed to make it easy to add a user-enabled option to a
30 # configured header file and have the define set in one shot. This would
31 # require that the package's configure file (see `tribits_configure_file()`_)
32 # have the line::
33 #
34 # #cmakedefine <macroDefineName>
35 #
36 # NOTE: This also calls `tribits_pkg_export_cache_var()`_ to export the
37 # variables ``<userOptionName>`` and ``<macroDefineName>`` (when ``NONCACHE``
38 # is **not** passed). This also requires that local variables with the same
39 # names of these cache variables not be assigned with a different value from
40 # these cache variables. If they are, then an error will occur later when
41 # these variables are read.
42 #
43 # NOTE: The define var name ``<macroDefineName>`` can be empty "" in which
44 # case all logic related to ``<macroDefineName>`` is skipped. (But in this
45 # case, it would be better to just call::
46 #
47 # set(<userOptionName> <defaultValue> CACHE BOOL "<docStr>")
48 #
49 function(tribits_add_option_and_define USER_OPTION_NAME MACRO_DEFINE_NAME
50 DOCSTRING DEFAULT_VALUE
51 )
52 # Assert inputs
53 set(set_MACRO_DEFINE_NAME_noncache OFF)
54 if ("${ARGN}" STREQUAL "NONCACHE")
55 set(set_MACRO_DEFINE_NAME_noncache ON)
56 elseif (NOT "${ARGN}" STREQUAL "")
57 message(SEND_ERROR "Error, optional argument"
58 " '${ARGN}' can only be 'NONCACHE' or empty!")
59 endif()
60 # Assert these are not already defined local vars
61 if (DEFINED ${USER_OPTION_NAME})
62 if (NOT DEFINED CACHE{${USER_OPTION_NAME}})
63 message(SEND_ERROR "ERROR: The option"
64 " ${USER_OPTION_NAME}='${${USER_OPTION_NAME}}'"
65 " is already defined as a non-cache variable!")
66 endif()
67 endif()
68 if (DEFINED ${MACRO_DEFINE_NAME})
69 if (NOT DEFINED CACHE{${MACRO_DEFINE_NAME}})
70 message(SEND_ERROR "ERROR: The macro define name"
71 " ${MACRO_DEFINE_NAME}='${${MACRO_DEFINE_NAME}}'"
72 " is already defined as a non-cache variable!")
73 endif()
74 endif()
75 # Set ${USER_OPTION_NAME} as exported cache var
76 set( ${USER_OPTION_NAME} "${DEFAULT_VALUE}" CACHE BOOL "${DOCSTRING}" )
77 tribits_pkg_export_cache_var(${USER_OPTION_NAME})
78 # Set ${MACRO_DEFINE_NAME} as local or exported cache var
79 if(NOT "${MACRO_DEFINE_NAME}" STREQUAL "")
80 if(${USER_OPTION_NAME})
81 set(macroDefineValue ON)
82 else()
83 set(macroDefineValue OFF)
84 endif()
85 if (set_MACRO_DEFINE_NAME_noncache)
86 set(${MACRO_DEFINE_NAME} ${macroDefineValue} PARENT_SCOPE)
87 else()
88 global_set(${MACRO_DEFINE_NAME} ${macroDefineValue})
89 tribits_pkg_export_cache_var(${MACRO_DEFINE_NAME})
90 endif()
91 endif()
92 endfunction()