-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
167 lines (143 loc) · 5.53 KB
/
setup.py
File metadata and controls
167 lines (143 loc) · 5.53 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
160
161
162
163
164
165
166
167
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
import os
from pathlib import Path
import sys
use_system_lib = bool(int(os.environ.get("QUICKJS_USE_SYSTEM_LIB", 0)))
QUICKJS_DIR = Path("quickjs")
quickjs_sources = list(
map(
str,
[
QUICKJS_DIR / "cutils.c",
QUICKJS_DIR / "dtoa.c",
QUICKJS_DIR / "libregexp.c",
QUICKJS_DIR / "libunicode.c",
QUICKJS_DIR / "quickjs.c",
],
)
)
DEFINE_MACROS = (
[("WIN32_LEAN_AND_MEAN", "1"), ("_WIN32_WINNT", "0x0601")]
if sys.platform == "win32"
else []
)
EXTRA_COMPILE_ARGS = (
["/std:c11", "/experimental:c11atomics"] if sys.platform == "win32" else []
)
# Added so that things can compile a bit faster (hopefully)
EXTRA_IGNORE_COMPILE_ARGS = (
[
"/wd4018", # -Wno-sign-conversion
"/wd4061", # -Wno-implicit-fallthrough
"/wd4100", # -Wno-unused-parameter
"/wd4200", # -Wno-zero-length-array
"/wd4242", # -Wno-shorten-64-to-32
"/wd4244", # -Wno-shorten-64-to-32
"/wd4245", # -Wno-sign-compare
"/wd4267", # -Wno-shorten-64-to-32
"/wd4388", # -Wno-sign-compare
"/wd4389", # -Wno-sign-compare
"/wd4456", # Hides previous local declaration
"/wd4457", # Hides function parameter
"/wd4710", # Function not inlined
"/wd4711", # Function was inlined
"/wd4820", # Padding added after construct
"/wd4996", # -Wdeprecated-declarations
"/wd5045", # Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
]
if sys.platform == "win32"
else [
# IDK How to do all these flags yet but I am reviewing CMakeLists.txt in quickjs for clues...
"-Wno-implicit-fallthrough",
"-Wno-sign-compare",
"-Wno-missing-field-initializers",
"-Wno-unused-parameter",
"-Wno-unused-but-set-variable",
"-Wno-unused-result",
"-Wno-stringop-truncation",
"-Wno-array-bounds",
]
)
EXTRA_COMPILE_ARGS += EXTRA_IGNORE_COMPILE_ARGS
class quickjs_build_ext(build_ext):
# Brought over from winloop since these can be very useful.
user_options = build_ext.user_options + [
("cython-always", None, "run cythonize() even if .c files are present"),
(
"cython-annotate",
None,
"Produce a colorized HTML version of the Cython source.",
),
("cython-directives=", None, "Cythion compiler directives"),
]
def initialize_options(self):
self.cython_always = False
self.cython_annotate = False
self.cython_directives = None
self.parallel = True
super().initialize_options()
# Copied from winloop
def finalize_options(self):
need_cythonize = self.cython_always
cfiles = {}
for extension in self.distribution.ext_modules:
for i, sfile in enumerate(extension.sources):
if sfile.endswith(".pyx"):
prefix, _ = os.path.splitext(sfile)
cfile = prefix + ".c"
if os.path.exists(cfile) and not self.cython_always:
extension.sources[i] = cfile
else:
if os.path.exists(cfile):
cfiles[cfile] = os.path.getmtime(cfile)
else:
cfiles[cfile] = 0
need_cythonize = True
# from winloop & cyares
if need_cythonize:
# import pkg_resources
# Double check Cython presence in case setup_requires
# didn't go into effect (most likely because someone
# imported Cython before setup_requires injected the
# correct egg into sys.path.
try:
import Cython # type: ignore # noqa: F401
except ImportError:
raise RuntimeError("please install cython to compile cyjs from source")
from Cython.Build import cythonize
directives = {}
if self.cython_directives:
for directive in self.cython_directives.split(","):
k, _, v = directive.partition("=")
if v.lower() == "false":
v = False
if v.lower() == "true":
v = True
directives[k] = v
self.cython_directives = directives
self.distribution.ext_modules[:] = cythonize(
self.distribution.ext_modules,
compiler_directives=directives,
annotate=self.cython_annotate,
emit_linenums=self.debug,
# Try using a cache to help with compiling as well...
cache=True,
)
return super().finalize_options()
def pyx_ext(file: str):
return Extension(
f"cyjs.{file}",
[f"cyjs/{file}.pyx"] + quickjs_sources,
include_dirs=["quickjs"],
define_macros=DEFINE_MACROS,
# NOTE: You will need to fix stdalign.h like I did if yours didn't exist - Vizonex
extra_compile_args=EXTRA_COMPILE_ARGS,
)
if __name__ == "__main__":
setup(
ext_modules=[
pyx_ext("_cyjs"),
],
cmdclass={"build_ext": quickjs_build_ext},
)