From 182ad8d7a58289791339e81245419dc161fdae42 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Mon, 26 Sep 2022 17:44:56 +0200 Subject: [PATCH] (#13114) highfive: conan v2 support + fix CMake imported target + bump dependencies * conan v2 support * bump dependencies --- recipes/highfive/all/CMakeLists.txt | 7 - recipes/highfive/all/conanfile.py | 131 +++++++++++------- .../highfive/all/test_package/CMakeLists.txt | 12 +- .../highfive/all/test_package/conanfile.py | 25 ++-- .../all/test_v1_package/CMakeLists.txt | 11 ++ .../highfive/all/test_v1_package/conanfile.py | 17 +++ 6 files changed, 132 insertions(+), 71 deletions(-) delete mode 100644 recipes/highfive/all/CMakeLists.txt create mode 100644 recipes/highfive/all/test_v1_package/CMakeLists.txt create mode 100644 recipes/highfive/all/test_v1_package/conanfile.py diff --git a/recipes/highfive/all/CMakeLists.txt b/recipes/highfive/all/CMakeLists.txt deleted file mode 100644 index 4d393c7a86c09a..00000000000000 --- a/recipes/highfive/all/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.1.2) -project(cmake_wrapper) - -include(conanbuildinfo.cmake) -conan_basic_setup(TARGETS) - -add_subdirectory("source_subfolder") diff --git a/recipes/highfive/all/conanfile.py b/recipes/highfive/all/conanfile.py index 7328169f3ba4c4..2547f248b8e4c8 100644 --- a/recipes/highfive/all/conanfile.py +++ b/recipes/highfive/all/conanfile.py @@ -1,7 +1,11 @@ -from conans import CMake, ConanFile, tools -import os.path +from conan import ConanFile +from conan.tools.build import check_min_cppstd +from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout +from conan.tools.files import copy, get, replace_in_file, rmdir, save +import os +import textwrap -required_conan_version = ">=1.33.0" +required_conan_version = ">=1.50.0" class HighFiveConan(ConanFile): @@ -11,8 +15,7 @@ class HighFiveConan(ConanFile): topics = ("hdf5", "hdf", "data") homepage = "https://github.com/BlueBrain/HighFive" url = "https://github.com/conan-io/conan-center-index" - exports_sources = ["CMakeLists.txt"] - generators = "cmake", "cmake_find_package" + settings = "os", "arch", "compiler", "build_type" options = { "with_boost": [True, False], @@ -27,72 +30,98 @@ class HighFiveConan(ConanFile): "with_opencv": True, } - _cmake = None - - @property - def _source_subfolder(self): - return "source_subfolder" - - @property - def _build_subfolder(self): - return "build_subfolder" + def layout(self): + cmake_layout(self, src_folder="src") def requirements(self): - self.requires("hdf5/1.12.0") + self.requires("hdf5/1.13.1") if self.options.with_boost: - self.requires("boost/1.77.0") + self.requires("boost/1.80.0") if self.options.with_eigen: self.requires("eigen/3.4.0") if self.options.with_xtensor: - self.requires("xtensor/0.23.10") + self.requires("xtensor/0.24.2") if self.options.with_opencv: - self.requires("opencv/4.5.3") + self.requires("opencv/4.5.5") + + def package_id(self): + self.info.clear() + + def validate(self): + if self.settings.compiler.get_safe("cppstd"): + check_min_cppstd(self, 11) def source(self): - tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True) + get(self, **self.conan_data["sources"][self.version], + destination=self.source_folder, strip_root=True) - def build(self): - tools.replace_in_file( - os.path.join(self._source_subfolder, "CMake", "HighFiveTargetDeps.cmake"), + def generate(self): + tc = CMakeToolchain(self) + tc.variables["USE_BOOST"] = self.options.with_boost + tc.variables["USE_EIGEN"] = self.options.with_eigen + tc.variables["USE_XTENSOR"] = self.options.with_xtensor + tc.variables["USE_OPENCV"] = self.options.with_opencv + tc.variables["HIGHFIVE_UNIT_TESTS"] = False + tc.variables["HIGHFIVE_EXAMPLES"] = False + tc.variables["HIGHFIVE_BUILD_DOCS"] = False + tc.variables["HIGHFIVE_USE_INSTALL_DEPS"] = False + tc.generate() + deps = CMakeDeps(self) + deps.generate() + + def _patch_sources(self): + replace_in_file( + self, + os.path.join(self.source_folder, "CMake", "HighFiveTargetDeps.cmake"), "find_package(Eigen3 NO_MODULE)", "find_package(Eigen3 REQUIRED)", ) - tools.replace_in_file( - os.path.join(self._source_subfolder, "CMake", "HighFiveTargetDeps.cmake"), + replace_in_file( + self, + os.path.join(self.source_folder, "CMake", "HighFiveTargetDeps.cmake"), "EIGEN3_INCLUDE_DIRS", "Eigen3_INCLUDE_DIRS", ) - cmake = self._configure_cmake() + def build(self): + self._patch_sources() + cmake = CMake(self) + cmake.configure() cmake.build() - def _configure_cmake(self): - if self._cmake: - return self._cmake - self._cmake = CMake(self) - self._cmake.definitions["USE_BOOST"] = self.options.with_boost - self._cmake.definitions["USE_EIGEN"] = self.options.with_eigen - self._cmake.definitions["USE_XTENSOR"] = self.options.with_xtensor - self._cmake.definitions["USE_OPENCV"] = self.options.with_opencv - self._cmake.definitions["HIGHFIVE_UNIT_TESTS"] = False - self._cmake.definitions["HIGHFIVE_EXAMPLES"] = False - self._cmake.definitions["HIGHFIVE_BUILD_DOCS"] = False - self._cmake.definitions["HIGHFIVE_USE_INSTALL_DEPS"] = False - self._cmake.configure(build_folder=self._build_subfolder) - return self._cmake - - def package_id(self): - self.info.header_only() - def package(self): - self.copy(pattern="LICENSE", dst="licenses", src=self._source_subfolder) - cmake = self._configure_cmake() + copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) + cmake = CMake(self) cmake.install() - tools.rmdir(os.path.join(self.package_folder, "share")) + rmdir(self, os.path.join(self.package_folder, "share")) + + # TODO: to remove in conan v2 once legacy generators removed + self._create_cmake_module_alias_targets( + os.path.join(self.package_folder, self._module_file_rel_path), + {"HighFive": "HighFive::HighFive"}, + ) + + def _create_cmake_module_alias_targets(self, module_file, targets): + content = "" + for alias, aliased in targets.items(): + content += textwrap.dedent(f"""\ + if(TARGET {aliased} AND NOT TARGET {alias}) + add_library({alias} INTERFACE IMPORTED) + set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased}) + endif() + """) + save(self, module_file, content) + + @property + def _module_file_rel_path(self): + return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake") def package_info(self): - self.cpp_info.names["cmake_find_package"] = "HighFive" - self.cpp_info.names["cmake_find_package_multi"] = "HighFive" + self.cpp_info.set_property("cmake_file_name", "HighFive") + self.cpp_info.set_property("cmake_target_name", "HighFive") + self.cpp_info.bindirs = [] + self.cpp_info.libdirs = [] + self.cpp_info.resdirs = [] self.cpp_info.requires = ["hdf5::hdf5"] if self.options.with_boost: self.cpp_info.requires.append("boost::headers") @@ -102,3 +131,9 @@ def package_info(self): self.cpp_info.requires.append("xtensor::xtensor") if self.options.with_opencv: self.cpp_info.requires.append("opencv::opencv") + + # TODO: to remove in conan v2 once legacy generators removed + self.cpp_info.names["cmake_find_package"] = "HighFive" + self.cpp_info.names["cmake_find_package_multi"] = "HighFive" + self.cpp_info.build_modules["cmake_find_package"] = [self._module_file_rel_path] + self.cpp_info.build_modules["cmake_find_package_multi"] = [self._module_file_rel_path] diff --git a/recipes/highfive/all/test_package/CMakeLists.txt b/recipes/highfive/all/test_package/CMakeLists.txt index 49b6d7a88fe87b..9094d8eb2cd6d2 100644 --- a/recipes/highfive/all/test_package/CMakeLists.txt +++ b/recipes/highfive/all/test_package/CMakeLists.txt @@ -1,12 +1,8 @@ -cmake_minimum_required(VERSION 3.1) - +cmake_minimum_required(VERSION 3.8) project(test_package LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 11) - -include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup(TARGETS) -find_package(HighFive 2.3 REQUIRED) +find_package(HighFive REQUIRED CONFIG) add_executable(test_package test_package.cpp) -target_link_libraries(test_package HighFive::HighFive) +target_link_libraries(test_package PRIVATE HighFive) +target_compile_features(test_package PRIVATE cxx_std_11) diff --git a/recipes/highfive/all/test_package/conanfile.py b/recipes/highfive/all/test_package/conanfile.py index 817752e58b49ad..0a6bc68712d901 100644 --- a/recipes/highfive/all/test_package/conanfile.py +++ b/recipes/highfive/all/test_package/conanfile.py @@ -1,10 +1,19 @@ -from conans import CMake, ConanFile, tools -import os.path +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.cmake import CMake, cmake_layout +import os -class HighFiveTestConan(ConanFile): - settings = "os", "compiler", "build_type", "arch" - generators = "cmake", "cmake_find_package" +class TestPackageConan(ConanFile): + settings = "os", "arch", "compiler", "build_type" + generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv" + test_type = "explicit" + + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires(self.tested_reference_str) def build(self): cmake = CMake(self) @@ -12,6 +21,6 @@ def build(self): cmake.build() def test(self): - if tools.cross_building(self): - bin_path = os.path.join("bin", "test_package") - self.run(bin_path, run_environment=True) + if can_run(self): + bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package") + self.run(bin_path, env="conanrun") diff --git a/recipes/highfive/all/test_v1_package/CMakeLists.txt b/recipes/highfive/all/test_v1_package/CMakeLists.txt new file mode 100644 index 00000000000000..671f071b8ed9f7 --- /dev/null +++ b/recipes/highfive/all/test_v1_package/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.8) +project(test_package LANGUAGES CXX) + +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup(TARGETS) + +find_package(HighFive REQUIRED CONFIG) + +add_executable(test_package ../test_package/test_package.cpp) +target_link_libraries(test_package PRIVATE HighFive) +target_compile_features(test_package PRIVATE cxx_std_11) diff --git a/recipes/highfive/all/test_v1_package/conanfile.py b/recipes/highfive/all/test_v1_package/conanfile.py new file mode 100644 index 00000000000000..4453fcd822f20b --- /dev/null +++ b/recipes/highfive/all/test_v1_package/conanfile.py @@ -0,0 +1,17 @@ +from conans import CMake, ConanFile, tools +import os + + +class TestPackageConan(ConanFile): + settings = "os", "arch", "compiler", "build_type" + generators = "cmake", "cmake_find_package_multi" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def test(self): + if tools.cross_building(self): + bin_path = os.path.join("bin", "test_package") + self.run(bin_path, run_environment=True)