-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtool.py
More file actions
213 lines (181 loc) · 7.39 KB
/
Copy pathtool.py
File metadata and controls
213 lines (181 loc) · 7.39 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import argparse
import os
import shutil
import subprocess
top_level_arch = "x86"
ret_version = "v4.3"
def nice_name(arch):
match arch:
case "x86":
return "x86"
case "arm64":
return "Arm64"
case "arm32":
return "Arm"
case "riscv":
return "RISC-V"
case "ppc":
return "PowerPC"
# Process HTML file so it can be placed in a subdirectory and reference
# files from above directory
def pphtml(src_html, arch, top_level):
with open(src_html) as f:
data = f.read()
def get_link(link_to):
if arch == link_to and top_level_arch == link_to:
# link to self from top level
return ""
if arch == top_level_arch:
# link to other from top level
return link_to + "/"
if link_to == top_level_arch:
# link to top level from not top level
return "../"
# link to other from not top level
return "../" + link_to + "/"
data = data.replace("{{RET_VERSION}}", ret_version)
data = data.replace("{{TOP_LEVEL}}", "." if top_level else "..")
data = data.replace("{{CANONICAL_URL}}", "https://ret.futo.org/" if top_level else "https://ret.futo.org/" + arch)
data = data.replace("{{SWITCH_X86}}", get_link("x86"))
data = data.replace("{{SWITCH_ARM64}}", get_link("arm64"))
data = data.replace("{{SWITCH_ARM32}}", get_link("arm32"))
data = data.replace("{{SWITCH_RISCV}}", get_link("riscv"))
data = data.replace("{{SWITCH_PPC}}", get_link("ppc"))
data = data.replace("{{TITLE}}", "Ret - Online " + nice_name(arch) + " Assembler and Disassembler")
data = data.replace("{{DESCRIPTION}}", "Online assembler and disassembler supporting ARM64, x86, ARM, Thumb, and RISC-V. Runs entirely client-side in WebAssembly.")
return data
def ignore_build(dir, contents):
return ["build"] if "build" in contents else []
def deploy_target(arch):
global top_level_arch
top_level = arch == top_level_arch
print("Compiling target " + arch)
base_dst = ""
if top_level:
base_dst = "deploy"
shutil.copytree("www", base_dst, ignore=ignore_build, symlinks=False, dirs_exist_ok=True)
else:
base_dst = f"deploy/{arch}"
os.makedirs(base_dst, exist_ok=True)
shutil.copyfile("www/index.html", os.path.join(base_dst, "index.html"))
build_dir = os.path.join(base_dst, "build")
if os.path.exists(build_dir):
shutil.rmtree(build_dir)
os.makedirs(build_dir, exist_ok=True)
shutil.copyfile(f"build_{arch}/ret.js", os.path.join(build_dir, "ret.js"))
shutil.copyfile(f"build_{arch}/ret.wasm", os.path.join(build_dir, "ret.wasm"))
with open(os.path.join(base_dst, "index.html"), "w") as f:
f.write(pphtml("www/index.html", arch, top_level))
def deploy():
if os.path.exists("deploy"):
shutil.rmtree("deploy")
os.makedirs("deploy", exist_ok=True)
deploy_target("x86")
deploy_target("arm64")
deploy_target("arm32")
deploy_target("riscv")
deploy_target("ppc")
def examples():
out = open("www/examples.js", "w")
out.write("// autogenerated by tool.py\n")
out.write(
"""
var examples = [];
function addExample(name, arch, data) {
examples.push({
name: name,
arch: arch,
data: data
});
}
""".strip() + "\n")
def encode_js(data):
return data.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '')
def add(name, arch, file):
d = open("examples/" + file, "r")
out.write("addExample(\"" + name + "\", \"" + arch + "\", \"" + encode_js(d.read()) + "\");\n")
# "Hello World" file is required for each arch
add("Hello World", "rv", "rv-hello.S")
add("Registers", "rv", "rv-regs.S")
add("Functions", "rv", "rv-func.S")
add("Sierpinski", "rv", "rv-sierpinski.S")
add("Hello World", "arm32", "arm32-hello.S")
add("Registers", "arm32", "arm32-regs.S")
add("Jumps", "arm32", "arm32-jumps.S")
add("Conditions", "arm32", "arm32-conditions.S")
add("Functions", "arm32", "arm32-functions.S")
add("Stack", "arm32", "arm32-stack.S")
add("Hello World", "arm64", "arm64-hello.S")
add("Registers", "arm64", "arm64-registers.S")
add("Stack", "arm64", "arm64-stack.S")
add("Functions", "arm64", "arm64-functions.S")
add("Exception Levels", "arm64", "arm64-el.S")
add("SIMD", "arm64", "arm64-simd.S")
add("Mandelbrot", "arm64", "arm64-mandelbrot.S")
add("Hello World", "ppc64", "ppc64-hello.s")
add("Hello World", "ppc32", "ppc32-hello.s")
add("Hello World (GNU)", "x86gnu", "x86-hello-gnu.asm")
add("Hello World (AT&T)", "x86att", "x86-hello-gnu.asm")
add("Hello World (NASM)", "x86nasm", "x86-hello-nasm.asm")
add("Hello World", "x86intel", "x86-hello.asm")
add("Registers", "x86intel", "x86-regs.asm")
add("Functions", "x86intel", "x86-functions.asm")
add("Sierpinski", "x86intel", "x86-sierpinski.asm")
out.close()
print("updated www/examples.js")
def serve():
from http.server import SimpleHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse
class CORSHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
super().end_headers()
def send_html(self, top_level, arch):
global top_level_arch
with open("www/index.html") as f:
data = f.read()
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(pphtml("www/index.html", arch, top_level).encode("utf-8"))
def do_GET(self):
path = urlparse(self.path).path
print(urlparse(self.path))
if path == "/":
self.send_html(True, top_level_arch)
elif path.strip("/") in ("arm64", "arm32", "x86", "riscv", "ppc"):
self.send_html(False, path.strip("/"))
else:
super().do_GET()
def translate_path(self, path):
root = os.path.abspath("www")
build = os.path.abspath("build")
path = urlparse(path).path
for p in ("/arm64", "/arm32", "/x86", "/riscv", "/ppc"):
if path.startswith(p + "/build"):
return os.path.join(build, path.removeprefix(p + "/build/"))
if path == p or path.startswith(p + "/"):
return os.path.join(root, path.removeprefix(p).lstrip("/"))
if path.startswith("/build"):
return os.path.join(build, path.removeprefix("/build/"))
return os.path.join(root, path.lstrip("/"))
print("http://localhost:8000/")
HTTPServer(("localhost", 8000), CORSHandler).serve_forever()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--deploy", action="store_true")
parser.add_argument("--zip", action="store_true")
parser.add_argument("--examples", action="store_true")
parser.add_argument("--serve", action="store_true")
args = parser.parse_args()
if args.deploy:
deploy()
if args.examples:
examples()
if args.serve:
serve()
if args.zip:
subprocess.run(["zip", "-r", "ret.zip", "deploy"], check=True)
if __name__ == "__main__":
main()