-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
setup.py
executable file
·262 lines (222 loc) · 8.4 KB
/
setup.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/python
from Cython.Build import cythonize
from setuptools import setup, Extension, Distribution
from subprocess import call, PIPE, Popen
import os
import re
import shlex
import subprocess
import sys
min_required_hidapi_version = "0.14"
libusb_pkgconfig = "libusb-1.0 >= 1.0.9"
hidapi_libusb_pkgconfig = "hidapi-libusb >= " + min_required_hidapi_version
hidapi_hidraw_pkgconfig = "hidapi-hidraw >= " + min_required_hidapi_version
hidapi_pkgconfig = "hidapi >= " + min_required_hidapi_version
tld = os.path.abspath(os.path.dirname(__file__))
embedded_hidapi_topdir = os.path.join(tld, "hidapi")
embedded_hidapi_include = os.path.join(embedded_hidapi_topdir, "hidapi")
def to_bool(bool_str):
if str(bool_str).lower() in ["true", "t", "1", "on", "yes", "y"]:
return True
return False
def get_extension_compiler_type():
"""
Returns a compiler to be used by setuptools to build Extensions
Taken from https://github.com/pypa/setuptools/issues/2806#issuecomment-961805789
"""
d = Distribution()
build_ext = Distribution().get_command_obj("build_ext")
build_ext.finalize_options()
# register an extension to ensure a compiler is created
build_ext.extensions = [Extension("ignored", ["ignored.c"])]
# disable building fake extensions
build_ext.build_extensions = lambda: None
# run to populate self.compiler
build_ext.run()
return build_ext.compiler.compiler_type
# this could have been just pkgconfig.configure_extension(), but: https://github.com/matze/pkgconfig/issues/65
# additionally contains a few small improvements
def pkgconfig_configure_extension(ext, package):
pkg_config_exe = os.environ.get("PKG_CONFIG", None) or "pkg-config"
def exists(package):
cmd = f"{pkg_config_exe} --exists '{package}'"
return call(shlex.split(cmd)) == 0
if not exists(package):
raise Exception(f"pkg-config package '{package}' not found")
def query_pkg_config(package, *options):
cmd = f"{pkg_config_exe} {' '.join(options)} '{package}'"
proc = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
return out.rstrip().decode("utf-8")
def query_and_extend(option, target):
os_opts = ["--msvc-syntax"] if get_extension_compiler_type() == "msvc" else []
flags = query_pkg_config(package, *os_opts, option)
flags = flags.replace('\\"', "")
if flags:
target.extend(re.split(r"(?<!\\) ", flags))
query_and_extend("--cflags", ext.extra_compile_args)
query_and_extend("--libs", ext.extra_link_args)
return ext
def hidapi_src(platform):
return os.path.join(embedded_hidapi_topdir, platform, "hid.c")
def check_deprecated_without_libusb():
# already the default behavior, but accept the old option
if "--without-libusb" in sys.argv:
sys.argv.remove("--without-libusb")
print(
"Without libusb is already a default, '--without-libusb' option is redundant and deprecated"
)
def hid_from_embedded_hidapi():
# TODO: what about MinGW/msys?
if sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
modules = [
Extension(
"hid",
sources=["hid.pyx", hidapi_src("windows")],
include_dirs=[embedded_hidapi_include],
extra_compile_args=["-DHID_API_NO_EXPORT_DEFINE"],
libraries=["setupapi"],
)
]
elif sys.platform.startswith("darwin"):
macos_sdk_path = (
subprocess.check_output(["xcrun", "--show-sdk-path"]).decode().strip()
)
modules = [
Extension(
"hid",
sources=["hid.pyx", hidapi_src("mac")],
include_dirs=[embedded_hidapi_include],
# TODO: remove -Wno-unreachable-code when the time comes: https://github.com/cython/cython/issues/3172
extra_compile_args=[
"-isysroot",
macos_sdk_path,
"-Wno-unreachable-code",
],
# TODO: remove '-framework AppKit' after switching to 0.14.1 or newer
extra_link_args=[
"-framework",
"IOKit",
"-framework",
"CoreFoundation",
"-framework",
"AppKit",
],
)
]
elif sys.platform.startswith("linux"):
modules = []
if "--with-libusb" in sys.argv:
sys.argv.remove("--with-libusb")
HIDAPI_WITH_LIBUSB = True
else:
HIDAPI_WITH_LIBUSB = to_bool(os.getenv("HIDAPI_WITH_LIBUSB"))
if HIDAPI_WITH_LIBUSB:
hidraw_module = "hidraw"
modules.append(
pkgconfig_configure_extension(
Extension(
"hid",
sources=["hid.pyx", hidapi_src("libusb")],
include_dirs=[embedded_hidapi_include],
),
libusb_pkgconfig,
)
)
else:
hidraw_module = "hid"
check_deprecated_without_libusb()
modules.append(
Extension(
hidraw_module,
sources=["hidraw.pyx", hidapi_src("linux")],
include_dirs=[embedded_hidapi_include],
libraries=["udev"],
)
)
else:
modules = [
pkgconfig_configure_extension(
Extension(
"hid",
sources=["hid.pyx", hidapi_src("libusb")],
include_dirs=[embedded_hidapi_include],
),
libusb_pkgconfig,
)
]
return modules
def hid_from_system_hidapi():
if sys.platform.startswith("linux"):
modules = []
if "--with-libusb" in sys.argv:
sys.argv.remove("--with-libusb")
HIDAPI_WITH_LIBUSB = True
else:
HIDAPI_WITH_LIBUSB = to_bool(os.getenv("HIDAPI_WITH_LIBUSB"))
if HIDAPI_WITH_LIBUSB:
hidraw_module = "hidraw"
modules.append(
pkgconfig_configure_extension(
Extension("hid", sources=["hid.pyx"]), hidapi_libusb_pkgconfig
)
)
else:
hidraw_module = "hid"
check_deprecated_without_libusb()
modules.append(
pkgconfig_configure_extension(
Extension(hidraw_module, sources=["hidraw.pyx"]),
hidapi_hidraw_pkgconfig,
)
)
else:
modules = [
pkgconfig_configure_extension(
Extension("hid", sources=["hid.pyx"]), hidapi_pkgconfig
)
]
return modules
def find_version():
filename = os.path.join(tld, "hid.pyx")
with open(filename) as f:
text = f.read()
match = re.search(r"^__version__ = \"(.*)\"$", text, re.MULTILINE)
if not match:
raise RuntimeError("cannot find version")
return match.group(1)
if "--with-system-hidapi" in sys.argv:
sys.argv.remove("--with-system-hidapi")
HIDAPI_SYSTEM_HIDAPI = True
else:
HIDAPI_SYSTEM_HIDAPI = to_bool(os.getenv("HIDAPI_SYSTEM_HIDAPI"))
if HIDAPI_SYSTEM_HIDAPI:
modules = hid_from_system_hidapi()
else:
modules = hid_from_embedded_hidapi()
setup(
name="hidapi",
version=find_version(),
description="A Cython interface to the hidapi from https://github.com/libusb/hidapi",
long_description=open("README.rst", "rt").read(),
author="Pavol Rusnak",
author_email="pavol@rusnak.io",
maintainer="Pavol Rusnak",
maintainer_email="pavol@rusnak.io",
url="https://github.com/trezor/cython-hidapi",
package_dir={"hid": "hidapi/*"},
classifiers=[
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX",
"License :: OSI Approved :: BSD License",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
],
ext_modules=cythonize(modules, language_level=3),
install_requires=["setuptools>=19.0"],
)