CMake: zlib dependency not properly propagted by hdf5 targets

see https://github.com/Microsoft/vcpkg/issues/4895

1 Like

zlib in hdf5 is private and static because zlib is used internally and hdf5 does not have a requirement to expose the zlib interface.
The requirement for zlib linking should be then under downstream application control.

Allen

So i found the real culprit. I don’t know if this is a HDF5 or a CMake issue.

The problem is within hdf5-targets.cmake which is different between debug and release builds.

E.g.:
In debug it contains:

set_target_properties(hdf5::hdf5-static PROPERTIES
  INTERFACE_COMPILE_DEFINITIONS "\$<IF:\$<CONFIG:Debug>,DEBUG,NDEBUG>"
  INTERFACE_INCLUDE_DIRECTORIES "G:/MPINew/mpi_vcpkg/buildtrees/hdf5/x64-windows-dbg/src;G:/MPINew/mpi_vcpkg/buildtrees/hdf5/src/CMake-hdf5-1.10.4/hdf5-1.10.4/src"
  INTERFACE_LINK_LIBRARIES "\$<\$<C_COMPILER_ID:MSVC>:>;\$<\$<CXX_COMPILER_ID:MSVC>:>;\$<\$<NOT:\$<CONFIG:DEBUG>>:G:/MPINew/mpi_vcpkg/installed/x64-windows/lib/zlib.lib>;\$<\$<CONFIG:DEBUG>:G:/MPINew/mpi_vcpkg/installed/x64-windows/debug/lib/zlibd.lib>;\$<LINK_ONLY:szip-static>;\$<LINK_ONLY:\$<\$<BOOL:OFF>:>>;\$<\$<NOT:\$<PLATFORM_ID:Windows>>:>"
)

the important part here is:

$<$<NOT:$CONFIG:DEBUG>:G:/MPINew/mpi_vcpkg/installed/x64-windows/lib/zlib.lib>;$<$CONFIG:DEBUG:G:/MPINew/mpi_vcpkg/installed/x64-windows/debug/lib/zlibd.lib>;

while in Release:

set_target_properties(hdf5::hdf5-static PROPERTIES
  INTERFACE_COMPILE_DEFINITIONS "\$<IF:\$<CONFIG:Debug>,DEBUG,NDEBUG>"
  INTERFACE_INCLUDE_DIRECTORIES "G:/MPINew/mpi_vcpkg/buildtrees/hdf5/x64-windows-rel/src;G:/MPINew/mpi_vcpkg/buildtrees/hdf5/src/CMake-hdf5-1.10.4/hdf5-1.10.4/src"
  INTERFACE_LINK_LIBRARIES "\$<\$<C_COMPILER_ID:MSVC>:>;\$<\$<CXX_COMPILER_ID:MSVC>:>;G:/MPINew/mpi_vcpkg/installed/x64-windows/lib/zlib.lib;\$<LINK_ONLY:szip-static>;\$<LINK_ONLY:\$<\$<BOOL:OFF>:>>;\$<\$<NOT:\$<PLATFORM_ID:Windows>>:>"
)

the important part here:

G:/MPINew/mpi_vcpkg/installed/x64-windows/lib/zlib.lib;

so the release version contains less information.
So my question now would be: Is HDF5 generating the two different files or is this a CMake issue?

(also the LINK_ONLY:szip-static will not work. )

Any progress on this issue ?

Unfortunately, I don’t think I’ve gotten this correct yet. Although, all the dependent links now show:

INTERFACE_LINK_LIBRARIES
“$<$<C_COMPILER_ID:MSVC>:>;$<$<CXX_COMPILER_ID:MSVC>:>;
$<LINK_ONLY:zlib-static>;$<LINK_ONLY:szip-static>;
$<LINK_ONLY:$<$BOOL:OFF:>>;$<$<NOT:$<PLATFORM_ID:Windows>>:>”

Allen

I think the problem lies with szip and how it defines the target. with only szip-static cmake things it its a library and not an imported target. So szip should define targest SZIP::szip-static and SZIP::szip-shared so that cmake has a chance to figure out that szip-static and szip-shared are targets and not libraries.

screw that: The real problem is that hdf5-config.cmake does never reach the code:
include (${PACKAGE_PREFIX_DIR}/share/SZIP/SZIP-targets.cmake)
thus not including the szip-targets and failing to link against szip.

the code is guarded by:
if (NOT TARGET "hdf5")
if (${HDF5_PACKAGE_NAME}_ENABLE_SZIP_SUPPORT AND ${HDF5_PACKAGE_NAME}_PACKAGE_EXTLIBS AND NOT TARGET "szip")

Maybe the last AND should be an OR?

concerning the zlib release/debug issue. I found a comment in the vtk port of vcpkg:

For VTK vcpkg_fixup_cmake_targets is not enough:
Files for system third party dependencies are written to modules that
are located in the paths share/vtk/Modules and debug/share/vtk/Modules.
In the release folder, only the release libraries are referenced (e.g. “C:/vcpkg/installed/x64-windows/lib/zlib.lib”).
But in the debug folder both libraries (e.g. “optimized;C:/vcpkg/installed/x64-windows/lib/zlib.lib;debug;C:/vcpkg/installed/x64-windows/debug/lib/zlibd.lib”)
or only the debug library (e.g. “C:/vcpkg/installed/x64-windows/debug/lib/hdf5_D.lib”) is referenced.
This is because VCPKG appends only the release library prefix (…/x64-windows/lib)
when configuring release but both (…/x64-windows/lib and …/x64-windows/debug/lib)
when configuring debug.

so the zlib issue is not an hdf5 issue but an vcpkg issue

I will take a look and try some things - maybe we can get a solution for the next release of hdf5.
Also maybe the AND NOT TARGET “zlib/szip” should just be deleted?

Allen

Ok I think I figured this one out:

The problem is that vcpkg builds without HDF5_PACKAGE_EXTLIBS=ON so the szip target file is not included in hdf5config.cmake. On the other hand hdf5config.cmake is missing a find_package(SZIP) call if HDF5_PACKAGE_EXTLIBS=OFF and the target szip is not yet defined!

The part in the config should look like:

if (${HDF5_PACKAGE_NAME}_ENABLE_SZIP_SUPPORT AND NOT TARGET “szip”)
if(${HDF5_PACKAGE_NAME}_PACKAGE_EXTLIBS)
include (${PACKAGE_PREFIX_DIR}/share/SZIP/SZIP-targets.cmake)
else()
find_package(SZIP REQUIRED)
endif()
endif()

the same for zlib (although currently zlib does not define targets and as such the full paths to the libs is embedded by cmake within hdf5-targets.cmake )