@@ -92,17 +92,22 @@ if(_zlib)
9292 find_package (ZLIB REQUIRED)
9393
9494 if (_szip)
95- # Szip even though not directly used because if system static links libhdf5 with szip,
95+ # Szip even though not used by h5fortran.
96+ # If system HDF5 dynamically links libhdf5 with szip,
9697 # our builds will fail if we don't also link szip.
97- find_package (SZIP REQUIRED)
98- set (CMAKE_REQUIRED_LIBRARIES ${HDF5_Fortran_LIBRARIES} ${HDF5_C_LIBRARIES} SZIP::SZIP ZLIB::ZLIB ${CMAKE_DL_LIBS} )
99- else ()
100- set (CMAKE_REQUIRED_LIBRARIES ${HDF5_Fortran_LIBRARIES} ${HDF5_C_LIBRARIES} ZLIB::ZLIB ${CMAKE_DL_LIBS} )
98+ # however, we don't require SZIP for this case as other HDF5 libraries may statically
99+ # link SZIP.
100+ find_package (SZIP)
101+ if (SZIP_FOUND)
102+ list (APPEND CMAKE_REQUIRED_LIBRARIES SZIP::SZIP)
103+ endif ()
101104 endif ()
102- else ()
103- set ( CMAKE_REQUIRED_LIBRARIES ${HDF5_Fortran_LIBRARIES} ${HDF5_C_LIBRARIES} ${CMAKE_DL_LIBS} )
105+
106+ list ( APPEND CMAKE_REQUIRED_LIBRARIES ZLIB::ZLIB )
104107endif ()
105108
109+ list (APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_DL_LIBS} )
110+
106111set (THREADS_PREFER_PTHREAD_FLAG true )
107112find_package (Threads)
108113if (Threads_FOUND)
@@ -116,11 +121,12 @@ endfunction(detect_config)
116121
117122# === main program
118123
124+ set (CMAKE_REQUIRED_LIBRARIES)
119125set (_lsuf hdf5 hdf5/serial)
120126set (_psuf static ${_lsuf} )
121127
122128# we don't use pkg-config directly because some distros pkg-config for HDF5 is broken
123- # however so far at least we've see the paths are often correct
129+ # however at least the paths are often correct
124130
125131find_package (PkgConfig)
126132if (PkgConfig_FOUND)
@@ -169,6 +175,7 @@ if(Fortran IN_LIST HDF5_FIND_COMPONENTS)
169175 DOC "HDF5 Fortran modules" )
170176
171177 if (HDF5_Fortran_HL_LIBRARY AND HDF5_Fortran_LIBRARY AND HDF5_Fortran_INCLUDE_DIR)
178+ list (APPEND CMAKE_REQUIRED_LIBRARIES ${HDF5_Fortran_LIBRARIES} )
172179 set (HDF5_Fortran_FOUND true )
173180 set (HDF5_HL_FOUND true )
174181 endif ()
@@ -219,6 +226,7 @@ find_path(HDF5_INCLUDE_DIR
219226 DOC "HDF5 C header" )
220227
221228if (HDF5_C_HL_LIBRARY AND HDF5_C_LIBRARY AND HDF5_INCLUDE_DIR)
229+ list (APPEND CMAKE_REQUIRED_LIBRARIES ${HDF5_C_LIBRARIES} )
222230 set (HDF5_C_FOUND true )
223231 set (HDF5_HL_FOUND true )
224232endif ()
@@ -279,12 +287,9 @@ if(HDF5_Fortran_FOUND AND NOT HDF5_Fortran_links)
279287 set (HDF5_links false )
280288endif ()
281289
282- set (CMAKE_REQUIRED_INCLUDES)
283- set (CMAKE_REQUIRED_LIBRARIES)
284-
285290include (FindPackageHandleStandardArgs)
286291find_package_handle_standard_args(HDF5
287- REQUIRED_VARS HDF5_links
292+ REQUIRED_VARS HDF5_C_LIBRARIES HDF5_links
288293 VERSION_VAR HDF5_VERSION
289294 HANDLE_COMPONENTS)
290295
0 commit comments