Skip to content

Commit

Permalink
[cmake] Directly use imported targets provided for hdf5
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Jul 22, 2024
1 parent 18c560f commit 665a549
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
30 changes: 5 additions & 25 deletions c++/h5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
3 changes: 3 additions & 0 deletions share/cmake/h5-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion test/c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 665a549

Please sign in to comment.