-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
executable file
·378 lines (301 loc) · 10.2 KB
/
build.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
#!/usr/bin/env python3
import argparse
from pathlib import Path
import subprocess as subp
import shutil
import platform
import os
# COMPILATION STUFF
# =====================
def compile_po(src: Path, dst: Path, domain: str):
for srcfile in src.glob("*.po"):
dstdir = dst / srcfile.stem / "LC_MESSAGES"
dstfile = dstdir / f"{domain}.mo"
dstdir.mkdir(parents=True, exist_ok=True)
subp.run(["msgfmt", srcfile, "-o", dstfile]).check_returncode()
# INSTALL FUNCTIONS
# ======================
INSTALL_DIRS = {
# For Windows, AppImage, etc.
"portable": {
"bin": ".",
"core": ".",
"plugin": "./plugin",
"data": "./data",
"i18n": "./i18n",
},
# For Linux distro packages
"unix": {
"bin": "./bin",
"core": "./lib/m64prs",
"plugin": "./lib/m64prs/plugin",
"data": "./share/m64prs",
"i18n": "./share/locale"
}
}
def copy_if_newer(src: Path, dst: Path) -> None:
if not src.exists():
raise FileNotFoundError(src)
if dst.is_dir():
dst = dst.joinpath(src.name)
if dst.exists() and dst.stat().st_mtime_ns > src.stat().st_mtime_ns:
return
print(f"copy: {src} -> {dst}")
shutil.copy(src, dst)
pass
def exe_name(name: str) -> str:
match platform.system():
case "Windows":
return f"{name}.exe"
case _:
return name
def dll_name(name: str) -> str:
match platform.system():
case "Windows":
return f"{name}.dll"
case "Darwin":
return f"lib{name}.dylib"
case "Linux":
return f"lib{name}.so"
case _:
return name
def plugin_name(name: str) -> str:
match platform.system():
case "Windows":
return f"{name}.dll"
case "Darwin":
return f"{name}.dylib"
case "Linux":
return f"{name}.so"
case _:
return name
def install_debug_info(srcdir: Path, dstdir: Path, srcfile: str, dstfile: str | None = None):
if platform.system() != "Windows":
return
if not (srcdir/f"{srcfile}.pdb").exists():
return
if dstfile is None:
dstfile = srcfile
copy_if_newer(srcdir/f"{srcfile}.pdb", dstdir/f"{dstfile}.pdb")
def install_exe(srcdir: Path, dstdir: Path, srcfile: str, dstfile: str | None = None):
if dstfile is None:
dstfile = srcfile
copy_if_newer(srcdir/exe_name(srcfile), dstdir/exe_name(dstfile))
def install_dll(srcdir: Path, dstdir: Path, srcfile: str, dstfile: str | None = None):
if dstfile is None:
dstfile = srcfile
copy_if_newer(srcdir/dll_name(srcfile), dstdir/dll_name(dstfile))
def install_plugin(srcdir: Path, dstdir: Path, srcfile: str, dstfile: str | None = None):
if dstfile is None:
dstfile = srcfile
srcfile = plugin_name(srcfile)
dstfile = plugin_name(dstfile)
copy_if_newer(srcdir/srcfile, dstdir/dstfile)
def install_data(srcdir: Path, dstdir: Path):
for item in srcdir.iterdir():
copy_if_newer(item, dstdir.joinpath(item.name))
# CLEAN FUNCTIONS
# ======================
def yeet_dir(dir: Path):
if dir.exists() and dir.is_dir():
shutil.rmtree(dir)
# COMMANDS
# ======================
def build(args: argparse.Namespace, extra: list[str]):
project_dir = Path(__file__).parent
# setup directories
install_root_dir = None
target_dir = None
if args.release:
install_root_dir = project_dir.joinpath("install/release")
target_dir = project_dir.joinpath("target/release")
else:
install_root_dir = project_dir.joinpath("install/debug")
target_dir = project_dir.joinpath("target/debug")
install_root_dir.mkdir(parents=True, exist_ok=True)
native_root_dir = project_dir.joinpath("m64prs/native")
native_target_dir = native_root_dir.joinpath("target")
m64prs_i18n_dir = project_dir.joinpath("m64prs/gtk/i18n")
scheme = INSTALL_DIRS[args.install_scheme]
default_scheme_name = next(iter(INSTALL_DIRS.keys()))
[bin_dir, core_dir, data_dir, plugin_dir, i18n_dir] = [
(install_root_dir / scheme[key]) for key in
["bin", "core", "data", "plugin", "i18n"]
]
for dir in [bin_dir, core_dir, data_dir, plugin_dir, i18n_dir]:
dir.mkdir(parents=True, exist_ok=True)
# compile cargo
cargo_args = [
"cargo", "build",
"-p", "m64prs-gtk",
"-p", "tasinput-bridge",
"-p", "tasinput-ui",
]
if args.release:
cargo_args.append("--release")
if args.install_scheme != default_scheme_name:
cargo_args.extend(["-F", f"install-{args.install_scheme}"])
subp.run(
cargo_args,
cwd=project_dir,
).check_returncode()
compile_po(m64prs_i18n_dir, i18n_dir, "m64prs")
# copy binaries
install_exe(target_dir, bin_dir, "m64prs-gtk")
install_debug_info(target_dir, bin_dir, "m64prs_gtk")
install_dll(native_target_dir, core_dir, "mupen64plus")
install_debug_info(native_target_dir, core_dir, "mupen64plus")
# copy native plugins
install_plugin(native_target_dir, plugin_dir, "mupen64plus-video-rice")
install_plugin(native_target_dir, plugin_dir, "mupen64plus-audio-sdl")
install_plugin(native_target_dir, plugin_dir, "mupen64plus-input-sdl")
install_plugin(native_target_dir, plugin_dir, "mupen64plus-rsp-hle")
install_debug_info(native_target_dir, plugin_dir, "mupen64plus-video-rice")
install_debug_info(native_target_dir, plugin_dir, "mupen64plus-audio-sdl")
install_debug_info(native_target_dir, plugin_dir, "mupen64plus-input-sdl")
install_debug_info(native_target_dir, plugin_dir, "mupen64plus-rsp-hle")
# copy tasinput-rs
copy_if_newer(target_dir/dll_name("tasinput_bridge"),
plugin_dir/plugin_name("mupen64plus-input-tasinput"))
install_debug_info(target_dir, plugin_dir,
"tasinput_bridge", "mupen64plus_input_tasinput")
install_exe(target_dir, plugin_dir, "tasinput-ui")
install_debug_info(target_dir, plugin_dir, "tasinput-ui")
# copy Windows dependencies
if platform.system() == "Windows":
arch_name = None
if platform.machine() in ["x86"]:
arch_name = "x86"
elif platform.machine() in ["AMD64"]:
arch_name = "x64"
else:
assert False
deps_dir = project_dir.joinpath(
"m64prs/native/mupen64plus-win32-deps")
NATIVE_DEP_NAMES = [
("freetype-2.13.0", "freetype"),
("libpng-1.6.39", "libpng16"),
("SDL2_net-2.2.0", "SDL2_net"),
("SDL2-2.26.3", "SDL2"),
("zlib-1.2.13", "zlib"),
]
for dep_name, lib_name in NATIVE_DEP_NAMES:
install_dll(deps_dir / dep_name / "lib" /
arch_name, bin_dir, lib_name)
# copy data files
NATIVE_DATA_PATHS = [
"mupen64plus-core/data",
"mupen64plus-input-sdl/data",
"mupen64plus-video-rice/data",
]
for data_path in NATIVE_DATA_PATHS:
install_data(native_root_dir / data_path, data_dir)
def run(args: argparse.Namespace, extra: list[str]):
build(args, extra)
project_dir = Path(__file__).parent
install_root_dir = None
if args.release:
install_root_dir = project_dir.joinpath("install/release")
else:
install_root_dir = project_dir.joinpath("install/debug")
bin_dir = install_root_dir / INSTALL_DIRS[args.install_scheme]['bin']
run_args = [bin_dir.joinpath(exe_name("m64prs-gtk"))]
run_args.extend(extra)
subp.run(
run_args
).check_returncode()
def clean(args: argparse.Namespace, extra: list[str]):
root_dir = Path(__file__).parent
yeet_dir(root_dir.joinpath("target"))
yeet_dir(root_dir.joinpath("install"))
native_dir = root_dir.joinpath("m64prs/native")
build_script_name = \
"m64prs-build-win.py" \
if platform.system() == "Windows" \
else "m64prs-build-unix.py"
if platform.system() == "Windows":
yeet_dir(native_dir.joinpath("target"))
else:
subp.run([
"python3", native_dir.joinpath(build_script_name), "clean"
]).check_returncode()
pass
# CLI
# ======================
def build_options(subcli: argparse.ArgumentParser):
scheme_list = list(INSTALL_DIRS.keys())
subcli.add_argument(
"-r", "--release",
help="Do a release build (instead of a debug build).",
action="store_true",
default=False
)
subcli.add_argument(
"-s", "--install-scheme",
help="The install scheme to use.",
choices=scheme_list,
default=scheme_list[0],
metavar="<scheme>"
)
def create_cli():
cli = argparse.ArgumentParser(
description="Master build script for m64prs.",
add_help=False,
)
cli.add_argument(
"--help",
help="Show this help message and exit.",
action="help"
)
subclis = cli.add_subparsers(
title="subcommands",
metavar="<command>",
required=True,
help="The build command to use."
)
# build command
build_cli = subclis.add_parser(
"build",
add_help=False,
help="Build and install all dependencies"
)
build_cli.add_argument(
"--help",
help="Show this help message and exit.",
action="help"
)
build_options(build_cli)
build_cli.set_defaults(func=build)
run_cli = subclis.add_parser(
"run",
add_help=False,
help="Run the program"
)
run_cli.add_argument(
"--help",
help="Show this help message and exit.",
action="help"
)
build_options(run_cli)
run_cli.set_defaults(func=run)
clean_cli = subclis.add_parser(
"clean",
add_help=False,
help="Cleans all build artifacts."
)
clean_cli.add_argument(
"--help",
help="Show this help message and exit.",
action="help"
)
clean_cli.set_defaults(func=clean)
return cli
args, extra = create_cli().parse_known_args()
extra = [arg for arg in extra if arg != "--"]
try:
args.func(args, extra)
except subp.CalledProcessError as e:
print("Subprocess failed!")
print(e)
except KeyboardInterrupt:
print("Ctrl+C pressed! Stopping...")