Skip to content

Commit

Permalink
(#18723) libcpuid: migrate to Conan v2
Browse files Browse the repository at this point in the history
* libcpuid: migrate to Conan v2

* libcpuid: move DLLs from lib to bin

* libcpuid: remove unnecessary copying of DLLs

* libcpuid: fix test_package
  • Loading branch information
valgur authored Nov 10, 2023
1 parent b489695 commit c3bf6f8
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 65 deletions.
13 changes: 0 additions & 13 deletions recipes/libcpuid/all/CMakeLists.txt

This file was deleted.

2 changes: 0 additions & 2 deletions recipes/libcpuid/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ sources:
patches:
"0.5.1":
- patch_file: "patches/0.5.0-cmake-fixes.patch"
base_path: "source_subfolder"
"0.5.0":
- patch_file: "patches/0.5.0-cmake-fixes.patch"
base_path: "source_subfolder"
77 changes: 37 additions & 40 deletions recipes/libcpuid/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from conans import CMake, ConanFile, tools
from conans.errors import ConanInvalidConfiguration
import os

required_conan_version = ">=1.43.0"
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rmdir

required_conan_version = ">=1.53.0"


class LibCpuidConan(ConanFile):
name = "libcpuid"
description = "libcpuid is a small C library for x86 CPU detection and feature extraction"
topics = ("libcpuid", "detec", "cpu", "intel", "amd", "x86_64")
description = "libcpuid is a small C library for x86 CPU detection and feature extraction"
license = "https://github.com/anrieff/libcpuid"
homepage = "https://github.com/anrieff/libcpuid"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/anrieff/libcpuid"
topics = ("detec", "cpu", "intel", "amd", "x86_64")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
Expand All @@ -23,60 +27,53 @@ class LibCpuidConan(ConanFile):
"fPIC": True,
}

generators = "cmake"
_cmake = None

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

@property
def _build_subfolder(self):
return "build_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.rm_safe("compiler.libcxx")
self.settings.rm_safe("compiler.cppstd")

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

def package_id(self):
del self.info.settings.compiler
del self.info.settings.build_type

def validate(self):
if self.settings.arch not in ("x86", "x86_64"):
raise ConanInvalidConfiguration("libcpuid is only available for x86 and x86_64 architecture")

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], strip_root=True)

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["ENABLE_DOCS"] = False
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake
def generate(self):
tc = CMakeToolchain(self)
tc.variables["CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS"] = self.options.shared
tc.variables["ENABLE_DOCS"] = False
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("COPYING", src=self._source_subfolder, dst="licenses")
cmake = self._configure_cmake()
copy(self, "COPYING",
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"))
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "cpuid")
Expand All @@ -85,7 +82,7 @@ def package_info(self):
self.cpp_info.libs = ["cpuid"]

bin_path = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bin_path))
self.output.info(f"Appending PATH environment variable: {bin_path}")
self.env_info.PATH.append(bin_path)

# TODO: to remove in conan v2 once cmake_find_package_* generators removed
Expand Down
5 changes: 1 addition & 4 deletions recipes/libcpuid/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.15)
project(test_package C)

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

find_package(cpuid REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
Expand Down
27 changes: 21 additions & 6 deletions recipes/libcpuid/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
from conans import ConanFile, CMake, tools
import os

from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
from conan.tools.env import VirtualRunEnv


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeDeps", "CMakeToolchain"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str, run=True)

def layout(self):
cmake_layout(self)

def generate(self):
VirtualRunEnv(self).generate(scope="build")
VirtualRunEnv(self).generate(scope="run")

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.bindir, "test_package")
self.run(bin_path, env="conanrun")

self.run("cpuid_tool --report", run_environment=True)
self.run("cpuid_tool --report")
8 changes: 8 additions & 0 deletions recipes/libcpuid/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.15)
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/)
19 changes: 19 additions & 0 deletions recipes/libcpuid/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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)

self.run("cpuid_tool --report", run_environment=True)

0 comments on commit c3bf6f8

Please sign in to comment.