-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild_ext.py
More file actions
87 lines (77 loc) · 2.3 KB
/
Copy pathbuild_ext.py
File metadata and controls
87 lines (77 loc) · 2.3 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
82
83
84
85
86
87
"""Build optional cython modules."""
import os
from distutils.command.build_ext import build_ext
from os.path import join
from typing import Any
try:
from setuptools import Extension
except ImportError:
from distutils.core import Extension
utils_module = Extension(
"bluetooth_data_tools._utils_impl",
[
join("src", "bluetooth_data_tools", "_utils_impl.pyx"),
],
language="c",
extra_compile_args=["-O3", "-g0"],
)
time_module = Extension(
"bluetooth_data_tools._time_impl",
[
join("src", "bluetooth_data_tools", "_time_impl.pyx"),
],
language="c",
extra_compile_args=["-O3", "-g0"],
)
TO_CYTHONIZE = [
"src/bluetooth_data_tools/gap.py",
"src/bluetooth_data_tools/utils.py",
]
EXTENSIONS = [
Extension(
ext.removeprefix("src/").removesuffix(".py").replace("/", "."),
[ext],
language="c",
extra_compile_args=["-O3", "-g0"],
)
for ext in TO_CYTHONIZE
]
class BuildExt(build_ext):
def build_extensions(self) -> None:
if self.parallel is None: # type: ignore[has-type, unused-ignore]
self.parallel = os.cpu_count() or 1
std_flag = (
"/std:clatest" if self.compiler.compiler_type == "msvc" else "-std=gnu2x"
)
for ext in self.extensions:
if std_flag not in ext.extra_compile_args:
ext.extra_compile_args = [*ext.extra_compile_args, std_flag]
try:
super().build_extensions()
except Exception: # noqa: S110
pass
def build(setup_kwargs: Any) -> None:
if os.environ.get("SKIP_CYTHON", False):
return
try:
from Cython.Build import cythonize # noqa: PLC0415
setup_kwargs.update(
dict(
ext_modules=cythonize(
[
time_module,
utils_module,
*EXTENSIONS,
],
compiler_directives={"language_level": "3"}, # Python 3
),
cmdclass=dict(build_ext=BuildExt),
)
)
setup_kwargs["exclude_package_data"] = {
pkg: ["*.c"] for pkg in setup_kwargs["packages"]
}
except Exception:
if os.environ.get("REQUIRE_CYTHON"):
raise
pass