-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_ext.py
240 lines (190 loc) · 6.79 KB
/
build_ext.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
"""
This script provides triggers Cython build using distutils.
Default invocation, i.e., `python3 build_ext.py` does dome distutils magic to
build the extension inplace which corresponds to former call of `setup.py build_ext --inplace`.
original source: https://github.com/richard-hartmann/unified-cythonizing/edit/main/build_ext.py
Copyright 2023 Richard Hartmann
BSD (3 clause) License
"""
from Cython.Build import cythonize
import argparse
from distutils.command.build_ext import build_ext
from distutils.extension import Extension
from distutils.dist import Distribution
from distutils.errors import *
import os
import pathlib
import shutil
# if your cython code links against numpy, uncomment
# import numpy
# assume that the build_ext.py script is in the root directory of the package
root_path = pathlib.Path(__file__).absolute().parent
##############################################################
# #
# SIMPLY REGISTER YOUR EXTENSIONS HERE #
foo_c = Extension(
# the
"foo.foo_c",
sources=["./foo_c_ext/foo_c.pyx"],
extra_compile_args=["-O3"],
)
bar_c = Extension(
# the
"foo.bar.bar_c",
sources=[
"./foo_c_ext/bar_c.pyx",
],
extra_compile_args=["-O3"],
)
list_of_ext = [foo_c, bar_c]
# no further adjustments needed #
# #
##############################################################
class cd:
"""
Context manager for changing the current working directory
taken from https://stackoverflow.com/questions/431684/equivalent-of-shell-cd-command-to-change-the-working-directory/13197763#13197763
"""
def __init__(self, new_path):
self.new_path = os.path.expanduser(new_path)
def __enter__(self):
self.saved_path = os.getcwd()
os.chdir(self.new_path)
def __exit__(self, etype, value, traceback):
os.chdir(self.saved_path)
class cd_dec:
"""
a decorator which changes to current path (see 'cd' class) before calling a function
"""
def __init__(self, new_path):
self.new_path = new_path
def __call__(self, fnc):
self.fnc = fnc
return self._cd_dec_fnc
def _cd_dec_fnc(self, *args, **kwargs):
with cd(self.new_path):
self.fnc(*args, **kwargs)
# the following is adapted from https://stackoverflow.com/a/60163996
class BuildFailed(Exception):
pass
class ExtBuilder(build_ext):
def run(self):
try:
with cd(root_path):
build_ext.run(self)
except (DistutilsPlatformError, FileNotFoundError):
raise BuildFailed("File not found. Could not compile C extension.")
def build_extension(self, ext):
try:
with cd(root_path):
build_ext.build_extension(self, ext)
except (CCompilerError, DistutilsExecError, DistutilsPlatformError, ValueError):
raise BuildFailed("Could not compile C extension.")
@cd_dec(root_path)
def build(setup_kwargs):
"""
This function is mandatory in order to build the extensions.
"""
# NOTE that with cythonize, ext_modules must not be a list!
# so for more than one Cython extension things will need to be adapted
setup_kwargs.update(
{
"ext_modules": cythonize(list_of_ext),
"cmdclass": {"build_ext": ExtBuilder},
}
)
def cmd_build_ext():
# see https://stackoverflow.com/a/60525118
# distutils magic. This is essentially the same as calling
# python setup.py build_ext --inplace
setup_kwargs = {}
build(setup_kwargs)
dist = Distribution(attrs=setup_kwargs)
build_ext_cmd = dist.get_command_obj("build_ext")
build_ext_cmd.ensure_finalized()
build_ext_cmd.inplace = 1
build_ext_cmd.run()
@cd_dec(root_path)
def cmd_clean(yes=False):
dirs_to_remove = []
d = pathlib.Path("./build")
if d.exists():
dirs_to_remove.append(d.absolute())
d = pathlib.Path("./dist")
if d.exists():
dirs_to_remove.append(d.absolute())
files_to_remove = []
for e in list_of_ext:
# remove c binaries
full_mod_path = pathlib.Path(*e.name.split("."))
mod_path = full_mod_path.parent
mod_name = full_mod_path.stem
if mod_path.exists():
for f in mod_path.iterdir():
f = f.name
if f.startswith(mod_name) and f.endswith(".so"):
files_to_remove.append((mod_path / f).absolute())
# remove c/cpp source files
for s in e.sources:
s = pathlib.Path(s)
s_suff = s.suffix
s_stem = s.stem
s_parent = s.parent
if s_suff == ".pyx":
f = s_parent / (str(s_stem) + ".c")
if f.exists():
files_to_remove.append(f.absolute())
f = s_parent / (str(s_stem) + ".cpp")
if f.exists():
files_to_remove.append(f.absolute())
if len(dirs_to_remove) > 0:
print("remove the following directories")
for d in dirs_to_remove:
print(" *", d)
if len(files_to_remove) > 0:
print("(and) remove the following directories files")
for f in files_to_remove:
print(" *", f)
if len(dirs_to_remove) > 0 or len(files_to_remove) > 0:
if not yes:
answ = input("y/n [n]:")
else:
answ = "y"
if answ == "y":
for d in dirs_to_remove:
shutil.rmtree(d)
for f in files_to_remove:
os.remove(f)
print("files removed")
else:
print("abort!")
else:
print("noting to clean")
# note that upon poetry install / build, poetry actually triggers
# Command '['... .venv/bin/python', 'build_ext.py']'
# so the follows works such that this call will build the Cython extension
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"command",
help="what to do, choose between 'build_ext' or 'clear'\n"
+ " build_ext: triggers Cython inplace build (using distutils magic)\n"
+ " clean: remove dirs 'build' and 'dist' as well as libraries and c/cpp files generated by cython",
default="build_ext",
nargs="?",
type=str,
)
parser.add_argument(
"--yes", help="assume 'yes' when running 'clean'", action="store_true"
)
args = parser.parse_args()
if args.command == "build_ext":
cmd_build_ext()
elif args.command == "clean":
cmd_clean(yes=args.yes)
else:
parser.print_help()
raise ValueError(f"unknown command '{args.command}'")