This repository was archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
64 lines (46 loc) · 1.92 KB
/
build.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
import multiprocessing
from pathlib import Path
from typing import List
from setuptools import Extension, Distribution
from Cython.Build import cythonize
from Cython.Distutils.build_ext import new_build_ext as cython_build_ext
SOURCE_DIR = Path("SRC") # Replace "SRC" with your project's root
BUILD_DIR = Path("BUILD")
def build() -> None:
extension_modules = cythonize_helper(get_extension_modules())
# Use Setuptools to collect files
distribution = Distribution({
"ext_modules": extension_modules,
"cmdclass": {
"build_ext": cython_build_ext,
},
})
# Copy all files back to the source directory
# This ensures that Poetry can include them in its build
build_ext_cmd = distribution.get_command_obj("build_ext")
build_ext_cmd.ensure_finalized()
build_ext_cmd.inplace = 1
build_ext_cmd.run()
def get_extension_modules() -> List[Extension]:
extension_modules: List[Extension] = []
for py_file in SOURCE_DIR.rglob("*.py"):
module_path = py_file.with_suffix("")
module_path = str(module_path).replace("/", ".")
extension_module = Extension(
name=module_path,
sources=[str(py_file)]
)
extension_modules.append(extension_module)
return extension_modules
def cythonize_helper(extension_modules: List[Extension]) -> List[Extension]:
return cythonize(
module_list=extension_modules,
build_dir=BUILD_DIR, # Avoid building in the source tree
annotate=False, # No need to generate a .html output file
nthreads=multiprocessing.cpu_count()*2, # Parallelize the build
compiler_directives={"language_level": "3",
"annotation_typing": False}, # Indicate Python 3 usage to Cython without using type hints annotations
force=True, # (Optional) Always rebuild, even if files are untouched
)
if __name__ == "__main__":
build()