-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfigure.py
More file actions
executable file
·322 lines (275 loc) · 9.12 KB
/
configure.py
File metadata and controls
executable file
·322 lines (275 loc) · 9.12 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
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
#!/usr/bin/env python3
from argparse import ArgumentParser
from io import StringIO
import os
from sys import executable as PYTHON, platform
from typing import List
from ninja_syntax import Writer
parser = ArgumentParser()
parser.add_argument("tests", type=str, nargs="*", help="Tests to run" \
"(mod_ctx, decomp_ctx, mod_ctx_shuffle, test_mod_individual)")
parser.add_argument("--regions", type=str, nargs="+", help="Regions to test")
parser.add_argument("--seed", type=int, default=1, help="Shuffling seed")
parser.add_argument("--shuffle", type=int, default=50, help="Number of randomised orders to test")
parser.add_argument("--individual", action="store_true", help="Test every header on its own")
parser.add_argument("--codewarrior", type=str, help="mwcceppc.exe path")
parser.add_argument("-w", "--wine", type=str, help="Wine override (ignored on Windows)")
args = parser.parse_args()
outbuf = StringIO()
n = Writer(outbuf)
#############
# Variables #
#############
n.variable("builddir", "build")
n.variable("incdir", "include")
n.variable("seed", args.seed)
n.variable("validate_lst", f"{PYTHON} tools/validate_lst.py")
n.variable("incgen", f"{PYTHON} tools/incgen.py")
n.variable("incgen_single", f"{PYTHON} tools/incgen_single.py")
n.variable("devkitppc", os.environ.get("DEVKITPPC"))
n.variable("cpp", os.path.join("$devkitppc", "bin", "powerpc-eabi-cpp"))
MOD_INCLUDES = ["$incdir", "$mod_incdir"]
n.variable("mod_cc", os.path.join("$devkitppc", "bin", "powerpc-eabi-g++"))
n.variable("mod_incdir", "mod")
n.variable("mod_source", os.path.join("$builddir", "mod.c"))
n.variable(
"mod_machdep",
' '.join([
"-mno-sdata", # Disable SDA sections since not main binary
"-DGEKKO", # CPU preprocessor define
"-mcpu=750", # Set CPU to 750cl
"-meabi", # Set ppc abi to eabi
"-mhard-float", # Enable hardware floats
"-nostdlib", # Don't link std lib
"-mregnames", # Enable r prefix for registers in asm
"-ffreestanding", # Tell compiler environment isn't hosted
])
)
n.variable(
"mod_cflags",
' '.join([
"$mod_machdep",
"$mod_includes",
"-ffunction-sections", # Allow function deadstripping
"-fdata-sections", # Allow data deadstripping
"-g", # Emit debug info
"-O3", # High optimisation for speed
"-Wall", # Enable all warnings
"-Wextra", # Enable even more warnings
"-Wpedantic", # Enable even more warnings than that
"-Wshadow", # Enable variable shadowing warning
"-Werror", # Error on warnings
"-fmax-errors=1", # Stop after 1 error
])
)
n.variable(
"mod_cxxflags",
' '.join([
"$mod_cflags",
"-fno-exceptions", # Disable C++ exceptions
"-fno-rtti", # Disable runtime type info
"-std=gnu++17", # Use C++17 with GNU extensions
])
)
DECOMP_INCLUDES = [
"$incdir",
"$decomp_incdir",
]
if args.codewarrior:
cw = args.codewarrior.replace("/", os.sep)
if args.wine:
wine = args.wine
else:
wine = "wine"
if platform != "win32":
cw = f"{wine} {cw}"
else:
cw = None
n.variable("decomp_cc", cw)
n.variable("decomp_incdir", "decomp")
n.variable("decomp_source", os.path.join("$builddir", "decomp.c"))
n.variable(
"decomp_cxxflags",
' '.join([
"-lang c++",
"-W all",
"-W err",
"-fp fmadd",
"-Cpp_exceptions off",
"-O4",
"-use_lmw_stmw on",
"-str pool",
"-rostr",
"-sym dwarf-2",
"-ipa file",
])
)
REGIONS = [
"eu0",
"eu1",
"jp0",
"jp1",
"us0",
"us1",
"us2",
"kr0",
]
########
# Rules#
########
ALLOW_CHAIN = "cmd /c " if os.name == "nt" else ""
n.rule(
"incgen",
ALLOW_CHAIN + "$incgen $dirs -s $seed -i $iteration > $out"
)
n.rule(
"incgen_single",
ALLOW_CHAIN + "$incgen_single $header > $out"
)
n.rule(
"mod_cc",
command = "$mod_cc -MMD -MT $out -MF $out.d $mod_cxxflags $flags -c $in -o $out",
depfile = "$out.d",
deps = "gcc",
description = "Mod CC $out",
)
n.rule(
"decomp_cc",
command = ALLOW_CHAIN + "$cpp -M $in -MF $out.d $cppflags && " \
"$decomp_cc $decomp_cxxflags $flags -c $in -o $out",
description = "Decomp CC $in",
deps = "gcc",
depfile = "$out.d"
)
n.rule(
"validate_lst",
ALLOW_CHAIN + "$validate_lst $in && touch $out"
)
##########
# Builds #
##########
def incgen(source: str, dirs: List[str], iteration: int = 0):
n.build(
source,
rule="incgen",
inputs=[],
variables={
"dirs" : ' '.join(dirs),
"iteration" : str(iteration),
}
)
def incgen_single(source, header: str):
n.build(
source,
rule="incgen_single",
inputs=[],
variables={
"header" : header,
}
)
def compile(dest: str, source: str, includes: List[str], defines: List[str], decomp: bool = False):
define_flags = ' '.join(f"-D{d}" for d in defines)
gcc_include_flags = ' '.join(f"-I {d}" for d in includes)
if decomp:
mwcc_include_flags = "-I- " + ' '.join(f"-i {d}" for d in includes)
n.build(
dest,
"decomp_cc",
[source],
variables={
"cppflags" : f"{gcc_include_flags} {define_flags}",
"flags" : f"{mwcc_include_flags} {define_flags}",
}
)
else:
n.build(
dest,
"mod_cc",
[source],
variables={
"flags" : f"{gcc_include_flags} {define_flags}",
}
)
def compile_regions(dest: str, source: str, regions: List[str], includes: List[str],
defines: List[str], decomp: bool = False):
for region in regions:
compile(dest.format(region=region), source, includes, defines + [f"SPM_{region.upper()}"],
decomp)
def find_headers(dirname: str, base=None) -> List[str]:
"""Returns a list of all headers in a folder recursively"""
if base is None:
base = dirname
ret = []
for name in os.listdir(dirname):
path = os.path.join(dirname, name)
if os.path.isdir(path):
ret.extend(find_headers(path, base))
elif name.endswith('.h'):
ret.append(path[len(base)+1:])
return ret
# Test the headers in the modding setup
def test_mod_ctx(regions: List[str]):
compile_regions(os.path.join("$builddir", "{region}", "mod.o"), "$mod_source", regions,
MOD_INCLUDES, [])
# Test the headers in the relaxed namespace modding setup
def test_rns_mod_ctx(regions: List[str]):
compile_regions(os.path.join("$builddir", "{region}", "rns_mod.o"), "$mod_source", regions,
MOD_INCLUDES, ["RELAX_NAMESPACING"])
# Test the headers in the decomp setup
def test_decomp_ctx(regions: List[str]):
assert args.codewarrior, "Error: decomp_ctx test requires --codewarrior"
compile_regions(os.path.join("$builddir", "{region}", "decomp.o"), "$decomp_source", regions,
DECOMP_INCLUDES, ["DECOMP"], True)
# Test shuffled include orders
def test_mod_ctx_shuffle(regions: List[str]):
assert args.shuffle, "mod_ctx_shuffle test requires --shuffle"
for i in range(1, 1 + args.shuffle):
source = os.path.join("$builddir", f"shuffle_{args.seed}", f"{i}.cpp")
incgen(source, MOD_INCLUDES, i)
compile_regions(os.path.join("$builddir", "{region}", f"shuffle_mod_{args.seed}_{i}.o"),
source, regions, MOD_INCLUDES, [])
# Test individual headers
def test_mod_individual(regions: List[str]):
for header in find_headers("include"):
name = header.removeprefix("include/").removesuffix(".h")
source = os.path.join("$builddir", "individual", f"{name}.c")
incgen_single(source, header)
compile_regions(os.path.join("$builddir", "{region}", "individual", f"{name}.o"), source,
regions, MOD_INCLUDES, ["SPM_EU0"])
def lst_path(region: str) -> str:
if region == "eu1":
region = "eu0"
return os.path.join("linker", f"spm.{region}.lst")
def test_lsts(regions: List[str]):
for region in regions:
path = lst_path(region)
dest = os.path.join("$builddir", "lsts", f"{region}.ok")
n.build(
dest,
"validate_lst",
[path],
)
test_fns = {
"mod_ctx" : test_mod_ctx,
"rns_mod_ctx" : test_rns_mod_ctx,
"decomp_ctx" : test_decomp_ctx,
"mod_ctx_shuffle" : test_mod_ctx_shuffle,
"test_mod_individual" : test_mod_individual,
"validate_lst" : test_lsts,
}
incgen("$mod_source", MOD_INCLUDES)
default_tests = ["mod_ctx", "rns_mod_ctx", "validate_lst"]
if args.codewarrior:
incgen("$decomp_source", DECOMP_INCLUDES)
default_tests.append("decomp_ctx")
# Add requested tests
tests = set(args.tests if args.tests else default_tests)
regions = args.regions if args.regions else REGIONS
for test in tests:
if test not in test_fns:
assert False, f"Error: unknown test {test}"
else:
test_fns[test](regions)
with open("build.ninja", 'w') as f:
f.write(outbuf.getvalue())
n.close()