forked from VoylinsGamedevJourney/gde_gozen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·574 lines (464 loc) · 18.1 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·574 lines (464 loc) · 18.1 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/usr/bin/env python
"""
GDE GoZen Builder Script
This script handles the compilation of FFmpeg and the GDE GoZen plugin
for multiple platforms and architectures.
Windows and Linux can be build on Linux or Windows with WSL.
For MacOS you need to use MacOS itself else building fails.
For Web you need Emscripten installed.
You may also need to custom build the Godot web export debug/release template with:
`scons platform=web target=template_debug use_llvm=yes threads_enabled=yes dlink_enabled=yes\
extra_web_link_flags="-sINITIAL_MEMORY=1024MB -sSTACK_SIZE=5MB -sALLOW_MEMORY_GROWTH=1 -sUSE_PTHREADS=1" -j10`
"""
import os
import sys
import platform as os_platform
import subprocess
import glob
import shutil
THREADS: int = os.cpu_count() or 4
PATH_BUILD_WINDOWS: str = "build_on_windows.py"
ARCH_X86_64: str = "x86_64"
ARCH_ARM64: str = "arm64" # armv8
ARCH_ARMV7A: str = "armv7a"
ARCH_WASM32: str = "wasm32"
OS_LINUX: str = "linux"
OS_WINDOWS: str = "windows"
OS_MACOS: str = "macos"
OS_ANDROID: str = "android"
OS_WEB: str = "web"
TARGET_DEV: str = "debug"
TARGET_RELEASE: str = "release"
# WARNING: Change the path to you android sdk!
ANDROID_SDK_PATH: str = "/opt/android-sdk"
ANDROID_API_LEVEL: int = 24
ENABLED_MODULES = [
"--enable-swscale",
"--enable-demuxer=ogg",
"--enable-demuxer=matroska,webm",
]
ENABLE_AV1 = [
"--enable-libaom",
"--enable-decoder=av1",
"--enable-parser=av1",
]
DISABLED_MODULES = [
"--disable-muxers",
"--disable-encoders",
"--disable-postproc",
"--disable-avdevice",
"--disable-avfilter",
"--disable-sndio",
"--disable-doc",
"--disable-programs",
"--disable-ffprobe",
"--disable-htmlpages",
"--disable-manpages",
"--disable-podpages",
"--disable-txtpages",
"--disable-ffplay",
"--disable-ffmpeg",
"--disable-hwaccels",
]
def _print_options(title: str, options: list[str]) -> int:
# Helper function to print options and get the input.
i: int = 1
print(f"{title}:")
for option in options:
if i == 1:
print(f"{i}. {option}; (default)")
else:
print(f"{i}. {option};")
i += 1
user_input: str = input("> ")
if user_input.strip() == "":
return 1
try:
return int(user_input)
except ValueError:
print("Invalid input. Using default option (1).")
return 1
def get_ndk_host_tag() -> str:
match os_platform.system().lower():
case "linux": return "linux-x86_64"
case "darwin": return "darwin-x86_64"
case "windows": return "windows-x86_64"
case _:
print(f"Invalid host system: {os_platform.system()}")
sys.exit(2)
def compile_ffmpeg(platform: str, arch: str) -> None:
if os.path.exists("./ffmpeg/ffbuild/config.mak"):
print("Cleaning FFmpeg...")
subprocess.run(["make", "distclean"], cwd="./ffmpeg/")
subprocess.run(["rm", "-rf", "bin"], cwd="./ffmpeg/")
if platform == OS_LINUX:
compile_ffmpeg_linux(arch)
elif platform == OS_WINDOWS:
compile_ffmpeg_windows(arch)
elif platform == OS_MACOS:
compile_ffmpeg_macos(arch)
elif platform == OS_ANDROID:
compile_ffmpeg_android(arch)
elif platform == OS_WEB:
compile_ffmpeg_web()
def compile_ffmpeg_linux(arch: str) -> None:
print("Configuring FFmpeg for Linux ...")
path: str = f"./test_room/addons/gde_gozen/bin/linux_{arch}"
os.environ["PKG_CONFIG_PATH"] = "/usr/lib/pkgconfig"
os.makedirs(path, exist_ok=True)
cmd = [
"./configure",
"--prefix=./bin",
"--enable-shared",
f"--arch={arch}",
"--target-os=linux",
"--quiet",
"--enable-pic",
"--enable-pthreads",
"--extra-cflags=-fPIC",
"--extra-ldflags=-fPIC",
]
cmd += ENABLED_MODULES
cmd += ENABLE_AV1
cmd += DISABLED_MODULES
if arch == "arm64":
cmd += [
"--enable-cross-compile",
"--cross-prefix=aarch64-linux-gnu-",
"--cc=aarch64-linux-gnu-gcc",
]
if subprocess.run(cmd, cwd="./ffmpeg/").returncode != 0:
print("Error: FFmpeg failed!")
print("Compiling FFmpeg for Linux ...")
subprocess.run(["make", f"-j{THREADS}"], cwd="./ffmpeg/", check=True)
subprocess.run(["make", "install"], cwd="./ffmpeg/", check=True)
copy_linux_dependencies(path, arch)
def copy_linux_dependencies(path: str, arch: str):
print("Copying lib files ...")
for file in glob.glob("ffmpeg/bin/lib/*.so.*"):
if file.count(".") == 2:
shutil.copy2(file, path)
# AOM lib.
for file in glob.glob("/usr/lib/libaom.so*"):
if file.count(".") == 2:
shutil.copy2(file, path)
# AOM lib for Ubuntu.
if arch == ARCH_X86_64:
for file in glob.glob("/usr/lib/x86_64*/libaom.so*", recursive=True):
if file.count(".") == 2:
shutil.copy2(file, path)
else:
for file in glob.glob("/usr/lib/aarch64*/libaom.so*", recursive=True):
shutil.copy2(file, path)
print("Compiling FFmpeg for Linux finished!")
def compile_ffmpeg_windows(arch: str) -> None:
print("Configuring FFmpeg for Windows ...")
path: str = f"./test_room/addons/gde_gozen/bin/windows_{arch}"
os.environ["PKG_CONFIG_LIBDIR"] = f"/usr/{arch}-w64-mingw32/lib/pkgconfig"
os.environ["PKG_CONFIG_PATH"] = f"/usr/{arch}-w64-mingw32/lib/pkgconfig"
os.makedirs(path, exist_ok=True)
cmd = [
"./configure",
"--prefix=./bin",
"--enable-shared",
f"--arch={arch}",
"--target-os=mingw32",
"--enable-cross-compile",
f"--cross-prefix={arch}-w64-mingw32-",
"--quiet",
"--extra-libs=-lpthread",
"--extra-ldflags=-fpic",
"--extra-cflags=-fPIC",
]
cmd += ENABLED_MODULES
cmd += ENABLE_AV1
cmd += DISABLED_MODULES
result = subprocess.run(cmd, cwd="./ffmpeg/")
if result.returncode != 0:
print("Error: FFmpeg failed!")
print("Compiling FFmpeg for Windows ...")
subprocess.run(["make", f"-j{THREADS}"], cwd="./ffmpeg/")
subprocess.run(["make", "install"], cwd="./ffmpeg/")
print("Copying lib files ...")
for file in glob.glob("ffmpeg/bin/bin/*.dll"):
shutil.copy2(file, path)
# Somehow some distro"s put the dll"s in bin, and others in lib.
if os.path.exists("/usr/x86_64-w64-mingw32/bin/libwinpthread-1.dll"):
subprocess.run(["cp", "/usr/x86_64-w64-mingw32/bin/libwinpthread-1.dll", path], check=True)
subprocess.run(["cp", "/usr/x86_64-w64-mingw32/bin/libaom.dll", path], check=True)
else:
subprocess.run(["cp", "/usr/x86_64-w64-mingw32/lib/libwinpthread-1.dll", path], check=True)
subprocess.run(["cp", "/usr/x86_64-w64-mingw32/lib/libaom.dll", path], check=True)
print("Compiling FFmpeg for Windows finished!")
def compile_ffmpeg_macos(arch: str) -> None:
print("Configuring FFmpeg for MacOS ...")
path_debug: str = "./test_room/addons/gde_gozen/bin/macos/debug/lib"
path_release: str = "./test_room/addons/gde_gozen/bin/macos/release/lib"
os.makedirs(path_debug, exist_ok=True)
os.makedirs(path_release, exist_ok=True)
cmd = [
"./configure",
"--prefix=./bin",
"--enable-shared",
f"--arch={arch}",
"--quiet",
"--extra-ldflags=-mmacosx-version-min=10.13",
"--extra-cflags=-fPIC -mmacosx-version-min=10.13",
]
cmd += ENABLED_MODULES
cmd += ENABLE_AV1
cmd += DISABLED_MODULES
result = subprocess.run(cmd, cwd="./ffmpeg/")
if result.returncode != 0:
print("Error: FFmpeg failed!")
print("Compiling FFmpeg for MacOS ...")
subprocess.run(["make", f"-j{THREADS}"], cwd="./ffmpeg/", check=True)
subprocess.run(["make", "install"], cwd="./ffmpeg/", check=True)
print("Copying lib files ...")
for file in glob.glob("./ffmpeg/bin/lib/*.dylib"):
shutil.copy2(file, path_debug)
shutil.copy2(file, path_release)
print("Compiling FFmpeg for MacOS finished!")
def compile_ffmpeg_android(arch: str) -> None:
print("Configuring FFmpeg for Android ...")
path: str = "./test_room/addons/gde_gozen/bin/android_"
ndk: str = os.getenv("ANDROID_NDK_ROOT")
if not ndk:
ndk = os.getenv("ANDROID_NDK")
if not ndk or not os.path.isdir(ndk):
print("ANDROID_NDK(_ROOT) environment variable is not set or invalid!")
sys.exit(1)
# Getting correct settings.
host_tag: str = get_ndk_host_tag()
target_arch: str = ""
arch_flags: str = ""
ffmpeg_arch: str = ""
strip_tool: str = ""
if arch == ARCH_ARM64:
path += "arm64"
target_arch = "aarch64-linux-android"
arch_flags = "-march=armv8-a"
ffmpeg_arch = "aarch64"
else: # armv7a
path += "arm32"
target_arch = "armv7a-linux-androideabi"
arch_flags = "-march=armv7-a -mfloat-abi=softfp -mfpu=neon"
ffmpeg_arch = "arm"
main_folder: str = f"{ndk}/toolchains/llvm/prebuilt/{host_tag}"
toolchain_bin: str = f"{main_folder}/bin"
toolchain_sysroot: str = f"{main_folder}/sysroot"
cc: str = f"{toolchain_bin}/{target_arch}{ANDROID_API_LEVEL}-clang"
cxx: str = f"{toolchain_bin}/{target_arch}{ANDROID_API_LEVEL}-clangxx"
strip_tool: str = f"{toolchain_bin}/llvm-strip"
cmd = [
"./configure",
"--prefix=./bin",
"--enable-shared",
f"--arch={ffmpeg_arch}",
"--target-os=android",
"--enable-pic",
"--enable-cross-compile",
f"--cc={cc}",
f"--cxx={cxx}",
f"--sysroot={toolchain_sysroot}",
f"--strip={strip_tool}",
"--extra-cflags=-fPIC",
f"--extra-ldflags={arch_flags}",
]
cmd += ENABLED_MODULES
# TODO: Implement a way to add AV1 support for Android.
# cmd += ENABLE_AV1
cmd += DISABLED_MODULES
if arch == ARCH_ARMV7A:
cmd += ["--disable-vulkan"]
result = subprocess.run(cmd, cwd="./ffmpeg/")
if result.returncode != 0:
print("Error: FFmpeg failed!")
print("Compiling FFmpeg for Android ...")
subprocess.run(["make", f"-j{THREADS}"], cwd="./ffmpeg/", check=True)
subprocess.run(["make", "install"], cwd="./ffmpeg/", check=True)
print("Copying lib files ...")
os.makedirs(path, exist_ok=True)
for file in glob.glob("ffmpeg/bin/lib/*.so*"):
shutil.copy2(file, path)
print("Compiling FFmpeg for Android finished!")
def compile_ffmpeg_web() -> None:
print("Install/activate emsdk ...")
subprocess.run(["emsdk/emsdk", "install", "3.1.64"], check=True)
subprocess.run(["emsdk/emsdk", "activate", "3.1.64"], check=True)
emsdk_output = subprocess.check_output(
["bash", "-c", "source ./emsdk/emsdk_env.sh && env"], text=True)
for line in emsdk_output.splitlines():
key, _, value = line.partition("=")
os.environ[key] = value
print("Configuring FFmpeg for Web ...")
path: str = "./test_room/addons/gde_gozen/bin/web"
target_include_dir: str = f"{path}/include"
ffmpeg_bin_dir: str = "ffmpeg/bin"
ffmpeg_lib_dir: str = f"{ffmpeg_bin_dir}/lib"
ffmpeg_include_dir: str = f"{ffmpeg_bin_dir}/include"
os.makedirs(path, exist_ok=True)
cmd = [
"emconfigure",
"./configure",
"--cc=emcc",
"--cxx=em++",
"--ar=emar",
"--ranlib=emranlib",
"--nm=emnm",
"--enable-static",
"--disable-shared",
"--prefix=./bin",
"--enable-cross-compile",
"--target-os=none",
"--arch=wasm32",
"--cpu=generic",
"--disable-asm",
"--extra-cflags=-O3 -msimd128 -DNDEBUG -pthread -sUSE_PTHREADS=1 -sASYNCIFY=1 -fPIC",
"--extra-ldflags=-O3 -msimd128 -pthread -sUSE_PTHREADS=1 -sALLOW_MEMORY_GROWTH=1 -sASYNCIFY=1 -fPIC -sWASM_BIGINT=1",
"--enable-pic",
"--enable-small",
"--disable-everything",
"--enable-avcodec",
"--enable-avformat",
"--enable-avutil",
"--enable-swscale",
"--enable-swresample",
"--enable-network",
"--enable-demuxer=mov,mp4,m4a,3gp,3g2,mj2",
"--enable-demuxer=matroska,webm",
"--enable-demuxer=aac",
"--enable-decoder=vp9",
"--enable-decoder=h264",
"--enable-decoder=opus",
"--enable-decoder=pcm_s16le",
"--enable-decoder=aac",
"--enable-parser=h264",
"--enable-parser=aac",
"--enable-parser=opus",
"--enable-parser=vorbis",
"--enable-parser=mpegaudio",
"--enable-parser=vp9",
"--enable-bsf=h264_mp4toannexb",
"--enable-bsf=aac_adtstoasc",
"--enable-bsf=extract_extradata",
"--enable-bsf=noise",
"--enable-protocol=file,http",
]
cmd += DISABLED_MODULES
print(f"Running cmd: {cmd}")
result = subprocess.run(cmd, cwd="./ffmpeg/", check=True)
if result.returncode != 0:
print("Error: FFmpeg configure failed for Emscripten!")
sys.exit(1)
print("Compiling FFmpeg for Web (using emmake)...")
subprocess.run(["emmake", "make", f"-j{THREADS}"], cwd="./ffmpeg/", check=True)
subprocess.run(["emmake", "make", "install"], cwd="./ffmpeg/", check=True)
print("Copying static lib files (.a) ...")
for file in glob.glob(os.path.join(ffmpeg_lib_dir, "*.a")):
print(f"Copying {file} to {path}")
shutil.copy2(file, path)
if os.path.exists(target_include_dir):
shutil.rmtree(target_include_dir)
shutil.copytree(ffmpeg_include_dir, target_include_dir)
print("Compiling FFmpeg for Web finished!")
def macos_fix(arch) -> None:
# This is a fix for the MacOS builds to get the libraries to properly connect to
# the gdextension library. Without it, the FFmpeg libraries can"t be found.
print("Running fix for MacOS builds ...")
debug_binary: str = f"./test_room/addons/gde_gozen/bin/macos/debug/libgozen.macos.template_debug.{arch}.dylib"
release_binary: str = f"./test_room/addons/gde_gozen/bin/macos/release/libgozen.macos.template_release.{arch}.dylib"
debug_bin_folder: str = "./test_room/addons/gde_gozen/bin/macos/debug/lib"
release_bin_folder: str = "./test_room/addons/gde_gozen/bin/macos/release/lib"
print("Updating @loader_path for MacOS builds")
if os.path.exists(debug_binary):
for file in os.listdir(debug_bin_folder):
print("Fixing file ", file)
subprocess.run(["install_name_tool", "-change", f"./bin/lib/{file}", f"@loader_path/lib/{file}", debug_binary], check=True)
print("Fixing folder ", debug_binary)
subprocess.run(["otool", "-L", debug_binary], cwd="./")
else:
print("No debug folder found for MacOS!")
if os.path.exists(release_binary):
for file in os.listdir(release_bin_folder):
print("Fixing file ", file)
subprocess.run(["install_name_tool", "-change", f"./bin/lib/{file}", f"@loader_path/lib/{file}", release_binary], check=True)
print("Fixing folder ", release_binary)
subprocess.run(["otool", "-L", release_binary], cwd="./")
else:
print("No release folder found for MacOS!")
def main():
print("v===================v")
print("| GDE GoZen builder |")
print("^===================^")
if sys.version_info < (3, 10):
print("Python 3.10+ is required to run this script!")
sys.exit(2)
if os_platform.system() == "Windows":
# Oh no, Windows detected. ^^"
subprocess.run([sys.executable, PATH_BUILD_WINDOWS], cwd="./", check=True)
sys.exit(3)
if os.path.exists("./ffmpeg/.config"):
match _print_options("Init/Update submodules", ["no", "initialize", "update"]):
case 2:
subprocess.run(["git", "submodule", "update",
"--init", "--recursive"], cwd="./")
case 3:
subprocess.run(["git", "submodule", "update",
"--recursive", "--remote"], cwd="./")
else:
subprocess.run(["git", "submodule", "update", "--init", "--recursive"], cwd="./")
# Arm64 isn"t supported yet by mingw for Windows, so x86_64 only.
title_arch: str = "Choose architecture"
platform: str = OS_LINUX
arch: str = ARCH_X86_64
match _print_options("Select platform", [OS_LINUX, OS_WINDOWS, OS_MACOS, OS_ANDROID, OS_WEB]):
case 2:
platform = OS_WINDOWS
case 3:
platform = OS_MACOS
arch = ARCH_ARM64
case 4:
platform = OS_ANDROID
if _print_options(title_arch, [ARCH_ARM64, ARCH_ARMV7A]) == 2:
arch = ARCH_ARMV7A
else:
arch = ARCH_ARM64
case 5:
platform = OS_WEB
arch = ARCH_WASM32
case _: # Linux
if _print_options(title_arch, [ARCH_X86_64, ARCH_ARM64]) == 2:
arch = ARCH_ARM64
target: str = TARGET_DEV
if _print_options("Select target", [TARGET_DEV, TARGET_RELEASE]) == 2:
target = TARGET_RELEASE
clean_scons = True
if _print_options("Clean Scons?", ["yes", "no"]) == 2:
clean_scons = False
if _print_options("(Re)compile ffmpeg?", ["yes", "no"]) == 1:
compile_ffmpeg(platform, arch)
# Godot requires arm32 instead of armv7a.
if arch == ARCH_ARMV7A:
arch = "arm32"
cmd = ["scons", f"-j{THREADS}", f"target=template_{target}", f"platform={platform}", f"arch={arch}"]
env = os.environ.copy()
if platform == OS_ANDROID:
# We need to check if ANDROID_HOME is set to the sdk folder.
if os.getenv("ANDROID_HOME") is None:
if os_platform.system() == "Linux":
print("Linux detected for setting ANDROID_HOME")
env["ANDROID_HOME"] = os.getenv("ANDROID_HOME", ANDROID_SDK_PATH)
if clean_scons:
clean_cmd = ["scons", "--clean", f"-j{THREADS}", f"target=template_{target}", f"platform={platform}", f"arch={arch}"]
subprocess.run(clean_cmd, cwd="./", env=env)
subprocess.run(cmd, cwd="./", env=env)
if platform == OS_MACOS:
macos_fix(arch)
print("")
print("v=========================v")
print("| Done building GDE GoZen |")
print("^=========================^")
print("")
if __name__ == "__main__":
main()