-
Notifications
You must be signed in to change notification settings - Fork 19
/
configure.py
292 lines (257 loc) · 7.35 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
#!/usr/bin/env python3
###
# Generates build files for the project.
# This file also includes the project configuration,
# such as compiler flags and the object matching status.
#
# Usage:
# python3 configure.py
# ninja
#
# Append --help to see available options.
###
import argparse
import json
import sys
from pathlib import Path
from typing import Any, Dict, List, Union
from tools.project import *
from tools.defines_common import (
cflags_includes,
DEFAULT_VERSION,
VERSIONS
)
parser = argparse.ArgumentParser()
parser.add_argument(
"mode",
choices=["configure", "progress"],
default="configure",
help="script mode (default: configure)",
nargs="?",
)
parser.add_argument(
"-v",
"--version",
choices=VERSIONS,
type=str.upper,
default=VERSIONS[DEFAULT_VERSION],
help="version to build",
)
parser.add_argument(
"--build-dir",
metavar="DIR",
type=Path,
default=Path("build"),
help="base build directory (default: build)",
)
parser.add_argument(
"--binutils",
metavar="BINARY",
type=Path,
help="path to binutils (optional)",
)
parser.add_argument(
"--compilers",
metavar="DIR",
type=Path,
help="path to compilers (optional)",
)
parser.add_argument(
"--map",
action="store_true",
help="generate map file(s)",
)
parser.add_argument(
"--debug",
action="store_true",
help="build with debug info (non-matching)",
)
if not is_windows():
parser.add_argument(
"--wrapper",
metavar="BINARY",
type=Path,
help="path to wibo or wine (optional)",
)
parser.add_argument(
"--dtk",
metavar="BINARY | DIR",
type=Path,
help="path to decomp-toolkit binary or source (optional)",
)
parser.add_argument(
"--objdiff",
metavar="BINARY | DIR",
type=Path,
help="path to objdiff-cli binary or source (optional)",
)
parser.add_argument(
"--sjiswrap",
metavar="EXE",
type=Path,
help="path to sjiswrap.exe (optional)",
)
parser.add_argument(
"--verbose",
action="store_true",
help="print verbose output",
)
parser.add_argument(
"--non-matching",
dest="non_matching",
action="store_true",
help="builds equivalent (but non-matching) or modded objects",
)
parser.add_argument(
"--no-progress",
dest="progress",
action="store_false",
help="disable progress calculation",
)
args = parser.parse_args()
config = ProjectConfig()
config.version = str(args.version)
# Apply arguments
config.build_dir = args.build_dir
config.dtk_path = args.dtk
config.objdiff_path = args.objdiff
config.binutils_path = args.binutils
config.compilers_path = args.compilers
config.generate_map = args.map
config.non_matching = args.non_matching
config.sjiswrap_path = args.sjiswrap
config.progress = args.progress
if not is_windows():
config.wrapper = args.wrapper
# Don't build asm unless we're --non-matching
if not config.non_matching:
config.asm_dir = None
# Tool versions
config.binutils_tag = "2.42-1"
config.compilers_tag = "20240706"
config.dtk_tag = "v1.1.4"
config.objdiff_tag = "v2.3.3"
config.sjiswrap_tag = "v1.1.1"
config.wibo_tag = "0.6.11"
# Project
config_dir = Path("config") / config.version
config_json_path = config_dir / "config.json"
objects_path = config_dir / "objects.json"
config.config_path = config_dir / "config.yml"
config.check_sha_path = config_dir / "build.sha1"
config.reconfig_deps = [
config_json_path,
objects_path,
]
# Build flags
flags = json.load(open(config_json_path, "r", encoding="utf-8"))
progress_categories: dict[str, str] = flags["progress_categories"]
asflags: list[str] = flags["asflags"]
ldflags: list[str] = flags["ldflags"]
cflags: dict[str, dict] = flags["cflags"]
def get_cflags(name: str) -> list[str]:
return cflags[name]["flags"]
def add_cflags(name: str, flags: list[str]):
cflags[name]["flags"] = [*flags, *cflags[name]["flags"]]
def get_cflags_base(name: str) -> str:
return cflags[name].get("base", None)
def are_cflags_inherited(name: str) -> bool:
return "inherited" in cflags[name]
def set_cflags_inherited(name: str):
cflags[name]["inherited"] = True
def apply_base_cflags(key: str):
if are_cflags_inherited(key):
return
base = get_cflags_base(key)
if base is None:
add_cflags(key, cflags_includes)
else:
apply_base_cflags(base)
add_cflags(key, get_cflags(base))
set_cflags_inherited(key)
# Set up base flags
base_cflags = get_cflags("base")
base_cflags.append(f"-d VERSION_{config.version}")
# Set conditionally-added flags
# cflags
if args.debug:
base_cflags.append("-sym dwarf-2,full")
# Causes code generation memes, use only in desperation
# base_cflags.append("-pragma \"debuginline on\"")
else:
base_cflags.append("-DNDEBUG=1")
# ldflags
if args.debug:
ldflags.append("-gdwarf-2")
if config.generate_map:
ldflags.extend(["-mapunused", "-listclosure"])
# Apply cflag inheritance
for key in cflags.keys():
apply_base_cflags(key)
config.asflags = [
*asflags,
f"--defsym VERSION_{config.version}",
]
config.ldflags = ldflags
config.linker_version = "Wii/1.3"
config.shift_jis = False
config.progress_all = False
# Object files
Matching = True
Equivalent = config.non_matching
NonMatching = False
config.warn_missing_config = True
config.warn_missing_source = False
def get_object_completed(status: str) -> bool:
if status == "MISSING":
return NonMatching
elif status == "Matching":
return Matching
elif status == "NonMatching":
return NonMatching
elif status == "Equivalent":
return Equivalent
elif status == "LinkIssues":
return NonMatching
assert False, f"Invalid object status {status}"
libs: list[dict] = []
objects: dict[str, dict] = json.load(open(objects_path, "r", encoding="utf-8"))
for (lib, lib_config) in objects.items():
# config_cflags: str | list[str]
config_cflags: list[str] = lib_config.pop("cflags")
lib_cflags = get_cflags(config_cflags) if isinstance(config_cflags, str) else config_cflags
lib_objects: list[Object] = []
# config_objects: dict[str, str | dict]
config_objects: dict[str, Union[str, dict[str, Union[str, Any]]]] = lib_config.pop("objects")
if len(config_objects) < 1:
continue
for (path, obj_config) in config_objects.items():
if isinstance(obj_config, str):
completed = get_object_completed(obj_config)
lib_objects.append(Object(completed, path))
else:
completed = get_object_completed(obj_config["status"])
if "cflags" in obj_config:
object_cflags = obj_config["cflags"]
if isinstance(object_cflags, str):
obj_config["cflags"] = get_cflags(object_cflags)
lib_objects.append(Object(completed, path, **obj_config))
libs.append({
"lib": lib,
"cflags": lib_cflags,
"host": False,
"objects": lib_objects,
**lib_config
})
config.libs = libs
# Progress tracking categories
config.progress_categories = [ProgressCategory(name, desc) for (name, desc) in progress_categories.items()]
config.progress_each_module = args.verbose
if args.mode == "configure":
# Write build.ninja and objdiff.json
generate_build(config)
elif args.mode == "progress":
# Print progress and write progress.json
calculate_progress(config)
else:
sys.exit("Unknown mode: " + args.mode)