Skip to content

Commit 6c6addd

Browse files
committed
FindHDF5: work with serial or parallel HDF5 library
1 parent 2df8f99 commit 6c6addd

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ if(HDF5_VERSION VERSION_LESS 1.8.7)
3838
message(WARNING "H5Fortran requires HDF5 >= 1.8.7")
3939
endif()
4040

41+
if(hdf5_parallel OR HDF5_IS_PARALLEL)
42+
find_package(MPI COMPONENTS Fortran REQUIRED)
43+
target_link_libraries(HDF5::HDF5 INTERFACE MPI::MPI_Fortran)
44+
endif()
45+
4146
# --- h5fortran library
4247

4348
add_library(h5fortran)

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.1.0
1+
4.1.1

cmake/Modules/FindHDF5.cmake

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,21 @@ if(NOT h5_conf)
7474
return()
7575
endif()
7676

77-
# get version
78-
# from CMake/Modules/FindHDF5.cmake
79-
check_symbol_exists(H5_HAVE_FILTER_SZIP ${h5_conf} have_szip)
80-
check_symbol_exists(H5_HAVE_FILTER_DEFLATE ${h5_conf} have_zlib)
81-
82-
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
83-
check_symbol_exists(H5_HAVE_PARALLEL ${h5_conf} _h5_parallel)
84-
if(_h5_parallel)
85-
if(NOT TARGET MPI::MPI_C)
86-
find_package(MPI COMPONENTS C)
87-
endif()
88-
if(MPI_C_FOUND)
89-
set(HDF5_parallel_FOUND true PARENT_SCOPE)
90-
set(HDF5_IS_PARALLEL true CACHE BOOL "HDF5 library compiled with parallel IO support")
91-
endif()
92-
endif()
77+
# check HDF5 features that require link of external libraries.
78+
check_symbol_exists(H5_HAVE_FILTER_SZIP ${h5_conf} hdf5_have_szip)
79+
check_symbol_exists(H5_HAVE_FILTER_DEFLATE ${h5_conf} hdf5_have_zlib)
9380

94-
if(NOT HDF5_IS_PARALLEL)
95-
set(HDF5_parallel_FOUND false PARENT_SCOPE)
96-
return()
97-
endif()
81+
# Always check for HDF5 MPI support because HDF5 link fails if MPI is linked into HDF5.
82+
check_symbol_exists(H5_HAVE_PARALLEL ${h5_conf} HDF5_IS_PARALLEL)
83+
84+
if(HDF5_IS_PARALLEL)
85+
set(HDF5_parallel_FOUND true PARENT_SCOPE)
86+
else()
87+
set(HDF5_parallel_FOUND false PARENT_SCOPE)
9888
endif()
9989

90+
# get version
91+
# from CMake/Modules/FindHDF5.cmake
10092
file(STRINGS ${h5_conf} _def
10193
REGEX "^[ \t]*#[ \t]*define[ \t]+H5_VERSION[ \t]+" )
10294
if( "${_def}" MATCHES
@@ -118,10 +110,10 @@ if(NOT SZIP_ROOT)
118110
set(SZIP_ROOT "${ZLIB_ROOT}")
119111
endif()
120112

121-
if(have_zlib)
113+
if(hdf5_have_zlib)
122114
find_package(ZLIB)
123115

124-
if(have_szip)
116+
if(hdf5_have_szip)
125117
# Szip even though not used by default.
126118
# If system HDF5 dynamically links libhdf5 with szip, our builds will fail if we don't also link szip.
127119
# however, we don't require SZIP for this case as other HDF5 libraries may statically link SZIP.
@@ -144,7 +136,8 @@ if(UNIX)
144136
list(APPEND CMAKE_REQUIRED_LIBRARIES m)
145137
endif()
146138

147-
if(MPI_C_FOUND)
139+
if(HDF5_IS_PARALLEL)
140+
find_package(MPI COMPONENTS C)
148141
list(APPEND CMAKE_REQUIRED_LIBRARIES MPI::MPI_C)
149142
endif()
150143

@@ -380,10 +373,10 @@ if(HDF5_FOUND)
380373
set_target_properties(HDF5::HDF5 PROPERTIES
381374
INTERFACE_LINK_LIBRARIES "${HDF5_LIBRARIES}"
382375
INTERFACE_INCLUDE_DIRECTORIES "${HDF5_INCLUDE_DIRS}")
383-
if(have_zlib)
376+
if(hdf5_have_zlib)
384377
target_link_libraries(HDF5::HDF5 INTERFACE ZLIB::ZLIB)
385378
endif()
386-
if(have_szip)
379+
if(hdf5_have_szip)
387380
target_link_libraries(HDF5::HDF5 INTERFACE SZIP::SZIP)
388381
endif()
389382

0 commit comments

Comments
 (0)