From 7ec9a535b2c8b24c38abd331fcecc5fb5068ec7d Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Tue, 5 Nov 2024 16:19:06 +0100 Subject: [PATCH] Guard targets in `HighFiveConfig.cmake`. (#1053) --- cmake/HighFiveConfig.cmake | 16 ++++--- .../test_cmake_integration.sh | 43 ++++++++++++------- .../test_dependent_library/CMakeLists.txt | 23 ++++++++++ 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/cmake/HighFiveConfig.cmake b/cmake/HighFiveConfig.cmake index 33ce7b7b0..a263264cf 100644 --- a/cmake/HighFiveConfig.cmake +++ b/cmake/HighFiveConfig.cmake @@ -8,12 +8,16 @@ if(HIGHFIVE_FIND_HDF5) find_dependency(HDF5) endif() -include("${CMAKE_CURRENT_LIST_DIR}/HighFiveTargets.cmake") +if(NOT TARGET HighFive) + include("${CMAKE_CURRENT_LIST_DIR}/HighFiveTargets.cmake") -if(HDF5_IS_PARALLEL) - find_dependency(MPI) - target_link_libraries(HighFive::HighFive INTERFACE MPI::MPI_C MPI::MPI_CXX) + if(HDF5_IS_PARALLEL) + find_dependency(MPI) + target_link_libraries(HighFive::HighFive INTERFACE MPI::MPI_C MPI::MPI_CXX) + endif() + + add_library(HighFive ALIAS HighFive::HighFive) + add_library(HighFiveInclude ALIAS HighFive::Include) endif() -add_library(HighFive ALIAS HighFive::HighFive) -add_library(HighFiveInclude ALIAS HighFive::Include) + diff --git a/tests/cmake_integration/test_cmake_integration.sh b/tests/cmake_integration/test_cmake_integration.sh index cf80fbfdd..74fd743c1 100644 --- a/tests/cmake_integration/test_cmake_integration.sh +++ b/tests/cmake_integration/test_cmake_integration.sh @@ -16,6 +16,15 @@ HIGHFIVE_INSTALL_DIR="${HIGHFIVE_BUILD_DIR}/install" export HIGHFIVE_GIT_REPOSITORY="file://$(realpath "$HIGHFIVE_DIR")" export HIGHFIVE_GIT_TAG=$(git rev-parse HEAD) +make_submodule() { + local project_dir="$1" + local dep_dir="${project_dir}/deps/HighFive" + + rm "${dep_dir}" || true + mkdir -p "$(dirname "${dep_dir}")" + ln -sf "${HIGHFIVE_DIR}" "${dep_dir}" +} + test_dependent_library() { local project="dependent_library" local project_dir="${TEST_DIR}/${project}" @@ -35,33 +44,35 @@ test_dependent_library() { cmake --build "${build_dir}" --verbose --target install - local test_project="test_dependent_library" - local test_build_dir="${TMP_DIR}/test_build" - local test_install_dir="${TMP_DIR}/test_build/install" - rm -rf ${test_build_dir} || true + for vendor in submodule fetch_content external none + do + local test_project="test_dependent_library" + local test_build_dir="${TMP_DIR}/test_build" + local test_install_dir="${TMP_DIR}/test_build/install" + + make_submodule ${test_project} - cmake -DUSE_BOOST=${use_boost} \ - -DCMAKE_PREFIX_PATH="${HIGHFIVE_INSTALL_DIR};${install_dir}" \ - -DCMAKE_INSTALL_PREFIX="${test_install_dir}" \ - -B "${test_build_dir}" "${test_project}" - cmake --build "${test_build_dir}" --verbose - ctest --test-dir "${test_build_dir}" --verbose + rm -rf ${test_build_dir} || true + cmake -DUSE_BOOST=${use_boost} \ + -DVENDOR_STRATEGY=${vendor} \ + -DCMAKE_PREFIX_PATH="${HIGHFIVE_INSTALL_DIR};${install_dir}" \ + -DCMAKE_INSTALL_PREFIX="${test_install_dir}" \ + -B "${test_build_dir}" "${test_project}" + + cmake --build "${test_build_dir}" --verbose + ctest --test-dir "${test_build_dir}" --verbose + done done } test_application() { local project="application" local project_dir="${TEST_DIR}/${project}" - local dep_dir="${TEST_DIR}/${project}/deps/HighFive" - - rm "${dep_dir}" || true - ln -sf "${HIGHFIVE_DIR}" "${dep_dir}" - echo ${HIGHFIVE_DIR} - echo ${dep_dir} + make_submodule ${project_dir} for vendor in submodule fetch_content external do diff --git a/tests/cmake_integration/test_dependent_library/CMakeLists.txt b/tests/cmake_integration/test_dependent_library/CMakeLists.txt index c9023f98a..15851c3c6 100644 --- a/tests/cmake_integration/test_dependent_library/CMakeLists.txt +++ b/tests/cmake_integration/test_dependent_library/CMakeLists.txt @@ -7,8 +7,31 @@ if(NOT DEFINED CMAKE_CXX_STANDARD) set(CMAKE_CXX_EXTENSIONS OFF) endif() +set(VENDOR_STRATEGY "submodule" CACHE STRING "Use 'submodule' for Git submodules, 'fetch_content' for FetchContent, 'external' for `find_package`, 'none' for making to attempt at finding HighFive.") + add_executable(test_hi5_dependent test_dependent_library.cpp) +if(${VENDOR_STRATEGY} STREQUAL "submodule") + # When vendoring via a Git submodule, this is the correct + # line to include HighFive. + add_subdirectory("deps/HighFive" EXCLUDE_FROM_ALL) +elseif(${VENDOR_STRATEGY} STREQUAL "fetch_content") + include(FetchContent) + FetchContent_Declare(HighFive + GIT_REPOSITORY $ENV{HIGHFIVE_GIT_REPOSITORY} + GIT_TAG $ENV{HIGHFIVE_GIT_TAG} + ) + FetchContent_MakeAvailable(HighFive) +elseif(${VENDOR_STRATEGY} STREQUAL "external") + # When HighFive is installed like regular software and then "found", do the + # following: + find_package(HighFive REQUIRED) +endif() + +if(NOT ${VENDOR_STRATEGY} STREQUAL "none") + target_link_libraries(test_hi5_dependent PUBLIC HighFive::HighFive) +endif() + if(NOT USE_BOOST) find_package(Hi5Dependent REQUIRED) target_link_libraries(test_hi5_dependent PUBLIC Hi5Dependent::Read Hi5Dependent::Write)