From f514e7201150d646e3566e9b341afb838f7663a4 Mon Sep 17 00:00:00 2001 From: "John W. Parent" <45471568+johnwparent@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:18:34 -0500 Subject: netcdf-c package: fix hdf5 linking on Windows (#42749) --- .../netcdf-c/netcdfc-mpi-win-support.patch | 59 ------------- ...netcdfc_correct_and_export_link_interface.patch | 96 ++++++++++++++++++++++ .../repos/builtin/packages/netcdf-c/package.py | 13 ++- 3 files changed, 106 insertions(+), 62 deletions(-) delete mode 100644 var/spack/repos/builtin/packages/netcdf-c/netcdfc-mpi-win-support.patch create mode 100644 var/spack/repos/builtin/packages/netcdf-c/netcdfc_correct_and_export_link_interface.patch diff --git a/var/spack/repos/builtin/packages/netcdf-c/netcdfc-mpi-win-support.patch b/var/spack/repos/builtin/packages/netcdf-c/netcdfc-mpi-win-support.patch deleted file mode 100644 index 7075e39dfb..0000000000 --- a/var/spack/repos/builtin/packages/netcdf-c/netcdfc-mpi-win-support.patch +++ /dev/null @@ -1,59 +0,0 @@ -diff --git a/CMakeLists.txt b/CMakeLists.txt -index 9b057311..37e96a96 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -1471,6 +1471,7 @@ ENDIF() - - # Enable Parallel IO with netCDF-4/HDF5 files using HDF5 parallel I/O. - SET(STATUS_PARALLEL "OFF") -+set(IMPORT_MPI "") - OPTION(ENABLE_PARALLEL4 "Build netCDF-4 with parallel IO" "${HDF5_PARALLEL}") - IF(ENABLE_PARALLEL4 AND ENABLE_HDF5) - IF(NOT HDF5_PARALLEL) -@@ -1492,6 +1493,7 @@ IF(ENABLE_PARALLEL4 AND ENABLE_HDF5) - FILE(COPY "${netCDF_BINARY_DIR}/tmp/run_par_tests.sh" - DESTINATION ${netCDF_BINARY_DIR}/h5_test - FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) -+ set(IMPORT_MPI "include(CMakeFindDependencyMacro)\nfind_dependency(mpi COMPONENTS C)") - ENDIF() - ENDIF() - -diff --git a/liblib/CMakeLists.txt b/liblib/CMakeLists.txt -index e3eddc0f..0493cb9d 100644 ---- a/liblib/CMakeLists.txt -+++ b/liblib/CMakeLists.txt -@@ -50,6 +50,7 @@ ADD_LIBRARY(netcdf nc_initialize.c ${LARGS} ) - - IF(MPI_C_INCLUDE_PATH) - target_include_directories(netcdf PUBLIC ${MPI_C_INCLUDE_PATH}) -+ target_link_libraries(netcdf MPI::MPI_C) - ENDIF(MPI_C_INCLUDE_PATH) - - IF(MOD_NETCDF_NAME) -diff --git a/netCDFConfig.cmake.in b/netCDFConfig.cmake.in -index 9d68eec5..dae2429e 100644 ---- a/netCDFConfig.cmake.in -+++ b/netCDFConfig.cmake.in -@@ -14,6 +14,8 @@ set(netCDF_LIBRARIES netCDF::netcdf) - # include target information - include("${CMAKE_CURRENT_LIST_DIR}/netCDFTargets.cmake") - -+@IMPORT_MPI@ -+ - # Compiling Options - # - set(netCDF_C_COMPILER "@CC_VERSION@") -diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt -index 65891d82..15567c8f 100644 ---- a/plugins/CMakeLists.txt -+++ b/plugins/CMakeLists.txt -@@ -62,6 +62,9 @@ MACRO(buildplugin TARGET TARGETLIB) - set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "/INCREMENTAL:NO /DEBUG /OPT:REF /OPT:ICF") - # Set file name & location - set_target_properties(${TARGET} PROPERTIES COMPILE_PDB_NAME ${TARGET} COMPILE_PDB_OUTPUT_DIR ${CMAKE_BINARY_DIR}) -+ IF(MPI_C_INCLUDE_PATH) -+ target_include_directories(${TARGET} PRIVATE ${MPI_C_INCLUDE_PATH}) -+ ENDIF(MPI_C_INCLUDE_PATH) - ENDIF() - ENDMACRO() - diff --git a/var/spack/repos/builtin/packages/netcdf-c/netcdfc_correct_and_export_link_interface.patch b/var/spack/repos/builtin/packages/netcdf-c/netcdfc_correct_and_export_link_interface.patch new file mode 100644 index 0000000000..f1b826367d --- /dev/null +++ b/var/spack/repos/builtin/packages/netcdf-c/netcdfc_correct_and_export_link_interface.patch @@ -0,0 +1,96 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index de95010c..25229f9c 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -664,6 +664,7 @@ ENDIF(ENABLE_STRICT_NULL_BYTE_HEADER_PADDING) + # * + ## + SET(USE_HDF5 ${ENABLE_HDF5}) ++SET(IMPORT_HDF5 "") + IF(USE_HDF5) + + ## +@@ -671,7 +672,6 @@ IF(USE_HDF5) + ## + SET(HDF5_VERSION_REQUIRED 1.8.10) + +- + ## + # Accommodate developers who have hdf5 libraries and + # headers on their system, but do not have a the hdf +@@ -744,6 +744,9 @@ IF(USE_HDF5) + ELSE(MSVC) + FIND_PACKAGE(HDF5 COMPONENTS C HL REQUIRED) + ENDIF(MSVC) ++ # Export HDF5 Dependency so consumers can properly use ++ # exported link interface ++ set(IMPORT_HDF5 "find_dependency(HDF5 COMPONENTS C HL)") + + ## + # Next, check the HDF5 version. This will inform which +@@ -1481,6 +1484,7 @@ ENDIF() + + # Enable Parallel IO with netCDF-4/HDF5 files using HDF5 parallel I/O. + SET(STATUS_PARALLEL "OFF") ++set(IMPORT_MPI "") + OPTION(ENABLE_PARALLEL4 "Build netCDF-4 with parallel IO" "${HDF5_PARALLEL}") + IF(ENABLE_PARALLEL4 AND ENABLE_HDF5) + IF(NOT HDF5_PARALLEL) +@@ -1502,6 +1506,7 @@ IF(ENABLE_PARALLEL4 AND ENABLE_HDF5) + FILE(COPY "${netCDF_BINARY_DIR}/tmp/run_par_tests.sh" + DESTINATION ${netCDF_BINARY_DIR}/h5_test + FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) ++ set(IMPORT_MPI "find_dependency(MPI COMPONENTS C)") + ENDIF() + ENDIF() + +@@ -2652,6 +2657,8 @@ endif(DEFINED ENV{LIB_FUZZING_ENGINE}) + # cmake should be able to find netcdf using find_package and find_library. + # The EXPORT call is paired with one in liblib. + set(ConfigPackageLocation ${CMAKE_INSTALL_LIBDIR}/cmake/netCDF) ++set(IMPORT_FIND_DEP "include(CMakeFindDependencyMacro)") ++ + + install(EXPORT netCDFTargets + DESTINATION ${ConfigPackageLocation} +diff --git a/liblib/CMakeLists.txt b/liblib/CMakeLists.txt +index e3eddc0f..0493cb9d 100644 +--- a/liblib/CMakeLists.txt ++++ b/liblib/CMakeLists.txt +@@ -50,6 +50,7 @@ ADD_LIBRARY(netcdf nc_initialize.c ${LARGS} ) + + IF(MPI_C_INCLUDE_PATH) + target_include_directories(netcdf PUBLIC ${MPI_C_INCLUDE_PATH}) ++ target_link_libraries(netcdf MPI::MPI_C) + ENDIF(MPI_C_INCLUDE_PATH) + + IF(MOD_NETCDF_NAME) +diff --git a/netCDFConfig.cmake.in b/netCDFConfig.cmake.in +index 9d68eec5..eece09cb 100644 +--- a/netCDFConfig.cmake.in ++++ b/netCDFConfig.cmake.in +@@ -14,6 +14,10 @@ set(netCDF_LIBRARIES netCDF::netcdf) + # include target information + include("${CMAKE_CURRENT_LIST_DIR}/netCDFTargets.cmake") + ++@IMPORT_FIND_DEP@ ++@IMPORT_MPI@ ++@IMPORT_HDF5@ ++ + # Compiling Options + # + set(netCDF_C_COMPILER "@CC_VERSION@") +diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt +index 65891d82..15567c8f 100644 +--- a/plugins/CMakeLists.txt ++++ b/plugins/CMakeLists.txt +@@ -62,6 +62,9 @@ MACRO(buildplugin TARGET TARGETLIB) + set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "/INCREMENTAL:NO /DEBUG /OPT:REF /OPT:ICF") + # Set file name & location + set_target_properties(${TARGET} PROPERTIES COMPILE_PDB_NAME ${TARGET} COMPILE_PDB_OUTPUT_DIR ${CMAKE_BINARY_DIR}) ++ IF(MPI_C_INCLUDE_PATH) ++ target_include_directories(${TARGET} PRIVATE ${MPI_C_INCLUDE_PATH}) ++ ENDIF(MPI_C_INCLUDE_PATH) + ENDIF() + ENDMACRO() + diff --git a/var/spack/repos/builtin/packages/netcdf-c/package.py b/var/spack/repos/builtin/packages/netcdf-c/package.py index af8a817c9f..84f854fec9 100644 --- a/var/spack/repos/builtin/packages/netcdf-c/package.py +++ b/var/spack/repos/builtin/packages/netcdf-c/package.py @@ -58,9 +58,12 @@ class NetcdfC(CMakePackage, AutotoolsPackage): # with the following patch: patch("4.8.1-win-hdf5-with-zlib.patch", when="@4.8.1: platform=windows") - # TODO: fetch from the upstream repo once https://github.com/Unidata/netcdf-c/pull/2595 - # is accepted: - patch("netcdfc-mpi-win-support.patch", when="platform=windows") + # TODO: https://github.com/Unidata/netcdf-c/pull/2595 contains some of the changes + # made in this patch but is not sufficent to replace the patch. There is currently + # no upstream PR (or set of PRs) covering all changes in this path. + # When #2595 lands, this patch should be updated to include only + # the changes not incorporated into that PR + patch("netcdfc_correct_and_export_link_interface.patch", when="platform=windows") # Some of the patches touch configure.ac and, therefore, require forcing the autoreconf stage: _force_autoreconf_when = [] @@ -342,6 +345,10 @@ class CMakeBuilder(BaseBuilder, cmake.CMakeBuilder): if "platform=windows" in self.pkg.spec: # Enforce the usage of the vendored version of bzip2 on Windows: base_cmake_args.append(self.define("Bz2_INCLUDE_DIRS", "")) + if "+shared" in self.pkg.spec["hdf5"]: + base_cmake_args.append(self.define("NC_FIND_SHARED_LIBS", True)) + else: + base_cmake_args.append(self.define("NC_FIND_SHARED_LIBS", False)) return base_cmake_args -- cgit v1.2.3-70-g09d2