-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsetup.py
More file actions
89 lines (65 loc) · 2.25 KB
/
setup.py
File metadata and controls
89 lines (65 loc) · 2.25 KB
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
""" setup.py
Compile the Cython extensions for PySpike.
All packaging metadata (name, version, dependencies, classifiers, ...) lives in
pyproject.toml. This file only declares the C extension modules, because that
still needs imperative setup() configuration.
To compile cython files in-place:
python setup.py build_ext --inplace
Copyright 2014-2026, Mario Mulansky <mario.mulansky@gmx.net>
Distributed under the BSD License
"""
import os.path
from setuptools import Extension, setup
try:
from Cython.Distutils import build_ext
except ImportError:
use_cython = False
else:
use_cython = True
class numpy_include(os.PathLike):
"""Defers import of numpy until the build environment is in place.
pyproject.toml lists numpy as a build-system requirement, so by the time
setup.py actually runs build_ext, numpy is importable. We can't import it
at module top level, though, because setuptools imports setup.py before
build-system requires are installed.
"""
def __str__(self):
import numpy
return numpy.get_include()
def __fspath__(self):
return str(self)
_CYTHON_MODULES = (
"cython_add",
"cython_get_tau",
"cython_profiles",
"cython_distances",
"cython_directionality",
"cython_simulated_annealing",
)
def _all_c_sources_present():
return all(
os.path.isfile(f"pyspike/cython/{name}.c") for name in _CYTHON_MODULES
)
use_c = _all_c_sources_present()
if not use_cython and not use_c:
print("Cython not installed and no pre-generated .c files found. "
"PySpike will fall back to the pure-Python backend (slow).")
cmdclass = {}
ext_modules = []
if use_cython: # Cython is available, compile .pyx -> .c -> binary
ext_modules = [
Extension(f"pyspike.cython.{name}", [f"pyspike/cython/{name}.pyx"])
for name in _CYTHON_MODULES
]
cmdclass["build_ext"] = build_ext
elif use_c: # No Cython, but pre-generated .c files are present
ext_modules = [
Extension(f"pyspike.cython.{name}", [f"pyspike/cython/{name}.c"])
for name in _CYTHON_MODULES
]
# else: neither Cython nor .c files — fall through to pure-Python backend.
setup(
cmdclass=cmdclass,
ext_modules=ext_modules,
include_dirs=[numpy_include()],
)