Skip to content

Commit

Permalink
(#14312) cpu_features: conan v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceIm authored Nov 28, 2022
1 parent 74374b3 commit 2d1a4fd
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 60 deletions.
9 changes: 0 additions & 9 deletions recipes/cpu_features/all/CMakeLists.txt

This file was deleted.

6 changes: 2 additions & 4 deletions recipes/cpu_features/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ sources:
url: "https://github.com/google/cpu_features/archive/refs/tags/v0.6.0.tar.gz"
sha256: "95a1cf6f24948031df114798a97eea2a71143bd38a4d07d9a758dda3924c1932"
patches:
"0.6.0":
- patch_file: "patches/0.6.0-0001-fix-bundle-install.patch"
base_path: "source_subfolder"
"0.7.0":
- patch_file: "patches/0.7.0-0001-support-aarch64-macos.patch"
base_path: "source_subfolder"
"0.6.0":
- patch_file: "patches/0.6.0-0001-fix-bundle-install.patch"
68 changes: 32 additions & 36 deletions recipes/cpu_features/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.apple import is_apple_os
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rmdir
from conan.tools.scm import Version
import os

required_conan_version = ">=1.43.0"
required_conan_version = ">=1.53.0"


class CpuFeaturesConan(ConanFile):
Expand All @@ -22,57 +26,49 @@ class CpuFeaturesConan(ConanFile):
"fPIC": True,
}

generators = "cmake",
_cmake = None

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

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

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
self.options.rm_safe("fPIC")
self.settings.compiler.rm_safe("cppstd")
self.settings.compiler.rm_safe("libcxx")

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

def source(self):
tools.get(**self.conan_data["sources"][self.version],
strip_root=True, destination=self._source_subfolder)

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
if tools.Version(self.version) < "0.7.0":
self._cmake.definitions["BUILD_PIC"] = self.options.get_safe("fPIC", True)
if tools.Version(self.version) >= "0.7.0":
self._cmake.definitions["BUILD_TESTING"] = False
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
if Version(self.version) < "0.7.0":
tc.variables["BUILD_PIC"] = self.options.get_safe("fPIC", True)
if Version(self.version) >= "0.7.0":
tc.variables["BUILD_TESTING"] = False
# TODO: should be handled by CMake helper
if tools.is_apple_os(self.settings.os) and self.settings.arch in ["armv8", "armv8_32", "armv8.3"]:
self._cmake.definitions["CMAKE_SYSTEM_PROCESSOR"] = "aarch64"
self._cmake.configure() # Does not support out of source builds
return self._cmake
if is_apple_os(self) and self.settings.arch in ["armv8", "armv8_32", "armv8.3"]:
tc.variables["CMAKE_SYSTEM_PROCESSOR"] = "aarch64"
tc.variables["CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS"] = True
tc.generate()

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
cmake = self._configure_cmake()
apply_conandata_patches(self)
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
self.copy("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, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "CpuFeatures")
Expand Down
9 changes: 3 additions & 6 deletions recipes/cpu_features/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(test_package)
project(test_package LANGUAGES C)

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

find_package(CpuFeatures CONFIG REQUIRED)
find_package(CpuFeatures REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} CpuFeatures::cpu_features)
target_link_libraries(${PROJECT_NAME} PRIVATE CpuFeatures::cpu_features)
19 changes: 14 additions & 5 deletions recipes/cpu_features/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os


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):
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")
8 changes: 8 additions & 0 deletions recipes/cpu_features/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/cpu_features/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)

0 comments on commit 2d1a4fd

Please sign in to comment.