-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_package.py
56 lines (42 loc) · 1.62 KB
/
build_package.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
import shutil
from importlib.util import find_spec
from pathlib import Path
from setuptools import Extension
from setuptools.command.build_ext import build_ext
from setuptools.dist import Distribution
# TODO follow this example to make building easier:
# https://github.com/FedericoStra/cython-package-example/blob/master/setup.py
def build_cython_extensions() -> None:
# Flag to enable Cython code generation during install / build. This is
# enabled during development to generated the C++ files that will be
# compiled
USE_CYTHON = find_spec("Cython") is not None
ext = ".pyx" if USE_CYTHON else ".cpp"
extensions = [
Extension(
"cynetdiff.models",
["src/cynetdiff/models" + ext],
language="c++",
extra_compile_args=["-O3"],
),
]
if USE_CYTHON:
from Cython.Build import cythonize # isort: skip
# when using setuptools, you should import setuptools before Cython,
# otherwise, both might disagree about the class to use.
# http://docs.cython.org/en/latest/src/userguide/parallelism.html#compiling
extensions = cythonize(
extensions,
annotate=True,
compiler_directives={"language_level": "3str"},
)
dist = Distribution({"ext_modules": extensions})
cmd = build_ext(dist)
cmd.ensure_finalized()
cmd.run()
for output in cmd.get_outputs():
output = Path(output)
relative_extension = "src" / output.relative_to(cmd.build_lib)
shutil.copyfile(output, relative_extension)
if __name__ == "__main__":
build_cython_extensions()