Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for exporting package info vars into <Package>Config.cmake files (#516) #520

Merged
17 changes: 9 additions & 8 deletions test/core/ExamplesUnitTests/TribitsExampleApp_Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ function(TribitsExampleApp_NoFortran fullOrComponents sharedOrStatic)
MESSAGE "Copy source for TribitsExampleProject"
CMND ${CMAKE_COMMAND}
ARGS -E copy_directory
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject .
WORKING_DIRECTORY TribitsExampleProject
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject
TribitsExampleProject

TEST_1
MESSAGE "Do the configure of TribitsExampleProject"
Expand Down Expand Up @@ -344,6 +344,7 @@ function(TribitsExampleApp_NoFortran fullOrComponents sharedOrStatic)
${tribitsExProjUseComponentsArg}
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleApp
PASS_REGULAR_EXPRESSION_ALL
"WithSubpackagesA_SPECIAL_VALUE = '3'"
"-- Configuring done"
"-- Generating done"
"-- Build files have been written to: .*/${testName}/app_build"
Expand Down Expand Up @@ -428,8 +429,8 @@ function(TribitsExampleApp_EnableSingleSubpackage fullOrComponents sharedOrStati
MESSAGE "Copy source for TribitsExampleProject"
CMND ${CMAKE_COMMAND}
ARGS -E copy_directory
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject .
WORKING_DIRECTORY TribitsExampleProject
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject
TribitsExampleProject

TEST_1
MESSAGE "Make Withsubpackages OPTIONAL subpackages REQUIRED"
Expand Down Expand Up @@ -567,8 +568,8 @@ function(TribitsExampleApp_ALL_ST byProjectOrPackage sharedOrStatic serialOrM
MESSAGE "Copy source for TribitsExampleProject"
CMND ${CMAKE_COMMAND}
ARGS -E copy_directory
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject .
WORKING_DIRECTORY TribitsExampleProject
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject
TribitsExampleProject

TEST_1
MESSAGE "Do the configure of TribitsExampleProject"
Expand Down Expand Up @@ -963,8 +964,8 @@ function(TribitsExampleApp_ALL_ST_tpl_link_options byProjectOrPackage sharedOrSt
MESSAGE "Copy source for TribitsExampleProject"
CMND ${CMAKE_COMMAND}
ARGS -E copy_directory
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject .
WORKING_DIRECTORY TribitsExampleProject
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject
TribitsExampleProject

TEST_1
MESSAGE "Write configuration fragment file to deal with semi-colon problem"
Expand Down
26 changes: 19 additions & 7 deletions test/core/ExamplesUnitTests/TribitsExampleProject_Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function(TribitsExampleProject_ALL_ST_NoFortran sharedOrStatic serialOrMpi)
-DTribitsExProj_DUMP_CPACK_SOURCE_IGNORE_FILES=ON
-DTribitsExProj_DUMP_PACKAGE_DEPENDENCIES=ON
-DTribitsExProj_ENABLE_INSTALL_CMAKE_CONFIG_FILES=ON
-DWithSubpackagesA_SPECIAL_VALUE=5
-DCMAKE_CXX_FLAGS=-DSIMPLECXX_SHOW_DEPRECATED_WARNINGS=1
-DCMAKE_INSTALL_PREFIX=install
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject
Expand Down Expand Up @@ -310,6 +311,17 @@ function(TribitsExampleProject_ALL_ST_NoFortran sharedOrStatic serialOrMpi)
ALWAYS_FAIL_ON_NONZERO_RETURN

TEST_5
MESSAGE "Grep the contents of the WithSubpackagesAConfig.cmake file in build dir"
CMND cat ARGS cmake_packages/WithSubpackagesA/WithSubpackagesAConfig.cmake
PASS_REGULAR_EXPRESSION_ALL
"# Enables/Disables for upstream package dependencies"
"set[(]WithSubpackagesA_ENABLE_SimpleCxx ON[)]"
"# Exported cache variables"
"set[(]WithSubpackages_ENABLE_DEBUG .OFF.[)]"
"set[(]HAVE_WITHSUBPACKAGES_DEBUG .OFF.[)]"
"set[(]WithSubpackagesA_SPECIAL_VALUE .5.[)]"

TEST_6
MESSAGE "Build 'install' target using raw 'make'"
CMND make ARGS install ${CTEST_BUILD_FLAGS}
PASS_REGULAR_EXPRESSION_ALL
Expand Down Expand Up @@ -343,7 +355,7 @@ function(TribitsExampleProject_ALL_ST_NoFortran sharedOrStatic serialOrMpi)
"Installing: .+/install/lib/cmake/WithSubpackagesC/WithSubpackagesCTargets-release.cmake"
ALWAYS_FAIL_ON_NONZERO_RETURN

TEST_6
TEST_7
MESSAGE "Create and configure a dummy project that calls"
" find_package(WithSubpackages) from the install tree"
CMND ${CMAKE_COMMAND}
Expand Down Expand Up @@ -390,7 +402,7 @@ function(TribitsExampleProject_ALL_ST_NoFortran sharedOrStatic serialOrMpi)
"WithSubpackages::all_libs INTERFACE_LINK_LIBRARIES: 'WithSubpackagesA::pws_a[;]WithSubpackagesB::pws_b[;]WithSubpackagesC::pws_c'"
ALWAYS_FAIL_ON_NONZERO_RETURN

TEST_7
TEST_8
MESSAGE "Create and configure a dummy project that calls"
" find_package(TribitsExProj) with no components"
CMND ${CMAKE_COMMAND}
Expand Down Expand Up @@ -448,7 +460,7 @@ function(TribitsExampleProject_ALL_ST_NoFortran sharedOrStatic serialOrMpi)
"-- Generating done"
ALWAYS_FAIL_ON_NONZERO_RETURN

TEST_8
TEST_9
MESSAGE "Create and configure a dummy project that calls find_package(TribitsExProj)"
" with required and optional components"
CMND ${CMAKE_COMMAND}
Expand Down Expand Up @@ -478,7 +490,7 @@ function(TribitsExampleProject_ALL_ST_NoFortran sharedOrStatic serialOrMpi)
"-- Generating done"
ALWAYS_FAIL_ON_NONZERO_RETURN

TEST_9
TEST_10
MESSAGE "Run find_package() from two different subdirs with related packages"
WORKING_DIRECTORY find_package_two_dirs
CMND ${CMAKE_COMMAND}
Expand All @@ -492,7 +504,7 @@ function(TribitsExampleProject_ALL_ST_NoFortran sharedOrStatic serialOrMpi)
"-- Generating done"
ALWAYS_FAIL_ON_NONZERO_RETURN

TEST_10
TEST_11
MESSAGE "Create the tarball"
CMND make ARGS package_source
PASS_REGULAR_EXPRESSION_ALL
Expand All @@ -509,12 +521,12 @@ function(TribitsExampleProject_ALL_ST_NoFortran sharedOrStatic serialOrMpi)
"CPack: - package: .*/ExamplesUnitTests/${testName}/tribitsexproj-1.1-Source.tar.bz2 generated."
ALWAYS_FAIL_ON_NONZERO_RETURN

TEST_11
TEST_12
MESSAGE "Untar the tarball"
CMND tar ARGS -xzf tribitsexproj-1.1-Source.tar.gz
ALWAYS_FAIL_ON_NONZERO_RETURN

TEST_12
TEST_13
MESSAGE "Make sure right directories are excluded"
CMND diff
ARGS -qr
Expand Down
6 changes: 6 additions & 0 deletions tribits/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
ChangeLog for TriBITS
----------------------------------------

## 2022-08-22:

* **Added:** Added support for exporting cache variables for packages in their
`<Package>Config.cmake` files using the new function
`tribits_pkg_export_cache_var()`.

## 2022-08-18:

* **Changed:** Made setting parent package tests/examples enable/disable
Expand Down
7 changes: 7 additions & 0 deletions tribits/core/package_arch/TribitsAddOptionAndDefine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ include(GlobalSet)
#
# #cmakedefine <macroDefineName>
#
# NOTE: This also calls `tribits_pkg_export_cache_var()`_ to export the
# variables ``<userOptionName>`` and ``<macroDefineName>``.
#
macro(tribits_add_option_and_define USER_OPTION_NAME MACRO_DEFINE_NAME
DOCSTRING DEFAULT_VALUE
)
Expand All @@ -70,6 +73,10 @@ 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(${MACRO_DEFINE_NAME})
endif()
endmacro()

# 2008/10/05: rabartl: ToDo: Add an option to automatically add the macro
Expand Down
140 changes: 102 additions & 38 deletions tribits/core/package_arch/TribitsPackageMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,47 @@ 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 Expand Up @@ -180,25 +221,7 @@ macro(tribits_package_decl PACKAGE_NAME_IN)
message("\nTRIBITS_PACKAGE_DECL: ${PACKAGE_NAME_IN}")
endif()

if (CURRENTLY_PROCESSING_SUBPACKAGE)
tribits_report_invalid_tribits_usage(
"Cannot call tribits_package_decl() in a subpackage."
" Use tribits_subpackage() instead"
" error in ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
endif()

if(${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED)
tribits_report_invalid_tribits_usage(
"tribits_package_decl() called more than once in Package ${PACKAGE_NAME}"
" This may be because tribits_package_decl() was explicitly called more than once or"
" TRIBITS_PACKAGE_DECL was called after TRIBITS_PACKAGE. You do not need both."
" If your package has subpackages then do not call tribits_package() instead call:"
" tribits_pacakge_decl() then tribits_process_subpackages() then tribits package_def()"
)
endif()

# Set flag to check that macros are called in the correct order
set(${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED TRUE)
tribits_package_decl_assert_call_context()

#
# A) Parse the input arguments
Expand Down Expand Up @@ -236,6 +259,7 @@ macro(tribits_package_decl PACKAGE_NAME_IN)
#

tribits_set_common_vars(${PACKAGE_NAME_IN})
tribits_pkg_init_exported_vars(${PACKAGE_NAME_IN})

set(${PACKAGE_NAME_IN}_DISABLE_STRONG_WARNINGS OFF
CACHE BOOL
Expand Down Expand Up @@ -268,6 +292,31 @@ macro(tribits_package_decl PACKAGE_NAME_IN)
endmacro()


macro(tribits_package_decl_assert_call_context)

if (CURRENTLY_PROCESSING_SUBPACKAGE)
tribits_report_invalid_tribits_usage(
"Cannot call tribits_package_decl() in a subpackage."
" Use tribits_subpackage() instead"
" error in ${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
endif()

if(${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED)
tribits_report_invalid_tribits_usage(
"tribits_package_decl() called more than once in Package ${PACKAGE_NAME}"
" This may be because tribits_package_decl() was explicitly called more than once or"
" TRIBITS_PACKAGE_DECL was called after TRIBITS_PACKAGE. You do not need both."
" If your package has subpackages then do not call tribits_package() instead call:"
" tribits_pacakge_decl() then tribits_process_subpackages() then tribits package_def()"
)
endif()

# Set flag to check that macros are called in the correct order
set(${PACKAGE_NAME}_TRIBITS_PACKAGE_DECL_CALLED TRUE)

endmacro()


# @MACRO: tribits_package_def()
#
# Macro called in `<packageDir>/CMakeLists.txt`_ after subpackages are
Expand All @@ -291,6 +340,30 @@ endmacro()
#
macro(tribits_package_def)

if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
message("\nTRIBITS_PACKAGE_DEF: ${PACKAGE_NAME}")
endif()

tribits_package_def_assert_call_context()

if (NOT ${PROJECT_NAME}_ENABLE_${PACKAGE_NAME})
if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
message("\n${PACKAGE_NAME} not enabled so exiting package processing")
endif()
return()
endif()

# Reset in case were changed by subpackages
tribits_set_common_vars(${PACKAGE_NAME})

# Define package linkage variables
tribits_define_linkage_vars(${PACKAGE_NAME})

endmacro()


macro(tribits_package_def_assert_call_context)

# check that this is not being called from a subpackage
if(NOT ${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED)
if (CURRENTLY_PROCESSING_SUBPACKAGE)
Expand All @@ -311,23 +384,6 @@ macro(tribits_package_def)
"${CURRENT_SUBPACKAGE_CMAKELIST_FILE}")
endif()

if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
message("\nTRIBITS_PACKAGE_DEF: ${PACKAGE_NAME}")
endif()

if (NOT ${PROJECT_NAME}_ENABLE_${PACKAGE_NAME})
if (${PROJECT_NAME}_VERBOSE_CONFIGURE)
message("\n${PACKAGE_NAME} not enabled so exiting package processing")
endif()
return()
endif()

# Reset in case were changed by subpackages
tribits_set_common_vars(${PACKAGE_NAME})

# Define package linkage variables
tribits_define_linkage_vars(${PACKAGE_NAME})

set(${PACKAGE_NAME}_TRIBITS_PACKAGE_DEF_CALLED TRUE)

endmacro()
Expand All @@ -353,6 +409,13 @@ endmacro()
# side-effects (and variables set) after calling this macro.
#
macro(tribits_package PACKAGE_NAME_IN)
tribits_package_assert_call_context()
tribits_package_decl(${PACKAGE_NAME_IN} ${ARGN})
tribits_package_def()
endmacro()


macro(tribits_package_assert_call_context)

if (CURRENTLY_PROCESSING_SUBPACKAGE)
if (NOT ${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED)
Expand Down Expand Up @@ -381,8 +444,6 @@ macro(tribits_package PACKAGE_NAME_IN)

set(${PACKAGE_NAME}_TRIBITS_PACKAGE_CALLED TRUE)

tribits_package_decl(${PACKAGE_NAME_IN} ${ARGN})
tribits_package_def()
endmacro()


Expand Down Expand Up @@ -440,6 +501,9 @@ endmacro()
# typically called in the package's `<packageDir>/CMakeLists.txt`_ file (see
# the example ``SimpleCxx/CMakeLists.txt``).
#
# NOTE: This also calls `tribits_pkg_export_cache_var()`_ to export the
# variable ``${PACKAGE_NAME}_ENABLE_DEBUG``.
#
macro(tribits_add_debug_option)
tribits_add_option_and_define(
${PACKAGE_NAME}_ENABLE_DEBUG
Expand Down
Loading