-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.py
222 lines (192 loc) · 8.05 KB
/
build.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python3
import glob
import sys
import os
import subprocess
import shutil
import hashlib
import colorama
from typing import List
BINARIES: List[str] = ['boot', 'Tower', 'Transitions', 'Boss', 'game']
BUILD_DIR: str = 'build'
TOOLS_DIR: str = 'tools'
BASENAME: str = 'gauntlet'
SPLAT: str = f'{TOOLS_DIR}/splat/split.py'
M2CTX: str = f'{TOOLS_DIR}/m2ctx.py'
COMPRESS: str = f'{TOOLS_DIR}/compress.py'
ASMPROCESSOR: str = f'{TOOLS_DIR}/asm-processor/asm_processor.py'
REMCOMS: str = f'{TOOLS_DIR}/clearcomments.py'
N64CRC: str = f'{TOOLS_DIR}/n64crc'
FIRSTDIFF: str = 'first_diff.py'
PYTHON: str = 'python3'
def verify_checksum(bin_file: str, sha1sum_file: str) -> bool:
# Read the SHA1 checksum from the binary file
with open(bin_file, 'rb') as f:
sha1 = hashlib.sha1()
while True:
data = f.read(65536)
if not data:
break
sha1.update(data)
bin_checksum = sha1.hexdigest()
# Read the SHA1 checksum from the file that contains the checksum
with open(sha1sum_file, 'r') as f:
file_checksum = f.readline().split()[0]
# Compare the checksums and return the result
if bin_checksum == file_checksum:
return True
else:
return False
# I decided that I hate Ninja
def read_build_ninja_original() -> List[str]:
with open('build.ninja_', 'r') as original_file:
lines = original_file.readlines()
for i, line in enumerate(lines):
lines[i] = line.replace('__BASENAME__', BASENAME)
if line.startswith('PYTHON = PYTHON_VALUE'):
lines[i] = f'PYTHON = {PYTHON}\n'
return lines
def generate_build_ninja(permuter: bool, binary_name: str, write_mode: str) -> None:
c_files = glob.glob(f'src/{binary_name}/**/*.c', recursive=True)
asm_files = [file for file in glob.glob(f'asm/{binary_name}/**/*.s', recursive=True) + glob.glob(f'src/{binary_name}/**/*.s', recursive=True) if 'nonmatching' not in file]
bin_files = glob.glob(f'assets/{binary_name}/**/*.bin', recursive=True)
output_lines = []
if write_mode == 'w':
output_lines = read_build_ninja_original()
o_files = []
output_lines.append(f'\n\n# Output for {binary_name}\n')
for c_file in c_files:
obj_file = os.path.join(f'{BUILD_DIR}/{binary_name}', c_file + '.o')
if not permuter:
output_lines.append(f'build {BUILD_DIR}/{binary_name}/{c_file}.d: compile_c_cpp {c_file}\n')
output_lines.append(f'build {obj_file}_: compile_c {c_file} | {BUILD_DIR}/{binary_name}/{c_file}.d\n')
output_lines.append(f'build {obj_file}: compile_c_strip {obj_file}_\n\n')
else:
output_lines.append(f'build {obj_file}: compile_c {c_file}\n')
o_files.append(obj_file)
os.makedirs(os.path.join('build', binary_name, os.path.dirname(c_file)), exist_ok=True)
for asm_file in asm_files:
obj_file = os.path.join(f'{BUILD_DIR}/{binary_name}', asm_file + '.o')
output_lines.append(f'build {obj_file}: assemble_s {asm_file}\n')
o_files.append(obj_file)
os.makedirs(os.path.join('build', binary_name, os.path.dirname(asm_file)), exist_ok=True)
for bin_file in bin_files:
obj_file = os.path.join(f'{BUILD_DIR}/{binary_name}', bin_file + '.o')
output_lines.append(f'build {obj_file}: compile_bin {bin_file}\n')
o_files.append(obj_file)
os.makedirs(os.path.join('build', binary_name, os.path.dirname(bin_file)), exist_ok=True)
o_files_str = ' '.join(o_files)
output_lines.append(f'\nbuild {BUILD_DIR}/{binary_name}.elf: link_{binary_name} {o_files_str}\n')
if binary_name == 'boot':
requirements = " | "
for requirement in BINARIES:
if requirement is not 'boot':
requirements = f'{requirements} {BUILD_DIR}/{requirement}.bin'
output_lines.append(f'build {BUILD_DIR}/{BASENAME}.z64: objcopy {BUILD_DIR}/{binary_name}.elf {requirements}\n')
else:
output_lines.append(f'build {BUILD_DIR}/{binary_name}.bin: objcopy {BUILD_DIR}/{binary_name}.elf\n')
with open('build.ninja', write_mode) as build_file:
build_file.writelines(output_lines)
def invoke_ninja() -> None:
subprocess.run(['ninja'], check = True)
# Commands
def command_initial() -> None:
subprocess.run(['make', '-C', TOOLS_DIR], check = True)
def command_clean() -> None:
if os.path.exists('build'):
shutil.rmtree('build')
def command_nuke() -> None:
command_clean()
if os.path.exists('asm'):
shutil.rmtree('asm')
if os.path.exists('assets'):
shutil.rmtree('assets')
autos = glob.glob('*auto.txt')
for autofile in autos:
try:
os.remove(autofile)
except:
print(f'Failed to remove {autofile}')
def command_context() -> None:
try:
os.remove('ctx.c')
os.remove('ctx_includes.c')
except OSError:
pass
include_files = []
for root, dirs, files in os.walk('include/'):
for file in files:
if file.endswith('.h'):
include_files.append(os.path.join(root, file))
for root, dirs, files in os.walk('src/'):
for file in files:
if file.endswith('.h'):
include_files.append(os.path.join(root, file))
ctxinc = 'ctx_includes.c'
with open(ctxinc, 'w') as f:
for include_file in include_files:
f.write(f'#include "{include_file}"\n')
subprocess.run([PYTHON, M2CTX, ctxinc], check=True)
def build(permuter: bool) -> None:
write_mode = 'w'
for binary_name in BINARIES:
generate_build_ninja(permuter, binary_name, write_mode)
write_mode = 'a'
invoke_ninja()
print('================')
for binary_name in BINARIES:
tname = f'{binary_name}.bin'
if binary_name == 'boot':
tname = f'{BASENAME}.z64'
subprocess.run([N64CRC, f'{BUILD_DIR}/{tname}'])
if verify_checksum(f'{BUILD_DIR}/{tname}', f'{binary_name}_checksum.sha1'):
print(f'{binary_name}: OK!')
else:
print(f'{binary_name}: NOT OK!!')
if __name__ == '__main__':
colorama.init()
PYTHON = sys.executable
build_used = False
args = sys.argv[1:]
for value in args:
trunc = value.split('=')
arg = trunc[0]
if arg == 'initial':
command_initial()
if arg == 'nuke':
command_nuke()
if arg == 'clean':
command_clean()
if arg == 'setup':
try:
os.remove('files/syms/symbol_addrs.txt')
except:
pass
for binary_name in BINARIES:
subprocess.run(['cpp', '-CC', '-P', f'files/syms/symbol_addrs_original.{binary_name}.txt'], stdout = open(f'files/syms/symbol_addrs.{binary_name}.txt', 'w'))
subprocess.run([PYTHON, REMCOMS, f'files/syms/symbol_addrs.{binary_name}.txt'])
subprocess.run([PYTHON, SPLAT, f'{BASENAME}.yaml'], check = True)
subprocess.run([PYTHON, COMPRESS, 'assets/boot/Tower.bin', 'files/Tower'], check = True)
subprocess.run([PYTHON, COMPRESS, 'assets/boot/Transitions.bin', 'files/Transitions'], check = True)
subprocess.run([PYTHON, COMPRESS, 'assets/boot/Boss.bin', 'files/Boss'], check = True)
subprocess.run([PYTHON, COMPRESS, 'assets/boot/game.bin', 'files/game'], check = True)
for binary_name in BINARIES:
if binary_name == 'boot': continue
if os.path.exists(f'{binary_name}.yaml'):
subprocess.run([PYTHON, SPLAT, f'{binary_name}.yaml'], check = True)
if arg == 'context':
command_context()
if arg == 'build':
build_used = True
if len(trunc) > 1:
if int(trunc[1]) == 1:
build(False)
else:
build(False)
if arg == 'permuter':
build_used = True
build(True)
if arg == 'diff':
subprocess.run([PYTHON, FIRSTDIFF], check = True)
if build_used == False:
build(False)