Skip to content

Commit

Permalink
Merge pull request #521 from bartlettroscoe/516-allow-empty-define-var
Browse files Browse the repository at this point in the history
tribits_add_option_and_define(): restore optional macro define var arg, refactor (#516)

Should also fix trilinos/Trilinos#10925
  • Loading branch information
bartlettroscoe committed Aug 24, 2022
2 parents c85490f + df5821a commit 4ab6cae
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 83 deletions.
2 changes: 1 addition & 1 deletion test/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tribits_add_advanced_test( TestingFunctionMacro_UnitTests
-D${PROJECT_NAME}_TRIBITS_DIR=${${PROJECT_NAME}_TRIBITS_DIR}
-P "${CMAKE_CURRENT_SOURCE_DIR}/TestingFunctionMacro_UnitTests.cmake"
PASS_REGULAR_EXPRESSION_ALL
"Final UnitTests Result: num_run = 703"
"Final UnitTests Result: num_run = 708"
"Final UnitTests Result: PASSED"
)

Expand Down
32 changes: 31 additions & 1 deletion test/core/TestingFunctionMacro_UnitTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ include(TribitsETISupport)
include(TribitsFindPythonInterp)
include(TribitsStripQuotesFromStr)
include(TribitsStandardizePaths)
include(TribitsAddOptionAndDefine)
include(TribitsFilepathHelpers)
include(TribitsGetVersionDate)
include(TribitsTplFindIncludeDirsAndLibraries)
Expand Down Expand Up @@ -409,6 +410,34 @@ function(unittest_tribits_strip_quotes_from_str)
endfunction()


function(unittest_tribits_add_option_and_define)

message("\n***")
message("*** Testing tribits_add_option_and_define()")
message("***\n")

message("\nPass both option and define var, default YES")
tribits_add_option_and_define(optName1 macroOptName1 "doc" YES)
unittest_compare_const(optName1 "YES")
unittest_compare_const(macroOptName1 "ON")
unset(optName1 CACHE)
unset(macroOptName1 CACHE)

message("\nPass both option and define var, default FALSE")
tribits_add_option_and_define(optName2 macroOptName2 "doc" FALSE)
unittest_compare_const(optName2 "FALSE")
unittest_compare_const(macroOptName2 "OFF")
unset(optName2 CACHE)
unset(macroOptName2 CACHE)

message("\nPass only option var, not define var, default YES")
tribits_add_option_and_define(optName3 "" "doc" YES)
unittest_compare_const(optName3 "YES")
unset(optName3 CACHE)

endfunction()


function(unittest_tribits_get_raw_git_commit_utc_time)

message("\n***")
Expand Down Expand Up @@ -4587,6 +4616,7 @@ unittest_tribits_dir_is_basedir()
unittest_tribits_get_dir_array_below_base_dir()
unittest_tribits_misc()
unittest_tribits_strip_quotes_from_str()
unittest_tribits_add_option_and_define()
unittest_tribits_get_version_date_from_raw_git_commit_utc_time()
unittest_tribits_get_raw_git_commit_utc_time()
unittest_tribits_git_repo_sha1()
Expand Down Expand Up @@ -4646,4 +4676,4 @@ message("*** Determine final result of all unit tests")
message("***\n")

# Pass in the number of expected tests that must pass!
unittest_final_result(703)
unittest_final_result(708)
18 changes: 14 additions & 4 deletions tribits/core/package_arch/TribitsAddOptionAndDefine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
# ************************************************************************
# @HEADER

include(TribitsPkgExportCacheVars)
include(GlobalSet)


# @MACRO: tribits_add_option_and_define()
#
# Add an option and a define variable in one shot.
# Add an option and an optional macro define variable in one shot.
#
# Usage::
#
Expand All @@ -59,7 +60,16 @@ include(GlobalSet)
# #cmakedefine <macroDefineName>
#
# NOTE: This also calls `tribits_pkg_export_cache_var()`_ to export the
# variables ``<userOptionName>`` and ``<macroDefineName>``.
# 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.
#
# 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
Expand All @@ -73,8 +83,8 @@ macro(tribits_add_option_and_define USER_OPTION_NAME MACRO_DEFINE_NAME
global_set(${MACRO_DEFINE_NAME} OFF)
endif()
endif()
if (COMMAND tribits_pkg_export_cache_var)
tribits_pkg_export_cache_var(${USER_OPTION_NAME})
tribits_pkg_export_cache_var(${USER_OPTION_NAME})
if(NOT ${MACRO_DEFINE_NAME} STREQUAL "")
tribits_pkg_export_cache_var(${MACRO_DEFINE_NAME})
endif()
endmacro()
Expand Down
24 changes: 0 additions & 24 deletions tribits/core/package_arch/TribitsGeneralMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -426,27 +426,3 @@ function(tribits_trace_file_processing TYPE_IN PROCESSING_TYPE_IN FILE_PATH)
endif()

endfunction()


# @MACRO: tribits_assert_cache_and_local_vars_same_value()
#
# Asset that a cache variable and a possible local variable (if it exists)
# have the same value.
#
# Usage::
#
# tribits_assert_cache_and_local_vars_same_value(<cacheVarName>)
#
# If the local var ``<cacheVarName>`` and the cache var ``<cacheVarName>``
# both exist but have different values, then ``message(SEND_ERROR ...)`` is
# called with an informative error message.
#
macro(tribits_assert_cache_and_local_vars_same_value cacheVarName)
set(cacheVarValue "$CACHE{${cacheVarName}}")
set(localValue "${${cacheVarName}}")
if (NOT localValue STREQUAL cacheVarValue)
message_wrapper(SEND_ERROR "ERROR: The cache variable ${cacheVarName} with the"
" cache var value '${cacheVarValue}' is not the same value as the local"
" variable ${cacheVarName} with value '${localValue}'!")
endif()
endmacro()
42 changes: 1 addition & 41 deletions tribits/core/package_arch/TribitsPackageMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ include(RemoveGlobalDuplicates)
include(TribitsGatherBuildTargets)

include(TribitsAddOptionAndDefine)
include(TribitsPkgExportCacheVars)
include(TribitsLibraryMacros)
include(TribitsAddExecutable)
include(TribitsAddExecutableAndTest)
Expand All @@ -78,47 +79,6 @@ macro(tribits_define_linkage_vars PACKAGE_NAME_IN)
endmacro()


# Macro that sets up data-structures for variables to be exported
#
macro(tribits_pkg_init_exported_vars PACKAGE_NAME_IN)
global_set(${PACKAGE_NAME_IN}_PKG_VARS_TO_EXPORT "")
endmacro()


# @MACRO: tribits_pkg_export_cache_var()
#
# Macro that registers a package-level cache var to be exported in the
# ``<Package>Config.cmake`` file
#
# Usage::
#
# tribits_pkg_export_cache_var(<cacheVarName>)
#
# where ``<cacheVarName>`` must be the name of a cache variable (or an error
# will occur).
#
# NOTE: This will also export this variable to the
# ``<Package><Spkg>Config.cmake`` file for every enabled subpackage (if this
# is called from a ``CMakeLists.txt`` file of a top-level package that has
# subpackages). That way, any top-level package cache vars are provided by
# any of the subpackages' ``<Package><Spkg>Config.cmake`` files.
#
macro(tribits_pkg_export_cache_var cacheVarName)
if (DEFINED ${PACKAGE_NAME}_PKG_VARS_TO_EXPORT)
# Assert this is a cache var
get_property(cacheVarIsCacheVar CACHE ${cacheVarName} PROPERTY VALUE SET)
if (NOT cacheVarIsCacheVar)
message(SEND_ERROR
"ERROR: The variable ${cacheVarName} is NOT a cache var and cannot"
" be exported!")
endif()
# Add to the list of package cache vars to export
append_global_set(${PACKAGE_NAME}_PKG_VARS_TO_EXPORT
${cacheVarName})
endif()
endmacro()


# Macro that defines variables that create global targets
#
macro(tribits_define_target_vars PARENT_PACKAGE_NAME_IN)
Expand Down
130 changes: 130 additions & 0 deletions tribits/core/package_arch/TribitsPkgExportCacheVars.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# @HEADER
# ************************************************************************
#
# TriBITS: Tribal Build, Integrate, and Test System
# Copyright 2013 Sandia Corporation
#
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the Corporation nor the names of the
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ************************************************************************
# @HEADER


# @MACRO: tribits_pkg_export_cache_var()
#
# Macro that registers a package-level cache var to be exported in the
# ``<Package>Config.cmake`` file
#
# Usage::
#
# tribits_pkg_export_cache_var(<cacheVarName>)
#
# where ``<cacheVarName>`` must be the name of a cache variable (or an error
# will occur).
#
# NOTE: This will also export this variable to the
# ``<Package><Spkg>Config.cmake`` file for every enabled subpackage (if this
# is called from a ``CMakeLists.txt`` file of a top-level package that has
# subpackages). That way, any top-level package cache vars are provided by
# any of the subpackages' ``<Package><Spkg>Config.cmake`` files.
#
macro(tribits_pkg_export_cache_var cacheVarName)
if (DEFINED ${PACKAGE_NAME}_PKG_VARS_TO_EXPORT)
# Assert this is a cache var
get_property(cacheVarIsCacheVar CACHE ${cacheVarName} PROPERTY VALUE SET)
if (NOT cacheVarIsCacheVar)
message(SEND_ERROR
"ERROR: The variable ${cacheVarName} is NOT a cache var and cannot"
" be exported!")
endif()
# Add to the list of package cache vars to export
append_global_set(${PACKAGE_NAME}_PKG_VARS_TO_EXPORT
${cacheVarName})
endif()
endmacro()


# @MACRO: tribits_assert_cache_and_local_vars_same_value()
#
# Asset that a cache variable and a possible local variable (if it exists)
# have the same value.
#
# Usage::
#
# tribits_assert_cache_and_local_vars_same_value(<cacheVarName>)
#
# If the local var ``<cacheVarName>`` and the cache var ``<cacheVarName>``
# both exist but have different values, then ``message(SEND_ERROR ...)`` is
# called with an informative error message.
#
macro(tribits_assert_cache_and_local_vars_same_value cacheVarName)
set(cacheVarValue "$CACHE{${cacheVarName}}")
set(localValue "${${cacheVarName}}")
if (NOT localValue STREQUAL cacheVarValue)
message_wrapper(SEND_ERROR "ERROR: The cache variable ${cacheVarName} with the"
" cache var value '${cacheVarValue}' is not the same value as the local"
" variable ${cacheVarName} with value '${localValue}'!")
endif()
endmacro()


# Function that sets up data-structures for package-level cache var to be
# exported
#
function(tribits_pkg_init_exported_vars PACKAGE_NAME_IN)
global_set(${PACKAGE_NAME_IN}_PKG_VARS_TO_EXPORT "")
endfunction()


# Function that injects set() statements for a package's exported cache vars into
# a string.
#
# This is used to create set() statements to be injected into a package's
# ``<Package>Config.cmake`` file.
#
function(tribits_pkg_append_set_commands_for_exported_vars packageName
configFileStrInOut
)
set(configFileStr "${${configFileStrInOut}}")
if (NOT "${${packageName}_PARENT_PACKAGE}" STREQUAL "")
foreach(exportedCacheVar IN LISTS ${${packageName}_PARENT_PACKAGE}_PKG_VARS_TO_EXPORT)
tribits_assert_cache_and_local_vars_same_value(${exportedCacheVar})
string(APPEND configFileStr
"set(${exportedCacheVar} \"${${exportedCacheVar}}\")\n")
endforeach()
endif()
foreach(exportedCacheVar IN LISTS ${packageName}_PKG_VARS_TO_EXPORT)
tribits_assert_cache_and_local_vars_same_value(${exportedCacheVar})
string(APPEND configFileStr
"set(${exportedCacheVar} \"${${exportedCacheVar}}\")\n")
endforeach()
set(${configFileStrInOut} "${configFileStr}" PARENT_SCOPE)
endfunction()
14 changes: 2 additions & 12 deletions tribits/core/package_arch/TribitsWriteClientExportFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
# @HEADER

include(TribitsGeneralMacros)
include(TribitsPkgExportCacheVars)

###
### WARNING: See "NOTES TO DEVELOPERS" at the bottom of the file
Expand Down Expand Up @@ -579,18 +580,7 @@ function(tribits_append_dependent_package_config_file_includes_and_enables packa
# Put in set() statements for exported cache vars
string(APPEND configFileStr
"\n# Exported cache variables\n")
if (NOT "${${packageName}_PARENT_PACKAGE}" STREQUAL "")
foreach(exportedCacheVar IN LISTS ${${packageName}_PARENT_PACKAGE}_PKG_VARS_TO_EXPORT)
tribits_assert_cache_and_local_vars_same_value(${exportedCacheVar})
string(APPEND configFileStr
"set(${exportedCacheVar} \"${${exportedCacheVar}}\")\n")
endforeach()
endif()
foreach(exportedCacheVar IN LISTS ${packageName}_PKG_VARS_TO_EXPORT)
tribits_assert_cache_and_local_vars_same_value(${exportedCacheVar})
string(APPEND configFileStr
"set(${exportedCacheVar} \"${${exportedCacheVar}}\")\n")
endforeach()
tribits_pkg_append_set_commands_for_exported_vars(${packageName} configFileStr)

# Include configurations of dependent packages
string(APPEND configFileStr
Expand Down

0 comments on commit 4ab6cae

Please sign in to comment.