-
Notifications
You must be signed in to change notification settings - Fork 4
/
configure.py
392 lines (336 loc) · 9.72 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
#!/usr/bin/env python3
from io import StringIO
import os
import sys
from typing import Dict, List
from ninja_syntax import Writer
import yaml
#########
# Paths #
#########
# Project dirs
SRCDIR = "source"
INCDIR = "include"
LINKDIR = "linker"
BUILDDIR = "build"
OUTDIR = "out"
TOOLSDIR = "tools"
# Project files
ASSETS_YML = "assets.yml"
INCBIN = "$toolsdir/incbin.S"
# Libraries
SPM_HEADERS = "spm-headers"
#########
# Tools #
#########
# Devkitppc
DEVKITPPC = os.environ.get("DEVKITPPC")
assert DEVKITPPC is not None, "Error: DEVKITPPC environment variable not set"
CC = os.path.join("$devkitppc", "bin", "powerpc-eabi-gcc")
# Elf2rel
ELF2REL = os.environ.get("ELF2REL")
if ELF2REL is None:
TTYDTOOLS = os.environ.get("TTYDTOOLS")
if TTYDTOOLS is not None:
ELF2REL = os.path.join(TTYDTOOLS, "bin", "elf2rel")
assert ELF2REL is not None, "Error: ELF2REL environment variable not set"
##############
# Tool Flags #
##############
# C/C++/Asm include flags
INCLUDES = ' '.join(
"-I " + d
for d in [
"$incdir",
os.path.join("$spm_headers", "include"),
os.path.join("$spm_headers", "mod"),
]
)
# Machine-dependant flags
# Passed to C, C++ and asm compilation, and the linker
MACHDEP = ' '.join([
"-mno-sdata", # Disable SDA sections since not main binary
"-mgcn", # Use ogc linker
"-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
"-nostdinc", # Disable including std lib headers
"-ffreestanding", # Tell compiler environment isn't hosted
])
# Base C flags
# Passed to C and C++ Compilation
CFLAGS = ' '.join([
"$machdep",
"$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
"-Wshadow", # Enable variable shadowing warning
"-Werror", # Error on warnings
])
# C++ flags
# Passed only to C++ compilation
CXXFLAGS = ' '.join([
"$cflags",
"-fno-exceptions", # Disable C++ exceptions
"-fno-rtti", # Disable runtime type info
"-std=gnu++17", # Use C++17 with GNU extensions
])
# Asm flags
# Passed only to asm compilation
ASFLAGS = ' '.join([
"$machdep",
"-x assembler-with-cpp", # Enable C preprocessor
])
# Linker flags
LDFLAGS = ' '.join([
"$machdep",
"-lgcc",
"-r", # Partially link (elf2rel finishes)
"-e _prolog", # Set entry to _prolog
"-u _prolog", # Require _prolog to be defined
"-u _epilog", # Require _epilog to be defined
"-u _unresolved", # Require _unresolved to be defined
"-g", # Output debug info
','.join([
"-Wl", # Pass the following options to the linker
"--gc-sections", # Strip unused sections
"--force-group-allocation", # Merge section groups
]),
])
#######################
# Ninja file creation #
#######################
def emit_vars(n: Writer):
"""Emits the variables to a ninja file"""
# Project dirs
n.variable("srcdir", SRCDIR)
n.variable("incdir", INCDIR)
n.variable("linkdir", LINKDIR)
n.variable("builddir", BUILDDIR)
n.variable("outdir", OUTDIR)
n.variable("toolsdir", TOOLSDIR)
# Project files
n.variable("incbin", INCBIN)
# Libraries
n.variable("spm_headers", SPM_HEADERS)
# Devkitppc
n.variable("devkitppc", DEVKITPPC)
n.variable("cc", CC)
# Elf2rel
n.variable("elf2rel", ELF2REL)
# Tool flags
n.variable("includes", INCLUDES)
n.variable("machdep", MACHDEP)
n.variable("cflags", CFLAGS)
n.variable("cxxflags", CXXFLAGS)
n.variable("asflags", ASFLAGS)
n.variable("ldflags", LDFLAGS)
n.newline()
def emit_rules(n: Writer):
"""Emits the rules to a ninja file"""
# .c -> .o compilation
# Variables to pass in:
# verflags: defines for the region being compiled
n.rule(
"cc",
command = "$cc -MMD -MT $out -MF $out.d $cflags -c $in -o $out",
depfile = "$out.d",
deps = "gcc",
description = "CC $out"
)
# .cpp -> .o compilation
# Variables to pass in:
# verflags: defines for the region being compiled
n.rule(
"cxx",
command = "$cc -MMD -MT $out -MF $out.d $cxxflags $verflags -c $in -o $out",
depfile = "$out.d",
deps = "gcc",
description = "CXX $out"
)
# .s -> .o compilation
n.rule(
"as",
command = "$cc -MD -MT $out -MF $out.d $asflags -c $in -o $out",
depfile = "$out.d",
deps = "gcc",
description = "AS $out"
)
# binary -> .o compilation
# Variables to pass in:
# symbol: symbol name to give the asset (size will be <symbol>_size)
# align: alignment to give the asset
# section: section to place the asset in
n.rule(
"incbin",
command = (
"$cc -MD -MT $out -MF $out.d $asflags -c $incbin -o $out -I . "
"-DPATH=$in -DNAME=$symbol -DALIGN=$align -DSECTION=$section"
),
depfile = "$out.d",
deps = "gcc",
description = "INCBIN $out"
)
# .o & .ld -> .elf linking
# Variables to pass in:
# map: map file output path
n.rule(
"ld",
command = "$cc $in $ldflags -o $out -Wl,-Map,$map",
description = "LD $out"
)
# .elf -> .rel linking
# Variables to pass in:
# lst: lst symbol file input path
n.rule(
"elf2rel",
command = "$elf2rel $in -s $lst",
description = "elf2rel $out"
)
n.newline()
def find_files(path: str) -> List[str]:
"""Finds all files recursively in a directory"""
ret = []
for iname in os.listdir(path):
# Build full path
ipath = os.path.join(path, iname)
if os.path.isdir(ipath):
# Add all files within dir
ret.extend(find_files(ipath))
else:
# Add file
ret.append(ipath)
return ret
def emit_build(n: Writer, ver: str):
"""Emits the build statements for a version to a ninja file"""
# Emit source builds
ofiles = []
verflags = f"-DSPM_{ver.upper()}"
for path in find_files(SRCDIR):
# Get output name
ofile = os.path.join("$builddir", ver, path + ".o")
ofiles.append(ofile)
# Choose rule based on file extension
_, ext = os.path.splitext(path)
if ext == ".c":
# C source code
n.build(
ofile,
rule = "cc",
inputs = path,
variables = { "verflags" : verflags }
)
elif ext == ".cpp":
# C++ source code
n.build(
ofile,
rule = "cxx",
inputs = path,
variables = { "verflags" : verflags }
)
elif ext in (".s", ".S"):
# Asm source code
n.build(
ofile,
rule = "as",
inputs = path
)
else:
assert False, f"Unknown file type {ext} for {path}"
# Emit asset builds
with open(ASSETS_YML) as f:
assets: Dict = yaml.load(f, yaml.Loader)
if assets is None:
assets = {}
assert isinstance(assets, Dict), "Invalid format assets.yml"
for path, cfg in assets.items():
# Convert path if needed (AS only supports / separator)
path = path.replace('\\', '/')
# Get output name
ofile = os.path.join("$builddir", ver, path + ".o")
ofiles.append(ofile)
# Build
assert "symbol" in cfg, f"assets.yml missing field 'symbol' missing for asset {path}"
n.build(
ofile,
rule = "incbin",
inputs = path,
implicit = INCBIN,
variables = {
"symbol" : cfg["symbol"],
"align" : cfg.get("align", 0x4),
"section" : cfg.get("section", ".rodata")
}
)
# Find ld scripts
ldfiles = find_files(LINKDIR)
# Emit elf build
elf_name = os.path.join("$outdir", f"{ver}.elf")
map_name = os.path.join("$outdir", f"{ver}.map")
n.build(
elf_name,
rule = "ld",
inputs = ofiles + ldfiles,
implicit_outputs = map_name,
variables = { "map" : map_name }
)
# Emit rel build
rel_name = os.path.join("$outdir", f"{ver}.rel")
lst_ver = "eu0" if ver == "eu1" else ver
lst_name = os.path.join("$spm_headers", "linker", f"spm.{lst_ver}.lst")
n.build(
rel_name,
rule = "elf2rel",
inputs = elf_name,
implicit = lst_name,
variables = { "lst" : lst_name }
)
# Make a default target
n.default(rel_name)
# Add short phony
n.build(
ver,
rule = "phony",
inputs = rel_name
)
versions = [
"eu0",
"eu1",
"jp0",
"jp1",
"us0",
"us1",
"us2",
"kr0",
]
def main(versions: List[str]):
# Setup ninja
outbuf = StringIO()
n = Writer(outbuf)
n.variable("ninja_required_version", "1.3")
n.newline()
# Emit
emit_vars(n)
emit_rules(n)
for ver in versions:
emit_build(n, ver)
# Write to file
with open("build.ninja", 'w') as f:
f.write(outbuf.getvalue())
n.close()
if __name__=="__main__":
# Enable versions passed in comand line, default to all
target_versions = sys.argv[1:]
for v in target_versions:
assert v in versions, f"Unknown version {v}"
if len(target_versions) == 0:
target_versions = versions
# Make script
main(target_versions)