-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.py
More file actions
131 lines (116 loc) · 4.55 KB
/
setup.py
File metadata and controls
131 lines (116 loc) · 4.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
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
import os
import platform
import pprint
import shutil
import subprocess
import sys
import sysconfig
from pathlib import Path
from tempfile import TemporaryDirectory
from setuptools import setup
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.extension import Extension
from wheel.bdist_wheel import bdist_wheel
_is_free_threaded = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
# set the platform-specific files, libeep first, pyeep second.
if platform.system() == "Linux":
_pyeep = (
f"pyeep{sysconfig.get_config_var('EXT_SUFFIX')}"
if _is_free_threaded
else "pyeep.abi3.so"
)
lib_files = ["libEep.so", _pyeep]
elif platform.system() == "Windows":
# CMakeLists.txt forces SUFFIX .pyd on Windows, so the output filename is
# always pyeep.pyd regardless of free-threading or SOABI.
lib_files = ["Eep.dll", "pyeep.pyd"]
elif platform.system() == "Darwin":
_pyeep = (
f"pyeep{sysconfig.get_config_var('EXT_SUFFIX')}"
if _is_free_threaded
else "pyeep.abi3.so"
)
lib_files = ["libEep.dylib", _pyeep]
else:
lib_files = []
class CMakeExtension(Extension):
"""Dummy wrapper for CMake build."""
def __init__(self, name, py_limited_api=False) -> None:
# don't invoke the original build_ext for this special extension
super().__init__(name, sources=[], py_limited_api=py_limited_api)
class build_ext(_build_ext): # noqa: D101
def build_extensions(self) -> None: # noqa: D102
pass # cmake handles everything, skip default setuptools build
def run(self) -> None:
"""Build libeep with cmake as part of the extension build process."""
src_dir = Path(__file__).parent / "src" / "libeep"
# This is an unfortunate hack to get new env vars within a GH Actions step
# (no way to use before-build to inject env vars back to the env)
check_env = os.environ
if "GITHUB_ENV" in check_env:
print("Using GITHUB_ENV instead of os.environ:") # noqa: T201
check_env = dict(
line.split("=", maxsplit=1)
for line in Path(os.environ["GITHUB_ENV"])
.read_text("utf-8")
.splitlines()
if "=" in line
)
pprint.pprint(check_env) # noqa: T203
with TemporaryDirectory() as build_dir: # str
args = [
"cmake",
"-S",
str(src_dir),
"-B",
build_dir,
"-DCMAKE_BUILD_TYPE=Release",
f"-DPython3_EXECUTABLE={sys.executable}",
]
if _is_free_threaded:
args.append("-DUSE_STABLE_ABI=OFF")
for key in (
"CMAKE_GENERATOR",
"CMAKE_GENERATOR_PLATFORM",
"Python3_LIBRARY",
"Python3_SABI_LIBRARY",
):
if key in check_env:
args.append(f"-D{key}={check_env[key]}") # noqa: PERF401
subprocess.run(args, check=True)
subprocess.run(
["cmake", "--build", build_dir, "--config", "Release"], check=True
)
# locate the built files and copy then to antio.libeep
build_dir = Path(build_dir)
if platform.system() == "Windows":
lib = build_dir / "Release" / lib_files[0]
pyeep = build_dir / "python" / "Release" / lib_files[1]
else:
lib = build_dir / lib_files[0]
pyeep = build_dir / "python" / lib_files[1]
for elt in (lib, pyeep):
dst = Path(self.build_lib) / "antio" / "libeep" / elt.name
dst.parent.mkdir(parents=True, exist_ok=True)
print(f"Moving {elt} to {dst}") # noqa: T201
shutil.move(elt, dst)
super().run()
# Adapted from
# https://github.com/joerick/python-abi3-package-sample/blob/main/setup.py
class bdist_wheel_abi3(bdist_wheel): # noqa: D101
def get_tag(self): # noqa: D102
python, abi, plat = super().get_tag()
if python.startswith("cp") and not abi.endswith("t"):
# on CPython, our wheels are abi3 and compatible back to 3.2,
# but let's set it to our min version anyway
return "cp311", "abi3", plat
return python, abi, plat
setup(
ext_modules=[
CMakeExtension("antio.libeep.pyeep", py_limited_api=not _is_free_threaded)
],
cmdclass={
"build_ext": build_ext,
"bdist_wheel": bdist_wheel_abi3,
},
)