Skip to content

Commit

Permalink
Merge pull request TriBITSPub#564 from bartlettroscoe/tril-10355-asse…
Browse files Browse the repository at this point in the history
…rt-non-cache-vars

tribits_add_option_and_define(): Error out if non-cache vars already defined (trilinos/Trilinos#10355)

See trilinos/Trilinos#10355 and trilinos/Trilinos#11583
  • Loading branch information
bartlettroscoe authored Feb 23, 2023
2 parents 5470aab + b06d3da commit 1a6335a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
77 changes: 52 additions & 25 deletions tribits/core/package_arch/TribitsAddOptionAndDefine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,55 +41,82 @@ include(TribitsPkgExportCacheVars)
include(GlobalSet)


# @MACRO: tribits_add_option_and_define()
# @FUNCTION: tribits_add_option_and_define()
#
# Add an option and an optional macro define variable in one shot.
#
# Usage::
#
# tribits_add_option_and_define( <userOptionName> <macroDefineName>
# "<docStr>" <defaultValue> )
# "<docStr>" <defaultValue> [NONCACHE])
#
# This macro sets the user cache ``BOOL`` variable ``<userOptionName>`` and if
# it is true, then sets the global (internal cache) macro define variable
# ``<macroDefineName>`` to ``ON``, and otherwise sets it to ``OFF``. This is
# designed to make it easy to add a user-enabled option to a configured header
# file and have the define set in one shot. This would require that the
# package's configure file (see `tribits_configure_file()`_) have the line::
# ``<macroDefineName>`` to ``ON``, and otherwise sets it to ``OFF``. If
# ``NONCACHE`` is passed in, then ``<macroDefineName>`` is set as a non-cache
# local varaible instead of a cache variable.
#
# This is designed to make it easy to add a user-enabled option to a
# configured header file and have the define set in one shot. This would
# require that the package's configure file (see `tribits_configure_file()`_)
# have the line::
#
# #cmakedefine <macroDefineName>
#
# NOTE: This also calls `tribits_pkg_export_cache_var()`_ to export the
# variables ``<userOptionName>`` and ``<macroDefineName>``. This also
# requires that local variables with the same names of these cache variables
# not be assigned with a different value from these cache variables. If they
# are, then an error will occur later when these variables are read.
# variables ``<userOptionName>`` and ``<macroDefineName>`` (when ``NONCACHE``
# is **not** passed). This also requires that local variables with the same
# names of these cache variables not be assigned with a different value from
# these cache variables. If they are, then an error will occur later when
# these variables are read.
#
# NOTE: The define var name ``<macroDefineName>`` can be empty "" in which
# case all logic related to ``<macroDefineName>`` is skipped. (But in this
# case, it would be better to just call::
#
# set(<userOptionName> <defaultValue> CACHE BOOL "<docStr>")
#
macro(tribits_add_option_and_define USER_OPTION_NAME MACRO_DEFINE_NAME
DOCSTRING DEFAULT_VALUE
function(tribits_add_option_and_define USER_OPTION_NAME MACRO_DEFINE_NAME
DOCSTRING DEFAULT_VALUE
)
#message("TRIBITS_ADD_OPTION_AND_DEFINE: '${USER_OPTION_NAME}' '${MACRO_DEFINE_NAME}' '${DEFAULT_VALUE}'")
# Assert inputs
set(set_MACRO_DEFINE_NAME_noncache OFF)
if ("${ARGN}" STREQUAL "NONCACHE")
set(set_MACRO_DEFINE_NAME_noncache ON)
elseif (NOT "${ARGN}" STREQUAL "")
message(SEND_ERROR "Error, optional argument"
" '${ARGN}' can only be 'NONCACHE' or empty!")
endif()
# Assert these are not already defined local vars
if (DEFINED ${USER_OPTION_NAME})
if (NOT DEFINED CACHE{${USER_OPTION_NAME}})
message(SEND_ERROR "ERROR: The option"
" ${USER_OPTION_NAME}='${${USER_OPTION_NAME}}'"
" is already defined as a non-cache variable!")
endif()
endif()
if (DEFINED ${MACRO_DEFINE_NAME})
if (NOT DEFINED CACHE{${MACRO_DEFINE_NAME}})
message(SEND_ERROR "ERROR: The macro define name"
" ${MACRO_DEFINE_NAME}='${${MACRO_DEFINE_NAME}}'"
" is already defined as a non-cache variable!")
endif()
endif()
# Set ${USER_OPTION_NAME} as exported cache var
set( ${USER_OPTION_NAME} "${DEFAULT_VALUE}" CACHE BOOL "${DOCSTRING}" )
tribits_pkg_export_cache_var(${USER_OPTION_NAME})
# Set ${MACRO_DEFINE_NAME} as local or exported cache var
if(NOT "${MACRO_DEFINE_NAME}" STREQUAL "")
if(${USER_OPTION_NAME})
global_set(${MACRO_DEFINE_NAME} ON)
set(macroDefineValue ON)
else()
global_set(${MACRO_DEFINE_NAME} OFF)
set(macroDefineValue OFF)
endif()
if (set_MACRO_DEFINE_NAME_noncache)
set(${MACRO_DEFINE_NAME} ${macroDefineValue} PARENT_SCOPE)
else()
global_set(${MACRO_DEFINE_NAME} ${macroDefineValue})
tribits_pkg_export_cache_var(${MACRO_DEFINE_NAME})
endif()
endif()
tribits_pkg_export_cache_var(${USER_OPTION_NAME})
if(NOT "${MACRO_DEFINE_NAME}" STREQUAL "")
tribits_pkg_export_cache_var(${MACRO_DEFINE_NAME})
endif()
endmacro()

# 2008/10/05: rabartl: ToDo: Add an option to automatically add the macro
# define to any XXX_config.h file that gets configured by
# tribits_configure_file(...). This will help to eliminate
# duplication.
endfunction()
2 changes: 1 addition & 1 deletion tribits/doc/guides/TribitsMacroFunctionDocTemplate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@FUNCTION: tribits_add_executable() +
@FUNCTION: tribits_add_executable_and_test() +
@FUNCTION: tribits_add_library() +
@MACRO: tribits_add_option_and_define() +
@FUNCTION: tribits_add_option_and_define() +
@MACRO: tribits_add_show_deprecated_warnings_option() +
@FUNCTION: tribits_add_test() +
@MACRO: tribits_add_test_directories() +
Expand Down

0 comments on commit 1a6335a

Please sign in to comment.