-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
80 lines (66 loc) · 2.71 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
import os
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext as _build_ext
try:
from Cython.Build import cythonize
except ImportError:
cythonize = None
""" this code is inspired by https://github.com/FedericoStra/cython-package-example """
class build_ext(_build_ext):
"""
From https://stackoverflow.com/questions/19919905/how-to-bootstrap-numpy-installation-in-setup-py/21621689#21621689
"""
def finalize_options(self):
_build_ext.finalize_options(self)
# Prevent numpy from thinking it is still in its setup process:
import numpy
print("Building package with numpy version {}".format(numpy.__version__))
self.include_dirs.append(numpy.get_include())
# https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#distributing-cython-modules
def no_cythonize(extensions, **_ignore):
for extension in extensions:
sources = []
for sfile in extension.sources:
path, ext = os.path.splitext(sfile)
if ext in (".pyx", ".py"):
if extension.language == "c++":
ext = ".cpp"
else:
ext = ".c"
sfile = path + ext
sources.append(sfile)
extension.sources[:] = sources
return extensions
CYTHONIZE = bool(int(os.getenv("CYTHONIZE", 0))) and cythonize is not None
extensions = [
Extension(
"hadrons.cython_files.continuous_beam",
["hadrons/cython_files/continuous_beam.pyx"],
),
Extension(
"hadrons.cython_files.initial_recombination",
["hadrons/cython_files/initial_recombination.pyx"],
),
Extension(
"electrons.cython.continuous_e_beam", ["electrons/cython/continuous_e_beam.pyx"]
),
Extension("electrons.cython.pulsed_e_beam", ["electrons/cython/pulsed_e_beam.pyx"]),
]
if CYTHONIZE:
# description of compiler directives: https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#compiler-directives
# language_level 3 enables and enforces Python 3 semantics
# embedsignature Cython will embed a textual copy of the call signature in the docstring of all Python visible functions and classes
compiler_directives = {"language_level": 3, "embedsignature": True}
extensions = cythonize(
module_list=extensions, compiler_directives=compiler_directives
)
else:
extensions = no_cythonize(extensions)
with open("requirements.txt") as fp:
install_requires = fp.read().strip().split("\n")
setup(
cmdclass={"build_ext": build_ext},
ext_modules=extensions,
install_requires=install_requires,
setup_requires=["numpy", "cython"],
)