|
1 | 1 | from setuptools import setup, Extension |
2 | | -from Cython.Build import cythonize |
3 | | -import numpy |
4 | 2 | import os |
5 | 3 |
|
| 4 | +try: |
| 5 | + # Cython is optional when building from an sdist that includes generated C |
| 6 | + from Cython.Build import cythonize # type: ignore |
| 7 | + HAVE_CYTHON = True |
| 8 | +except Exception: |
| 9 | + HAVE_CYTHON = False |
| 10 | + |
| 11 | +import numpy |
| 12 | + |
6 | 13 | # Create __init__.py for the cython package if it doesn't exist |
7 | 14 | cython_dir = "tsnkit/simulation/cython" |
8 | 15 | init_file = os.path.join(cython_dir, "__init__.py") |
9 | 16 | if not os.path.exists(init_file): |
10 | 17 | with open(init_file, "w") as f: |
11 | 18 | f.write("# Cython simulation extensions\n") |
12 | 19 |
|
13 | | -# Define Cython extensions |
| 20 | +src_pyx = "tsnkit/simulation/cython/simulation_core.pyx" |
| 21 | +src_c = "tsnkit/simulation/cython/simulation_core.c" |
| 22 | +source = src_pyx if os.path.exists(src_pyx) else src_c |
| 23 | + |
| 24 | +# Define Cython/C extensions |
14 | 25 | extensions = [ |
15 | 26 | Extension( |
16 | 27 | "tsnkit.simulation.cython.simulation_core", |
17 | | - ["tsnkit/simulation/cython/simulation_core.pyx"], |
| 28 | + [source], |
18 | 29 | include_dirs=[numpy.get_include()], |
19 | 30 | define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")], |
20 | | - extra_compile_args=["/O2"] if os.name == "nt" else ["-O3", "-ffast-math"] |
| 31 | + extra_compile_args=["/O2"] if os.name == "nt" else ["-O3", "-ffast-math"], |
21 | 32 | ) |
22 | 33 | ] |
23 | 34 |
|
24 | 35 | setup( |
25 | 36 | name="tsnkit-cython-extensions", |
26 | | - ext_modules=cythonize(extensions, compiler_directives={ |
27 | | - 'language_level': 3, |
28 | | - 'boundscheck': False, |
29 | | - 'wraparound': False, |
30 | | - 'initializedcheck': False, |
31 | | - 'cdivision': True, |
32 | | - }), |
| 37 | + ext_modules=( |
| 38 | + cythonize( |
| 39 | + extensions, |
| 40 | + compiler_directives={ |
| 41 | + "language_level": 3, |
| 42 | + "boundscheck": False, |
| 43 | + "wraparound": False, |
| 44 | + "initializedcheck": False, |
| 45 | + "cdivision": True, |
| 46 | + }, |
| 47 | + ) |
| 48 | + if HAVE_CYTHON and source.endswith(".pyx") |
| 49 | + else extensions |
| 50 | + ), |
33 | 51 | zip_safe=False, |
34 | 52 | ) |
0 commit comments