This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
forked from Magdoll/cDNA_Cupcake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
89 lines (71 loc) · 2.41 KB
/
build.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
# lifted from https://github.com/sdispater/pendulum
import shutil
from distutils.command.build_ext import build_ext
from distutils.core import Distribution, Extension
from distutils.errors import CCompilerError, DistutilsExecError, DistutilsPlatformError
from pathlib import Path
import numpy as np
from Cython.Build import cythonize
# C Extensions
extensions = [
Extension(
"cupcake.tofu.branch.intersection_unique",
include_dirs=[np.get_include()],
sources=["src/cupcake/tofu/branch/intersection_unique.pyx"],
),
Extension(
"cupcake.tofu.branch.c_branch",
include_dirs=[np.get_include()],
sources=["src/cupcake/tofu/branch/c_branch.pyx"],
),
]
class BuildFailed(Exception):
pass
class ExtBuilder(build_ext):
# This class allows C extension building to fail.
built_extensions = []
def run(self):
try:
build_ext.run(self)
except (DistutilsPlatformError, FileNotFoundError):
print(
" Unable to build the C extensions, "
"cDNA_Cupcake will use the pure python code instead."
)
def build_extension(self, ext):
try:
build_ext.build_extension(self, ext)
except (CCompilerError, DistutilsExecError, DistutilsPlatformError, ValueError):
print(
f" Unable to build the '{ext.name}' C extension, cDNA_Cupcake "
f"will use the pure python version of the extension."
)
def build(setup_kwargs):
"""
This function is mandatory in order to build the extensions.
"""
distribution = Distribution(
{
"name": "src/cupcake",
"ext_modules": cythonize(
extensions, compiler_directives={"language_level": "3"}
),
}
)
distribution.package_dir = "cupcake"
cmd = ExtBuilder(distribution)
cmd.ensure_finalized()
cmd.run()
# Copy built extensions back to the project
for output in cmd.get_outputs():
output = Path(output)
relative_extension = Path("src").joinpath(output.relative_to(cmd.build_lib))
if not output.exists():
continue
shutil.copyfile(output, relative_extension)
mode = relative_extension.stat().st_mode
mode |= (mode & 0o444) >> 2
relative_extension.chmod(mode)
return setup_kwargs
if __name__ == "__main__":
build({})