-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfigure.py
executable file
·285 lines (224 loc) · 8.1 KB
/
configure.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/python3
import argparse
import os
import shutil
import subprocess
import sys
import re
from pathlib import Path
from typing import Dict, List, Set, Union
from fix_gp import main as fix_gp_main
import ninja_syntax
import splat
import splat.scripts.split as split
from splat.segtypes.linker_entry import LinkerEntry
ROOT = Path(__file__).parent
TOOLS_DIR = ROOT / "tools"
YAML_FILE = "config/sotc_preview.yaml"
BASENAME = "SCPS_150.97"
LD_PATH = f"{BASENAME}.ld"
ELF_PATH = f"build/{BASENAME}"
MAP_PATH = f"build/{BASENAME}.map"
PRE_ELF_PATH = f"build/{BASENAME}.elf"
COMMON_INCLUDES = "-Iinclude -I include/sdk/ee -I include/sdk -I include/gcc"
COMPILER = "ee-gcc2.96"
GAME_CC_DIR = f"{TOOLS_DIR}/cc/{COMPILER}/bin"
CUSTOM_SPECS_FILE = f"{TOOLS_DIR}/cc/{COMPILER}/lib/regnames.specs"
GAME_COMPILE_CMD = f"{GAME_CC_DIR}/ee-gcc -c {COMMON_INCLUDES} -O2 -g2 $regnames"
# Custom spec rule that invokes the preprocessor before assembling
# avoids us having to manually pipe each step and just use GCC
# it defines PREPROCESS_ASM so it can be checked in source if need be
CUSTOM_SPECS = """@c:
cc1 -lang-c %{ansi:-std=c89} %(cpp_options) -DPREPROCESS_ASM %(cc1_options) -o %{|!pipe:%g.si} |
%(trad_capable_cpp) -lang-asm %{v} %I -I./ %{|!pipe:%g.si} -o %{|!pipe:%g.s} |
as %(asm_options) %{!pipe:%g.s} %A
"""
def create_custom_specs():
specs_file = Path(CUSTOM_SPECS_FILE)
if not specs_file.exists():
with specs_file.open("w") as f:
f.write(CUSTOM_SPECS)
def exec_shell(command: List[str], stdout=subprocess.PIPE) -> str:
ret = subprocess.run(command, stdout=stdout, stderr=subprocess.PIPE, text=True)
return ret.stdout
def clean():
if os.path.exists(".splache"):
os.remove(".splache")
if os.path.exists(CUSTOM_SPECS_FILE):
os.remove(CUSTOM_SPECS_FILE)
shutil.rmtree("asm", ignore_errors=True)
shutil.rmtree("assets", ignore_errors=True)
shutil.rmtree("build", ignore_errors=True)
def write_permuter_settings():
rel_cc_dir = Path(GAME_CC_DIR).relative_to(ROOT)
with open("permuter_settings.toml", "w") as f:
f.write(
f"""compiler_command = "{rel_cc_dir}/ee-gcc -c {COMMON_INCLUDES} -O2 -g2"
assembler_command = "mips-linux-gnu-as -march=r5900 -Iinclude"
compiler_type = "gcc"
[preserve_macros]
[decompme.compilers]
"{rel_cc_dir}/ee-gcc" = "{COMPILER}"
"""
)
def build_stuff(linker_entries: List[LinkerEntry]):
built_objects: Set[Path] = set()
def build(
object_paths: Union[Path, List[Path]],
src_paths: List[Path],
task: str,
variables: Dict[str, str] = {},
implicit_outputs: List[str] = [],
):
if not isinstance(object_paths, list):
object_paths = [object_paths]
object_strs = [str(obj) for obj in object_paths]
for object_path in object_paths:
if object_path.suffix == ".o":
built_objects.add(object_path)
ninja.build(
outputs=object_strs,
rule=task,
inputs=[str(s) for s in src_paths],
variables=variables,
implicit_outputs=implicit_outputs,
)
ninja = ninja_syntax.Writer(open(str(ROOT / "build.ninja"), "w"), width=9999)
# Rules
cross = "mips-linux-gnu-"
ld_args = "-EL -T config/undefined_syms_auto.txt -T config/undefined_funcs_auto.txt -T config/undefined_syms.txt -Map $mapfile -T $in -o $out"
ninja.rule(
"as",
description="as $in",
command=f"cpp {COMMON_INCLUDES} $in | {cross}as -no-pad-sections -EL -march=5900 -mabi=eabi -Iinclude -o $out",
)
ninja.rule(
"cc",
description="cc $in",
command=f"{GAME_COMPILE_CMD} $in -o $out && {cross}strip $out -N dummy-symbol-name",
)
ninja.rule(
"ld",
description="link $out",
command=f"{cross}ld {ld_args}",
)
ninja.rule(
"sha1sum",
description="sha1sum $in",
command="sha1sum -c $in && touch $out",
)
ninja.rule(
"elf",
description="elf $out",
command=f"{cross}objcopy $in $out -O binary",
)
for entry in linker_entries:
seg = entry.segment
if seg.type[0] == ".":
continue
if entry.object_path is None:
continue
if isinstance(seg, splat.segtypes.common.asm.CommonSegAsm) or isinstance(
seg, splat.segtypes.common.data.CommonSegData
):
build(entry.object_path, entry.src_paths, "as")
elif isinstance(seg, splat.segtypes.common.cpp.CommonSegCpp):
build(entry.object_path, entry.src_paths, "cpp")
elif isinstance(seg, splat.segtypes.common.c.CommonSegC):
if split.config["options"]["named_regs_for_c_funcs"]:
regnames = "--specs=regnames.specs"
else:
regnames = ""
build(entry.object_path, entry.src_paths, "cc", variables={"regnames": regnames})
elif isinstance(
seg, splat.segtypes.common.databin.CommonSegDatabin
) or isinstance(seg, splat.segtypes.common.rodatabin.CommonSegRodatabin):
build(entry.object_path, entry.src_paths, "as")
else:
print(f"ERROR: Unsupported build segment type {seg.type}")
sys.exit(1)
ninja.build(
PRE_ELF_PATH,
"ld",
LD_PATH,
implicit=[str(obj) for obj in built_objects],
variables={"mapfile": MAP_PATH},
)
ninja.build(
ELF_PATH,
"elf",
PRE_ELF_PATH,
)
ninja.build(
ELF_PATH + ".ok",
"sha1sum",
"checksum.sha1",
implicit=[ELF_PATH],
)
# Pattern to workaround unintended nops around loops
COMMENT_PART = r"\/\* (.+) ([0-9A-Z]{2})([0-9A-Z]{2})([0-9A-Z]{2})([0-9A-Z]{2}) \*\/"
INSTRUCTION_PART = r"(\b(bne|bnel|beq|beql|bnez|bnezl|beqzl|bgez|bgezl|bgtz|bgtzl|blez|blezl|bltz|bltzl|b)\b.*)"
OPCODE_PATTERN = re.compile(f"{COMMENT_PART} {INSTRUCTION_PART}")
PROBLEMATIC_FUNCS = set(
[
"OutputLinkerScriptFile",
"LoaderSysResetSystem",
"func_00105A60",
"_putString",
"InitDisp",
]
)
def replace_instructions_with_opcodes(asm_folder: Path) -> None:
nm_folder = ROOT / asm_folder / "nonmatchings"
for p in nm_folder.rglob("*.s"):
if p.stem not in PROBLEMATIC_FUNCS:
continue
with p.open("r") as file:
content = file.read()
if re.search(OPCODE_PATTERN, content):
# Reference found
# Embed the opcode, we have to swap byte order for correct endianness
content = re.sub(
OPCODE_PATTERN,
r"/* \1 \2\3\4\5 */ .word 0x\5\4\3\2 /* \6 */",
content,
)
# Write the updated content back to the file
with p.open("w") as file:
file.write(content)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Configure the project")
parser.add_argument(
"-c",
"--clean",
help="Clean extraction and build artifacts",
action="store_true",
)
parser.add_argument(
"-csrc",
"--cleansrc",
help="Clean the 'src' folder",
action="store_true",
)
parser.add_argument(
"-noloop",
"--no-short-loop-workaround",
help="Do not replace branch instructions with raw opcodes for functions that trigger the short loop bug",
action="store_true",
)
args = parser.parse_args()
if args.clean:
clean()
if args.cleansrc:
shutil.rmtree("src", ignore_errors=True)
split.main([YAML_FILE], modes="all", verbose=False)
linker_entries = split.linker_writer.entries
create_custom_specs()
build_stuff(linker_entries)
write_permuter_settings()
if not split.config["options"]["use_gp_rel_macro_nonmatching"]:
fix_gp_main()
if not args.no_short_loop_workaround:
replace_instructions_with_opcodes(split.config["options"]["asm_path"])
if not os.path.isfile("compile_commands.json"):
exec_shell(["ninja", "-t", "compdb"], open("compile_commands.json", "w"))