-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.py
490 lines (372 loc) · 17.1 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
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
#!/usr/bin/env python3
import argparse
import contextlib
import hashlib
import glob
import multiprocessing
import os
import os.path
import platform
import shutil
import subprocess
import sys
import tempfile
import urllib.request
import zipfile
#----------------------------------------------------------------
libs_dir = os.path.abspath("extern")
cache_dir = os.path.abspath("cache")
dist_dir = os.path.abspath("dist")
game_name = "BillyFrontier" # no spaces
game_name_human = "Billy Frontier" # spaces and other special characters allowed
game_package = "io.jor.billyfrontier" # unique package name
game_ver = "1.1.1"
source_check = "Source/System/Areas/Shootout.c" # some file that's likely to be from the game's source tree
sdl_ver = "2.28.3"
appimagetool_ver = "13"
lib_hashes = { # sha-256
"SDL2-2.28.3.tar.gz": "7acb8679652701a2504d734e2ba7543ec1a83e310498ddd22fd44bf965eb5518",
"SDL2-2.28.3.dmg": "3375d4c3b77d0ce991ef4fbf77feb39c6c0c7d817ebb43b5510997e9bc2ef785",
"SDL2-devel-2.28.3-VC.zip": "f56f362502cc7289463cc4a684c0f013e07fb65b376ddb35681698d2c6fabd22",
"appimagetool-x86_64.AppImage": "df3baf5ca5facbecfc2f3fa6713c29ab9cefa8fd8c1eac5d283b79cab33e4acb", # appimagetool v13
"appimagetool-aarch64.AppImage": "334e77beb67fc1e71856c29d5f3f324ca77b0fde7a840fdd14bd3b88c25c341f",
}
NPROC = multiprocessing.cpu_count()
SYSTEM = platform.system()
MACHINE = platform.machine()
if SYSTEM == "Windows":
os.system("") # hack to get ANSI color escapes to work
#----------------------------------------------------------------
parser = argparse.ArgumentParser(description=F"Configure, build, and package {game_name_human}")
if SYSTEM == "Darwin":
default_generator = "Xcode"
default_architecture = None
help_configure = "generate Xcode project"
help_build = "build app from Xcode project"
help_package = "package up the game into a DMG"
elif SYSTEM == "Windows":
default_generator = "Visual Studio 17 2022"
default_architecture = "x64"
help_configure = F"generate {default_generator} solution"
help_build = F"build exe from {default_generator} solution"
help_package = "package up the game into a ZIP"
else:
default_generator = None
default_architecture = None
help_configure = "generate project"
help_build = "build binary"
help_package = "package up the game into an AppImage"
parser.add_argument("-1", "--dependencies", default=False, action="store_true", help="step 1: fetch and set up dependencies (SDL)")
parser.add_argument("-2", "--configure", default=False, action="store_true", help=f"step 2: {help_configure}")
parser.add_argument("-3", "--build", default=False, action="store_true", help=f"step 3: {help_build}")
parser.add_argument("-4", "--package", default=False, action="store_true", help=f"step 4: {help_package}")
parser.add_argument("-G", metavar="<generator>", default=default_generator,
help=F"cmake project generator for step 2 (default: {default_generator})")
parser.add_argument("-A", metavar="<arch>", default=default_architecture,
help=F"cmake platform name for step 2 (default: {default_architecture})")
parser.add_argument("--dist-dir", metavar="<dir>", default=dist_dir,
help=F"where to store build artifacts in step 4 (default: {os.path.relpath(dist_dir)})")
parser.add_argument("--print-artifact-name", default=False, action="store_true",
help="print artifact name and exit")
if SYSTEM == "Linux":
parser.add_argument("--system-sdl", default=False, action="store_true",
help="use system SDL instead of building SDL from scratch")
parser.add_argument("--no-appimage", default=False, action="store_true",
help="don't generate an AppImage in step 4")
args = parser.parse_args()
dist_dir = os.path.abspath(args.dist_dir)
#----------------------------------------------------------------
@contextlib.contextmanager
def chdir(path):
origin = os.getcwd()
try:
os.chdir(path)
yield
finally:
os.chdir(origin)
def die(message):
print(F"\x1b[1;31m{message}\x1b[0m", file=sys.stderr)
sys.exit(1)
def log(message):
print(message, file=sys.stderr)
def fatlog(message):
starbar = len(message) * '*'
print(F"\n{starbar}\n{message}\n{starbar}", file=sys.stderr)
def hash_file(path):
hasher = hashlib.sha256()
with open(path, 'rb') as f:
while True:
chunk = f.read(64*1024)
if not chunk:
break
hasher.update(chunk)
return hasher.hexdigest()
def get_package(url):
name = url[url.rfind('/')+1:]
if name in lib_hashes:
reference_hash = lib_hashes[name]
else:
die(F"Build script lacks reference checksum for {name}")
path = os.path.normpath(F"{cache_dir}/{name}")
if os.path.exists(path):
log(F"Not redownloading: {path}")
else:
log(F"Downloading: {url}")
os.makedirs(cache_dir, exist_ok=True)
urllib.request.urlretrieve(url, path)
actual_hash = hash_file(path)
if reference_hash != actual_hash:
die(F"Bad checksum for {name}: expected {reference_hash}, got {actual_hash}")
return path
def call(cmd, **kwargs):
cmdstr = ""
for token in cmd:
cmdstr += " "
if " " in token:
cmdstr += F"\"{token}\""
else:
cmdstr += token
log(F">{cmdstr}")
try:
return subprocess.run(cmd, check=True, **kwargs)
except subprocess.CalledProcessError as e:
die(F"Aborting setup because: {e}")
def rmtree_if_exists(path):
if os.path.exists(path):
shutil.rmtree(path)
def rm_if_exists(path):
with contextlib.suppress(FileNotFoundError):
os.remove(path)
def zipdir(zipname, topleveldir, arc_topleveldir):
with zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as zipf:
for root, dirs, files in os.walk(topleveldir):
for file in files:
filepath = os.path.join(root, file)
arcpath = os.path.join(arc_topleveldir, filepath[len(topleveldir)+1:])
log(F"Zipping: {arcpath}")
zipf.write(filepath, arcpath)
#----------------------------------------------------------------
class Project:
def __init__(self, dir_name):
self.dir_name = dir_name
self.gen_args = []
self.gen_env = {}
self.build_configs = []
self.build_args = []
def prepare_dependencies(self):
raise NotImplementedError("prepare_dependencies not implemented for this platform")
def configure(self):
fatlog(F"Configuring {self.dir_name}")
rmtree_if_exists(self.dir_name)
env = None
if self.gen_env:
env = os.environ.copy()
env.update(self.gen_env)
call(["cmake", "-S", ".", "-B", self.dir_name] + self.gen_args, env=env)
def build(self):
build_command = ["cmake", "--build", self.dir_name]
if self.build_configs:
build_command += ["--config", self.build_configs[0]]
if self.build_args:
build_command += ["--"] + self.build_args
call(build_command)
def package(self):
raise NotImplementedError("package not implemented for this platform")
def get_artifact_path(self):
return os.path.join(dist_dir, self.get_artifact_name())
def get_artifact_name(self):
raise NotImplementedError("get_artifact_name not implemented for this platform")
def copy_documentation(self, appdir, full=True):
shutil.copy(F"{self.dir_name}/ReadMe.txt", F"{appdir}")
shutil.copy(F"LICENSE.md", F"{appdir}/License.txt")
if full:
os.makedirs(f"{appdir}/Documentation", exist_ok=True)
for docfile in ["Instructions.pdf"]:
shutil.copy(docfile, F"{appdir}")
class WindowsProject(Project):
def __init__(self, dir_name="build-msvc"):
super().__init__(dir_name)
# On Windows, ship a PDB file along with the Release build.
# Avoid RelWithDebInfo because bottom-of-the-barrel AVs may raise a false positive with such builds.
self.build_configs = ["Release", "Debug"]
self.build_args = ["-m"] # multiprocessor compilation
def get_artifact_name(self):
return F"{game_name}-{game_ver}-windows-x64.zip"
def prepare_dependencies(self):
rmtree_if_exists(F"{libs_dir}/SDL2-{sdl_ver}")
sdl_zip_path = get_package(F"https://libsdl.org/release/SDL2-devel-{sdl_ver}-VC.zip")
shutil.unpack_archive(sdl_zip_path, libs_dir)
def package(self):
release_config = self.build_configs[0]
windows_dlls = ["SDL2.dll", "msvcp140.dll", "vcruntime140.dll", "vcruntime140_1.dll"]
# Prep DLLs with cmake (copied to {cache_dir}/install/bin)
call(["cmake", "--install", self.dir_name, "--prefix", F"{cache_dir}/install"])
appdir = F"{cache_dir}/{game_name}-{game_ver}"
rmtree_if_exists(appdir)
os.makedirs(F"{appdir}", exist_ok=True)
# Copy executable, PDB, assets and libs
shutil.copy(F"{self.dir_name}/{release_config}/{game_name}.exe", appdir)
shutil.copy(F"{self.dir_name}/{release_config}/{game_name}.pdb", appdir)
shutil.copytree("Data", F"{appdir}/Data")
for dll in windows_dlls:
shutil.copy(F"{cache_dir}/install/bin/{dll}", appdir)
self.copy_documentation(appdir)
rm_if_exists(self.get_artifact_path())
zipdir(self.get_artifact_path(), appdir, F"{game_name}-{game_ver}")
class MacProject(Project):
def __init__(self, dir_name="build-xcode"):
super().__init__(dir_name)
self.build_configs = ["RelWithDebInfo"]
self.build_args += ["-j", str(NPROC), "-quiet"]
def get_artifact_name(self):
return F"{game_name}-{game_ver}-mac.dmg"
def prepare_dependencies(self):
sdl2_framework = "SDL2.framework"
sdl2_framework_target_path = F"{libs_dir}/{sdl2_framework}"
rmtree_if_exists(sdl2_framework_target_path)
sdl_dmg_path = get_package(F"https://libsdl.org/release/SDL2-{sdl_ver}.dmg")
# Mount the DMG and copy SDL2.framework to extern/
with tempfile.TemporaryDirectory() as mount_point:
call(["hdiutil", "attach", sdl_dmg_path, "-mountpoint", mount_point, "-quiet"])
shutil.copytree(F"{mount_point}/{sdl2_framework}", sdl2_framework_target_path, symlinks=True)
call(["hdiutil", "detach", mount_point, "-quiet"])
if "CODE_SIGN_IDENTITY" in os.environ:
call(["codesign", "--force", "--timestamp", "--sign", os.environ["CODE_SIGN_IDENTITY"], sdl2_framework_target_path])
else:
print("SDL will not be codesigned. Set the CODE_SIGN_IDENTITY environment variable if you want to sign it.")
def package(self):
release_config = self.build_configs[0]
appdir = F"{self.dir_name}/{release_config}"
# Human-friendly name for .app
os.rename(F"{appdir}/{game_name}.app", F"{appdir}/{game_name_human}.app")
self.copy_documentation(appdir)
rm_if_exists(self.get_artifact_path())
call(["hdiutil", "create",
"-fs", "HFS+",
"-srcfolder", appdir,
"-volname", F"{game_name_human} {game_ver}",
self.get_artifact_path()])
class LinuxProject(Project):
def __init__(self, dir_name, config_name, custom_sdl_path, as_appimage):
super().__init__(dir_name)
self.gen_args += ["-DCMAKE_BUILD_TYPE=" + config_name]
self.build_args += ["-j", str(NPROC)]
self.build_configs = [config_name]
if custom_sdl_path:
self.gen_env["SDL2DIR"] = custom_sdl_path
self.use_system_sdl = False
else:
self.use_system_sdl = True
self.as_appimage = as_appimage
def get_artifact_name(self, extension=None):
if extension is None:
if self.as_appimage:
extension = ".AppImage"
else:
extension = ".AppDir"
return F"{game_name}-{game_ver}-linux-{MACHINE}{extension}"
def prepare_dependencies(self):
if self.use_system_sdl:
return
sdl_source_dir = F"{libs_dir}/SDL2-{sdl_ver}"
sdl_build_dir = F"{sdl_source_dir}/build"
sdl_prefix_dir = F"{sdl_source_dir}/install"
rmtree_if_exists(sdl_source_dir)
sdl_zip_path = get_package(F"https://libsdl.org/release/SDL2-{sdl_ver}.tar.gz")
shutil.unpack_archive(sdl_zip_path, libs_dir)
with chdir(sdl_source_dir):
call(["cmake", "-S", ".", "-B", "build", F"-DCMAKE_INSTALL_PREFIX={sdl_prefix_dir}"])
call(["cmake", "--build", sdl_build_dir])
call(["cmake", "--install", sdl_build_dir])
def package(self):
if self.as_appimage:
appdir = cache_dir + "/" + self.get_artifact_name(extension=".AppDir")
else:
appdir = self.get_artifact_path()
assert appdir.endswith(".AppDir")
rmtree_if_exists(appdir)
# Prepare directory tree before copying files
for d in ["", "usr/bin", "usr/lib", "usr/share/metainfo", "usr/share/applications"]:
os.makedirs(F"{appdir}/{d}", exist_ok=True)
# Copy executable and assets
shutil.copy(F"{self.dir_name}/{game_name}", F"{appdir}/usr/bin") # executable
shutil.copytree("Data", F"{appdir}/Data")
self.copy_documentation(appdir, full=False)
# Copy XDG stuff
shutil.copy(F"packaging/{game_package}.desktop", appdir)
shutil.copy(F"packaging/{game_package}.png", appdir)
shutil.copy(F"packaging/{game_package}.appdata.xml", F"{appdir}/usr/share/metainfo")
shutil.copy(F"packaging/{game_package}.desktop", F"{appdir}/usr/share/applications") # must copy desktop file there as well, for validation
# Copy AppImage kicker script
shutil.copy(F"packaging/AppRun", appdir)
os.chmod(F"{appdir}/AppRun", 0o755)
# Copy SDL (if not using system SDL)
if not self.use_system_sdl:
for file in glob.glob(F"{libs_dir}/SDL2-{sdl_ver}/install/lib/libSDL2*.so*"):
shutil.copy(file, F"{appdir}/usr/lib", follow_symlinks=False)
# Invoke appimagetool
if self.as_appimage:
appimagetool_path = get_package(F"https://github.com/AppImage/AppImageKit/releases/download/{appimagetool_ver}/appimagetool-{MACHINE}.AppImage")
os.chmod(appimagetool_path, 0o755)
rm_if_exists(self.get_artifact_path())
call([appimagetool_path, appdir, self.get_artifact_path()])
#----------------------------------------------------------------
if __name__ == "__main__":
# Make sure we're running from the correct directory...
if not os.path.exists(source_check): # some file that's likely to be from the game's source tree
die(F"STOP - Please run this script from the root of the {game_name} source repo")
#----------------------------------------------------------------
# Set up project metadata
if SYSTEM == "Windows":
project = WindowsProject()
elif SYSTEM == "Darwin":
project = MacProject()
elif SYSTEM == "Linux":
custom_sdl_path = ""
if not args.system_sdl:
custom_sdl_path = F"{libs_dir}/SDL2-{sdl_ver}/install"
project = LinuxProject("build", "RelWithDebInfo", custom_sdl_path, not args.no_appimage)
else:
die(F"Unsupported system for configure step: {SYSTEM}")
common_gen_args = []
if args.G:
common_gen_args += ["-G", args.G]
if args.A:
common_gen_args += ["-A", args.A]
project.gen_args += common_gen_args
#----------------------------------------------------------------
# Gather build steps
if args.print_artifact_name:
print(project.get_artifact_name())
sys.exit(0)
fatlog(F"{game_name} {game_ver} build script")
if not (args.dependencies or args.configure or args.build or args.package):
log("No build steps specified, running all of them.")
args.dependencies = True
args.configure = True
args.build = True
args.package = True
#----------------------------------------------------------------
# Prepare dependencies
if args.dependencies:
fatlog("Setting up dependencies")
# Check that our submodules are here
if not os.path.exists("extern/Pomme/CMakeLists.txt"):
die("Submodules appear to be missing.\n"
+ "Did you clone the submodules recursively? Try this: git submodule update --init --recursive")
project.prepare_dependencies()
#----------------------------------------------------------------
# Configure projects
if args.configure:
project.configure()
#----------------------------------------------------------------
# Build the game
if args.build:
fatlog(F"Building the game: {project.dir_name}")
project.build()
#----------------------------------------------------------------
# Package the game
if args.package:
fatlog(F"Packaging the game")
os.makedirs(dist_dir, exist_ok=True)
project.package()