forked from anrieff/libcpuid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
346 additions
and
544 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Classes storing CPU information | ||
=============================== | ||
|
||
.. autoclass:: libcpuid.cpuid.CPUID | ||
:members: | ||
|
||
.. autoclass:: libcpuid.cpusgx.CPUSGX | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Module for compiling the C FFI. | ||
""" | ||
|
||
import subprocess | ||
import os | ||
from cffi import FFI | ||
|
||
|
||
class FFIBuildException(Exception): | ||
"""Generic exception for errors occuring during the CFFI build.""" | ||
|
||
|
||
def find_header(): | ||
""" | ||
Obtains libcpuid header file location via pkg-config. | ||
""" | ||
try: | ||
cflags = ( | ||
subprocess.check_output(["pkg-config", "libcpuid", "--cflags-only-I"]) | ||
.decode() | ||
.strip() | ||
.split() | ||
) | ||
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 | ||
|
||
# Find an existing libcpuid header file | ||
header_path = None # pylint: disable=invalid-name | ||
for cflag in cflags: | ||
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). | ||
""" | ||
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 | ||
|
||
|
||
header = find_header() | ||
|
||
ffibuilder = FFI() | ||
ffibuilder.cdef(preprocess_header(header)) | ||
ffibuilder.set_source_pkgconfig( | ||
"libcpuid._libcpuid_cffi", ["libcpuid"], "#include <libcpuid.h>" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Script for compiling the C FFI for the live documentation. | ||
""" | ||
|
||
import subprocess | ||
import sys | ||
import os | ||
from cffi import FFI | ||
|
||
if __name__ == "__main__": | ||
header = sys.argv[1] | ||
install_dir = sys.argv[2] | ||
library_dir = os.path.join(os.getcwd(), install_dir, "lib") | ||
ffibuilder = FFI() | ||
ffibuilder.cdef( | ||
subprocess.check_output(["gcc", "-U __GNUC__", "-E", header]).decode() | ||
) | ||
ffibuilder.set_source( | ||
"python.src.libcpuid._libcpuid_cffi", | ||
"#include <libcpuid.h>", | ||
libraries=["cpuid"], | ||
library_dirs=[library_dir], | ||
include_dirs=[os.path.join(install_dir, "include", "libcpuid")], | ||
extra_link_args=[f"-Wl,-rpath={library_dir}"], | ||
) | ||
ffibuilder.compile(verbose=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
[build-system] | ||
requires = ["setuptools"] | ||
requires = ["setuptools", "cffi"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "libcpuid" | ||
version = "0.1.0" | ||
readme = "README.md" | ||
dependencies = ["cffi"] | ||
license = {text = "BSD-3-Clause"} | ||
authors = [{name = "Pavol Žáčik", email = "[email protected]"}] | ||
description = "Python bindings for the libcpuid C library" | ||
description = "Python bindings for the libcpuid C library" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from setuptools import setup | ||
|
||
setup( | ||
cffi_modules=["ffi_build.py:ffibuilder"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.