-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
357 lines (306 loc) · 12.3 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import configparser
import glob
import os
import platform
import re
import subprocess
import sys
import multiprocessing.pool
import setuptools
from distutils import log
from distutils.errors import CompileError
from distutils.command.clean import clean as _clean
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.build_clib import build_clib as _build_clib
from setuptools.command.sdist import sdist as _sdist
from setuptools.extension import Library, Extension
try:
from Cython.Build import cythonize
except ImportError as err:
cythonize = err
# --- Constants -----------------------------------------------------------------
SETUP_FOLDER = os.path.relpath(os.path.realpath(os.path.join(__file__, os.pardir)))
SYSTEM = platform.system()
MACHINE = platform.machine()
if re.match("^mips", MACHINE):
TARGET_CPU = "mips"
elif re.match("^arm", MACHINE):
TARGET_CPU = "arm"
elif re.match("^aarch64", MACHINE):
TARGET_CPU = "aarch64"
elif re.match("(x86_64)|(AMD64|amd64)|(^i.86$)", MACHINE):
TARGET_CPU = "x86"
elif re.match("^(powerpc|ppc)", MACHINE):
TARGET_CPU = "ppc"
# --- Utils ------------------------------------------------------------------
def _eprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
# --- Commands ------------------------------------------------------------------
class sdist(_sdist):
"""A `sdist` that generates a `pyproject.toml` on the fly.
"""
def run(self):
# build `pyproject.toml` from `setup.cfg`
c = configparser.ConfigParser()
c.add_section("build-system")
c.set("build-system", "requires", str(self.distribution.setup_requires))
c.set("build-system", 'build-backend', '"setuptools.build_meta"')
with open("pyproject.toml", "w") as pyproject:
c.write(pyproject)
# run the rest of the packaging
_sdist.run(self)
class build_ext(_build_ext):
"""A `build_ext` that disables optimizations if compiled in debug mode.
"""
# --- Compatibility with `setuptools.Command`
def finalize_options(self):
_build_ext.finalize_options(self)
# transfer arguments to the build_clib method
self._clib_cmd = self.get_finalized_command("build_clib")
self._clib_cmd.debug = self.debug
self._clib_cmd.force = self.force
self._clib_cmd.verbose = self.verbose
self._clib_cmd.define = self.define
self._clib_cmd.include_dirs = self.include_dirs
self._clib_cmd.compiler = self.compiler
self._clib_cmd.parallel = self.parallel
# --- Build code ---
def build_extension(self, ext):
# show the compiler being used
_eprint("building", ext.name, "with", self.compiler.compiler_type, "compiler")
# add debug symbols if we are building in debug mode
if self.debug:
if self.compiler.compiler_type in {"unix", "cygwin", "mingw32"}:
ext.extra_compile_args.append("-g")
elif self.compiler.compiler_type == "msvc":
ext.extra_compile_args.append("/Z7")
if sys.implementation.name == "cpython":
ext.define_macros.append(("CYTHON_TRACE_NOGIL", 1))
else:
ext.define_macros.append(("CYTHON_WITHOUT_ASSERTIONS", 1))
# add C++11 flags
if self.compiler.compiler_type in {"unix", "cygwin", "mingw32"}:
ext.extra_compile_args.append("-std=c++11")
elif self.compiler.compiler_type == "msvc":
ext.extra_compile_args.append("/std:c11")
# add Windows flags
if self.compiler.compiler_type == "msvc":
ext.define_macros.append(("WIN32", 1))
# update link and include directories
for name in ext.libraries:
lib = self._clib_cmd.get_library(name)
libfile = self.compiler.library_filename(
lib.name, output_dir=self._clib_cmd.build_clib
)
ext.depends.append(libfile)
ext.extra_objects.append(libfile)
# build the rest of the extension as normal
_build_ext.build_extension(self, ext)
def build_extensions(self):
# check `cythonize` is available
if isinstance(cythonize, ImportError):
raise RuntimeError("Cython is required to run `build_ext` command") from cythonize
# use debug directives with Cython if building in debug mode
cython_args = {
"include_path": ["include"],
"compiler_directives": {
"cdivision": True,
"nonecheck": False,
},
"compile_time_env": {
"SYS_IMPLEMENTATION_NAME": sys.implementation.name,
"SYS_VERSION_INFO_MAJOR": sys.version_info.major,
"SYS_VERSION_INFO_MINOR": sys.version_info.minor,
"SYS_VERSION_INFO_MICRO": sys.version_info.micro,
"TARGET_CPU": TARGET_CPU,
"AVX2_BUILD_SUPPORT": False,
"NEON_BUILD_SUPPORT": False,
"SSE2_BUILD_SUPPORT": False,
}
}
if self.force:
cython_args["force"] = True
if self.debug:
cython_args["annotate"] = True
cython_args["compiler_directives"]["cdivision_warnings"] = True
cython_args["compiler_directives"]["warn.undeclared"] = True
cython_args["compiler_directives"]["warn.unreachable"] = True
cython_args["compiler_directives"]["warn.maybe_uninitialized"] = True
cython_args["compiler_directives"]["warn.unused"] = True
cython_args["compiler_directives"]["warn.unused_arg"] = True
cython_args["compiler_directives"]["warn.unused_result"] = True
cython_args["compiler_directives"]["warn.multiple_declarators"] = True
else:
cython_args["compiler_directives"]["boundscheck"] = False
cython_args["compiler_directives"]["wraparound"] = False
# compile the C library
if not self.distribution.have_run.get("build_clib", False):
self._clib_cmd.run()
# add the include dirs
for ext in self.extensions:
ext.include_dirs.append(self._clib_cmd.build_clib)
# cythonize the extensions (retaining platform-specific sources)
self.extensions = cythonize(self.extensions, **cython_args)
# fix extensions
for ext in self.extensions:
ext._needs_stub = False
# build the extensions as normal
_build_ext.build_extensions(self)
class build_clib(_build_clib):
"""A custom `build_clib` that splits the `training.c` file from Prodigal.
"""
# --- Autotools-like helpers ---
def _publicize(self, input, output):
with open(input, "rb") as src:
with open(output, "wb") as dst:
for line in src:
dst.write(line.replace(b"private:", b"public:"))
# --- Compatibility with base `build_clib` command ---
def check_library_list(self, libraries):
pass
def get_library_names(self):
return [ lib.name for lib in self.libraries ]
def get_source_files(self):
return [ source for lib in self.libraries for source in lib.sources ]
def get_library(self, name):
return next(lib for lib in self.libraries if lib.name == name)
# --- Build code ---
def build_libraries(self, libraries):
# build each library only if the sources are outdated
self.mkpath(self.build_clib)
for library in libraries:
libname = self.compiler.library_filename(library.name, output_dir=self.build_clib)
self.make_file(library.sources, libname, self.build_library, (library,))
def build_library(self, library):
# show the compiler being used
_eprint("building", library.name, "with", self.compiler.compiler_type, "compiler")
# add debug flags if we are building in debug mode
if self.debug:
if self.compiler.compiler_type in {"unix", "cygwin", "mingw32"}:
library.extra_compile_args.append("-g")
elif self.compiler.compiler_type == "msvc":
library.extra_compile_args.append("/Z7")
# add C++11 flags
if self.compiler.compiler_type in {"unix", "cygwin", "mingw32"}:
library.extra_compile_args.append("-std=c++11")
elif self.compiler.compiler_type == "msvc":
library.extra_compile_args.append("/std:c11")
# add Windows flags
if self.compiler.compiler_type == "msvc":
library.define_macros.append(("WIN32", 1))
# expose all private members and copy headers to build directory
for header in library.depends:
output = os.path.join(self.build_clib, os.path.basename(header))
self.make_file(
[header],
output,
self._publicize,
(header, output)
)
# copy sources to build directory
sources = [
os.path.join(self.build_temp, os.path.basename(source))
for source in library.sources
]
for source, source_copy in zip(library.sources, sources):
self.make_file(
[source],
source_copy,
self.copy_file,
(source, source_copy)
)
# store compile args
compile_args = (
library.define_macros,
library.include_dirs,
self.debug,
library.extra_compile_args,
None,
library.depends,
)
# manually prepare sources and get the names of object files
objects = [
s.replace(".cpp", self.compiler.obj_extension)
for s in sources
]
# only compile outdated files
with multiprocessing.pool.ThreadPool(self.parallel) as pool:
pool.map(
lambda args: self.make_file(
[args[0]],
args[1],
self.compiler.compile,
([args[0]], None, *compile_args)
),
zip(sources, objects)
)
# link into a static library
libfile = self.compiler.library_filename(
library.name,
output_dir=self.build_clib,
)
self.make_file(
objects,
libfile,
self.compiler.create_static_lib,
(objects, library.name, self.build_clib, None, self.debug)
)
class clean(_clean):
"""A `clean` that removes intermediate files created by Cython.
"""
def run(self):
source_dir = os.path.join(os.path.dirname(__file__), "pymuscle5")
patterns = ["*.html"]
if self.all:
patterns.extend(["*.so", "*.c", "*.cpp"])
for pattern in patterns:
for file in glob.glob(os.path.join(source_dir, pattern)):
log.info("removing {!r}".format(file))
os.remove(file)
_clean.run(self)
# --- Setup ---------------------------------------------------------------------
setuptools.setup(
libraries=[
Library(
"muscle",
language="c++",
sources=[
x
for x in glob.glob(os.path.join(SETUP_FOLDER, "vendor", "muscle", "src", "*.cpp"))
if os.path.basename(x) not in {"main.cpp", "make_a2m.cpp"}
],
depends=[
x
for x in glob.glob(os.path.join(SETUP_FOLDER, "vendor", "muscle", "src", "*.h"))
],
include_dirs=[
os.path.join(SETUP_FOLDER, "pymuscle5"),
os.path.join(SETUP_FOLDER, "include"),
os.path.join(SETUP_FOLDER, "pymuscle5", "patch"),
],
),
],
ext_modules=[
Extension(
"pymuscle5._muscle",
language="c++",
sources=[
os.path.join(SETUP_FOLDER, "pymuscle5", "_muscle.pyx"),
],
include_dirs=[
os.path.join(SETUP_FOLDER, "pymuscle5"),
os.path.join(SETUP_FOLDER, "include"),
os.path.join(SETUP_FOLDER, "pymuscle5", "patch"),
],
libraries=[
"muscle",
],
),
],
cmdclass={
"sdist": sdist,
"build_ext": build_ext,
"build_clib": build_clib,
"clean": clean
}
)