|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| -""" |
3 |
| -Build cython extension modules. |
| 2 | +import os |
| 3 | +from setuptools import Extension |
4 | 4 |
|
5 |
| -The shared library can also be built manually using the command: |
6 |
| -$ cythonize -X language_level=3 -a -i ./iscc_core/cdc.py |
7 |
| -$ cythonize -X language_level=3 -a -i ./iscc_core/minhash.py |
8 |
| -$ cythonize -X language_level=3 -a -i ./iscc_core/simhash.py |
9 |
| -$ cythonize -X language_level=3 -a -i ./iscc_core/dct.py |
10 |
| -$ cythonize -X language_level=3 -a -i ./iscc_core/wtahash.py |
11 |
| -""" |
12 |
| -try: |
13 |
| - from Cython.Build import cythonize, build_ext |
14 |
| -except ImportError: |
15 |
| - # dummy build function for poetry |
16 |
| - def build(setup_kwargs): |
17 |
| - pass |
18 | 5 |
|
19 |
| -else: |
| 6 | +def build(setup_kwargs): |
| 7 | + try: |
| 8 | + from Cython.Build import cythonize |
20 | 9 |
|
21 |
| - class build_ext_gracefull(build_ext): |
22 |
| - def run(self): |
23 |
| - try: |
24 |
| - print("Trying to compile C accelerator modules") |
25 |
| - super().run() |
26 |
| - print("Successfully comiled C accelerator modules") |
27 |
| - except Exception as e: |
28 |
| - print(e) |
29 |
| - print("********************************************************************") |
30 |
| - print("Failed to compile C accelerator module, falling back to pure python.") |
31 |
| - print("********************************************************************") |
| 10 | + use_cython = True |
| 11 | + except ImportError: |
| 12 | + use_cython = False |
32 | 13 |
|
33 |
| - def build_extensions(self): |
34 |
| - try: |
35 |
| - print("Trying to compile C accelerator modules") |
36 |
| - super().build_extensions() |
37 |
| - print("Successfully comiled C accelerator modules") |
38 |
| - except Exception as e: |
39 |
| - print(e) |
40 |
| - print("********************************************************************") |
41 |
| - print("Failed to compile C accelerator module, falling back to pure python.") |
42 |
| - print("********************************************************************") |
43 |
| - |
44 |
| - def build(setup_kwargs): |
| 14 | + if use_cython: |
45 | 15 | try:
|
46 |
| - setup_kwargs.update( |
47 |
| - dict( |
48 |
| - ext_modules=cythonize( |
49 |
| - [ |
50 |
| - "iscc_core/cdc.py", |
51 |
| - "iscc_core/minhash.py", |
52 |
| - "iscc_core/simhash.py", |
53 |
| - "iscc_core/dct.py", |
54 |
| - "iscc_core/wtahash.py", |
55 |
| - ] |
56 |
| - ), |
57 |
| - cmdclass=dict(build_ext=build_ext_gracefull), |
58 |
| - ) |
| 16 | + ext_modules = cythonize( |
| 17 | + [ |
| 18 | + Extension("iscc_core.cdc", ["iscc_core/cdc.py"]), |
| 19 | + Extension("iscc_core.minhash", ["iscc_core/minhash.py"]), |
| 20 | + Extension("iscc_core.simhash", ["iscc_core/simhash.py"]), |
| 21 | + Extension("iscc_core.dct", ["iscc_core/dct.py"]), |
| 22 | + Extension("iscc_core.wtahash", ["iscc_core/wtahash.py"]), |
| 23 | + ], |
| 24 | + compiler_directives={"language_level": "3"}, |
59 | 25 | )
|
| 26 | + setup_kwargs.update({"ext_modules": ext_modules}) |
| 27 | + print("Cython modules prepared for compilation") |
60 | 28 | except Exception as e:
|
61 |
| - print(e) |
62 |
| - print("********************************************************************") |
63 |
| - print("Failed to compile C accelerator module, falling back to pure python.") |
64 |
| - print("********************************************************************") |
| 29 | + print(f"Failed to prepare Cython modules: {e}") |
| 30 | + print("Falling back to pure Python") |
| 31 | + else: |
| 32 | + print("Cython not available, using pure Python") |
0 commit comments