-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplease
executable file
·162 lines (120 loc) · 3.86 KB
/
please
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
#!/usr/bin/env python3
import sys
from subprocess import run as execute
DISABLED_WARNINGS = [
"-Wno-pointer-sign",
"-Wno-incompatible-library-redeclaration",
"-Wno-incompatible-pointer-types",
"-Wno-builtin-requires-header",
"-Wno-int-conversion",
# "-Wno-builtin-declaration-mismatch"
]
commands = {}
def command_handler(function):
commands[function.__name__] = function
return function
def generate_source_file(istream, ostream, identifier):
def generate_single_byte(byte):
if ord(' ') <= byte <= ord('~'):
if byte == ord('\"') or byte == ord('\\'):
ostream.write('\\')
ostream.write(chr(byte))
elif byte == ord('\n'):
ostream.write('\\n"\n"')
else:
ostream.write(f'\\x{byte:02x}')
ostream.write('#include <stddef.h>\n\n')
ostream.write('#ifdef PRELOAD_INCLUDED\n')
ostream.write('extern const char {}[];\n'.format(identifier))
ostream.write('extern const size_t {}_len;\n\n'.format(identifier))
ostream.write('const char {}[] =\n"'.format(identifier))
while True:
input_byte = istream.read(1)
if input_byte == b"":
break
generate_single_byte(ord(input_byte))
ostream.write('";\n\n')
ostream.write('const size_t {}_len = sizeof({}) - 1;\n'.format(identifier, identifier))
ostream.write('#endif\n')
# Commands
@command_handler
def clion(_arguments):
"""
Builds all configurations for CLion.
"""
for path in [
"build/debug",
"build/release",
"build/coverage",
]:
print(f"Building {path}...")
execute(["cmake", "--build", path])
@command_handler
def test_all(_arguments):
"""
Runs debug and release tests.
"""
execute(["./tests/runner.py", "clion:debug"])
execute(["./tests/runner.py", "clion:release"])
@command_handler
def test_debug(_arguments):
"""
Builds the debug compiler and runs the tests.
"""
execute(["cmake", "--build", "build/debug"])
execute(["./tests/runner.py", "clion:debug"])
@command_handler
def test_release(_arguments):
"""
Builds the release compiler and runs the tests.
"""
execute(["cmake", "--build", "build/release"])
execute(["./tests/runner.py", "clion:release"])
@command_handler
def generate_preload(_arguments):
"""
Generates preload.c from preload.aa.
"""
istream = open("runtime/preload.aa", "rb")
ostream = open("source/preload.c", "w")
try:
generate_source_file(istream, ostream, "preload_source")
except Exception as e:
print(f"Failed generating preload: {e}")
finally:
ostream.close()
istream.close()
@command_handler
def compile_file(arguments):
"""
Uses the compiler to build a program.
"""
clion([]) # Make sure the compiler is built.
execute(["./build/debug/atcc", *arguments])
execute(["clang", "-O3", *DISABLED_WARNINGS, "-o", "program", "generated.c"])
execute(["./program"])
@command_handler
def llvm(arguments):
"""
Uses the compiler to build a program using the LLVM backend.
"""
clion([]) # Make sure the compiler is built.
execute(["./build/debug/atcc", "-b", "llvm", "runtime/llvm.aa", *arguments])
execute(["clang", "-O3", *DISABLED_WARNINGS, "-o", "program", "generated.o"])
execute(["./program"])
def main(args):
if len(args) < 1:
print("[please] Please specify a command.")
print("[please] Available commands:")
for command in commands:
print(f" {command}: {commands[command].__doc__}")
return
command = args[0].replace("-", "_")
arguments = args[1:]
if handler := commands.get(command):
handler(arguments)
print("[please] Done.")
else:
print(f"[please] Unknown command: {command}")
if __name__ == '__main__':
main(sys.argv[1:])