From 181ece4d6b879e0dfcff2ba462b70dd0ef9eda92 Mon Sep 17 00:00:00 2001 From: "Roscoe A. Bartlett" Date: Fri, 19 Aug 2022 20:24:20 -0600 Subject: [PATCH] Factor out code that checks for call context (#200) That code just clutters up the main macros() and makes them hard to reason about and maintain. --- .../package_arch/TribitsPackageMacros.cmake | 95 +++++++++++-------- .../TribitsSubPackageMacros.cmake | 18 ++-- 2 files changed, 65 insertions(+), 48 deletions(-) diff --git a/tribits/core/package_arch/TribitsPackageMacros.cmake b/tribits/core/package_arch/TribitsPackageMacros.cmake index de132128c..68f2f5937 100644 --- a/tribits/core/package_arch/TribitsPackageMacros.cmake +++ b/tribits/core/package_arch/TribitsPackageMacros.cmake @@ -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 @@ -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 `/CMakeLists.txt`_ after subpackages are @@ -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) @@ -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() @@ -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) @@ -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() diff --git a/tribits/core/package_arch/TribitsSubPackageMacros.cmake b/tribits/core/package_arch/TribitsSubPackageMacros.cmake index 316063528..4db15ba65 100644 --- a/tribits/core/package_arch/TribitsSubPackageMacros.cmake +++ b/tribits/core/package_arch/TribitsSubPackageMacros.cmake @@ -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 @@ -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( @@ -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()