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
  • Loading branch information
philippeVerney committed Jan 9, 2024
1 parent ea0aafb commit 1bbbfd0
Show file tree
Hide file tree
Showing 2 changed files with 17 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
31 changes: 14 additions & 17 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,25 @@ 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)
# Do not mix with hdf5::hdf5 (lowercase) which does not export HDF5 DEFINITIONS every time on windows
target_link_libraries (${CPP_LIBRARY_NAME} PRIVATE HDF5::HDF5)
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 +198,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 1bbbfd0

Please sign in to comment.