forked from moonbitlang/moonbit-compiler
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
executable file
·101 lines (75 loc) · 3.1 KB
/
test.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
#!/bin/python3
import argparse
import os
class DirContext:
def __init__(self, path):
self.new_path = path
self.original = None
def __enter__(self):
self.original = os.getcwd()
os.chdir(self.new_path)
def __exit__(self, exc_type, exc_value, traceback):
os.chdir(self.original)
parser = argparse.ArgumentParser(prog = "test", description = "Tests the MoonBit compiler.")
parser.add_argument("-d", "--debug", action="store_true", help="enable stack traces on debug")
parser.add_argument("-w", "--wasm", action="store_true", help="build to WASM rather than RISC-V")
parser.add_argument("-i", "--build-index", action="store_true", help="build OCaml index and exit")
parser.add_argument("-b", "--build-only", action="store_true", help="build without testing")
parser.add_argument("-v", "--verbose", action="store_true", help="interpreter outputs detailed values")
parser.add_argument("-t", "--test", type=str, help="execute this test case only")
args = parser.parse_args()
if args.wasm:
target = "wasm-gc"
dest = "target.wat"
else:
target = "riscv"
dest = "target.s"
core = "~/.moon/lib/core"
bundled = f"{core}/target/wasm-gc/release/bundle"
debug = "OCAMLRUNPARAM=b" if args.debug else ""
verbose = "-DVERBOSE" if args.verbose else ""
def try_remove(path):
if os.path.exists(path):
os.remove(path)
# Build
if args.build_index:
os.system("dune build @ocaml-index")
print("Done")
exit(0)
print("Building MoonBit compiler...")
os.system("dune build -p moonbit-lang")
# WASM does not require an interpreter
if not args.wasm:
print("Building SSA interpreter...")
os.makedirs("test/build", exist_ok=True)
os.system(f"clang++ -std=c++20 {verbose} test/interpreter.cpp -Wall -g -o test/build/interpreter")
if args.build_only:
print("Done.")
exit(0)
success = True
with DirContext("test"):
cases = os.listdir("src") if args.test is None else [args.test]
for src in cases:
print(f"Execute task: {src}")
# Note build-package is ignorant of target. It builds to a common IR.
os.system(f"moonc build-package src/{src}/{src}.mbt -is-main -std-path {bundled} -o build/{src}.core")
# Linkage emits target code.
ret = os.system(f"{debug} moonc link-core {bundled}/core.core build/{src}.core -o build/{dest} -pkg-config-path {src}/moon.pkg.json -pkg-sources {core}:{src} -target {target}")
if ret != 0:
print("Compiler generated an error. Failed.")
success = False
continue;
if args.wasm:
print("WASM target does not support testing. Exit.")
break;
# Remove intermediate files that we don't need.
try_remove(f"build/{src}.core")
try_remove(f"build/{src}.mi")
# Test.
os.system(f"build/interpreter build/{dest}.ssa > build/output.txt 2> build/debug.txt")
diff = os.system(f"diff build/output.txt src/{src}/{src}.ans")
if diff == 0:
print("Passed.")
else:
success = False
exit(0 if success else 1)