-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
81 lines (67 loc) · 2.55 KB
/
setup.py
File metadata and controls
81 lines (67 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Build helper for the optional _usb_accel C extension.
If libusb-1.0-dev is installed, the extension compiles automatically
during ``pip install``. If not, the package installs as pure Python
and falls back to pyusb for USB communication.
"""
import os
import subprocess
import sys
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
class OptionalBuildExt(build_ext):
"""build_ext that treats all extensions as optional.
If compilation fails (e.g., missing libusb-1.0-dev), the install
proceeds without the C extension.
"""
def build_extension(self, ext):
try:
super().build_extension(ext)
except Exception as e:
print(
f"\nWARNING: Failed to build {ext.name}: {e}\n"
"Falling back to pure-Python pyusb transport.\n"
"Install libusb-1.0-dev to enable the C acceleration extension.\n",
file=sys.stderr,
)
def _get_libusb_flags():
"""Get compiler/linker flags for libusb-1.0 via pkg-config."""
try:
cflags = subprocess.check_output(
["pkg-config", "--cflags", "libusb-1.0"],
text=True, stderr=subprocess.DEVNULL,
).strip().split()
libs = subprocess.check_output(
["pkg-config", "--libs", "libusb-1.0"],
text=True, stderr=subprocess.DEVNULL,
).strip().split()
return cflags, libs
except (subprocess.CalledProcessError, FileNotFoundError):
return None, None
def _build_ext_modules():
"""Return ext_modules list, or empty list if libusb not available."""
# Source path must be relative to setup.py (setuptools requirement).
# setup.py is at the repo root; _usb_accel.c is in libredgetpu/.
setup_dir = os.path.dirname(os.path.abspath(__file__))
c_source_abs = os.path.join(setup_dir, "libredgetpu", "_usb_accel.c")
if not os.path.isfile(c_source_abs):
return []
cflags, libs = _get_libusb_flags()
if cflags is None:
print(
"NOTE: pkg-config cannot find libusb-1.0; "
"_usb_accel C extension will not be built.\n"
"Install libusb-1.0-dev to enable it.",
file=sys.stderr,
)
return []
c_source_rel = os.path.join("libredgetpu", "_usb_accel.c")
return [Extension(
"libredgetpu._usb_accel",
sources=[c_source_rel],
extra_compile_args=cflags + ["-O2"],
extra_link_args=libs,
)]
setup(
ext_modules=_build_ext_modules(),
cmdclass={"build_ext": OptionalBuildExt},
)