Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
103 lines (82 loc) · 2.74 KB
/
Copy pathsetup.py
File metadata and controls
103 lines (82 loc) · 2.74 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""setup.py kept alongside pyproject.toml to build the optional native cffi
extension ``autopdex.sim_state._native._topo_hash``.
If cffi or a C compiler is unavailable the build degrades gracefully: a short
warning is printed and the extension is skipped, so the package still installs
and falls back to the pure-NumPy topology path at runtime.
"""
from __future__ import annotations
import sys
from setuptools import setup
from setuptools.command.build_ext import build_ext
try:
from setuptools.errors import (
CCompilerError,
CompileError,
LinkError,
LibError,
PlatformError,
)
except ImportError: # pragma: no cover - compatibility with older setuptools
from distutils.errors import ( # type: ignore[no-redef]
CCompilerError,
CompileError,
LinkError,
LibError,
DistutilsPlatformError,
)
PlatformError = DistutilsPlatformError
OPTIONAL_BUILD_ERRORS = (
CCompilerError,
CompileError,
LinkError,
LibError,
PlatformError,
OSError,
)
class optional_build_ext(build_ext):
"""Build optional native extensions if possible, otherwise skip them."""
def run(self):
try:
super().run()
except OPTIONAL_BUILD_ERRORS as exc:
if self._all_extensions_optional():
self.warn(
"autopdex: optional native topology extension "
f"could not be built: {exc}. "
"Falling back to the pure-NumPy path."
)
return
raise
def build_extension(self, ext):
try:
super().build_extension(ext)
except OPTIONAL_BUILD_ERRORS as exc:
if getattr(ext, "optional", False):
self.warn(
"autopdex: optional native topology extension "
f"{ext.name!r} could not be built: {exc}. "
"Falling back to the pure-NumPy path."
)
return
raise
def _all_extensions_optional(self) -> bool:
return bool(self.extensions) and all(
getattr(ext, "optional", False) for ext in self.extensions
)
ext_modules = []
try:
sys.path.insert(0, "autopdex/sim_state/_native")
from _topo_hash_cffi import ffi
topo_hash_ext = ffi.distutils_extension()
topo_hash_ext.optional = True
ext_modules = [topo_hash_ext]
except Exception as exc: # pragma: no cover - depends on build environment
print(
"autopdex: could not prepare native topology extension "
f"(_topo_hash): {exc}. Falling back to the pure-NumPy path.",
file=sys.stderr,
)
setup(
ext_modules=ext_modules,
cmdclass={"build_ext": optional_build_ext},
)