-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
122 lines (97 loc) · 3.27 KB
/
run.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
import sys
import os
import subprocess
def printHelp(arg0: str) -> None:
print("usage: {} [--dbg] [INPUT.asm]".format(arg0))
def clone_submodules() -> None:
def clone_submodules() -> None:
print("--- Running `git submodule init`")
subprocess.run(["git", "submodule", "init"])
print("--- Running `git submodule update --remote`")
subprocess.run(["git", "submodule", "update", "--remote"])
if not os.path.isdir("lbvm_asm"):
clone_submodules()
elif len(os.listdir("lbvm_asm")) == 0:
clone_submodules()
def make_bin_dir() -> None:
if not os.path.isdir("bin"):
print("--- Making `bin` directory")
os.mkdir("bin")
def cmd_exists(cmd: str) -> bool:
try:
result = subprocess.run(["which", cmd], stdout=subprocess.PIPE)
return result.returncode == 0
except:
return False
def check_requirements(arg0: str) -> None:
if os.name != "posix":
print("{} requires UNIX-like systems".format(arg0))
exit(1)
def require_cmd(cmd: str, notes: str = None) -> None:
notes_ = "" if notes == None else " " + notes
if not cmd_exists(cmd):
print("requires `{}`{}".format(cmd, notes_))
exit(1)
require_cmd("git")
require_cmd("make")
require_cmd("clang")
require_cmd(
"cabal", notes="(install GHCup and cabal at https://www.haskell.org/ghcup/)"
)
def main() -> None:
args = iter(sys.argv)
arg0 = next(args)
check_requirements(arg0)
inputPath: str = None
dbg = False
for arg in args:
if arg == "--help":
printHelp(arg0)
exit(0)
elif arg == "--dbg":
dbg = True
else:
if inputPath != None:
print("multiple input paths are not supported")
printHelp(arg0)
exit(1)
inputPath = arg
if inputPath == None:
print("expects one argument for input file")
printHelp(arg0)
exit(1)
clone_submodules()
make_bin_dir()
print("--- Running `cabal build` @ ./lbvm_asm")
result = subprocess.run(["cabal", "build"], cwd="lbvm_asm")
if result.returncode != 0:
print(
"--- `cabal run` exited with non zero code, try deleting `lbvm_asm/` and then run {} again".format(
arg0
)
)
exit(1)
inputPath_ = "../" + inputPath
print("--- Running `cabal run exes -- {}` @ ./lbvm_asm".format(inputPath_))
result = subprocess.run(["cabal", "run", "exes", "--", inputPath_], cwd="lbvm_asm", stdout=subprocess.PIPE)
if result.returncode != 0:
print(result.stdout.decode())
print("`cabal run` returned with non-zero exit code")
exit(result.returncode)
print("--- Writing to code.h")
codeHFile = open("code.h", "w+")
codeHFile.write(result.stdout.decode())
codeHFile.close()
if dbg:
subprocess.run(["bat", "-l", "c", "code.h"], check=True)
print("--- Running `make all`")
subprocess.run(["make", "all"], check=True)
print("--- Running `bin/lbvm`")
result = (
subprocess.run(["bin/lbvm", "--dbg"])
if dbg
else subprocess.run(["bin/lbvm"])
)
exit(result.returncode)
if __name__ == "__main__":
main()