-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
159 lines (132 loc) · 5.48 KB
/
setup.py
File metadata and controls
159 lines (132 loc) · 5.48 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import filecmp
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from build_utils.property_preprocessor import preprocess_properties
from build_utils.constants_preprocessor import preprocess_constants
from build_utils.cython_guard_preprocessor import preprocess_cython_guards
import multiprocessing
import os
import platform
import shutil
from multiprocessing import cpu_count
import numpy as np
from setuptools import Extension, setup
CYTHON = platform.python_implementation() == "CPython"
ROOT_DIR = "pyboy_advance"
DEBUG = bool(os.getenv("GITHUB_ACTIONS"))
if not CYTHON:
setup()
exit(0)
from Cython.Build import cythonize # noqa
from Cython.Compiler import DebugFlags, Errors # noqa
from Cython.Distutils import build_ext as _build_ext # noqa
def patched_error(position, message):
if message == "Python object cannot be passed as a varargs parameter":
# We ignore this error to pass PyObject* to logging
return
else:
# print("Errors.error:", repr(position), repr(message)) ###
if position is None:
raise Errors.InternalError(message)
err = Errors.CompileError(position, message)
if DebugFlags.debug_exception_on_error:
raise Exception(err) # debug
Errors.report_error(err)
return err
Errors.error = patched_error
class BuildExt(_build_ext):
def initialize_options(self):
super().initialize_options()
# This is only for Cythonize. See finalize_options.
if sys.platform == "win32":
thread_count = 0 # Disables multiprocessing
else:
thread_count = cpu_count()
# Fixing issue with nthreads in Cython
if ((3, 8) <= sys.version_info[:2] and sys.platform == "darwin") or (
(3, 14) <= sys.version_info[:2] and sys.platform == "linux"
):
multiprocessing.set_start_method("fork", force=True)
cflags = ["-O3"]
if not DEBUG:
cflags.append("-DCYTHON_WITHOUT_ASSERTIONS")
# NOTE: For performance. Check if other platforms need an equivalent change.
if sys.platform == "darwin":
cflags.append(
"-DCYTHON_INLINE=inline __attribute__ ((__unused__)) __attribute__((always_inline))"
)
py_pxd_files = list(prep_pxd_py_files())
preprocessed_files = []
for src_file in py_pxd_files:
preprocessed_file = src_file.replace(".py", ".preprocessed.py")
shutil.copy(src_file, preprocessed_file)
preprocessed_files.append(preprocessed_file)
preprocessed_files = preprocess_cython_guards(preprocessed_files)
preprocessed_files = preprocess_properties(preprocessed_files)
preprocessed_files = preprocess_constants(preprocessed_files)
build_files = []
for preprocessed_file in preprocessed_files:
build_file = preprocessed_file.replace(".preprocessed.py", ".build.py")
build_files.append(build_file)
# Only replace the .build.py file if there are changes to avoid updating
# its last modified timestamp
if not os.path.isfile(build_file) or (
not filecmp.cmp(preprocessed_file, build_file, shallow=False)
):
shutil.copy(preprocessed_file, build_file)
os.remove(preprocessed_file)
cythonize_files = map(
lambda src: Extension(
src.split(".")[0].replace(os.sep, "."),
[src],
extra_compile_args=cflags,
extra_link_args=[] if DEBUG else ["-s", "-w"],
include_dirs=[np.get_include()],
),
build_files,
)
self.distribution.ext_modules = cythonize(
[*cythonize_files],
nthreads=thread_count,
annotate=True,
gdb_debug=False,
language_level=3,
compiler_directives={
"annotation_typing": False,
"boundscheck": False,
"cdivision": True,
"cdivision_warnings": False,
"infer_types": True,
"initializedcheck": False,
"nonecheck": False,
"overflowcheck": False,
"wraparound": False,
"legacy_implicit_noexcept": True,
},
)
def finalize_options(self):
# Use '-j' on the commandline to specify parallelism. Otherwise auto-detect.
if getattr(self, "parallel", None) is None:
self.parallel = cpu_count()
super().finalize_options()
def prep_pxd_py_files():
ignore_py_files = ["__main__.py", "__init__.py", "conftest.py"]
# Cython doesn't trigger a recompile on .py files, where only the .pxd file has changed. So we fix this here.
# We also yield the py_files that have a .pxd file, as we feed these into the cythonize call.
for root, dirs, files in os.walk(ROOT_DIR):
for f in files:
base, ext = f.split(".", 1)
if ext == "py" and f not in ignore_py_files:
yield os.path.join(root, f)
if ext == "pxd":
py_file = os.path.join(root, base) + ".build.py"
if os.path.isfile(py_file):
if os.path.getmtime(os.path.join(root, f)) > os.path.getmtime(py_file):
os.utime(py_file)
setup(
cmdclass={
"build_ext": BuildExt,
},
ext_modules=[Extension("", [""])], # Added to trigger a binary wheel
)