From 665a549de26d5063b510b6526d98471970eee678 Mon Sep 17 00:00:00 2001 From: Nils Wentzell Date: Mon, 22 Jul 2024 18:03:37 -0400 Subject: [PATCH] [cmake] Directly use imported targets provided for hdf5 --- c++/h5/CMakeLists.txt | 30 +++++------------------------- share/cmake/h5-config.cmake.in | 3 +++ test/c++/CMakeLists.txt | 2 +- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/c++/h5/CMakeLists.txt b/c++/h5/CMakeLists.txt index b2c46477..854c1af6 100644 --- a/c++/h5/CMakeLists.txt +++ b/c++/h5/CMakeLists.txt @@ -28,35 +28,15 @@ message(STATUS "-------- HDF5 detection -------------") find_package(HDF5 REQUIRED C HL) -# Create an interface target -add_library(hdf5 INTERFACE) -add_library(h5::hdf5 ALIAS hdf5) -target_include_directories(hdf5 SYSTEM INTERFACE ${HDF5_INCLUDE_DIRS}) -if(TARGET hdf5::hdf5 AND TARGET hdf5::hdf5_hl) - # Can't directly link against imported targets as they are not exportable - # Instead extract information on library locations - get_target_property(HDF5_LIBRARIES hdf5::hdf5 IMPORTED_LOCATION) - get_target_property(HDF5_HL_LIBRARIES hdf5::hdf5_hl IMPORTED_LOCATION) - # Starting with cmake 3.23 we may need to use IMPORTED_LOCATION_RELEASE property - if(NOT HDF5_LIBRARIES) - get_target_property(HDF5_LIBRARIES hdf5::hdf5 IMPORTED_LOCATION_RELEASE) - get_target_property(HDF5_HL_LIBRARIES hdf5::hdf5_hl IMPORTED_LOCATION_RELEASE) - endif() -endif() -target_link_libraries(hdf5 INTERFACE "${HDF5_LIBRARIES}" ${HDF5_HL_LIBRARIES}) -target_compile_options(hdf5 INTERFACE ${HDF5_DEFINITIONS}) +# Link against interface target and export +target_link_libraries(h5_c PRIVATE hdf5::hdf5 hdf5::hdf5_hl) if(HDF5_VERSION VERSION_GREATER 1.10) - target_compile_definitions(hdf5 INTERFACE H5_USE_110_API) + target_compile_definitions(h5_c PRIVATE H5_USE_110_API) endif() -if(HDF5_VERSION VERSION_GREATER_EQUAL 1.13) - target_compile_definitions(hdf5 INTERFACE H5_VER_GE_113) +if(HDF5_VERSION VERSIONSS_GREATER_EQUAL 1.13) + target_compile_definitions(h5_c PRIVATE H5_VER_GE_113) endif() -# Link against interface target and export -target_link_libraries(h5_c PRIVATE hdf5) -install(TARGETS hdf5 EXPORT h5-targets) - - # ========= Static Analyzer Checks ========== option(ANALYZE_SOURCES OFF "Run static analyzer checks if found (clang-tidy, cppcheck)") diff --git a/share/cmake/h5-config.cmake.in b/share/cmake/h5-config.cmake.in index b6bb5bbd..1674327d 100644 --- a/share/cmake/h5-config.cmake.in +++ b/share/cmake/h5-config.cmake.in @@ -23,6 +23,9 @@ set(@PROJECT_NAME@_ROOT @CMAKE_INSTALL_PREFIX@ CACHE STRING "@PROJECT_NAME@ root #endfunction() #find_dep(depname 1.0) +# Find hdf5 dependency +find_package(HDF5 REQUIRED C HL) + # Include the exported targets of this project include(@CMAKE_INSTALL_FULL_LIBDIR@/cmake/@PROJECT_NAME@/@PROJECT_NAME@-targets.cmake) diff --git a/test/c++/CMakeLists.txt b/test/c++/CMakeLists.txt index 59fca850..df3e7b42 100644 --- a/test/c++/CMakeLists.txt +++ b/test/c++/CMakeLists.txt @@ -11,7 +11,7 @@ foreach(test ${all_tests}) get_filename_component(test_name ${test} NAME_WE) get_filename_component(test_dir ${test} DIRECTORY) add_executable(${test_name} ${test}) - target_link_libraries(${test_name} ${PROJECT_NAME}::${PROJECT_NAME}_c ${PROJECT_NAME}_warnings gtest_main h5::hdf5) + target_link_libraries(${test_name} ${PROJECT_NAME}::${PROJECT_NAME}_c ${PROJECT_NAME}_warnings gtest_main hdf5::hdf5 hdf5::hdf5_hl) set_property(TARGET ${test_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${test_dir}) add_test(NAME ${test_name} COMMAND ${test_name} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${test_dir}) # Run clang-tidy if found