ffi #jni_main: accept #jni_main as Android entry; modern smoke shape
Loosens lower.zig's `checkRequiredEntryPoints` to accept either a
`#jni_main #jni_class("...")` decl OR the legacy `android_main`
trampoline. The diagnostic now shows both options when neither is
present.
Updates the slice 2 smoke (`examples/ffi-jni-main-01-emit.sx`) to
express the modern shape — drops `android_main`, declares
`Bundle :: #foreign #jni_class("android/os/Bundle")`, and overrides
`onCreate :: (self: *Self, b: *Bundle) { }` inside the #jni_main class.
The emitted Java now correctly declares `void onCreate(Bundle b)` as
@Override + a matching `private native void sx_onCreate(Bundle b)`
delegate, verified via dexdump.
Full retirement of `android_main` (deleting native_app_glue from the
Android link path, dropping `AndroidPlatform.run_frame_loop`, migrating
chess/EGL demo to the Java-driven lifecycle) is multi-slice rework
and stays as follow-up.
This commit is contained in:
@@ -12,27 +12,20 @@
|
|||||||
// --apk /tmp/sxjnimain.apk --bundle-id co.swipelab.sxjnimain \
|
// --apk /tmp/sxjnimain.apk --bundle-id co.swipelab.sxjnimain \
|
||||||
// -o /tmp/libsxjnimain.so examples/ffi-jni-main-01-emit.sx
|
// -o /tmp/libsxjnimain.so examples/ffi-jni-main-01-emit.sx
|
||||||
// unzip -l /tmp/sxjnimain.apk | grep classes.dex
|
// 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/std.sx";
|
||||||
#import "modules/compiler.sx";
|
#import "modules/compiler.sx";
|
||||||
|
|
||||||
|
// `*Bundle` resolves through the class registry to `android.os.Bundle`
|
||||||
|
// in the emitted Java — needed for `onCreate`'s @Override to match
|
||||||
|
// NativeActivity's superclass signature.
|
||||||
|
Bundle :: #foreign #jni_class("android/os/Bundle") { }
|
||||||
|
|
||||||
// `#jni_main` flags this as the launchable Android Activity class. The
|
// `#jni_main` flags this as the launchable Android Activity class. The
|
||||||
// empty body intentionally has zero methods — slice 2 just verifies the
|
// `onCreate` body is empty for now — slice 4 wires `RegisterNatives`
|
||||||
// .java/.dex pipeline; `onCreate` overriding lands once slice 4 wires
|
// so the `sx_onCreate` native delegate actually binds to a sx-side fn.
|
||||||
// `RegisterNatives` so the `sx_<method>` symbols actually resolve.
|
SxApp :: #jni_main #jni_class("co/swipelab/sxjnimain/SxApp") {
|
||||||
SxApp :: #jni_main #jni_class("co/swipelab/sxjnimain/SxApp") { }
|
onCreate :: (self: *Self, b: *Bundle) { }
|
||||||
|
}
|
||||||
|
|
||||||
main :: () -> s32 { 0; }
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -219,34 +219,45 @@ pub const Lowering = struct {
|
|||||||
self.checkRequiredEntryPoints();
|
self.checkRequiredEntryPoints();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// On Android, the OS loader calls `android_main(app: *void)` — there's
|
/// On Android, the OS loads the .so via one of two entry paths:
|
||||||
/// no `main()` invocation from the system. If the user hasn't defined
|
///
|
||||||
/// `android_main`, native_app_glue can't find it at runtime and the
|
/// 1. **`#jni_main` Activity** (preferred) — Java-side Activity class
|
||||||
/// activity dies with an unhelpful "library doesn't export
|
/// declared as `Foo :: #jni_main #jni_class("...") { onCreate :: ... }`.
|
||||||
/// `android_main`" error after the .so is loaded. Catch this at
|
/// The Java class drives lifecycle; sx provides native delegates.
|
||||||
/// compile time with a clear hint pointing at the platform module
|
/// 2. **`android_main` trampoline** (legacy NativeActivity path) — user
|
||||||
/// that provides the helper.
|
/// defines `android_main(app: *void)`, native_app_glue's
|
||||||
|
/// `ANativeActivity_onCreate` jumps into it on a worker thread.
|
||||||
|
///
|
||||||
|
/// Either satisfies the check. When both are missing, the .so won't
|
||||||
|
/// have an entry point Android can reach, so emit a diagnostic that
|
||||||
|
/// shows both options.
|
||||||
fn checkRequiredEntryPoints(self: *Lowering) void {
|
fn checkRequiredEntryPoints(self: *Lowering) void {
|
||||||
const tc = self.target_config orelse return;
|
const tc = self.target_config orelse return;
|
||||||
if (!tc.isAndroid()) return;
|
if (!tc.isAndroid()) return;
|
||||||
|
|
||||||
|
var it = self.foreign_class_map.iterator();
|
||||||
|
while (it.next()) |entry| {
|
||||||
|
const fcd = entry.value_ptr.*;
|
||||||
|
if (fcd.is_main and !fcd.is_foreign and fcd.runtime == .jni_class) return;
|
||||||
|
}
|
||||||
|
|
||||||
const wanted = self.module.types.internString("android_main");
|
const wanted = self.module.types.internString("android_main");
|
||||||
var has_defn = false;
|
|
||||||
for (self.module.functions.items) |func| {
|
for (self.module.functions.items) |func| {
|
||||||
if (func.name != wanted) continue;
|
if (func.name != wanted) continue;
|
||||||
if (func.is_extern) continue;
|
if (func.is_extern) continue;
|
||||||
if (func.blocks.items.len == 0) continue;
|
if (func.blocks.items.len == 0) continue;
|
||||||
has_defn = true;
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (has_defn) return;
|
|
||||||
if (self.diagnostics) |diags| {
|
if (self.diagnostics) |diags| {
|
||||||
diags.addFmt(.err, null,
|
diags.addFmt(.err, null,
|
||||||
"target is Android but no `android_main` function defined. " ++
|
"target is Android but no entry point declared. " ++
|
||||||
"The OS calls `android_main(app: *void)` as the entry point — " ++
|
"Either declare a `#jni_main` Activity class:\n\n" ++
|
||||||
"add it to your main.sx (it can be a 3-line trampoline that " ++
|
" MyApp :: #jni_main #jni_class(\"co/example/MyApp\") {{\n" ++
|
||||||
"calls `sx_android_bootstrap(app)` then `main()` — see " ++
|
" onCreate :: (self: *Self, b: *Bundle) {{ /* ... */ }};\n" ++
|
||||||
"`examples/99-android-egl-clear.sx`).", .{});
|
" }}\n\n" ++
|
||||||
|
"or define an `android_main` trampoline that bootstraps the " ++
|
||||||
|
"legacy NativeActivity path (see `examples/99-android-egl-clear.sx`).", .{});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user