Skip to content

Commit

Permalink
(conan-io#13114) highfive: conan v2 support + fix CMake imported targ…
Browse files Browse the repository at this point in the history
…et + bump dependencies

* conan v2 support

* bump dependencies
  • Loading branch information
SpaceIm authored and kayoub5 committed Sep 29, 2022
1 parent 1716042 commit 182ad8d
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 71 deletions.
7 changes: 0 additions & 7 deletions recipes/highfive/all/CMakeLists.txt

This file was deleted.

131 changes: 83 additions & 48 deletions recipes/highfive/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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],
Expand All @@ -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")
Expand All @@ -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]
12 changes: 4 additions & 8 deletions recipes/highfive/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 17 additions & 8 deletions recipes/highfive/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
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)
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)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
11 changes: 11 additions & 0 deletions recipes/highfive/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions recipes/highfive/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 182ad8d

Please sign in to comment.