0.1.0-0-pre.7-fix.1

This commit is contained in:
2026-09-16 09:32:40 +02:00
parent 98b830da6e
commit 431665992c
6 changed files with 252 additions and 24 deletions

View File

@@ -1,14 +1,15 @@
#!/usr/bin/env python3
# file: scripts/build_android_rust.py
# version: 1
# version: 2
"""Build one Android Rust game entrypoint and stage it as Gradle jniLibs."""
"""Build one Android Rust game entrypoint and stage SDL3 and Rust jniLibs."""
from __future__ import annotations
import argparse
import os
import pathlib
import shutil
import subprocess
import sys
import zipfile
@@ -29,16 +30,42 @@ def read_sdl3_aar_name(properties_path: pathlib.Path) -> str:
return line.split("=", 1)[1].strip()
raise RuntimeError("sdl3AarName is missing from Android/gradle.properties")
def extract_sdl3_link_library(aar_path: pathlib.Path, abi: str, output_dir: pathlib.Path) -> pathlib.Path:
"""Extract the SDL3 shared library used only for the Rust linker search path."""
expected = f"jni/{abi}/libSDL3.so"
output_dir.mkdir(parents=True, exist_ok=True)
def find_sdl3_member(archive: zipfile.ZipFile, abi: str) -> str:
"""Return the SDL3 shared-library member for one Android ABI."""
candidates = (
f"prefab/modules/SDL3/libs/android.{abi}/libSDL3.so",
f"jni/{abi}/libSDL3.so",
)
members = set(archive.namelist())
for candidate in candidates:
if candidate in members:
return candidate
suffix = f"/libs/android.{abi}/libSDL3.so"
fallback = sorted(member for member in members if member.endswith(suffix))
if len(fallback) == 1:
return fallback[0]
raise RuntimeError(
f"SDL3 shared library for ABI {abi} is missing from the AAR; "
f"expected Prefab member {candidates[0]}"
)
def extract_sdl3_library(
aar_path: pathlib.Path,
abi: str,
link_dir: pathlib.Path,
runtime_dir: pathlib.Path,
) -> pathlib.Path:
"""Extract SDL3 for both Rust linking and APK runtime staging."""
link_dir.mkdir(parents=True, exist_ok=True)
runtime_dir.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(aar_path) as archive:
if expected not in archive.namelist():
raise RuntimeError(f"{expected} is missing from {aar_path}")
target = output_dir / "libSDL3.so"
target.write_bytes(archive.read(expected))
return target
member = find_sdl3_member(archive, abi)
payload = archive.read(member)
link_target = link_dir / "libSDL3.so"
runtime_target = runtime_dir / "libSDL3.so"
link_target.write_bytes(payload)
runtime_target.write_bytes(payload)
return link_target
def main() -> int:
"""Build the requested game entrypoint for one Android ABI."""
@@ -53,27 +80,44 @@ def main() -> int:
if not aar_path.is_file():
print(f"missing SDL3 AAR: {aar_path}", file=sys.stderr)
return 2
module = GAME_TO_MODULE[arguments.game]
jni_root = root / "Android" / module / "src" / "main" / "jniLibs"
runtime_dir = jni_root / arguments.abi
link_dir = root / "Android" / "build" / "rust-link" / arguments.abi
try:
extract_sdl3_link_library(aar_path, arguments.abi, link_dir)
extract_sdl3_library(aar_path, arguments.abi, link_dir, runtime_dir)
except (OSError, RuntimeError, zipfile.BadZipFile) as error:
print(str(error), file=sys.stderr)
return 2
module = GAME_TO_MODULE[arguments.game]
output = root / "Android" / module / "src" / "main" / "jniLibs"
environment = os.environ.copy()
rustflags = environment.get("RUSTFLAGS", "").strip()
link_flag = f"-L native={link_dir}"
environment["RUSTFLAGS"] = f"{rustflags} {link_flag}".strip()
environment["CARGO_NDK_PLATFORM"] = "21"
command = [
"cargo", "ndk", "-t", arguments.abi, "-o", str(output), "build",
"-p", "game-android-entrypoint", "--no-default-features", "--features", arguments.game,
"cargo",
"ndk",
"-t",
arguments.abi,
"-o",
str(jni_root),
"build",
"-p",
"game-android-entrypoint",
"--no-default-features",
"--features",
arguments.game,
]
if arguments.release:
command.append("--release")
completed = subprocess.run(command, cwd=root, env=environment, check=False)
return completed.returncode
if completed.returncode != 0:
return completed.returncode
expected = runtime_dir / "libgame_android_entrypoint.so"
if not expected.is_file():
print(f"missing Rust Android library after cargo-ndk build: {expected}", file=sys.stderr)
return 3
return 0
if __name__ == "__main__":
raise SystemExit(main())