Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions psprecomp/android/apk_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,39 @@ def sign_apk_bytes(data, key):
return entries + block + cd + bytes(eocd)


PAGE_16K = 16384


def elf_load_align(path):
"""The smallest p_align of the PT_LOAD segments of a 64-bit little-endian ELF, or None when the file is not one (a zip payload
shipped as lib*.so)."""
with open(path, "rb") as f:
h = f.read(64)
if len(h) < 64 or h[:4] != b"\x7fELF" or h[4] != 2 or h[5] != 1:
return None
phoff, = struct.unpack_from("<Q", h, 0x20)
phentsize, phnum = struct.unpack_from("<HH", h, 0x36)
f.seek(phoff)
ph = f.read(phentsize * phnum)
aligns = [struct.unpack_from("<Q", ph, i * phentsize + 0x30)[0] for i in range(phnum)
if struct.unpack_from("<I", ph, i * phentsize)[0] == 1] # PT_LOAD
return min(aligns) if aligns else None


def check_page_size(libs):
"""Warn about native libraries whose LOAD segments are aligned below 16 KB: a device running a 16 KB-page kernel (Android 15+, and
a developer option on Pixels) refuses to load them, and debuggable APKs show an "isn't 16 KB compatible" dialog (issue #41)."""
bad = []
for name, path in libs:
if name.startswith("lib/") and name.endswith(".so"):
al = elf_load_align(path)
if al is not None and al < PAGE_16K:
bad.append("%s (LOAD aligned to %d)" % (name, al))
for b in bad:
print("warning: not 16 KB page aligned: %s; link with -z max-page-size=16384" % b, file=sys.stderr)
return not bad


def build(skeleton, libs, out, key):
tmp = out + ".unsigned"
shutil.copyfile(skeleton, tmp)
Expand All @@ -117,6 +150,7 @@ def main():
if not path or not os.path.isfile(path):
sys.exit("--lib %s: file not found" % l)
libs.append((name, path))
check_page_size(libs)
build(a.skeleton, libs, a.output, json.load(open(a.key)))
print("built %s (%.1f MB)" % (a.output, os.path.getsize(a.output) / 1e6))

Expand Down
10 changes: 7 additions & 3 deletions psprecomp/android/build_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ def unpack(name):
t.extractall(d, members=[m for m in members if m.name])
return d

# 16 KB pages (Android 15+ devices may run a 16 KB kernel; Play requires it): the NDK r27 toolchain then links every shared
# library with -z max-page-size=16384 (the default from r28). android/apk_pack.py checks the result.
PAGE16K = ["-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"]

def cmake_dep(name, extra):
src = unpack(name); bd = out + "/build-" + name
run([a.cmake, "-S", src, "-B", bd, "-G", "Ninja", "-DCMAKE_TOOLCHAIN_FILE=" + tc, "-DANDROID_ABI=arm64-v8a",
"-DANDROID_PLATFORM=android-" + a.api, "-DCMAKE_BUILD_TYPE=Release", "-DCMAKE_INSTALL_PREFIX=" + deps,
"-DCMAKE_POLICY_VERSION_MINIMUM=3.5", "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"] + extra, env=env)
"-DCMAKE_POLICY_VERSION_MINIMUM=3.5", "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"] + PAGE16K + extra, env=env)
run([a.cmake, "--build", bd, "-j", j], env=env); run([a.cmake, "--install", bd], env=env)

if not os.path.isfile(deps + "/lib/libfreetype.a"):
Expand Down Expand Up @@ -80,7 +84,7 @@ def cmake_dep(name, extra):
shutil.copytree(lsrc, asrc + "/lib/linkernsbypass") # a git submodule upstream: not in the source tarball
bd = out + "/build-libadrenotools"
run([a.cmake, "-S", asrc, "-B", bd, "-G", "Ninja", "-DCMAKE_TOOLCHAIN_FILE=" + tc, "-DANDROID_ABI=arm64-v8a",
"-DANDROID_PLATFORM=android-" + a.api, "-DCMAKE_BUILD_TYPE=Release", "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"], env=env)
"-DANDROID_PLATFORM=android-" + a.api, "-DCMAKE_BUILD_TYPE=Release", "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"] + PAGE16K, env=env)
run([a.cmake, "--build", bd, "-j", j], env=env)
os.makedirs(deps + "/lib", exist_ok=True)
for pat in ("libadrenotools.a", "lib/linkernsbypass/liblinkernsbypass.a"):
Expand Down Expand Up @@ -139,7 +143,7 @@ def cmake_dep(name, extra):
with open(out + "/link.txt", "w") as f:
f.write("\n".join(link) + "\n")
with open(out + "/toolchain.cmake", "w") as f:
f.write('set(ANDROID_ABI arm64-v8a)\nset(ANDROID_PLATFORM android-%s)\ninclude("%s")\nset(PSPRECOMP_DEPS_CMAKE "%s/deps.cmake")\n' % (a.api, tc, out))
f.write('set(ANDROID_ABI arm64-v8a)\nset(ANDROID_PLATFORM android-%s)\nset(ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES ON)\ninclude("%s")\nset(PSPRECOMP_DEPS_CMAKE "%s/deps.cmake")\n' % (a.api, tc, out))
print("deps ready in", out)

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion psprecomp/android/build_hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def main():
bd = out + "/build"
os.makedirs(bd, exist_ok=True)
cm = [a.cmake, "-S", here + "/hello", "-B", bd, "-G", "Ninja", "-DCMAKE_TOOLCHAIN_FILE=" + tc, "-DANDROID_ABI=arm64-v8a",
"-DANDROID_PLATFORM=android-" + a.api, "-DCMAKE_BUILD_TYPE=Release", "-DSDL2_SRC=" + os.path.abspath(a.sdl_src), "-DCMAKE_POLICY_VERSION_MINIMUM=3.5"]
"-DANDROID_PLATFORM=android-" + a.api, "-DCMAKE_BUILD_TYPE=Release", "-DSDL2_SRC=" + os.path.abspath(a.sdl_src), "-DCMAKE_POLICY_VERSION_MINIMUM=3.5", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"]
if not a.main_so:
run(cm, env=env); run([a.cmake, "--build", bd, "-j", str(os.cpu_count())], env=env)
# 2. Java side: SDL's activity classes -> classes.dex
Expand Down
3 changes: 3 additions & 0 deletions psprecomp/docs/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ Not yet run: the GitHub workflow, and an update against a real release.
See [installer.md](installer.md), "Android progress": the renderer talks OpenGL ES through `runtime/include/psp_gl.h`; the release ships the prebuilt
Android core and libraries (SDL2, FreeType, FFmpeg decoders) built with NDK r27c, an unsigned APK skeleton (manifest + SDL's Java activity) and a
pure-Python packer/signer (APK Signature Scheme v2, `android/apk_pack.py`, a public debug key), so the player's machine needs only the NDK compiler.
Every native library is linked for 16 KB pages (`ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES`, i.e. `-z max-page-size=16384`; the on-device link
passes it to `ld.lld` directly), so the game loads on devices running a 16 KB-page kernel and debuggable builds show no "isn't 16 KB
compatible" dialog; `apk_pack.py` warns about any `lib/*.so` whose LOAD segments are aligned below 16 KB.

## Keeping the shader list complete

Expand Down
2 changes: 1 addition & 1 deletion psprecomp/games/dissidia/tools/android_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def write_toolchain(adir, work):
tc = os.path.join(bd, "toolchain.cmake")
ndk = os.path.join(adir, "ndk").replace("\\", "/")
with open(tc, "w") as f:
f.write('set(ANDROID_ABI %s)\nset(ANDROID_PLATFORM android-%d)\ninclude("%s/build/cmake/android.toolchain.cmake")\n'
f.write('set(ANDROID_ABI %s)\nset(ANDROID_PLATFORM android-%d)\nset(ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES ON)\ninclude("%s/build/cmake/android.toolchain.cmake")\n'
'set(PSPRECOMP_DEPS_CMAKE "%s/deps/deps.cmake")\n' % (v["abi"], v["api"], ndk, adir.replace("\\", "/")))
return bd, tc

Expand Down
Loading