diff --git a/.gitignore b/.gitignore index 0977c42..d07322e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /libcpuid-0.6.5.tar.gz +/libcpuid-0.7.0.tar.gz diff --git a/0001-Python-Refactor-the-build-of-the-bindings.patch b/0001-Python-Refactor-the-build-of-the-bindings.patch new file mode 100644 index 0000000..a97cae1 --- /dev/null +++ b/0001-Python-Refactor-the-build-of-the-bindings.patch @@ -0,0 +1,332 @@ +From c2acc569714d0a72051e4e1d34053f4f080d757b Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Pavol=20=C5=BD=C3=A1=C4=8Dik?= +Date: Sun, 1 Sep 2024 19:37:37 +0200 +Subject: [PATCH] Python: Refactor the build of the bindings + +When building the binding, link the dynamic +C library as built by `make` instead of linking +the system-installed C library. This simplifies +packaging in Linux distributions and also the +build of the live docs. +--- + .readthedocs.yml | 7 +- + python/pyproject.toml | 2 +- + python/src/libcpuid/_ffi_build.py | 116 +++++++++++++++++++++--- + python/src/libcpuid/_ffi_build_rtd.py | 31 ------- + python/src/libcpuid/_ffi_build_utils.py | 105 --------------------- + 5 files changed, 105 insertions(+), 156 deletions(-) + delete mode 100644 python/src/libcpuid/_ffi_build_rtd.py + delete mode 100644 python/src/libcpuid/_ffi_build_utils.py + +diff --git a/.readthedocs.yml b/.readthedocs.yml +index 0866584..8aa71c8 100644 +--- a/.readthedocs.yml ++++ b/.readthedocs.yml +@@ -14,13 +14,10 @@ build: + pre_install: + - libtoolize + - autoreconf --install +- - mkdir ./install +- - ./configure --prefix=`pwd`/install ++ - ./configure + - make +- - make install + - pip install cffi +- - python ./python/src/libcpuid/_ffi_build_rtd.py ./libcpuid/libcpuid.h ./install +- ++ - python python/src/libcpuid/_ffi_build.py --runtime-link + sphinx: + configuration: python/docs/conf.py + +diff --git a/python/pyproject.toml b/python/pyproject.toml +index 637b0e6..580a4c7 100644 +--- a/python/pyproject.toml ++++ b/python/pyproject.toml +@@ -1,5 +1,5 @@ + [build-system] +-requires = ["setuptools", "cffi"] ++requires = ["setuptools", "cffi", "wheel"] + build-backend = "setuptools.build_meta" + + [project] +diff --git a/python/src/libcpuid/_ffi_build.py b/python/src/libcpuid/_ffi_build.py +index 6e24d12..e3a7ea0 100644 +--- a/python/src/libcpuid/_ffi_build.py ++++ b/python/src/libcpuid/_ffi_build.py +@@ -3,23 +3,111 @@ Module for compiling the C FFI. + """ + + import os +-import sys ++import subprocess ++import tempfile ++import re ++import argparse ++from pathlib import Path + from cffi import FFI + +-sys.path.append(os.path.dirname(os.path.abspath(__file__))) + +-from _ffi_build_utils import ( # pylint: disable=import-error, wrong-import-position +- get_include_flags, +- find_header_file, +- preprocess_header, +- eval_sizeofs, ++class FFIBuildException(Exception): ++ """Generic exception for errors occuring during the CFFI build.""" ++ ++ ++def preprocess_header(header_path): ++ """ ++ Preprocesses the header file (python-cffi only accepts preprocessed C definitions) ++ at the given path and returns it as a string. ++ """ ++ try: ++ return subprocess.check_output( ++ ["gcc", "-U __GNUC__", "-E", header_path] ++ ).decode() ++ except subprocess.CalledProcessError as e: ++ if e.returncode == 127: ++ raise FFIBuildException( ++ "The gcc compiler is necessary to build python-libcpuid." ++ ) from e ++ raise FFIBuildException( ++ f"Error preprocessing the libcpuid header file: {e.stderr}" ++ ) from e ++ ++ ++def _get_sizeof_eval_source(sizeof): ++ return f""" ++#include ++#include ++ ++int main() {{ ++ printf("%ld", {sizeof}); ++ return 0; ++}} ++""" ++ ++ ++def eval_sizeofs(header, cflags): ++ """ ++ Evaluates each sizeof found in the given C header and replaces all ++ occurences of the sizeof with its computed value. ++ """ ++ sizeofs = set(re.findall(r"sizeof\([^\)]*\)", header)) ++ tmp_dir = tempfile.mkdtemp() ++ c_program_path = Path(tmp_dir, "sizeof.c") ++ executable_path = Path(tmp_dir, "sizeof") ++ ++ for sizeof in sizeofs: ++ with open(c_program_path, "w", encoding="UTF-8") as c_program_file: ++ c_program_file.write(_get_sizeof_eval_source(sizeof)) ++ subprocess.check_call(["gcc", c_program_path, *cflags, "-o", executable_path]) ++ size = subprocess.check_output([executable_path]).decode() ++ header = header.replace(sizeof, size) ++ ++ os.remove(c_program_path) ++ os.remove(executable_path) ++ os.rmdir(tmp_dir) ++ return header ++ ++ ++LIBCPUID_DIR = str(Path(*(Path(os.path.abspath(__file__)).parts[:-4]))) ++LIBCPUID_INCLUDE_DIR = str(Path(LIBCPUID_DIR, "libcpuid")) ++LIBCPUID_LIBRARY_DIR = str(Path(LIBCPUID_DIR, "libcpuid", ".libs")) ++LIBCPUID_MAIN_HEADER_FILENAME = "libcpuid.h" ++LIBCPUID_MAIN_HEADER_PATH = str( ++ Path(LIBCPUID_INCLUDE_DIR, LIBCPUID_MAIN_HEADER_FILENAME) + ) ++LIBCPUID_LIBRARY_NAME = "cpuid" ++PYTHON_SRC_DIR = str(Path(LIBCPUID_DIR, "python", "src")) ++ ++PREPROCESSED_HEADER = preprocess_header(LIBCPUID_MAIN_HEADER_PATH) ++EVAL_SIZEOF_CFLAGS = [ ++ f"-I{LIBCPUID_INCLUDE_DIR}", ++ f"-L{LIBCPUID_LIBRARY_DIR}", ++ f"-l{LIBCPUID_LIBRARY_NAME}", ++ f"-Wl,-rpath={LIBCPUID_LIBRARY_DIR}", ++] ++ ++NO_SIZEOF_HEADER = eval_sizeofs(PREPROCESSED_HEADER, EVAL_SIZEOF_CFLAGS) + +-include_flags = get_include_flags() +-preprocessed_header = preprocess_header(find_header_file(include_flags)) +-no_sizeof_header = eval_sizeofs(preprocessed_header, include_flags) + ffibuilder = FFI() +-ffibuilder.cdef(no_sizeof_header) +-ffibuilder.set_source_pkgconfig( +- "libcpuid._libcpuid_cffi", ["libcpuid"], "#include " +-) ++ffibuilder.cdef(NO_SIZEOF_HEADER) ++ ++set_source_kwargs = { ++ "module_name": "libcpuid._libcpuid_cffi", ++ "source": f"#include <{LIBCPUID_MAIN_HEADER_FILENAME}>", ++ "libraries": [LIBCPUID_LIBRARY_NAME], ++ "include_dirs": [LIBCPUID_INCLUDE_DIR], ++ "library_dirs": [LIBCPUID_LIBRARY_DIR], ++} ++ ++if __name__ == "__main__": ++ parser = argparse.ArgumentParser() ++ parser.add_argument("-r", "--runtime-link", action="store_true") ++ args = parser.parse_args() ++ if args.runtime_link: ++ set_source_kwargs["extra_link_args"] = [f"-Wl,-rpath={LIBCPUID_LIBRARY_DIR}"] ++ ++ffibuilder.set_source(**set_source_kwargs) ++ ++if __name__ == "__main__": ++ ffibuilder.compile(PYTHON_SRC_DIR) +diff --git a/python/src/libcpuid/_ffi_build_rtd.py b/python/src/libcpuid/_ffi_build_rtd.py +deleted file mode 100644 +index 09d09ba..0000000 +--- a/python/src/libcpuid/_ffi_build_rtd.py ++++ /dev/null +@@ -1,31 +0,0 @@ +-""" +-Script for compiling the C FFI for the live documentation. +-""" +- +-import sys +-import os +-from cffi import FFI +- +-sys.path.append(os.path.dirname(os.path.abspath(__file__))) +- +-from _ffi_build_utils import ( # pylint: disable=import-error, wrong-import-position +- preprocess_header, +- eval_sizeofs, +-) +- +-if __name__ == "__main__": +- header_path = sys.argv[1] +- install_dir = sys.argv[2] +- library_dir = os.path.join(os.getcwd(), install_dir, "lib") +- include_dir = os.path.join(install_dir, "include", "libcpuid") +- ffibuilder = FFI() +- ffibuilder.cdef(eval_sizeofs(preprocess_header(header_path), [f"-I{include_dir}"])) +- ffibuilder.set_source( +- "python.src.libcpuid._libcpuid_cffi", +- "#include ", +- libraries=["cpuid"], +- library_dirs=[library_dir], +- include_dirs=[include_dir], +- extra_link_args=[f"-Wl,-rpath={library_dir}"], +- ) +- ffibuilder.compile(verbose=True) +diff --git a/python/src/libcpuid/_ffi_build_utils.py b/python/src/libcpuid/_ffi_build_utils.py +deleted file mode 100644 +index 410bd8c..0000000 +--- a/python/src/libcpuid/_ffi_build_utils.py ++++ /dev/null +@@ -1,105 +0,0 @@ +-""" +-Utility functions for building the FFI. +-""" +- +-import subprocess +-import os +-import re +-import tempfile +- +- +-class FFIBuildException(Exception): +- """Generic exception for errors occuring during the CFFI build.""" +- +- +-def get_include_flags(): +- """ +- Obtains libcpuid include flags via pkg-config. +- """ +- try: +- cflags = ( +- subprocess.check_output(["pkg-config", "libcpuid", "--cflags-only-I"]) +- .decode() +- .strip() +- .split() +- ) +- return cflags +- except subprocess.CalledProcessError as e: +- if e.returncode == 127: +- raise FFIBuildException( +- "The pkg-config command is necessary to build python-libcpuid." +- ) from e +- if e.returncode == 1: +- raise FFIBuildException( +- "The libcpuid C library (devel) was not found." +- ) from e +- raise FFIBuildException("Error looking for the libcpuid library") from e +- +- +-def find_header_file(include_flags): +- """ +- Obtains main libcpuid header file location from include flags. +- """ +- header_path = None # pylint: disable=invalid-name +- for cflag in include_flags: +- header_candidate = os.path.join(cflag[2:], "libcpuid.h") +- if os.path.isfile(header_candidate): +- header_path = header_candidate +- break +- if header_path is None: +- raise FFIBuildException("Could not find header file of the libcpuid library.") +- return header_path +- +- +-def preprocess_header(header_path): +- """ +- Preprocesses the header file (python-cffi only accepts preprocessed C definitions) +- at the given path and returns it as a string. +- """ +- try: +- return subprocess.check_output( +- ["gcc", "-U __GNUC__", "-E", header_path] +- ).decode() +- except subprocess.CalledProcessError as e: +- if e.returncode == 127: +- raise FFIBuildException( +- "The gcc compiler is necessary to build python-libcpuid." +- ) from e +- raise FFIBuildException( +- f"Error preprocessing the libcpuid header file: {e.stderr}" +- ) from e +- +- +-def _get_sizeof_eval_source(sizeof): +- return f""" +-#include +-#include +- +-int main() {{ +- printf("%ld", {sizeof}); +- return 0; +-}} +-""" +- +- +-def eval_sizeofs(header, cflags): +- """ +- Evaluates each sizeof found in the given C header and replaces all +- occurences of the sizeof with its computed value. +- """ +- sizeofs = set(re.findall(r"sizeof\([^\)]*\)", header)) +- tmp_dir = tempfile.mkdtemp() +- c_program_path = os.path.join(tmp_dir, "sizeof.c") +- executable_path = os.path.join(tmp_dir, "sizeof") +- +- for sizeof in sizeofs: +- with open(c_program_path, "w", encoding="UTF-8") as c_program_file: +- c_program_file.write(_get_sizeof_eval_source(sizeof)) +- subprocess.check_call(["gcc", c_program_path, *cflags, "-o", executable_path]) +- size = subprocess.check_output([executable_path]).decode() +- header = header.replace(sizeof, size) +- +- os.remove(c_program_path) +- os.remove(executable_path) +- os.rmdir(tmp_dir) +- return header +-- +2.46.0 + diff --git a/0002-Python-Do-not-fail-sanity-tests-if-current-CPU-is-un.patch b/0002-Python-Do-not-fail-sanity-tests-if-current-CPU-is-un.patch new file mode 100644 index 0000000..5285110 --- /dev/null +++ b/0002-Python-Do-not-fail-sanity-tests-if-current-CPU-is-un.patch @@ -0,0 +1,41 @@ +From eae4874e4e29d3575c04f830c23fbe3040ca7416 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Pavol=20=C5=BD=C3=A1=C4=8Dik?= +Date: Sun, 1 Sep 2024 19:43:31 +0200 +Subject: [PATCH] Python: Do not fail sanity tests if current CPU is + unsupported + +--- + python/tests/sanity_test.py | 10 +++++++--- + 1 file changed, 7 insertions(+), 3 deletions(-) + +diff --git a/python/tests/sanity_test.py b/python/tests/sanity_test.py +index bf17dc4..4e18165 100644 +--- a/python/tests/sanity_test.py ++++ b/python/tests/sanity_test.py +@@ -5,6 +5,7 @@ import tempfile + import libcpuid + from libcpuid.info import CPUInfo, SystemInfo + from libcpuid.raw import CPURawData, CPURawDataArray ++from libcpuid.errors import CLibraryError + + + def test_cpu_name_in_vendor_list(): +@@ -12,9 +13,12 @@ def test_cpu_name_in_vendor_list(): + Checks that the current CPU codename appears + in the list of all CPUs of its vendor. + """ +- info = CPUInfo.from_current_cpu() +- cpulist = libcpuid.get_cpu_list(info.vendor) +- assert info.cpu_codename in cpulist ++ try: ++ info = CPUInfo.from_current_cpu() ++ cpulist = libcpuid.get_cpu_list(info.vendor) ++ assert info.cpu_codename in cpulist ++ except CLibraryError: ++ pass + + + def test_serialization(): +-- +2.46.0 + diff --git a/libcpuid-fix-cpuid_get_hypervisor.patch b/libcpuid-fix-cpuid_get_hypervisor.patch deleted file mode 100644 index 6b16b00..0000000 --- a/libcpuid-fix-cpuid_get_hypervisor.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/libcpuid/cpuid_main.c b/libcpuid/cpuid_main.c -index 791e18d..111f43f 100644 ---- a/libcpuid/cpuid_main.c -+++ b/libcpuid/cpuid_main.c -@@ -1748,7 +1748,7 @@ hypervisor_vendor_t cpuid_get_hypervisor(struct cpu_raw_data_t* raw, struct cpu_ - }; - - if (!data) { -- if ((r = cpu_identify(raw, data)) < 0) -+ if ((r = cpu_identify(raw, &mydata)) < 0) - return HYPERVISOR_UNKNOWN; - data = &mydata; - } diff --git a/libcpuid-fix-handle-leaks-in-rdmsr-c.patch b/libcpuid-fix-handle-leaks-in-rdmsr-c.patch deleted file mode 100644 index 8649eb7..0000000 --- a/libcpuid-fix-handle-leaks-in-rdmsr-c.patch +++ /dev/null @@ -1,33 +0,0 @@ -diff --git a/libcpuid/rdmsr.c b/libcpuid/rdmsr.c -index 7269652..0bb29c3 100644 ---- a/libcpuid/rdmsr.c -+++ b/libcpuid/rdmsr.c -@@ -96,6 +96,7 @@ struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num) - handle = (struct msr_driver_t*) malloc(sizeof(struct msr_driver_t)); - if (!handle) { - cpuid_set_error(ERR_NO_MEM); -+ close(fd); - return NULL; - } - handle->fd = fd; -@@ -1069,8 +1070,10 @@ int msr_serialize_raw_data(struct msr_driver_t* handle, const char* filename) - - /* Get cached decoded CPUID information */ - id = get_cached_cpuid(); -- if (id->vendor == VENDOR_UNKNOWN) -+ if (id->vendor == VENDOR_UNKNOWN) { -+ fclose(f); - return cpuid_get_error(); -+ } - - /* Get CPU stock speed */ - if (cpu_clock == 0) -@@ -1082,7 +1085,7 @@ int msr_serialize_raw_data(struct msr_driver_t* handle, const char* filename) - case VENDOR_HYGON: - case VENDOR_AMD: msr = amd_msr; break; - case VENDOR_INTEL: msr = intel_msr; break; -- default: return cpuid_set_error(ERR_CPU_UNKN); -+ default: fclose(f); return cpuid_set_error(ERR_CPU_UNKN); - } - - /* Print raw MSR values */ diff --git a/libcpuid-prevent-intel_fn11-array-overruns.patch b/libcpuid-prevent-intel_fn11-array-overruns.patch deleted file mode 100644 index b98b807..0000000 --- a/libcpuid-prevent-intel_fn11-array-overruns.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/libcpuid/recog_intel.c b/libcpuid/recog_intel.c -index e385a7c..a091843 100644 ---- a/libcpuid/recog_intel.c -+++ b/libcpuid/recog_intel.c -@@ -721,7 +721,7 @@ static int decode_intel_extended_topology(struct cpu_raw_data_t* raw, struct cpu - { - int i, level_type, num_smt = -1, num_core = -1; - -- for (i = 0; (raw->intel_fn11[i][EAX] != 0x0) && (raw->intel_fn11[i][EBX] != 0x0) && (i < MAX_INTELFN11_LEVEL); i++) { -+ for (i = 0; (i < MAX_INTELFN11_LEVEL) && (raw->intel_fn11[i][EAX] != 0x0) && (raw->intel_fn11[i][EBX] != 0x0); i++) { - level_type = EXTRACTS_BITS(raw->intel_fn11[i][ECX], 15, 8); - switch (level_type) { - case 0x01: diff --git a/libcpuid.spec b/libcpuid.spec index ce12816..314d76f 100644 --- a/libcpuid.spec +++ b/libcpuid.spec @@ -1,16 +1,14 @@ Name: libcpuid -Version: 0.6.5 -Release: 4%{?dist} -Summary: Provides CPU identification for x86 +Version: 0.7.0 +Release: 1%{?dist} +Summary: Provides CPU identification for x86 and ARM License: BSD-2-Clause URL: https://github.com/anrieff/libcpuid Source0: https://github.com/anrieff/libcpuid/archive/v%{version}.tar.gz#/%{name}-%{version}.tar.gz -ExcludeArch: aarch64 %arm ppc64le ppc64 s390x - -# https://github.com/anrieff/libcpuid/pull/199 -Patch0: %{name}-fix-handle-leaks-in-rdmsr-c.patch -Patch1: %{name}-fix-cpuid_get_hypervisor.patch -Patch2: %{name}-prevent-intel_fn11-array-overruns.patch +# https://github.com/anrieff/libcpuid/pull/203 +Patch0: 0001-Python-Refactor-the-build-of-the-bindings.patch +Patch1: 0002-Python-Do-not-fail-sanity-tests-if-current-CPU-is-un.patch +ExcludeArch: ppc64le s390x BuildRequires: automake BuildRequires: autoconf @@ -19,9 +17,16 @@ BuildRequires: gcc-c++ BuildRequires: git BuildRequires: libtool BuildRequires: make +BuildRequires: python3-devel +BuildRequires: python3-cffi +BuildRequires: python3-setuptools +BuildRequires: python3-wheel +BuildRequires: python3-pip +BuildRequires: python3-pytest +BuildRequires: pyproject-rpm-macros %description -Libcpuid provides CPU identification for the x86 (and x86_64). +Libcpuid provides CPU identification for the x86 (x86_64) and ARM architectures. %package devel Summary: Development files for %{name} @@ -33,6 +38,13 @@ developing applications that use %{name}. For details about the programming API, please see the docs on the project's site (http://libcpuid.sourceforge.net/) +%package -n python3-%{name} +Summary: Python bindings for the libcpuid library +Requires: %{name}%{_isa} = %{version}-%{release} + +%description -n python3-%{name} +The python3-%{name} package contains Python bindings for the libcpuid library. + %prep %autosetup -p1 -n %{name}-%{version} @@ -41,6 +53,10 @@ autoreconf -vfi %configure --disable-static %make_build +pushd python +%pyproject_wheel +popd + %install %make_install # WARNING: empty dependency_libs variable. remove the pointless .la @@ -48,6 +64,15 @@ rm %{buildroot}%{_libdir}/*.la %ldconfig_scriptlets +pushd python +%pyproject_install +popd + +%pyproject_save_files -L %{name} + +%check +LD_LIBRARY_PATH=%{buildroot}%{_libdir} %pytest python/tests + %files %doc Readme.md %license COPYING @@ -60,7 +85,16 @@ rm %{buildroot}%{_libdir}/*.la %{_libdir}/pkgconfig/%{name}.pc %{_mandir}/man3/*.3.* +%files -n python3-%{name} -f %{pyproject_files} +%doc python/README.md + + %changelog +* Mon Sep 02 2024 Pavol Žáčik - 0.7.0-1 +- Rebase to 0.7.0 +- Begin building the package for aarch64 +- Add Python bindings as a subpackage (python3-libcpuid) + * Mon Jun 24 2024 Troy Dawson - 0.6.5-4 - Bump release for June 2024 mass rebuild diff --git a/sources b/sources index 9d11058..41a3e5a 100644 --- a/sources +++ b/sources @@ -1 +1 @@ -SHA512 (libcpuid-0.6.5.tar.gz) = c38381e0f768a2cd2a5df601cbcc50d1cb93a276c477bc09e69ff61de01aef30c0b3163f4a0c8e7641caa3cf692d468410ebc90a3cad93e0476cddeb0c444d7b +SHA512 (libcpuid-0.7.0.tar.gz) = 26353763461cbbe664ddaa6933cbd9016e3b11d8a056bc8c2b92818dfe3a43fcda76a92270f716eeb00ae61b75288c7079add8d7ac2290a0a0d5c3bd7d898d44