-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
192 lines (156 loc) · 5.7 KB
/
setup.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import glob
import json
import os
import platform
import struct
import setuptools.command.build_ext
opcodes = {}
with open(os.path.join(os.path.dirname(__file__), "wasmpy/opcodes.json")) as fp:
data = json.load(fp)
replacements = data["replacements"]
functions = data["functions"]
for group in data["opcodes"]:
opcodes.update(
dict(
zip(
(i[0] for i in group["instructions"]),
(i + group["offset"] for i in range(len(group["instructions"]))),
)
)
)
def is_x86() -> bool:
return platform.machine() in ("x86", "i386", "i686", "AMD64", "x86_64")
class gen_opcodes(setuptools.Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# generate opcodes.cpp
if is_x86():
if struct.calcsize("P") == 4:
machine = "x86"
else:
machine = "x86_64"
else:
raise ValueError("Unknown architecture")
with open(f"wasmpy/arch/{machine}/lib/opcodes.cpp", "w+") as out:
out.writelines(
(
"// auto-generated\n\n",
'#include "opcodes.hpp"\n\n',
"bool decodeOperation(bytes buf, size_t offset, bytes *insts)\n{\n\t",
"switch (buf.at(offset))\n\t{\n\t",
)
)
for file in glob.glob(f"wasmpy/arch/{machine}/**/*.bin"):
inst = os.path.splitext(os.path.basename(file))[0]
if inst in opcodes:
with open(file, "rb") as fp:
data = ", ".join(str(i) for i in fp.read() if i != 0x90)
# apply replacements
if inst in replacements:
for replacement in replacements[inst]:
data = data.replace(*replacement)
out.write(f"case {opcodes[inst]}:\n\t\t")
out.write(f"*insts = {{{data}}};\n\t\t")
out.write("break;\n\n\t")
out.write(
'default:\n\t\tPyErr_SetString(PyExc_NotImplementedError, "unimplemented instruction");\n\t\treturn false;\n\t}\n\treturn true;\n}\n'
)
for file in glob.glob(f"wasmpy/arch/{machine}/**/*.bin"):
name = os.path.splitext(os.path.basename(file))[0]
if name not in opcodes:
with open(file, "rb") as fp:
data = ", ".join(str(i) for i in fp.read() if i != 0x90)
if os.path.splitext(name)[1] == "":
# apply replacements
if name in replacements:
for replacement in replacements[name]:
data = data.replace(*replacement)
if name in functions:
out.write(
f"\nbytes {name}({functions[name]})\n{{\n\treturn {{{data}}};\n}}\n"
)
else:
out.write(f"\nbytes {name} = {{{data}}};\n")
class build_ext(setuptools.command.build_ext.build_ext):
def run(self):
self.run_command("gen_opcodes")
setuptools.command.build_ext.build_ext.run(self)
plat = []
if platform.system() == "Linux":
plat = [("PLATFORM_LINUX", None)]
elif platform.system() == "Windows":
plat = [("PLATFORM_WINDOWS", None)]
arch = []
if is_x86():
if struct.calcsize("P") == 4:
machine = "x86"
arch = [("ARCH_X86", None)]
else:
machine = "x86_64"
arch = [("ARCH_X86_64", None)]
ext = [
setuptools.Extension(
"wasmpy.nativelib",
include_dirs=[
os.path.abspath("wasmpy/include"),
os.path.abspath(f"wasmpy/arch/{machine}/lib"),
],
sources=[
f"wasmpy/arch/{machine}/lib/lib.cpp",
f"wasmpy/arch/{machine}/lib/opcodes.cpp",
"wasmpy/globals.cpp",
"wasmpy/memories.cpp",
"wasmpy/nativelib.cpp",
],
define_macros=plat + arch,
py_limited_api=True,
),
setuptools.Extension(
"wasmpy.spectest",
sources=["wasmpy/spectest.cpp"],
py_limited_api=True,
),
]
with open("README.md", "r") as fp:
description = fp.read()
setuptools.setup(
name="wasmpy",
author="Olivia Ryan",
author_email="[email protected]",
description="WebAssembly in Python.",
long_description=description,
long_description_content_type="text/markdown",
url="https://github.com/olivi-r/wasmpy",
packages=["wasmpy", "wasmpy.binary", "wasmpy.text"],
package_data={"wasmpy": ["opcodes.json"]},
ext_modules=ext,
options={"bdist_wheel": {"py_limited_api": "cp36"}},
cmdclass={
"build_ext": build_ext,
"gen_opcodes": gen_opcodes,
},
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Environment :: WebAssembly",
"Programming Language :: C++",
"Programming Language :: Assembly",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: MIT License",
],
license="MIT",
python_requires=">=3.6",
install_requires=[
"sexpdata>=1.0.1;python_version>='3.7'",
"sexpdata==1.0.0;python_version=='3.6'",
],
)