Skip to content

Commit

Permalink
Improve zlib detection/linking when building HDF5 from source and add…
Browse files Browse the repository at this point in the history
… support for szip
  • Loading branch information
DavidAce committed Jan 7, 2021
1 parent 49b45f7 commit e6ea67e
Showing 1 changed file with 83 additions and 55 deletions.
138 changes: 83 additions & 55 deletions cmake/external_hdf5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,97 @@ if(ENV{CONDA_PREFIX})
message(WARNING "The current conda environment may conflict with the build of HDF5: $ENV{CONDA_PREFIX}")
endif()

# The following check is needed because HDF5 will blindly use
# find_package(ZLIB), which finds the shared library
# (even when a static is present) and use it to link to static
# hdf5 libraries, causing a build error. Here we circumvent that
# by specifying the shared/static zlib library explicitly as needed

set(HDF5_ENABLE_Z_LIB_SUPPORT ON)
if(BUILD_SHARED_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()

find_package(ZLIB)
set(ZLIB_LIBRARY "")
unset(ZLIB_LIBRARY)
unset(ZLIB_LIBRARY CACHE)
if(TARGET ZLIB::ZLIB)
get_target_property(ZLIB_LIBRARY ZLIB::ZLIB LOCATION)
get_filename_component(ZLIB_EXT ${ZLIB_LIBRARY} EXT)
get_filename_component(ZLIB_WE ${ZLIB_LIBRARY} NAME_WE)
get_filename_component(ZLIB_DIR ${ZLIB_LIBRARY} DIRECTORY)
if(NOT ZLIB_EXT MATCHES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
find_library(ZLIB_LIBRARY NAMES ${ZLIB_WE}${CMAKE_FIND_LIBRARY_SUFFIXES} libz${CMAKE_FIND_LIBRARY_SUFFIXES} HINTS ${ZLIB_DIR})
if(ZLIB_LIBRARY)
set(HDF5_ENABLE_Z_LIB_SUPPORT ON)

# The following check is needed because HDF5 will blindly use
# find_package(ZLIB), which finds the shared library
# (even when a static is present) and use it to link to static
# hdf5 libraries, causing a build error. Even worse, if the build
# succeeds it will hardcode the path to libz.so for as an interface
# library for the static library.
# Here we circumvent that by specifying "z" as the zlib library
# so the hdf5-static target gets $<LINK_ONLY:z> ---> "-lz" as link
# flag instead. Of course this only works if the shared/static library
# actualy exists.

if(NOT HDF5_ENABLE_Z_LIB_SUPPORT OR NOT ZLIB_LIBRARY)

set(HDF5_ENABLE_Z_LIB_SUPPORT ON)
find_package(ZLIB)
unset(ZLIB_LIBRARY)
if(TARGET ZLIB::ZLIB)
get_target_property(ZLIB_LIBRARY ZLIB::ZLIB LOCATION)
get_filename_component(ZLIB_EXT ${ZLIB_LIBRARY} EXT)
get_filename_component(ZLIB_WE ${ZLIB_LIBRARY} NAME_WE)
get_filename_component(ZLIB_DIR ${ZLIB_LIBRARY} DIRECTORY)
if(NOT ZLIB_EXT MATCHES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
message(STATUS "find_package(ZLIB) found library ${ZLIB_LIBRARY} which has incorrect suffix. Attempting fix")
find_library(ZLIB_LIBRARY NAMES ${ZLIB_WE}${CMAKE_FIND_LIBRARY_SUFFIXES} libz${CMAKE_FIND_LIBRARY_SUFFIXES} HINTS ${ZLIB_DIR})
if(ZLIB_LIBRARY)
set(ZLIB_LIBRARY z) # Replace the full path so that the linker can decide later instead
set(HDF5_ENABLE_Z_LIB_SUPPORT ON)
else()
unset(ZLIB_LIBRARY)
message(STATUS "Could not find static ZLIB: disabling ZLIB support for hdf5")
set(HDF5_ENABLE_Z_LIB_SUPPORT OFF)
endif()
else()
unset(ZLIB_LIBRARY)
message(STATUS "Could not find static ZLIB: disabling ZLIB support for hdf5")
set(HDF5_ENABLE_Z_LIB_SUPPORT OFF)
set(ZLIB_LIBRARY z) # Replace the full path so that the linker can decide later instead
message(STATUS "find_package(ZLIB) found library ${ZLIB_LIBRARY} which has the correct suffix")
endif()
else()
message(STATUS "Could not find ZLIB: disabling ZLIB support for hdf5")
set(HDF5_ENABLE_Z_LIB_SUPPORT OFF)
endif()
else()
message(STATUS "Could not find ZLIB: disabling ZLIB support for hdf5")
set(HDF5_ENABLE_Z_LIB_SUPPORT OFF)
endif()



# The following check is needed because HDF5 will blindly use
# find_package(SZIP), which finds the shared library
# (even when a static is present) and use it to link to static
# hdf5 libraries, causing a build error. Even worse, if the build
# succeeds it will hardcode the path to libsz.so for as an interface
# library for the static library.
# Here we circumvent that by specifying "sz" as the szip library
# so the hdf5-static target gets $<LINK_ONLY:sz> ---> "-lsz" as link
# flag instead. Of course this only works if the shared/static library
# actualy exists.

# The following check is needed because HDF5 will blindly use
# find_package(SZIP), which finds the shared library
# (even when a static is present) and use it to link to static
# hdf5 libraries, causing a build error. Here we circumvent that
# by specifying the shared/static SZIP library explicitly as needed

#set(HDF5_ENABLE_SZIP_SUPPORT ON)
#set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
#find_library(SZIP_LIBRARY NAMES sz szip szip-static libsz libszip libszip-static) # No built in findSZIP.cmake
#if(SZIP_LIBRARY)
# message(STATUS "Found SZIP: ${SZIP_LIBRARY}")
# get_filename_component(SZIP_EXT ${SZIP_LIBRARY} EXT)
# if(NOT BUILD_SHARED_LIBS AND SZIP_EXT MATCHES "${CMAKE_SHARED_LIBRARY_SUFFIX}")
# message(STATUS "Could not find static SZIP: disabling SZIP support for hdf5")
# set(HDF5_ENABLE_SZIP_SUPPORT OFF)
# else()
# find_library(AEC_LIBRARY NAMES aec)
# if(AEC_LIBRARY)
# message(STATUS "Found AEC: ${AEC_LIBRARY}")
# list(APPEND CMAKE_SHARED_LINKER_FLAGS -laec)
# list(APPEND SZIP_LIBRARY ${AEC_LIBRARY})
# endif()
# endif()
#
#else()
# unset(SZIP_LIBRARY)
# message(STATUS "Could not find SZIP: disabling SZIP support for hdf5")
# set(HDF5_ENABLE_SZIP_SUPPORT OFF)
#endif()

if(NOT HDF5_ENABLE_SZIP_SUPPORT OR NOT SZIP_LIBRARY)
set(HDF5_ENABLE_SZIP_SUPPORT ON)
find_library(SZIP_LIBRARY NAMES sz) # No built in FindSZIP.cmake
if(SZIP_LIBRARY)
message(STATUS "Found SZIP: ${SZIP_LIBRARY}")
get_filename_component(SZIP_EXT ${SZIP_LIBRARY} EXT)
if(SZIP_EXT MATCHES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
set(SZIP_LIBRARY sz)
find_library(AEC_LIBRARY NAMES aec)
if(AEC_LIBRARY)
message(STATUS "Found AEC: ${AEC_LIBRARY}")
set(SZIP_LIBRARY "sz$<SEMICOLON>aec")
endif()
else()
message(STATUS "Could not find SZIP with the correct extension: disabling SZIP support for HDF5")
set(HDF5_ENABLE_SZIP_SUPPORT OFF)
unset(SZIP_LIBRARY)
endif()
else()
message(STATUS "Could not find SZIP: disabling SZIP support for hdf5")
set(HDF5_ENABLE_SZIP_SUPPORT OFF)
unset(SZIP_LIBRARY)
endif()
endif()

ExternalProject_Add(external_hdf5
URL https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.12/hdf5-1.12.0/src/hdf5-1.12.0.tar.gz
Expand Down Expand Up @@ -108,17 +132,21 @@ ExternalProject_Add(external_hdf5
-DCMAKE_GENERATOR=${CMAKE_GENERATOR}
-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}
# HDF5 flags
-DHDF5_ENABLE_PARALLEL:BOOL=${HDF5_ENABLE_PARALLEL}
# -DBUILD_STATIC_EXECS:BOOL=OFF
# -DBUILD_STATIC_LIBS:BOOL=ON
-DBUILD_TESTING:BOOL=OFF
# -DZLIB_LIBRARY:PATH=${ZLIB_LIBRARY}
-DZLIB_LIBRARY=${ZLIB_LIBRARY}
-DSZIP_LIBRARY=${SZIP_LIBRARY}
-DHDF5_ENABLE_PARALLEL:BOOL=${HDF5_ENABLE_PARALLEL}
-DHDF5_ENABLE_Z_LIB_SUPPORT:BOOL=${HDF5_ENABLE_Z_LIB_SUPPORT}
-DZLIB_LIBRARY:PATH=${ZLIB_LIBRARY}
-DHDF5_ENABLE_SZIP_SUPPORT:BOOL=OFF # Complains about undefined reference to functions in libaec
-DHDF5_ENABLE_SZIP_SUPPORT:BOOL=${HDF5_ENABLE_SZIP_SUPPORT}
-DHDF5_BUILD_TOOLS:BOOL=ON
-DHDF5_BUILD_FORTRAN:BOOL=OFF
-DHDF5_BUILD_EXAMPLES:BOOL=OFF
-DHDF5_BUILD_JAVA:BOOL=OFF
-DHDF5_DISABLE_COMPILER_WARNINGS:BOOL=ON
-DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON
-DALLOW_UNSUPPORTED=ON
)
)

0 comments on commit e6ea67e

Please sign in to comment.