From 1bbbfd0beab5dad2cdd07a2c6661720487399305 Mon Sep 17 00:00:00 2001 From: Philippe Verney Date: Tue, 9 Jan 2024 17:23:57 +0100 Subject: [PATCH] Build Fix : Cannot dynamically link to HDF5 on Windows in CMAKE config mode --- CMakeLists.txt | 5 +++-- src/CMakeLists.txt | 31 ++++++++++++++----------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d7c5ded..ea0079de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) @@ -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}) @@ -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.") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0dac2d89..e3c573ca 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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. @@ -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 $