From 6952d26e78c5d8748989af9c8769b7991716e365 Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Fri, 16 Feb 2024 09:57:43 +0100 Subject: [PATCH] Fix CMake to run tests for optional dependencies. --- cmake/HighFiveOptionalDependencies.cmake | 53 ++++++++++++++++------- tests/unit/CMakeLists.txt | 3 ++ tests/unit/data_generator.hpp | 4 +- tests/unit/supported_types.hpp | 6 +-- tests/unit/tests_high_five.hpp | 2 +- tests/unit/tests_high_five_base.cpp | 18 ++++---- tests/unit/tests_high_five_easy.cpp | 8 ++-- tests/unit/tests_high_five_multi_dims.cpp | 4 +- 8 files changed, 61 insertions(+), 37 deletions(-) diff --git a/cmake/HighFiveOptionalDependencies.cmake b/cmake/HighFiveOptionalDependencies.cmake index 53d10ecba..6b4422e71 100644 --- a/cmake/HighFiveOptionalDependencies.cmake +++ b/cmake/HighFiveOptionalDependencies.cmake @@ -1,27 +1,48 @@ -if(HIGHFIVE_TEST_BOOST AND NOT TARGET HighFiveBoostDependency) +if(NOT TARGET HighFiveBoostDependency) add_library(HighFiveBoostDependency INTERFACE) - find_package(Boost REQUIRED) - target_link_libraries(HighFiveBoostDependency INTERFACE Boost::headers) - # TODO check if we need Boost::disable_autolinking to cause: - # -DBOOST_ALL_NO_LIB (does something on MSVC). + if(HIGHFIVE_TEST_BOOST) + find_package(Boost REQUIRED) + target_link_libraries(HighFiveBoostDependency INTERFACE Boost::headers) + # TODO check if we need Boost::disable_autolinking to cause: + # -DBOOST_ALL_NO_LIB (does something on MSVC). + target_compile_definitions(HighFiveBoostDependency INTERFACE HIGHFIVE_TEST_BOOST=1) + endif() endif() -if(HIGHFIVE_TEST_EIGEN AND NOT TARGET HighFiveEigenDependency) +if(NOT TARGET HighFiveEigenDependency) add_library(HighFiveEigenDependency INTERFACE) - find_package(Eigen3 REQUIRED NO_MODULE) - target_link_libraries(HighFiveEigenDependency INTERFACE Eigen3::Eigen) + if(HIGHFIVE_TEST_EIGEN AND NOT TARGET HighFiveEigenDependency) + find_package(Eigen3 REQUIRED NO_MODULE) + target_link_libraries(HighFiveEigenDependency INTERFACE Eigen3::Eigen) + target_compile_definitions(HighFiveEigenDependency INTERFACE HIGHFIVE_TEST_EIGEN=1) + endif() endif() -if(HIGHFIVE_TEST_XTENSOR AND NOT TARGET HighFiveXTensorDependency) +if(NOT TARGET HighFiveXTensorDependency) add_library(HighFiveXTensorDependency INTERFACE) - find_package(xtensor REQUIRED) - target_link_libraries(HighFiveXTensorDependency INTERFACE xtensor) + if(HIGHFIVE_TEST_XTENSOR AND NOT TARGET HighFiveXTensorDependency) + find_package(xtensor REQUIRED) + target_link_libraries(HighFiveXTensorDependency INTERFACE xtensor) + target_compile_definitions(HighFiveXTensorDependency INTERFACE HIGHFIVE_TEST_XTENSOR=1) + endif() endif() -if(HIGHFIVE_TEST_OPENCV AND NOT TARGET HighFiveOpenCVDependency) +if(NOT TARGET HighFiveOpenCVDependency) add_library(HighFiveOpenCVDependency INTERFACE) - find_package(OpenCV REQUIRED) - target_include_directories(HighFiveOpenCVDependency SYSTEM INTERFACE ${OpenCV_INCLUDE_DIRS}) - target_link_libraries(HighFiveOpenCVDependency INTERFACE ${OpenCV_LIBS}) - target_compile_definitions(HighFiveOpenCVDependency INTERFACE H5_USE_OPENCV) + if(HIGHFIVE_TEST_OPENCV AND NOT TARGET HighFiveOpenCVDependency) + find_package(OpenCV REQUIRED) + target_include_directories(HighFiveOpenCVDependency SYSTEM INTERFACE ${OpenCV_INCLUDE_DIRS}) + target_link_libraries(HighFiveOpenCVDependency INTERFACE ${OpenCV_LIBS}) + target_compile_definitions(HighFiveOpenCVDependency INTERFACE HIGHFIVE_TEST_OPENCV=1) + endif() +endif() + +if(NOT TARGET HighFiveOptionalDependencies) + add_library(HighFiveOptionalDependencies INTERFACE) + target_link_libraries(HighFiveOptionalDependencies INTERFACE + HighFiveBoostDependency + HighFiveEigenDependency + HighFiveXTensorDependency + HighFiveOpenCVDependency + ) endif() diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 93533ee91..048ccd9b6 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -9,6 +9,8 @@ endif() foreach(test_name tests_high_five_base tests_high_five_multi_dims tests_high_five_easy test_all_types test_high_five_selection tests_high_five_data_type test_legacy) add_executable(${test_name} "${test_name}.cpp") target_link_libraries(${test_name} HighFive HighFiveWarnings Catch2::Catch2WithMain) + target_link_libraries(${test_name} HighFiveOptionalDependencies) + catch_discover_tests(${test_name}) endforeach() @@ -18,6 +20,7 @@ if(HIGHFIVE_PARALLEL_HDF5) ## parallel MPI tests add_executable(tests_parallel_bin ${tests_parallel_src}) target_link_libraries(tests_parallel_bin HighFive HighFiveWarnings Catch2::Catch2) + target_link_libraries(tests_parallel_bin HighFiveOptionalDependencies) # We need to patch in a call to `mpirun` or equivalent when using # parallel tests. Somehow, this is not foreseen in Catch2, modify the diff --git a/tests/unit/data_generator.hpp b/tests/unit/data_generator.hpp index f5dc681c5..9c17dfe81 100644 --- a/tests/unit/data_generator.hpp +++ b/tests/unit/data_generator.hpp @@ -7,7 +7,7 @@ #include #include -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST #include #endif @@ -200,7 +200,7 @@ struct ContainerTraits>: public STLLikeContainerTraits struct ContainerTraits> { using container_type = typename boost::multi_array; diff --git a/tests/unit/supported_types.hpp b/tests/unit/supported_types.hpp index f708303b1..843814f91 100644 --- a/tests/unit/supported_types.hpp +++ b/tests/unit/supported_types.hpp @@ -6,7 +6,7 @@ #include #include -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST #include #endif @@ -30,7 +30,7 @@ struct STDArray { using type = std::array, n>; }; -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST template struct BoostMultiArray { template @@ -83,7 +83,7 @@ using scalar_types = typename ConcatenateTuples>::type; using supported_array_types = typename ConcatenateTuples< -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST typename ContainerProduct, scalar_types_boost>::type, typename ContainerProduct>, scalar_types_boost>::type, typename ContainerProduct>, scalar_types_boost>::type, diff --git a/tests/unit/tests_high_five.hpp b/tests/unit/tests_high_five.hpp index fa0cfd714..25839c69e 100644 --- a/tests/unit/tests_high_five.hpp +++ b/tests/unit/tests_high_five.hpp @@ -42,7 +42,7 @@ using base_test_types = std::tuple; -#ifdef H5_USE_HALF_FLOAT +#ifdef HIGHFIVE_TEST_HALF_FLOAT #include using float16_t = half_float::half; diff --git a/tests/unit/tests_high_five_base.cpp b/tests/unit/tests_high_five_base.cpp index bd00ce15c..0ef9715bd 100644 --- a/tests/unit/tests_high_five_base.cpp +++ b/tests/unit/tests_high_five_base.cpp @@ -27,11 +27,11 @@ #include #include "tests_high_five.hpp" -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST #include #endif -#ifdef H5_USE_EIGEN +#ifdef HIGHFIVE_TEST_EIGEN #include #endif @@ -1529,7 +1529,7 @@ struct CreateEmptyVector { } }; -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST template struct CreateEmptyBoostMultiArray { using container_type = boost::multi_array(n_dim)>; @@ -1546,7 +1546,7 @@ struct CreateEmptyBoostMultiArray { #endif -#ifdef H5_USE_EIGEN +#ifdef HIGHFIVE_TEST_EIGEN struct CreateEmptyEigenVector { using container_type = Eigen::VectorXi; @@ -1676,7 +1676,7 @@ void check_empty_everything(const std::vector& dims) { } } -#ifdef H5_USE_EIGEN +#ifdef HIGHFIVE_TEST_EIGEN template void check_empty_eigen(const std::vector&) {} @@ -1703,13 +1703,13 @@ void check_empty(const std::vector& dims) { check_empty_everything>(dims); } -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST SECTION("boost::multi_array") { check_empty_everything>(dims); } #endif -#ifdef H5_USE_EIGEN +#ifdef HIGHFIVE_TEST_EIGEN check_empty_eigen(dims); #endif } @@ -2559,7 +2559,7 @@ TEST_CASE("HighFiveDataTypeClass") { CHECK(((Float | String) & String) == String); } -#ifdef H5_USE_EIGEN +#ifdef HIGHFIVE_TEST_EIGEN template void test_eigen_vec(File& file, const std::string& test_flavor, const T& vec_input, T& vec_output) { @@ -2636,7 +2636,7 @@ TEST_CASE("HighFiveEigen") { CHECK_THROWS(test_eigen_vec(file, ds_name_flavor, vec_in, vec_out)); } -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST // boost::multi_array { ds_name_flavor = "BMultiEigenVector3f"; diff --git a/tests/unit/tests_high_five_easy.cpp b/tests/unit/tests_high_five_easy.cpp index e003c3234..16e64decc 100644 --- a/tests/unit/tests_high_five_easy.cpp +++ b/tests/unit/tests_high_five_easy.cpp @@ -22,7 +22,7 @@ #include -#ifdef H5_USE_XTENSOR +#ifdef HIGHFIVE_TEST_XTENSOR #include #include #endif @@ -179,7 +179,7 @@ TEST_CASE("H5Easy_Attribute_scalar") { CHECK(c == c_r); } -#ifdef H5_USE_XTENSOR +#ifdef HIGHFIVE_TEST_XTENSOR TEST_CASE("H5Easy_extend1d") { H5Easy::File file("h5easy_extend1d.h5", H5Easy::File::Overwrite); @@ -304,7 +304,7 @@ TEST_CASE("H5Easy_Attribute_xtensor") { } #endif -#ifdef H5_USE_EIGEN +#ifdef HIGHFIVE_TEST_EIGEN TEST_CASE("H5Easy_Eigen_MatrixX") { H5Easy::File file("h5easy_eigen_MatrixX.h5", H5Easy::File::Overwrite); @@ -439,7 +439,7 @@ TEST_CASE("H5Easy_Attribute_Eigen_MatrixX") { } #endif -#ifdef H5_USE_OPENCV +#ifdef HIGHFIVE_TEST_OPENCV TEST_CASE("H5Easy_OpenCV_Mat_") { H5Easy::File file("h5easy_opencv_Mat_.h5", H5Easy::File::Overwrite); diff --git a/tests/unit/tests_high_five_multi_dims.cpp b/tests/unit/tests_high_five_multi_dims.cpp index 31757d6c5..60ec66cae 100644 --- a/tests/unit/tests_high_five_multi_dims.cpp +++ b/tests/unit/tests_high_five_multi_dims.cpp @@ -13,7 +13,7 @@ #include -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST #include #include #endif @@ -128,7 +128,7 @@ TEMPLATE_LIST_TEST_CASE("vector of array", "[template]", numerical_test_types) { } -#ifdef H5_USE_BOOST +#ifdef HIGHFIVE_TEST_BOOST template void MultiArray3DTest() {