From 8e00e0bdc23096eb137b4636a2d6695a9073ab56 Mon Sep 17 00:00:00 2001 From: Dan Ibanez Date: Thu, 20 Oct 2022 21:59:46 -0600 Subject: [PATCH 01/10] add REQUIRED_HEADERS for TriBITS MPI TPL this is required to fix TriBITSPub/TriBITS#534 --- tribits/core/std_tpls/FindTPLMPI.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tribits/core/std_tpls/FindTPLMPI.cmake b/tribits/core/std_tpls/FindTPLMPI.cmake index f4917fd6a..f8483f178 100644 --- a/tribits/core/std_tpls/FindTPLMPI.cmake +++ b/tribits/core/std_tpls/FindTPLMPI.cmake @@ -50,7 +50,9 @@ if(WIN32 AND TPL_ENABLE_MPI) global_set(TPL_MPI_LIBRARIES ${MPI_LIBRARIES}) endif() -tribits_tpl_find_include_dirs_and_libraries(MPI) +tribits_tpl_find_include_dirs_and_libraries( + MPI + REQUIRED_HEADERS mpi.h) # NOTE: Above, we need to generate the MPI::all_libs target and the # MPIConfig.cmake file that will also provide the MPI::all_libs target. From 59617e7224674afc386aa0862ab9dc105d03f7c4 Mon Sep 17 00:00:00 2001 From: Dan Ibanez Date: Thu, 20 Oct 2022 22:03:18 -0600 Subject: [PATCH 02/10] Netcdf_ENABLE_MODERN mode for TriBITS Netcdf TPL when this CMake cache variable is set to a true value, the TriBITS Netcdf TPL code will only look for externally installed CMake config files for CMake packages called netCDF and possibly hdf5. Such files are installed by the latest versions of NetCDF and HDF5. This effectively allows loading NetCDF and its HDF5 dependency using the most modern CMake approach. This is needed to resolve TriBITSPub/TriBITS#533 --- tribits/common_tpls/FindTPLNetcdf.cmake | 228 +++++++++++++++--------- 1 file changed, 144 insertions(+), 84 deletions(-) diff --git a/tribits/common_tpls/FindTPLNetcdf.cmake b/tribits/common_tpls/FindTPLNetcdf.cmake index 13730f045..15cc11e45 100644 --- a/tribits/common_tpls/FindTPLNetcdf.cmake +++ b/tribits/common_tpls/FindTPLNetcdf.cmake @@ -44,98 +44,158 @@ # in order to trigger the right behavior in the function # tribits_tpl_find_include_dirs_and_libraries(). # - -set(REQUIRED_HEADERS netcdf.h) -set(REQUIRED_LIBS_NAMES netcdf) - -if (TPL_ENABLE_MPI) - set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} pnetcdf) +if (${CMAKE_VERSION} GREATER "3.13") + cmake_policy(SET CMP0074 NEW) endif() -# -# Second, search for Netcdf components (if allowed) using the standard -# find_package(NetCDF ...). -# -tribits_tpl_allow_pre_find_package(Netcdf Netcdf_ALLOW_PREFIND) -if (Netcdf_ALLOW_PREFIND) - - message("-- Using find_package(Netcdf ...) ...") - - set(CMAKE_MODULE_PATH - "${CMAKE_MODULE_PATH}" - "${CMAKE_CURRENT_LIST_DIR}/find_modules" - "${CMAKE_CURRENT_LIST_DIR}/utils" - ) +set(Netcdf_ALLOW_MODERN FALSE CACHE BOOL "Allow finding Netcdf as a modern CMake config file with exported targets (and only this way)") - find_package(NetCDF) +if (Netcdf_ALLOW_MODERN) - if (NetCDF_FOUND) - set(DOCSTR "List of semi-colon separated paths to look for the TPL Netcdf") - set(TPL_Netcdf_Enables_Netcdf4 ${NetCDF_NEEDS_HDF5} CACHE BOOL + set(minimum_modern_netCDF_version 4.7.4) + message("-- Netcdf_ALLOW_MODERN=${Netcdf_ALLOW_MODERN}") + message("-- Using find_package(netCDF ${minimum_modern_netCDF_version} CONFIG) ...") + find_package(netCDF ${minimum_modern_netCDF_version} CONFIG) + if (netCDF_FOUND) + message("-- Found netCDF_CONFIG=${netCDF_CONFIG}") + message("-- Generating Netcdf::all_libs and NetcdfConfig.cmake") + # instead of using tribits_extpkg_create_imported_all_libs_target_and_config_file, + # we will do what it does ourselves because we need to bring in two + # inner find packages (netCDF and hdf5) because netCDF does not properly + # do find_dependency(hdf5) + add_library(Netcdf::all_libs INTERFACE IMPORTED GLOBAL) + target_link_libraries(Netcdf::all_libs INTERFACE netCDF::netcdf) + set(configFileStr "") + string(APPEND configFileStr + "include(CMakeFindDependencyMacro)\n" ) + string(APPEND configFileStr + "set(netCDF_DIR \"${netCDF_DIR}\")\n" ) + string(APPEND configFileStr + "find_dependency(netCDF ${minimum_modern_netCDF_version} CONFIG)\n" + ) + if (netCDF_HAS_HDF5) + find_package(hdf5 CONFIG REQUIRED) + string(APPEND configFileStr + "set(hdf5_DIR \"${hdf5_DIR}\")\n" ) + string(APPEND configFileStr + "find_dependency(hdf5 CONFIG)\n" + ) + endif() + string(APPEND configFileStr + "add_library(Netcdf::all_libs INTERFACE IMPORTED GLOBAL)\n" + ) + string(APPEND configFileStr + "target_link_libraries(Netcdf::all_libs INTERFACE netCDF::netcdf)\n") + set(buildDirExternalPkgsDir + "${${PROJECT_NAME}_BINARY_DIR}/${${PROJECT_NAME}_BUILD_DIR_EXTERNAL_PKGS_DIR}") + set(tplConfigFile + "${buildDirExternalPkgsDir}/Netcdf/NetcdfConfig.cmake") + file(WRITE "${tplConfigFile}" "${configFileStr}") + # the following gets set by tribits_tpl_find_include_dirs_and_libraries + # and it is not well-documented that you need to set it yourself if not using + # tribits_tpl_find_include_dirs_and_libraries + set(TPL_Netcdf_NOT_FOUND FALSE) + message("-- TPL_Netcdf_PARALLEL = netCDF_HAS_PARALLEL = ${netCDF_HAS_PARALLEL}") + set(TPL_Netcdf_PARALLEL ${netCDF_HAS_PARALLEL}) + message("-- TPL_Netcdf_Enables_Netcdf4 = netCDF_HAS_NC4 = ${netCDF_HAS_NC4}") + set(TPL_Netcdf_Enables_Netcdf4 ${netCDF_HAS_NC4} CACHE BOOL "True if netcdf enables netcdf-4") - set(TPL_Netcdf_Enables_PNetcdf ${NetCDF_NEEDS_PNetCDF} CACHE BOOL - "True if netcdf enables pnetcdf") - set(TPL_Netcdf_PARALLEL ${NetCDF_PARALLEL} CACHE INTERNAL - "True if netcdf compiled with parallel enabled") - set(TPL_Netcdf_LIBRARY_DIRS ${_hdf5_LIBRARY_SEARCH_DIRS} CACHE PATH - "${DOCSTR} library files") - set(TPL_Netcdf_LIBRARIES ${NetCDF_LIBRARIES} CACHE PATH - "List of semi-colon separated library names (not 'lib' or extension).") - set(TPL_Netcdf_INCLUDE_DIRS ${NetCDF_INCLUDE_DIRS} CACHE PATH - "${DOCSTR} header files.") endif() + else() - # Curl library is only required if DAP is enabled; should detect inside - # FindNetCDF.cmake, but that is not being called... SEMS has DAP enabled; - # many HPC systems don't, but they override these settings... - find_program(NC_CONFIG "nc-config") - if (NC_CONFIG) - execute_process(COMMAND "nc-config --has-dap2" - OUTPUT_VARIABLE NETCDF_HAS_DAP2) - execute_process(COMMAND "nc-config --has-dap4" - OUTPUT_VARIABLE NETCDF_HAS_DAP4) + + set(REQUIRED_HEADERS netcdf.h) + set(REQUIRED_LIBS_NAMES netcdf) + + if (TPL_ENABLE_MPI) + set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} pnetcdf) endif() - if ((NOT NC_CONFIG) OR NETCDF_HAS_DAP2 OR NETCDF_HAS_DAP4) - set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} curl) + + # + # Second, search for Netcdf components (if allowed) using the standard + # find_package(NetCDF ...). + # + tribits_tpl_allow_pre_find_package(Netcdf Netcdf_ALLOW_PREFIND) + if (Netcdf_ALLOW_PREFIND) + + message("-- Using find_package(Netcdf ...) ...") + + set(CMAKE_MODULE_PATH + "${CMAKE_MODULE_PATH}" + "${CMAKE_CURRENT_LIST_DIR}/find_modules" + "${CMAKE_CURRENT_LIST_DIR}/utils" + ) + + find_package(NetCDF) + + if (NetCDF_FOUND) + set(DOCSTR "List of semi-colon separated paths to look for the TPL Netcdf") + set(TPL_Netcdf_Enables_Netcdf4 ${NetCDF_NEEDS_HDF5} CACHE BOOL + "True if netcdf enables netcdf-4") + set(TPL_Netcdf_Enables_PNetcdf ${NetCDF_NEEDS_PNetCDF} CACHE BOOL + "True if netcdf enables pnetcdf") + set(TPL_Netcdf_PARALLEL ${NetCDF_PARALLEL} CACHE INTERNAL + "True if netcdf compiled with parallel enabled") + set(TPL_Netcdf_LIBRARY_DIRS ${_hdf5_LIBRARY_SEARCH_DIRS} CACHE PATH + "${DOCSTR} library files") + set(TPL_Netcdf_LIBRARIES ${NetCDF_LIBRARIES} CACHE PATH + "List of semi-colon separated library names (not 'lib' or extension).") + set(TPL_Netcdf_INCLUDE_DIRS ${NetCDF_INCLUDE_DIRS} CACHE PATH + "${DOCSTR} header files.") + endif() + else() + # Curl library is only required if DAP is enabled; should detect inside + # FindNetCDF.cmake, but that is not being called... SEMS has DAP enabled; + # many HPC systems don't, but they override these settings... + find_program(NC_CONFIG "nc-config") + if (NC_CONFIG) + execute_process(COMMAND "nc-config --has-dap2" + OUTPUT_VARIABLE NETCDF_HAS_DAP2) + execute_process(COMMAND "nc-config --has-dap4" + OUTPUT_VARIABLE NETCDF_HAS_DAP4) + endif() + if ((NOT NC_CONFIG) OR NETCDF_HAS_DAP2 OR NETCDF_HAS_DAP4) + set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} curl) + endif() endif() -endif() -# -# Third, call tribits_tpl_find_include_dirs_and_libraries() -# -tribits_tpl_find_include_dirs_and_libraries( Netcdf - REQUIRED_HEADERS ${REQUIRED_HEADERS} - REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} - ) -# NOTE: If find_package(Netcdf ...) was called and successfully found Netcdf, -# then tribits_tpl_find_include_dirs_and_libraries() will use the already-set -# variables TPL_Netcdf_INCLUDE_DIRS and TPL_Netcdf_LIBRARIES and then print -# them out (and set some other standard variables as well). This is the final -# "hook" into the TriBITS TPL system. - -# If the `find_package(NetCDF)` is not run, then this may not be set -# Need to determine how this is set in the library that is being used... - -if ("${TPL_Netcdf_PARALLEL}" STREQUAL "") - assert_defined(TPL_Netcdf_INCLUDE_DIRS) - find_path(meta_path - NAMES "netcdf_meta.h" - HINTS ${TPL_Netcdf_INCLUDE_DIRS} - NO_DEFAULT_PATH) - - if (meta_path) - # Search meta for NC_HAS_PARALLEL setting... - # Note that there is both NC_HAS_PARALLEL and NC_HAS_PARALLEL4, only want first... - file(STRINGS "${meta_path}/netcdf_meta.h" netcdf_par_string REGEX "NC_HAS_PARALLEL ") - string(REGEX MATCH "[01]" netcdf_par_val "${netcdf_par_string}") - if (netcdf_par_val EQUAL 1) - set(TPL_Netcdf_PARALLEL True CACHE INTERNAL - "True if netcdf compiled with parallel enabled") - endif() - endif() - if ("${TPL_Netcdf_PARALLEL}" STREQUAL "") - set(TPL_Netcdf_PARALLEL False CACHE INTERNAL - "True if netcdf compiled with parallel enabled") - endif() + # + # Third, call tribits_tpl_find_include_dirs_and_libraries() + # + tribits_tpl_find_include_dirs_and_libraries( Netcdf + REQUIRED_HEADERS ${REQUIRED_HEADERS} + REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} + ) + # NOTE: If find_package(Netcdf ...) was called and successfully found Netcdf, + # then tribits_tpl_find_include_dirs_and_libraries() will use the already-set + # variables TPL_Netcdf_INCLUDE_DIRS and TPL_Netcdf_LIBRARIES and then print + # them out (and set some other standard variables as well). This is the final + # "hook" into the TriBITS TPL system. + + # If the `find_package(NetCDF)` is not run, then this may not be set + # Need to determine how this is set in the library that is being used... + + if ("${TPL_Netcdf_PARALLEL}" STREQUAL "") + assert_defined(TPL_Netcdf_INCLUDE_DIRS) + find_path(meta_path + NAMES "netcdf_meta.h" + HINTS ${TPL_Netcdf_INCLUDE_DIRS} + NO_DEFAULT_PATH) + + if (meta_path) + # Search meta for NC_HAS_PARALLEL setting... + # Note that there is both NC_HAS_PARALLEL and NC_HAS_PARALLEL4, only want first... + file(STRINGS "${meta_path}/netcdf_meta.h" netcdf_par_string REGEX "NC_HAS_PARALLEL ") + string(REGEX MATCH "[01]" netcdf_par_val "${netcdf_par_string}") + if (netcdf_par_val EQUAL 1) + set(TPL_Netcdf_PARALLEL True CACHE INTERNAL + "True if netcdf compiled with parallel enabled") + endif() + endif() + if ("${TPL_Netcdf_PARALLEL}" STREQUAL "") + set(TPL_Netcdf_PARALLEL False CACHE INTERNAL + "True if netcdf compiled with parallel enabled") + endif() + endif() + message(STATUS "TPL_Netcdf_PARALLEL is ${TPL_Netcdf_PARALLEL}") endif() -message(STATUS "TPL_Netcdf_PARALLEL is ${TPL_Netcdf_PARALLEL}") From 7fd57e5d254a8f2769a4b6732776178515483f72 Mon Sep 17 00:00:00 2001 From: "Roscoe A. Bartlett" Date: Sun, 23 Oct 2022 12:17:45 -0600 Subject: [PATCH 03/10] Don't wipe out TPL__INCLUDE_DIRS if set by user (TriBITSPub/TriBITS#534) See TriBITSPub/TriBITS#534 --- .../TribitsTplFindIncludeDirsAndLibraries.cmake | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tribits/core/package_arch/TribitsTplFindIncludeDirsAndLibraries.cmake b/tribits/core/package_arch/TribitsTplFindIncludeDirsAndLibraries.cmake index d3cb102cf..ad2621289 100644 --- a/tribits/core/package_arch/TribitsTplFindIncludeDirsAndLibraries.cmake +++ b/tribits/core/package_arch/TribitsTplFindIncludeDirsAndLibraries.cmake @@ -676,8 +676,11 @@ function(tribits_tpl_find_include_dirs_and_libraries TPL_NAME) advanced_set(TPL_${TPL_NAME}_INCLUDE_DIRS ${${TPL_NAME}_INCLUDE_DIRS} CACHE PATH "User provided include dirs in the absence of include files.") else() - # Library has no header files, no user override, so just set them to null - global_null_set(TPL_${TPL_NAME}_INCLUDE_DIRS) + if ("${TPL_${TPL_NAME}_INCLUDE_DIRS}" STREQUAL "") + # Library has no header files, no user override, so just set them to + # null (unless the user has already set this). + global_null_set(TPL_${TPL_NAME}_INCLUDE_DIRS) + endif() endif() endif() From 089d6edcadfb63217de5949d520bdbfe1c75e116 Mon Sep 17 00:00:00 2001 From: "Roscoe A. Bartlett" Date: Sun, 23 Oct 2022 12:19:42 -0600 Subject: [PATCH 04/10] Revert "add REQUIRED_HEADERS for TriBITS MPI TPL" (TriBITSPub/TriBITS#534) This reverts commit e49070d29fdd8c1d525bb22c52d7b2745271d7e2. This causes configure failures. --- tribits/core/std_tpls/FindTPLMPI.cmake | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tribits/core/std_tpls/FindTPLMPI.cmake b/tribits/core/std_tpls/FindTPLMPI.cmake index f8483f178..f4917fd6a 100644 --- a/tribits/core/std_tpls/FindTPLMPI.cmake +++ b/tribits/core/std_tpls/FindTPLMPI.cmake @@ -50,9 +50,7 @@ if(WIN32 AND TPL_ENABLE_MPI) global_set(TPL_MPI_LIBRARIES ${MPI_LIBRARIES}) endif() -tribits_tpl_find_include_dirs_and_libraries( - MPI - REQUIRED_HEADERS mpi.h) +tribits_tpl_find_include_dirs_and_libraries(MPI) # NOTE: Above, we need to generate the MPI::all_libs target and the # MPIConfig.cmake file that will also provide the MPI::all_libs target. From 355ba19d212343a4212288128fbe01f38cdb8f3b Mon Sep 17 00:00:00 2001 From: "Roscoe A. Bartlett" Date: Mon, 24 Oct 2022 06:27:47 -0600 Subject: [PATCH 05/10] Make Netcdf depend on HDF5, fix TriBITS find modules (TriBITSPub/TriBITS#533) This makes Netcdf depend on HDF5 using modern CMake targets. ToDo: Finish this commit message. --- tribits/common_tpls/FindTPLHDF5.cmake | 135 ++++++++++-------- tribits/common_tpls/FindTPLNetcdf.cmake | 66 ++------- .../FindTPLNetcdfDependencies.cmake | 2 + 3 files changed, 94 insertions(+), 109 deletions(-) create mode 100644 tribits/common_tpls/FindTPLNetcdfDependencies.cmake diff --git a/tribits/common_tpls/FindTPLHDF5.cmake b/tribits/common_tpls/FindTPLHDF5.cmake index 3d1fa9a26..a5e2e95ef 100644 --- a/tribits/common_tpls/FindTPLHDF5.cmake +++ b/tribits/common_tpls/FindTPLHDF5.cmake @@ -2,78 +2,97 @@ # See associated tribits/Copyright.txt file for copyright and license! # ######################################################################## -# -# First, set up the variables for the (backward-compatible) TriBITS way of -# finding HDF5. These are used in case find_package(HDF5 ...) is not called -# or does not find HDF5. Also, these variables need to be non-null in order -# to trigger the right behavior in the function -# tribits_tpl_find_include_dirs_and_libraries(). -# - -set(REQUIRED_HEADERS hdf5.h) -set(REQUIRED_LIBS_NAMES hdf5) - -if (HDF5_REQUIRE_FORTRAN) - set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} hdf5_fortran) -endif() +if (Netcdf_ALLOW_MODERN) -if (TPL_ENABLE_MPI) - set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} z) -endif() + #set(minimum_modern_HDF5_version 4.7.4) + print_var(Netcdf_ALLOW_MODERN) + message("-- Using find_package(HDF5 ${minimum_modern_HDF5_version} CONFIG) ...") + find_package(HDF5 ${minimum_modern_HDF5_version} CONFIG) + if (HDF5_FOUND) + message("-- Found HDF5_CONFIG=${HDF5_CONFIG}") + message("-- Generating Netcdf::all_libs and NetcdfConfig.cmake") + tribits_extpkg_create_imported_all_libs_target_and_config_file( + HDF5 + INNER_FIND_PACKAGE_NAME HDF5 + IMPORTED_TARGETS_FOR_ALL_LIBS HDF5::HDF5) + endif() -if (TPL_ENABLE_Netcdf) - set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} hdf5_hl) endif() -# -# Second, search for HDF5 components (if allowed) using the standard -# find_package(HDF5 ...). -# -tribits_tpl_allow_pre_find_package(HDF5 HDF5_ALLOW_PREFIND) -if (HDF5_ALLOW_PREFIND) +if (NOT TARGET HDF5::all_libs) - message("-- Using find_package(HDF5 ...) ...") + # First, set up the variables for the (backward-compatible) TriBITS way of + # finding HDF5. These are used in case find_package(HDF5 ...) is not called + # or does not find HDF5. Also, these variables need to be non-null in order + # to trigger the right behavior in the function + # tribits_tpl_find_include_dirs_and_libraries(). + + set(REQUIRED_HEADERS hdf5.h) + set(REQUIRED_LIBS_NAMES hdf5) - set(HDF5_COMPONENTS C) if (HDF5_REQUIRE_FORTRAN) - list(APPEND HDF5_COMPONENTS Fortran) + set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} hdf5_fortran) endif() if (TPL_ENABLE_MPI) - set(HDF5_PREFER_PARALLEL TRUE) + set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} z) endif() - find_package(HDF5 COMPONENTS ${HDF5_COMPONENTS}) - - # Make sure that HDF5 is parallel. - if (TPL_ENABLE_MPI AND NOT HDF5_IS_PARALLEL) - message(FATAL_ERROR "Trilinos is configured for MPI, HDF5 is not. - Did CMake find the correct libraries? - Try setting HDF5_INCLUDE_DIRS and/or HDF5_LIBRARY_DIRS explicitly. - ") + if (TPL_ENABLE_Netcdf) + set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} hdf5_hl) endif() - if (HDF5_FOUND) - # Tell TriBITS that we found HDF5 and there no need to look any further! - set(TPL_HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIRS} CACHE PATH - "HDF5 include dirs") - set(TPL_HDF5_LIBRARIES ${HDF5_LIBRARIES} CACHE FILEPATH - "HDF5 libraries") - set(TPL_HDF5_LIBRARY_DIRS ${HDF5_LIBRARY_DIRS} CACHE PATH - "HDF5 library dirs") + # + # Second, search for HDF5 components (if allowed) using the standard + # find_package(HDF5 ...). + # + tribits_tpl_allow_pre_find_package(HDF5 HDF5_ALLOW_PREFIND) + if (HDF5_ALLOW_PREFIND) + + message("-- Using find_package(HDF5 ...) ...") + + set(HDF5_COMPONENTS C) + if (HDF5_REQUIRE_FORTRAN) + list(APPEND HDF5_COMPONENTS Fortran) + endif() + + if (TPL_ENABLE_MPI) + set(HDF5_PREFER_PARALLEL TRUE) + endif() + + find_package(HDF5 COMPONENTS ${HDF5_COMPONENTS}) + + # Make sure that HDF5 is parallel. + if (TPL_ENABLE_MPI AND NOT HDF5_IS_PARALLEL) + message(FATAL_ERROR "Trilinos is configured for MPI, HDF5 is not. + Did CMake find the correct libraries? + Try setting HDF5_INCLUDE_DIRS and/or HDF5_LIBRARY_DIRS explicitly. + ") + endif() + + if (HDF5_FOUND) + # Tell TriBITS that we found HDF5 and there no need to look any further! + set(TPL_HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIRS} CACHE PATH + "HDF5 include dirs") + set(TPL_HDF5_LIBRARIES ${HDF5_LIBRARIES} CACHE FILEPATH + "HDF5 libraries") + set(TPL_HDF5_LIBRARY_DIRS ${HDF5_LIBRARY_DIRS} CACHE PATH + "HDF5 library dirs") + endif() + endif() -endif() + # + # Third, call tribits_tpl_find_include_dirs_and_libraries() + # + tribits_tpl_find_include_dirs_and_libraries( HDF5 + REQUIRED_HEADERS ${REQUIRED_HEADERS} + REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} + ) + # NOTE: If find_package(HDF5 ...) was called and successfully found HDF5, then + # tribits_tpl_find_include_dirs_and_libraries() will use the already-set + # variables TPL_HDF5_INCLUDE_DIRS and TPL_HDF5_LIBRARIES and then print them + # out (and set some other standard variables as well). This is the final + # "hook" into the TriBITS TPL system. -# -# Third, call tribits_tpl_find_include_dirs_and_libraries() -# -tribits_tpl_find_include_dirs_and_libraries( HDF5 - REQUIRED_HEADERS ${REQUIRED_HEADERS} - REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} - ) -# NOTE: If find_package(HDF5 ...) was called and successfully found HDF5, then -# tribits_tpl_find_include_dirs_and_libraries() will use the already-set -# variables TPL_HDF5_INCLUDE_DIRS and TPL_HDF5_LIBRARIES and then print them -# out (and set some other standard variables as well). This is the final -# "hook" into the TriBITS TPL system. +endif() diff --git a/tribits/common_tpls/FindTPLNetcdf.cmake b/tribits/common_tpls/FindTPLNetcdf.cmake index 15cc11e45..613412351 100644 --- a/tribits/common_tpls/FindTPLNetcdf.cmake +++ b/tribits/common_tpls/FindTPLNetcdf.cmake @@ -37,72 +37,36 @@ # ************************************************************************ # @HEADER -# -# First, set up the variables for the (backward-compatible) TriBITS way of -# finding Netcdf. These are used in case find_package(NetCDF ...) is not -# called or does not find NetCDF. Also, these variables need to be non-null -# in order to trigger the right behavior in the function -# tribits_tpl_find_include_dirs_and_libraries(). -# if (${CMAKE_VERSION} GREATER "3.13") cmake_policy(SET CMP0074 NEW) endif() set(Netcdf_ALLOW_MODERN FALSE CACHE BOOL "Allow finding Netcdf as a modern CMake config file with exported targets (and only this way)") -if (Netcdf_ALLOW_MODERN) +if (Netcdf_ALLOW_MODERN AND TARGET HDF5::HDF5) set(minimum_modern_netCDF_version 4.7.4) - message("-- Netcdf_ALLOW_MODERN=${Netcdf_ALLOW_MODERN}") + print_var(Netcdf_ALLOW_MODERN) message("-- Using find_package(netCDF ${minimum_modern_netCDF_version} CONFIG) ...") find_package(netCDF ${minimum_modern_netCDF_version} CONFIG) if (netCDF_FOUND) message("-- Found netCDF_CONFIG=${netCDF_CONFIG}") message("-- Generating Netcdf::all_libs and NetcdfConfig.cmake") - # instead of using tribits_extpkg_create_imported_all_libs_target_and_config_file, - # we will do what it does ourselves because we need to bring in two - # inner find packages (netCDF and hdf5) because netCDF does not properly - # do find_dependency(hdf5) - add_library(Netcdf::all_libs INTERFACE IMPORTED GLOBAL) - target_link_libraries(Netcdf::all_libs INTERFACE netCDF::netcdf) - set(configFileStr "") - string(APPEND configFileStr - "include(CMakeFindDependencyMacro)\n" ) - string(APPEND configFileStr - "set(netCDF_DIR \"${netCDF_DIR}\")\n" ) - string(APPEND configFileStr - "find_dependency(netCDF ${minimum_modern_netCDF_version} CONFIG)\n" - ) - if (netCDF_HAS_HDF5) - find_package(hdf5 CONFIG REQUIRED) - string(APPEND configFileStr - "set(hdf5_DIR \"${hdf5_DIR}\")\n" ) - string(APPEND configFileStr - "find_dependency(hdf5 CONFIG)\n" - ) - endif() - string(APPEND configFileStr - "add_library(Netcdf::all_libs INTERFACE IMPORTED GLOBAL)\n" - ) - string(APPEND configFileStr - "target_link_libraries(Netcdf::all_libs INTERFACE netCDF::netcdf)\n") - set(buildDirExternalPkgsDir - "${${PROJECT_NAME}_BINARY_DIR}/${${PROJECT_NAME}_BUILD_DIR_EXTERNAL_PKGS_DIR}") - set(tplConfigFile - "${buildDirExternalPkgsDir}/Netcdf/NetcdfConfig.cmake") - file(WRITE "${tplConfigFile}" "${configFileStr}") - # the following gets set by tribits_tpl_find_include_dirs_and_libraries - # and it is not well-documented that you need to set it yourself if not using - # tribits_tpl_find_include_dirs_and_libraries - set(TPL_Netcdf_NOT_FOUND FALSE) - message("-- TPL_Netcdf_PARALLEL = netCDF_HAS_PARALLEL = ${netCDF_HAS_PARALLEL}") - set(TPL_Netcdf_PARALLEL ${netCDF_HAS_PARALLEL}) - message("-- TPL_Netcdf_Enables_Netcdf4 = netCDF_HAS_NC4 = ${netCDF_HAS_NC4}") - set(TPL_Netcdf_Enables_Netcdf4 ${netCDF_HAS_NC4} CACHE BOOL - "True if netcdf enables netcdf-4") + tribits_extpkg_create_imported_all_libs_target_and_config_file( + Netcdf + INNER_FIND_PACKAGE_NAME netCDF + IMPORTED_TARGETS_FOR_ALL_LIBS netCDF::netcdf) endif() -else() +endif() + +if (NOT TARGET Netcdf::all_libs) + + # First, set up the variables for the (backward-compatible) TriBITS way of + # finding Netcdf. These are used in case find_package(Netcdf ...) is not + # called or does not find HDF5. Also, these variables need to be non-null + # in order to trigger the right behavior in the function + # tribits_tpl_find_include_dirs_and_libraries(). set(REQUIRED_HEADERS netcdf.h) set(REQUIRED_LIBS_NAMES netcdf) diff --git a/tribits/common_tpls/FindTPLNetcdfDependencies.cmake b/tribits/common_tpls/FindTPLNetcdfDependencies.cmake new file mode 100644 index 000000000..30efbe45c --- /dev/null +++ b/tribits/common_tpls/FindTPLNetcdfDependencies.cmake @@ -0,0 +1,2 @@ +tribits_extpkg_define_dependencies( Netcdf + DEPENDENCIES HDF5) From e9e2bf83e921a5d8330b8c803b33a2cd48736069 Mon Sep 17 00:00:00 2001 From: Dan Ibanez Date: Mon, 24 Oct 2022 12:38:57 -0600 Subject: [PATCH 06/10] fix modern HDF5 import (TriBITSPub/TriBITS#533) 1. set the minimum version to the one I'm testing with 2. use HDF5_EXPORT_LIBRARIES since the targets defined are hard to predict --- tribits/common_tpls/FindTPLHDF5.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tribits/common_tpls/FindTPLHDF5.cmake b/tribits/common_tpls/FindTPLHDF5.cmake index a5e2e95ef..43a8ad9e9 100644 --- a/tribits/common_tpls/FindTPLHDF5.cmake +++ b/tribits/common_tpls/FindTPLHDF5.cmake @@ -4,17 +4,18 @@ if (Netcdf_ALLOW_MODERN) - #set(minimum_modern_HDF5_version 4.7.4) + set(minimum_modern_HDF5_version 1.13.2) print_var(Netcdf_ALLOW_MODERN) message("-- Using find_package(HDF5 ${minimum_modern_HDF5_version} CONFIG) ...") find_package(HDF5 ${minimum_modern_HDF5_version} CONFIG) if (HDF5_FOUND) message("-- Found HDF5_CONFIG=${HDF5_CONFIG}") message("-- Generating Netcdf::all_libs and NetcdfConfig.cmake") + message("-- HDF5_EXPORT_LIBRARIES=${HDF5_EXPORT_LIBRARIES}") tribits_extpkg_create_imported_all_libs_target_and_config_file( HDF5 INNER_FIND_PACKAGE_NAME HDF5 - IMPORTED_TARGETS_FOR_ALL_LIBS HDF5::HDF5) + IMPORTED_TARGETS_FOR_ALL_LIBS ${HDF5_EXPORT_LIBRARIES}) endif() endif() From bcb00e2cd5adf42e0d0ac290733e5af552122bae Mon Sep 17 00:00:00 2001 From: Dan Ibanez Date: Mon, 24 Oct 2022 12:40:24 -0600 Subject: [PATCH 07/10] fix modern Netcdf import (TriBITSPub/TriBITS#533) 1. don't expect HDF5 target to be set. in the general case, Netcdf may not use HDF5, but also HDF5 has no predictable catch-all target 2. TPL_Netcdf_NOT_FOUND still has to be manually set to FALSE --- tribits/common_tpls/FindTPLNetcdf.cmake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tribits/common_tpls/FindTPLNetcdf.cmake b/tribits/common_tpls/FindTPLNetcdf.cmake index 613412351..6830412b5 100644 --- a/tribits/common_tpls/FindTPLNetcdf.cmake +++ b/tribits/common_tpls/FindTPLNetcdf.cmake @@ -43,7 +43,7 @@ endif() set(Netcdf_ALLOW_MODERN FALSE CACHE BOOL "Allow finding Netcdf as a modern CMake config file with exported targets (and only this way)") -if (Netcdf_ALLOW_MODERN AND TARGET HDF5::HDF5) +if (Netcdf_ALLOW_MODERN) set(minimum_modern_netCDF_version 4.7.4) print_var(Netcdf_ALLOW_MODERN) @@ -56,6 +56,7 @@ if (Netcdf_ALLOW_MODERN AND TARGET HDF5::HDF5) Netcdf INNER_FIND_PACKAGE_NAME netCDF IMPORTED_TARGETS_FOR_ALL_LIBS netCDF::netcdf) + set(TPL_Netcdf_NOT_FOUND FALSE) endif() endif() From b960317305c96b0f1543239f5749bdd6e1a394f7 Mon Sep 17 00:00:00 2001 From: Dan Ibanez Date: Tue, 25 Oct 2022 17:14:40 -0600 Subject: [PATCH 08/10] Netcdf modern is only used if HDF5 is modern see TriBITSPub/TriBITS#533 and also https://github.com/trilinos/Trilinos/pull/11175#discussion_r1004992219 --- tribits/common_tpls/FindTPLHDF5.cmake | 5 +++++ tribits/common_tpls/FindTPLNetcdf.cmake | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tribits/common_tpls/FindTPLHDF5.cmake b/tribits/common_tpls/FindTPLHDF5.cmake index 43a8ad9e9..f9113db2b 100644 --- a/tribits/common_tpls/FindTPLHDF5.cmake +++ b/tribits/common_tpls/FindTPLHDF5.cmake @@ -2,6 +2,8 @@ # See associated tribits/Copyright.txt file for copyright and license! # ######################################################################## +set(INTERNAL_IS_MODERN FALSE) + if (Netcdf_ALLOW_MODERN) set(minimum_modern_HDF5_version 1.13.2) @@ -16,10 +18,13 @@ if (Netcdf_ALLOW_MODERN) HDF5 INNER_FIND_PACKAGE_NAME HDF5 IMPORTED_TARGETS_FOR_ALL_LIBS ${HDF5_EXPORT_LIBRARIES}) + set(INTERNAL_IS_MODERN TRUE) endif() endif() +set(HDF5_FOUND_MODERN_CONFIG_FILE ${INTERNAL_IS_MODERN} CACHE BOOL "True if HDF5 was found by the modern method" FORCE) + if (NOT TARGET HDF5::all_libs) # First, set up the variables for the (backward-compatible) TriBITS way of diff --git a/tribits/common_tpls/FindTPLNetcdf.cmake b/tribits/common_tpls/FindTPLNetcdf.cmake index 6830412b5..f81e3b925 100644 --- a/tribits/common_tpls/FindTPLNetcdf.cmake +++ b/tribits/common_tpls/FindTPLNetcdf.cmake @@ -43,7 +43,7 @@ endif() set(Netcdf_ALLOW_MODERN FALSE CACHE BOOL "Allow finding Netcdf as a modern CMake config file with exported targets (and only this way)") -if (Netcdf_ALLOW_MODERN) +if (Netcdf_ALLOW_MODERN AND HDF5_FOUND_MODERN_CONFIG_FILE) set(minimum_modern_netCDF_version 4.7.4) print_var(Netcdf_ALLOW_MODERN) From cb5b113b2b0595d7902d6e04e7bac2b50600bac5 Mon Sep 17 00:00:00 2001 From: Dan Ibanez Date: Wed, 26 Oct 2022 13:04:43 -0600 Subject: [PATCH 09/10] small improvements (TriBITSPub/TriBITS#533) --- tribits/common_tpls/FindTPLHDF5.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tribits/common_tpls/FindTPLHDF5.cmake b/tribits/common_tpls/FindTPLHDF5.cmake index f9113db2b..57bb3c3ef 100644 --- a/tribits/common_tpls/FindTPLHDF5.cmake +++ b/tribits/common_tpls/FindTPLHDF5.cmake @@ -2,7 +2,7 @@ # See associated tribits/Copyright.txt file for copyright and license! # ######################################################################## -set(INTERNAL_IS_MODERN FALSE) +set(HDF5_INTERNAL_IS_MODERN FALSE) if (Netcdf_ALLOW_MODERN) @@ -18,12 +18,12 @@ if (Netcdf_ALLOW_MODERN) HDF5 INNER_FIND_PACKAGE_NAME HDF5 IMPORTED_TARGETS_FOR_ALL_LIBS ${HDF5_EXPORT_LIBRARIES}) - set(INTERNAL_IS_MODERN TRUE) + set(HDF5_INTERNAL_IS_MODERN TRUE) endif() endif() -set(HDF5_FOUND_MODERN_CONFIG_FILE ${INTERNAL_IS_MODERN} CACHE BOOL "True if HDF5 was found by the modern method" FORCE) +set(HDF5_FOUND_MODERN_CONFIG_FILE ${HDF5_INTERNAL_IS_MODERN} CACHE INTERNAL "True if HDF5 was found by the modern method") if (NOT TARGET HDF5::all_libs) From 4127bf3e4cda37614da92489ce7dba643dfabf45 Mon Sep 17 00:00:00 2001 From: "Roscoe A. Bartlett" Date: Fri, 28 Oct 2022 08:20:34 -0600 Subject: [PATCH 10/10] Remove check for CMake version 3.13 (#533) This file gets included in a TriBITS project which must have CMake 3.17+. --- tribits/common_tpls/FindTPLNetcdf.cmake | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tribits/common_tpls/FindTPLNetcdf.cmake b/tribits/common_tpls/FindTPLNetcdf.cmake index f81e3b925..d025130f7 100644 --- a/tribits/common_tpls/FindTPLNetcdf.cmake +++ b/tribits/common_tpls/FindTPLNetcdf.cmake @@ -37,9 +37,7 @@ # ************************************************************************ # @HEADER -if (${CMAKE_VERSION} GREATER "3.13") - cmake_policy(SET CMP0074 NEW) -endif() +cmake_policy(SET CMP0074 NEW) set(Netcdf_ALLOW_MODERN FALSE CACHE BOOL "Allow finding Netcdf as a modern CMake config file with exported targets (and only this way)")