forked from ethteck/kh1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.py
executable file
·206 lines (162 loc) · 5.51 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
#! /usr/bin/env python3
import argparse
import os
import shutil
import sys
from pathlib import Path
from typing import Dict, List, Set, Union
import ninja_syntax
import splat
import splat.scripts.split as split
from splat.segtypes.linker_entry import LinkerEntry
ROOT = Path(__file__).parent.resolve()
TOOLS_DIR = ROOT / "tools"
YAML_FILE = "kh.jp.yaml"
BASENAME = "SLPS_251.05"
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 -isystem include/sdk/ee -isystem include/gcc"
GAME_CC_DIR = f"{TOOLS_DIR}/cc/ee-gcc2.96"
LIB_CC_DIR = f"{TOOLS_DIR}/cc/ee-gcc2.9-991111/bin"
COMMON_COMPILE_FLAGS = "-O2 -G0 $g"
GAME_GCC_CMD = f"{GAME_CC_DIR}/bin/ee-gcc -c -B {GAME_CC_DIR}/bin/ee- {COMMON_INCLUDES} {COMMON_COMPILE_FLAGS} $in"
GAME_COMPILE_CMD = f"{GAME_GCC_CMD} -S -o - | {TOOLS_DIR}/masps2.py | {GAME_CC_DIR}/ee/bin/as {COMMON_COMPILE_FLAGS} -EL -mabi=eabi"
LIB_COMPILE_CMD = f"{LIB_CC_DIR}/ee-gcc -c -isystem include/gcc-991111 {COMMON_INCLUDES} {COMMON_COMPILE_FLAGS}"
NO_G_FILES = [
"xblade.c",
"gumi.c",
]
def clean():
if os.path.exists(".splache"):
os.remove(".splache")
shutil.rmtree("asm", ignore_errors=True)
shutil.rmtree("assets", ignore_errors=True)
shutil.rmtree("build", ignore_errors=True)
def write_permuter_settings():
with open("permuter_settings.toml", "w") as f:
f.write(
f"""compiler_command = "{GAME_COMPILE_CMD} -D__GNUC__"
assembler_command = "mips-linux-gnu-as -march=r5900 -mabi=eabi -Iinclude"
compiler_type = "gcc"
[preserve_macros]
[decompme.compilers]
"tools/build/cc/gcc/gcc" = "ee-gcc2.96"
"""
)
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 undefined_syms.txt -T undefined_syms_auto.txt -T undefined_funcs_auto.txt -Map $mapfile -T $in -o $out"
ninja.rule(
"as",
description="as $in",
command=f"cpp {COMMON_INCLUDES} $in -o - | {cross}as -no-pad-sections -EL -march=5900 -mabi=eabi -Iinclude -o $out",
)
ninja.rule(
"cc",
description="cc $in",
command=f"{GAME_COMPILE_CMD} -o $out && {cross}strip $out -N dummy-symbol-name",
)
ninja.rule(
"libcc",
description="cc $in",
command=f"{LIB_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.c.CommonSegC):
if any(
str(src_path).startswith("src/lib/") for src_path in entry.src_paths
):
build(entry.object_path, entry.src_paths, "libcc")
else:
if entry.src_paths[0].name in NO_G_FILES:
g = ""
else:
g = "-g"
build(entry.object_path, entry.src_paths, "cc", variables={"g": g})
elif isinstance(seg, splat.segtypes.common.databin.CommonSegDatabin):
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],
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Configure the project")
parser.add_argument(
"-c",
"--clean",
help="Clean extraction and build artifacts",
action="store_true",
)
args = parser.parse_args()
if args.clean:
clean()
split.main([YAML_FILE], modes="all", verbose=False)
linker_entries = split.linker_writer.entries
build_stuff(linker_entries)
write_permuter_settings()