Skip to content

Commit

Permalink
Factor out code that checks for call context (TriBITSPub#200)
Browse files Browse the repository at this point in the history
That code just clutters up the main macros() and makes them hard to reason
about and maintain.
  • Loading branch information
bartlettroscoe committed Aug 22, 2022
1 parent 3559a18 commit 181ece4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 48 deletions.
95 changes: 57 additions & 38 deletions tribits/core/package_arch/TribitsPackageMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,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 @@ -268,6 +250,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 +298,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 +342,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 +367,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 +402,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
18 changes: 8 additions & 10 deletions tribits/core/package_arch/TribitsSubPackageMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ function(tribits_subpackage_assert_call_context)
endfunction()



# @MACRO: tribits_subpackage_postprocess()
#
# Macro that performs standard post-processing after defining a `TriBITS
Expand All @@ -158,20 +157,22 @@ endfunction()
# this macro but limitations of the CMake language make it necessary to do so.
#
macro(tribits_subpackage_postprocess)
tribits_subpackage_postprocess_assert_call_context()
tribits_package_postprocess_common()
endmacro()

# check that this is not being called from a package
if (NOT CURRENTLY_PROCESSING_SUBPACKAGE)

# This is being called from a package
macro(tribits_subpackage_postprocess_assert_call_context)

# check that this is not being called from a package
if (NOT CURRENTLY_PROCESSING_SUBPACKAGE)
# This is being called from a package
tribits_report_invalid_tribits_usage(
"Cannot call tribits_subpackage_postprocess() from a package."
" Use tribits_package_postprocess() instead"
" ${CURRENT_PACKAGE_CMAKELIST_FILE}")

else()
# This is being caleld from a subpackage

# This is being caleld from a subpackage
# check to make sure this has not already been called
if (${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED)
tribits_report_invalid_tribits_usage(
Expand All @@ -185,12 +186,9 @@ macro(tribits_subpackage_postprocess)
"tribits_subpackage() must be called before tribits_subpackage_postprocess()"
" for the ${PARENT_PACKAGE_NAME} subpackage ${TRIBITS_SUBPACKAGE}")
endif()

endif()

# Set flags that are used to check that macros are called in the correct order
dual_scope_set(${SUBPACKAGE_FULLNAME}_TRIBITS_SUBPACKAGE_POSTPROCESS_CALLED TRUE)

tribits_package_postprocess_common()

endmacro()

0 comments on commit 181ece4

Please sign in to comment.