ffi #jni_main slice 2: AOT pipeline — .java + javac + d8 → classes.dex in APK

Compilation.lowering_jni_main_decls is populated by lowerToIR (iterating
foreign_class_map for is_main && !is_foreign && runtime==jni_class,
deduped by foreign_path); each entry carries the pre-rendered Java source
from jni_java_emit.emitJavaSource.

createApk extended: when the emission list is non-empty, write each
.java under <stage>/java/<pkg>/<Class>.java, javac --release 11 to
<stage>/classes/, d8 --release --lib <android_jar> --output <stage>
to produce <stage>/classes.dex, then zip the .dex into the unaligned
APK at root level. javac discovery: $JAVA_HOME/bin/javac first, then
`which javac`.

Manifest still hardcodes android.app.NativeActivity (slice 3 wires the
user's class name + android:hasCode="true"), so the bundled .dex is
present but unreferenced at runtime. End-to-end verified via dexdump on
the smoke example's APK — Lco/swipelab/sxjnimain/SxApp; extending
NativeActivity shows up in classes.dex. Non-#jni_main APK builds
(99-android-egl-clear.sx) produce the same shape as before.

Cross-compile tuple added for examples/ffi-jni-main-01-emit.sx
(compile-only — APK exercise is manual).
This commit is contained in:
agra
2026-05-20 14:42:03 +03:00
parent 7ea7ad778e
commit 1ae43495c2
7 changed files with 236 additions and 6 deletions

View File

@@ -0,0 +1,38 @@
// `#jni_main` pipeline slice 2 (PLAN-FFI.md): the compiler renders a
// `.java` source for a `#jni_main #jni_class("...")` declaration, runs
// `javac` + `d8`, and bundles `classes.dex` into the APK.
//
// Slice 2 only wires the plumbing — the manifest still points at
// `android.app.NativeActivity`, so the user's class isn't loaded at
// runtime. Slice 3 (manifest synthesis) and slice 4 (RegisterNatives)
// land in follow-up commits.
//
// Build to inspect APK contents (requires Android SDK + JDK):
// /Users/agra/projects/sx/zig-out/bin/sx build --target android \
// --apk /tmp/sxjnimain.apk --bundle-id co.swipelab.sxjnimain \
// -o /tmp/libsxjnimain.so examples/ffi-jni-main-01-emit.sx
// unzip -l /tmp/sxjnimain.apk | grep classes.dex
//
// Cross-compile test (compile-only): see tests/cross_compile.sh's
// `android | examples/ffi-jni-main-01-emit.sx` tuple. APK creation
// itself isn't exercised by cross_compile.sh — only that the example
// lowers and links cleanly with `#jni_main` in scope.
#import "modules/std.sx";
#import "modules/compiler.sx";
// `#jni_main` flags this as the launchable Android Activity class. The
// empty body intentionally has zero methods — slice 2 just verifies the
// .java/.dex pipeline; `onCreate` overriding lands once slice 4 wires
// `RegisterNatives` so the `sx_<method>` symbols actually resolve.
SxApp :: #jni_main #jni_class("co/swipelab/sxjnimain/SxApp") { }
main :: () -> s32 { 0; }
// Android NDK entry symbol — kept as a 3-line trampoline so this example
// passes `--target android` builds via `tests/cross_compile.sh`.
android_main :: (app: *void) {
inline if OS == .android {
main();
}
}