-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigure.py
executable file
·431 lines (349 loc) · 12.6 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#! /usr/bin/env python3
import argparse
import json
import os
import shutil
import subprocess
import sys
import re
from pathlib import Path
from typing import Any, Dict, List, Set, Union, Optional
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/xenosaga.yaml"
BASENAME = "SLUS_204.69"
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 -Isrc -isystem include/sdk/ee -isystem include/sdk -isystem include/gcc -isystem include/gcc/gcc-lib"
COMPILER_DIR = f"{TOOLS_DIR}/cc/ee-gcc2.96/bin"
COMPILER_FLAGS = "-O2 -Wa,-Iinclude"
COMPILER_FLAGS_CPP = "-O2 c++"
COMPILE_CMD = f"{COMPILER_DIR}/ee-gcc -c {COMMON_INCLUDES} {COMPILER_FLAGS}"
COMPILE_CMD_CPP = f"{COMPILER_DIR}/ee-gcc -c {COMMON_INCLUDES} {COMPILER_FLAGS_CPP}"
WIBO_VER = "0.6.11"
# CALCULATE PROGRESS TODO:
# python3 -m mapfile_parser progress build/SCPS_150.17.map asm asm/nonmatchings/
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("compile_commands.json"):
os.remove("compile_commands.json")
shutil.rmtree("asm", ignore_errors=True)
shutil.rmtree("assets", ignore_errors=True)
shutil.rmtree("build", ignore_errors=True)
gp_access_pattern = re.compile(
r"%(gp_rel)\(([^)]+)\)\(\$28\)"
) # Pattern for removing %gp_rel accesses
gp_add_pattern = re.compile(
r"addiu\s+(\$\d+), \$28, %gp_rel\(([^)]+)\)"
) # Pattern for replacing %gp_rel additions
def remove_gprel():
for root, dirs, files in os.walk("asm/nonmatchings/"):
for filename in files:
filepath = os.path.join(root, filename)
with open(filepath, "r") as file:
content = file.read()
# Search for any %gp_rel access
# INSTR REG, %gp_rel(SYMBOL)($28) -> INSTR REG, SYMBOL
if re.search(gp_access_pattern, content):
# Reference found, remove
content = re.sub(gp_access_pattern, r"\2", content)
# Write the updated content back to the file
with open(filepath, "w") as file:
file.write(content)
# Search for any %gp_rel additions
# addiu REG, $28, %gp_rel(SYMBOL) -> la REG, SYMBOL
if re.search(gp_add_pattern, content):
# Reference found, replace
content = re.sub(gp_add_pattern, r"la \1, \2", content)
# Write the updated content back to the file
with open(filepath, "w") as file:
file.write(content)
def write_permuter_settings():
with open("permuter_settings.toml", "w") as f:
f.write(
f"""compiler_command = "tools/cc/ee-gcc2.96/bin/ee-gcc -c -Iinclude -Iinclude/sdk/ee -Iinclude/gcc -Iinclude/gcc/gcc-lib -O2 -G8 -gstabs -D__GNUC__"
assembler_command = "mips-linux-gnu-as -march=r5900 -mabi=eabi -Iinclude"
compiler_type = "gcc"
[preserve_macros]
[decompme.compilers]
"tools/cc/ee-gcc2.96/bin/ee-gcc" = "ee-gcc2.9-991111-01"
"""
)
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 = f"-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 -o - | iconv -f=UTF-8 -t=EUC-JP $in | {cross}as -no-pad-sections -EL -march=5900 -mabi=eabi -Iinclude -o $out && python3 tools/buildtools/elf_patcher.py $out gas",
)
ninja.rule(
"cpp",
description="cpp $in",
command=f"{COMPILE_CMD_CPP} $in -o $out && {cross}strip $out -N dummy-symbol-name",
)
ninja.rule(
"cc",
description="cc $in",
command=f"{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",
)
decompctx = Path("tools") / "decompctx.py"
ninja.rule(
name="decompctx",
command=f"$python {decompctx} $in -o $out -d $out.d",
description="CTX $in",
depfile="$out.d",
deps="gcc",
)
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")
build(
entry.object_path.parent / f"{seg.name}.ctx",
entry.src_paths,
"decompctx",
)
elif isinstance(seg, splat.segtypes.common.c.CommonSegC):
build(entry.object_path, entry.src_paths, "cc")
build(
entry.object_path.parent / f"{seg.name}.ctx",
entry.src_paths,
"decompctx",
)
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],
)
# Generate objdiff.json
# Hacked together from dtk template. Not generically transferrable to other projects
def generate_objdiff_config(
linker_entries: List[LinkerEntry],
) -> None:
objdiff_config: Dict[str, Any] = {
"min_version": "1.0.0",
"custom_make": "ninja",
"build_target": False,
"watch_patterns": [
"*.c",
"*.cp",
"*.cpp",
"*.h",
"*.hpp",
"*.inc",
"*.py",
"*.yml",
"*.txt",
"*.json",
],
"units": [],
}
build_path = "build"
def add_unit(entry: LinkerEntry) -> None:
if entry.segment.type not in ["c", "cpp"]:
return
obj_name = entry.segment.name
unit_config: Dict[str, Any] = {
"name": entry.segment.name,
"target_path": Path("expected")
/ entry.object_path.relative_to(Path(build_path)),
}
unit_src_path = entry.segment.out_path()
if not unit_src_path.exists():
objdiff_config["units"].append(unit_config)
return
src_obj_path = entry.object_path
src_ctx_path = entry.object_path.parent / f"{entry.segment.name}.ctx"
unit_config["base_path"] = src_obj_path
# TODO: Detect fully matching TUs
unit_config["complete"] = not os.path.isdir(
f"asm/nonmatchings/{entry.segment.name}"
)
compiler_version = "ee-gcc2.96"
unit_config["scratch"] = {
"platform": "ps2",
"compiler": compiler_version,
"c_flags": COMPILER_FLAGS,
"ctx_path": src_ctx_path,
"build_ctx": True,
}
objdiff_config["units"].append(unit_config)
# Add units
for unit in linker_entries:
add_unit(unit)
# Write objdiff.json
with open("objdiff.json", "w", encoding="utf-8") as w:
def unix_path(input: Any) -> str:
return str(input).replace(os.sep, "/") if input else ""
json.dump(objdiff_config, w, indent=4, default=unix_path)
# Workaround hack to avoid the "short loop bug" inserting undesirable noops in some functions.
# replaces all asm instructions in each file in the `short_loop_funcs` array with the raw opcode bytes
opcode_pattern = re.compile(
r"\/\* [0-9A-Z]+ [0-9A-Z]+ ([0-9A-Z]{2})([0-9A-Z]{2})([0-9A-Z]{2})([0-9A-Z]{2}) .*"
) # Pattern for removing %gp_rel accesses
nop_bugged_funcs = set(
[
"Judge_MakeNewSavedata.s",
"xglRenderSyncMove.s",
"AlphaGroupLastEntry.s",
"nmlModelCalcEntryPartsClip.s",
"nmlModelEntry.s",
"nmlModelRenderTexture.s",
"nmlModelRenderCircle.s",
"nmlModelRenderDrop.s",
"nmlModelFlushSubNonAlpha.s",
"nmlModelFlushSubAlpha.s",
"nmlModelFlush.s",
"nmlPacketAddParts.s",
"nmlFilterSetVolumeCubeRender.s",
"xglJpegEncode.s",
"FFIDCT.s",
"ConvertYUV2RGB.s",
"Game_Data_Pop.s",
"checkItemBox.s",
"GameModeCfMain.s",
"command_loadsmd.s",
"xtxdec_sub.s",
"tyaSiPicOutput.s",
"STRING_toUInt.s",
"TWIN_create2.s",
"updateLight.s",
"ShopTest.s",
"OpenSubType01.s",
]
)
def replace_instructions_with_opcodes() -> None:
for root, dirs, files in os.walk("asm/nonmatchings/"):
for filename in files:
if filename not in nop_bugged_funcs:
continue
filepath = os.path.join(root, filename)
with open(filepath, "r") as file:
content = file.read()
if re.search(opcode_pattern, content):
# Reference found, replace
# Embed the opcode, we have to swap byte order for correct endianness
content = re.sub(opcode_pattern, r".long 0x\4\3\2\1", content)
# Write the updated content back to the file
with open(filepath, "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(
"-nogp",
"--no-gprel-removing",
help="Do not remove gp_rel references on the disassembly",
action="store_true",
)
parser.add_argument(
"-noop",
"--no-opcode-hack",
help="Do not replace asm 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
build_stuff(linker_entries)
generate_objdiff_config(linker_entries)
write_permuter_settings()
# We're done with everything, now get rid of the %gp_rel references
if not args.no_gprel_removing:
remove_gprel()
if not args.no_opcode_hack:
replace_instructions_with_opcodes()
if not os.path.isfile("compile_commands.json"):
exec_shell(["ninja", "-t", "compdb"], open("compile_commands.json", "w"))