-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure.py
More file actions
184 lines (143 loc) · 5.31 KB
/
configure.py
File metadata and controls
184 lines (143 loc) · 5.31 KB
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
#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
import sys
from pathlib import Path
# Project paths
ROOT = Path(__file__).parent
CONFIG_DIR = ROOT / "config"
SPLAT_YAML = CONFIG_DIR / "SLUS_204.69.yaml"
ISO_DIR = ROOT / "elf"
BUILD_DIR = ROOT / "build"
ELF_PATH = BUILD_DIR / "SLUS_204.69.elf"
# Original ELF
ORIGINAL_ELF = ISO_DIR / "SLUS_204.69"
ORIGINAL_SHA1 = "fd206d5715a322830f7fa9285fb4a09276ac2a63"
# Tools
PS2DEV = "/usr/local/ps2dev"
CROSS = f"{PS2DEV}/ee/bin/mips64r5900el-ps2-elf-"
AS = f"{CROSS}as"
LD = "/usr/local/ps2dev/ee/bin/mips64r5900el-ps2-elf-ld"
OBJCOPY = f"{CROSS}objcopy"
CC = f"{PS2DEV}/ee-gcc/bin/ee-gcc"
# Flags
ASFLAGS = "-march=r5900 -mabi=eabi -Iinclude --no-warn"
CC_ASFLAGS = "-march=r5900 -mabi=eabi -Iinclude --no-warn"
CFLAGS = "-O2 -G0 -fno-schedule-insns"
LDFLAGS = "--allow-multiple-definition -m elf32lr5900 --noinhibit-exec"
def clean():
import shutil
if BUILD_DIR.exists():
shutil.rmtree(BUILD_DIR)
print(f"Cleaned {BUILD_DIR}")
def split():
rom_path = CONFIG_DIR / "SLUS_204.69.rom"
if not rom_path.exists():
print("Generating ROM from ELF...")
subprocess.run(
[OBJCOPY, "-O", "binary", "--gap-fill=0x00",
str(ORIGINAL_ELF), str(rom_path)],
check=True,
)
print("Running Splat...")
subprocess.run(
[sys.executable, "-m", "splat", "split", str(SPLAT_YAML)],
check=True,
)
def find_asm_files():
asm_files = []
for path in sorted(ROOT.rglob("asm/**/*.s")):
asm_files.append(path.relative_to(ROOT))
return asm_files
def find_src_files():
src_files = []
src_dir = ROOT / "src"
if src_dir.exists():
for path in sorted(src_dir.rglob("*.c")):
src_files.append(path.relative_to(ROOT))
return src_files
def find_asset_files():
asset_files = []
assets_dir = ROOT / "assets"
if assets_dir.exists():
for path in sorted(assets_dir.rglob("*.bin")):
asset_files.append(path.relative_to(ROOT))
return asset_files
def generate_ninja(asm_files, src_files, asset_files):
BUILD_DIR.mkdir(parents=True, exist_ok=True)
ninja_path = ROOT / "build.ninja"
ld_script = CONFIG_DIR / "SLUS_204.69.ld"
with open(ninja_path, "w") as f:
f.write("# Generated by configure.py - do not edit manually\n")
f.write(f"builddir = {BUILD_DIR}\n\n")
f.write(f"rule as\n")
f.write(f" command = {AS} {ASFLAGS} -o $out $in\n")
f.write(f" description = AS $in\n\n")
f.write(f"rule cc\n")
f.write(f" command = {CC} {CFLAGS} -S -o $out.s $in && sed -i 's/\\tmove\\t\\(\\$$[0-9]*\\),\\(\\$$[0-9]*\\)/\\tdaddu\\t\\1,\\2,$$0/' $out.s && {AS} {CC_ASFLAGS} -o $out $out.s\n")
f.write(f" description = CC $in\n\n")
f.write(f"rule ld\n")
f.write(f" command = {LD} {LDFLAGS} -o $out -T $ldscript $in\n")
f.write(f" description = LD $out\n\n")
f.write(f"rule incbin\n")
f.write(f" command = echo \'.section .data\\n.balign 16\\n.incbin \"$in\"\' | {AS} {ASFLAGS} -o $out -\n")
f.write(f" description = INCBIN $in\n\n")
obj_files = []
for asm in asm_files:
obj = BUILD_DIR / asm.with_suffix(".o")
obj_files.append(str(obj))
f.write(f"build {obj}: as {asm}\n")
for src in src_files:
obj = BUILD_DIR / src.with_suffix(".o")
obj_files.append(str(obj))
f.write(f"build {obj}: cc {src}\n")
for asset in asset_files:
obj = BUILD_DIR / asset.with_suffix(".o")
obj_files.append(str(obj))
f.write(f"build {obj}: incbin {asset}\n")
f.write("\n")
f.write(f"build {ELF_PATH}: ld {' '.join(obj_files)}\n")
f.write(f" ldscript = {ld_script}\n\n")
f.write(f"default {ELF_PATH}\n")
print(f"Generated {ninja_path}")
print(f" Assembly files: {len(asm_files)}")
print(f" Source files: {len(src_files)}")
print(f" Asset files: {len(asset_files)}")
def generate_objdiff():
objdiff_config = {
"min_version": "2.0.0",
"custom_make": "ninja",
"build_target": str(ELF_PATH),
"target_dir": "expected",
"base_dir": "build",
}
objdiff_path = ROOT / "objdiff.json"
with open(objdiff_path, "w") as f:
json.dump(objdiff_config, f, indent=2)
print(f"Generated {objdiff_path}")
def main():
parser = argparse.ArgumentParser(description="Configure the Xenosaga Episode I decomp build")
parser.add_argument("--clean", action="store_true")
parser.add_argument("--split", action="store_true")
parser.add_argument("--no-split", action="store_true")
args = parser.parse_args()
if not ORIGINAL_ELF.exists():
print(f"Error: {ORIGINAL_ELF} not found.")
sys.exit(1)
if args.clean:
clean()
if args.split or (not args.no_split and not (ROOT / "asm").exists()):
split()
asm_files = find_asm_files()
src_files = find_src_files()
asset_files = find_asset_files()
if not asm_files:
print("Error: No assembly files found. Run with --split.")
sys.exit(1)
generate_ninja(asm_files, src_files, asset_files)
generate_objdiff()
print("\nBuild configured! Run 'ninja' to build.")
if __name__ == "__main__":
main()