Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xtensor: conan v2 support #14298

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions recipes/xtensor/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,13 @@ sources:
patches:
"0.24.2":
- patch_file: "patches/0.24.0-cxx11-abi.patch"
base_path: "source_subfolder"
"0.24.0":
- patch_file: "patches/0.24.0-cxx11-abi.patch"
base_path: "source_subfolder"
"0.23.10":
- patch_file: "patches/0.23.9-cxx11-abi.patch"
base_path: "source_subfolder"
"0.23.9":
- patch_file: "patches/0.23.9-cxx11-abi.patch"
base_path: "source_subfolder"
"0.21.5":
- patch_file: "patches/0.21.5-cxx11-abi.patch"
base_path: "source_subfolder"
"0.21.4":
- patch_file: "patches/0.21.4-cxx11-abi.patch"
base_path: "source_subfolder"
93 changes: 54 additions & 39 deletions recipes/xtensor/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from conans import ConanFile, tools
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, save
from conan.tools.layout import basic_layout
from conan.tools.scm import Version
import os
import textwrap

required_conan_version = ">=1.43.0"
required_conan_version = ">=1.52.0"


class XtensorConan(ConanFile):
Expand All @@ -26,87 +30,98 @@ class XtensorConan(ConanFile):
}

@property
def _source_subfolder(self):
return "source_subfolder"
def _min_cppstd(self):
return "14"

@property
def _compilers_minimum_version(self):
# https://github.com/xtensor-stack/xtensor/blob/master/README.md
return {
"Visual Studio": "14",
"msvc": "190",
"gcc": "4.9",
"clang": "4",
}

def export_sources(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
self.copy(patch["patch_file"])
export_conandata_patches(self)

def layout(self):
basic_layout(self, src_folder="src")

def requirements(self):
self.requires("xtl/0.7.4")
self.requires("nlohmann_json/3.10.5")
self.requires("nlohmann_json/3.11.2")
if self.options.xsimd:
if tools.Version(self.version) < "0.24.0":
if Version(self.version) < "0.24.0":
self.requires("xsimd/7.5.0")
else:
self.requires("xsimd/8.1.0")
self.requires("xsimd/9.0.1")
if self.options.tbb:
self.requires("onetbb/2021.3.0")
self.requires("onetbb/2021.7.0")

def package_id(self):
self.info.clear()

def validate(self):
if self.options.tbb and self.options.openmp:
raise ConanInvalidConfiguration(
"The options 'tbb' and 'openmp' can not be used together."
)

# https://github.com/xtensor-stack/xtensor/blob/master/README.md
# - On Windows platforms, Visual C++ 2015 Update 2, or more recent
# - On Unix platforms, gcc 4.9 or a recent version of Clang
version = tools.Version(self.settings.compiler.version)
compiler = self.settings.compiler
if compiler == "Visual Studio" and version < "16":
if self.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, self._min_cppstd)

def loose_lt_semver(v1, v2):
lv1 = [int(v) for v in v1.split(".")]
lv2 = [int(v) for v in v2.split(".")]
min_length = min(len(lv1), len(lv2))
return lv1[:min_length] < lv2[:min_length]

minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
if minimum_version and loose_lt_semver(str(self.settings.compiler.version), minimum_version):
raise ConanInvalidConfiguration(
"xtensor requires at least Visual Studio version 15.9, please use 16"
f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support.",
)
if (compiler == "gcc" and version < "5.0") or (
compiler == "clang" and version < "4"
):
raise ConanInvalidConfiguration("xtensor requires at least C++14")

def package_id(self):
self.info.header_only()

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):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
apply_conandata_patches(self)

def package(self):
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
self.copy(
"*.hpp", dst="include", src=os.path.join(self._source_subfolder, "include")
)
copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
copy(self, "*.hpp", src=os.path.join(self.source_folder, "include"), dst=os.path.join(self.package_folder, "include"))

# TODO: to remove in conan v2 once cmake_find_package* generators removed
self._create_cmake_module_alias_targets(
os.path.join(self.package_folder, self._module_file_rel_path),
{"xtensor": "xtensor::xtensor"}
)

@staticmethod
def _create_cmake_module_alias_targets(module_file, targets):
def _create_cmake_module_alias_targets(self, module_file, targets):
content = ""
for alias, aliased in targets.items():
content += textwrap.dedent("""\
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()
""".format(alias=alias, aliased=aliased))
tools.save(module_file, content)
""")
save(self, module_file, content)

@property
def _module_file_rel_path(self):
return os.path.join("lib", "cmake", "conan-official-{}-targets.cmake".format(self.name))
return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake")

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "xtensor")
self.cpp_info.set_property("cmake_target_name", "xtensor")
self.cpp_info.set_property("pkg_config_name", "xtensor")
self.cpp_info.bindirs = []
self.cpp_info.libdirs = []
if self.options.xsimd:
self.cpp_info.defines.append("XTENSOR_USE_XSIMD")
if self.options.tbb:
Expand Down
7 changes: 2 additions & 5 deletions recipes/xtensor/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
cmake_minimum_required(VERSION 3.8)
project(test_package CXX)
project(test_package LANGUAGES CXX)

include(CheckCXXCompilerFlag)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(xtensor REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} xtensor)
target_link_libraries(${PROJECT_NAME} PRIVATE xtensor)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

foreach(flag "-march=native" "-mtune=native")
Expand Down
21 changes: 15 additions & 6 deletions recipes/xtensor/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os

from conans import ConanFile, CMake, tools


class XtensorTestConan(ConanFile):
class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
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 not tools.cross_building(self):
self.run(os.path.join("bin", "test_package"), 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")
8 changes: 8 additions & 0 deletions recipes/xtensor/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
${CMAKE_CURRENT_BINARY_DIR}/test_package)
17 changes: 17 additions & 0 deletions recipes/xtensor/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, 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 not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)