Skip to content

Commit

Permalink
Build Fix : Cannot dynamically link to HDF5 on Windows in CMAKE confi…
Browse files Browse the repository at this point in the history
…g mode

The H5_BUILT_AS_DYNAMIC_LIB compile definition is missing when HDF5 is found by CMake using config mode on windows.
The reason is because hdf5::hdf5 cmake target is missing in this particular case. It is replaced by hdf5-shared target or hdf5::hdf5-shared in officail HDF5 sources.
  • Loading branch information
philippeVerney committed Jan 9, 2024
1 parent ea0aafb commit 0b3bfc2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set (FESAPI_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
set (Fesapi_VERSION_MAJOR 2)
set (Fesapi_VERSION_MINOR 9)
set (Fesapi_VERSION_PATCH 0)
set (Fesapi_VERSION_TWEAK 0)
set (Fesapi_VERSION_TWEAK 1)

set (Fesapi_VERSION ${Fesapi_VERSION_MAJOR}.${Fesapi_VERSION_MINOR}.${Fesapi_VERSION_PATCH}.${Fesapi_VERSION_TWEAK})

Expand Down Expand Up @@ -44,7 +44,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules/")

# hdf5
set (WITH_LOCAL_HDF5 OFF CACHE BOOL "Also builds and installs an example executable which allows to serialize and deserialize a basic EPC document.")
set (WITH_LOCAL_HDF5 OFF CACHE BOOL "Force usage of a local and exotic version HDF5.")
IF(WITH_LOCAL_HDF5)
set (HDF5_INCLUDE_DIRS HDF5_INCLUDE_DIRS-NOTFOUND CACHE PATH "Path to the directory which contains the hdf5 header files")
IF (NOT IS_DIRECTORY ${HDF5_INCLUDE_DIRS})
Expand Down Expand Up @@ -75,6 +75,7 @@ ELSE()
ENDIF()
ENDIF()

# Only the zlib.h file is mandatory. The libray may be not if, for instance, zlib symbols are already in minizip library (and HDF5 has no zlib support)
FIND_PACKAGE (ZLIB)
IF (NOT EXISTS ${ZLIB_INCLUDE_DIR})
MESSAGE(ERROR " The ZLIB_INCLUDE_DIR CMake variable is mandatory.")
Expand Down
48 changes: 31 additions & 17 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,42 @@ if (WIN32)
endif ()
endif (WIN32)

target_compile_definitions(${CPP_LIBRARY_NAME} PRIVATE ${HDF5_DEFINITIONS})

# Linker instructions
if (WITH_LOCAL_HDF5 OR CMAKE_VERSION VERSION_LESS "3.19")
# Do not use target because find_package has not been used
target_include_directories(${CPP_LIBRARY_NAME} SYSTEM PRIVATE ${HDF5_INCLUDE_DIRS})
target_compile_definitions(${CPP_LIBRARY_NAME} PRIVATE ${HDF5_DEFINITIONS})
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE ${HDF5_LIBRARIES})
else()
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE hdf5::hdf5)
# At least on windows
# hdf5-shared and hdf5-static are the prefered targets to use when using cmake config mode (which is called by the findHDF5 module) with official HDF5 sources
# hdf5::hdf5-shared and hdf5::hdf5-static are the prefered targets to use when using cmake config mode with official HDF5 cmake sources
# these targets are not defined when using pure cmake module mode
if (TARGET hdf5-static AND HDF5_USE_STATIC_LIBRARIES)
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE hdf5-static)
elseif (TARGET hdf5::hdf5-static AND HDF5_USE_STATIC_LIBRARIES)
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE hdf5::hdf5-static)
elseif (TARGET hdf5-shared AND NOT HDF5_USE_STATIC_LIBRARIES)
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE hdf5-shared)
elseif (TARGET hdf5::hdf5-shared AND NOT HDF5_USE_STATIC_LIBRARIES)
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE hdf5::hdf5-shared)
# At least on windows
# hdf5::hdf5 is the prefered target to use when using pure cmake module mode (which does not define *-shared and *-static targets)
# this target looks to incorrectly export compile definitions in case of shared lib forcing us to redefine the compile definitions
else ()
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE hdf5::hdf5)
target_compile_definitions(${CPP_LIBRARY_NAME} PRIVATE ${HDF5_DEFINITIONS})
endif()
endif()
target_link_libraries (${CPP_LIBRARY_NAME}
PRIVATE ZLIB::ZLIB
PRIVATE MINIZIP::MINIZIP)
if (WIN32)
if (EXISTS ${ZLIB_LIBRARY_DEBUG} AND EXISTS ${MINIZIP_LIBRARY_DEBUG} AND EXISTS ${HDF5_hdf5_LIBRARY_DEBUG})
set(CMAKE_CONFIGURATION_TYPES "Release;MinSizeRel;RelWithDebInfo;Debug" CACHE STRING "" FORCE)
endif ()
if (NOT EXISTS ${ZLIB_LIBRARY_DEBUG} OR NOT EXISTS ${MINIZIP_LIBRARY_DEBUG} OR NOT EXISTS ${HDF5_hdf5_LIBRARY_DEBUG})
set(CMAKE_CONFIGURATION_TYPES "Release;MinSizeRel;RelWithDebInfo" CACHE STRING "" FORCE)
endif ()
if (NOT EXISTS ${ZLIB_LIBRARY_RELEASE} OR NOT EXISTS ${MINIZIP_LIBRARY_RELEASE} OR NOT EXISTS ${HDF5_hdf5_LIBRARY_RELEASE})
set(CMAKE_CONFIGURATION_TYPES "Debug" CACHE STRING "" FORCE)
endif ()

if (TARGET ZLIB::ZLIB)
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE ZLIB::ZLIB MINIZIP::MINIZIP)
else ()
target_include_directories(${CPP_LIBRARY_NAME} SYSTEM PRIVATE ${ZLIB_INCLUDE_DIR})
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE MINIZIP::MINIZIP)
endif ()

if (WIN32)
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE Boost::boost)

# szip is linked because it is a potential dependency of hdf5 which is sometimes statically linked to fesapi. If hdf5 would be dynamically linked from fesapi (or if hdf5 does not depend at all to szip), szip could be not present in these linked libraries.
Expand Down Expand Up @@ -201,7 +215,7 @@ list(APPEND ALL_SOURCES_AND_HEADERS

target_sources(${CPP_LIBRARY_NAME} PRIVATE ${ALL_SOURCES_AND_HEADERS})

target_include_directories(${CPP_LIBRARY_NAME} SYSTEM PRIVATE ${ZLIB_INCLUDE_DIR} ${HDF5_INCLUDE_DIRS} ${MINIZIP_INCLUDE_DIR})
target_include_directories(${CPP_LIBRARY_NAME} SYSTEM PRIVATE ${MINIZIP_INCLUDE_DIR})

target_include_directories(${CPP_LIBRARY_NAME} INTERFACE
$<INSTALL_INTERFACE:include>
Expand Down

0 comments on commit 0b3bfc2

Please sign in to comment.