townengine/bin/twnbuild

63 lines
1.9 KiB
Plaintext
Raw Normal View History

#!/bin/env python3
from subprocess import getoutput, run
2025-02-04 04:32:25 +00:00
from os import getcwd
from os.path import expandvars
from pathlib import Path
from sys import argv
import tomllib
#TODO: support for default pack override
#TODO: automatic full rebuild on git head change (such as new commits)
has_ninja = getoutput("command -v ninja") != ""
has_clang = getoutput("command -v clang") != ""
target_web = "--target=web" in argv
#TODO: infer what "native" means for current env
build_dir = "build/web" if target_web else "build/native"
build_dir += "/release" if "--release" in argv else "/debug"
cmake = ["emcmake", "cmake"] if target_web else ["cmake"]
# cmake configuration command
command = []
# check whether clang is around (it's just better)
if has_clang:
command += ["-DCMAKE_C_COMPILER=clang"]
# check whether ninja is around (you better start running)
if has_ninja:
command += ["-G", "Ninja"]
command += ["-B", build_dir]
2025-02-17 09:30:17 +00:00
# TODO: have it --fast=1 instead, where separate --debug=0 would mean stripping the debug info
if "--release" in argv:
command += ["-DCMAKE_BUILD_TYPE=Release"]
2025-02-17 09:30:17 +00:00
elif "--debug" in argv:
command += ["-DCMAKE_BUILD_TYPE=Debug"]
2025-02-17 09:30:17 +00:00
if "--unified=1" in argv:
command += ["-DTWN_FEATURE_DYNLIB_GAME=ON"]
2025-02-17 09:30:17 +00:00
elif "--unified=0" in argv:
command += ["-DTWN_FEATURE_DYNLIB_GAME=OFF"]
2025-02-17 09:30:17 +00:00
if "--sanitize=1" in argv:
command += ["-DTWN_SANITIZE=ON"]
2025-02-17 09:30:17 +00:00
elif "--sanitize=0" in argv:
command += ["-DTWN_SANITIZE=OFF"]
2025-02-17 09:30:17 +00:00
command += [f"-DTWN_OUT_DIR={getcwd()}"]
# pass arbitrary arguments over
if "--" in argv:
command += argv[argv.index("--")+1:]
# if no root cmake file is present, infer it from `twn.toml:game.interpreter`
if not Path("CMakeLists.txt").is_file():
2025-02-03 19:26:51 +00:00
config = tomllib.loads(Path("data/twn.toml").read_text())
command += ["-S", expandvars(config["game"]["interpreter"])]
run(cmake + command, check=True)
run(["cmake"] + ["--build", build_dir, "--parallel"], check=True)