beetlebug

Android

ztcl builds for aarch64-linux-android and x86_64-linux-android: the runner ztcl, the library libtcl8.6.so (libztcl) for an app, and the unit tests as a program. The default API level is 29 (Android 10).

What a build needs

export ANDROID_NDK_HOME=$HOME/android/android-ndk-r28c
zig build install libztcl -Dtarget=aarch64-linux-android --prefix out/aarch64
zig build install libztcl -Dtarget=x86_64-linux-android --prefix out/x86_64
File What it is
bin/ztcl The runner: a static program with no C library, the same code as the Linux runner. It runs from adb shell and in a terminal app.
lib/libtcl8.6.so The library for an app. It links libc.so and libdl.so of the system.
include/, lib/libtclstub8.6.a, lib/tclConfig.sh, lib/pkgconfig/ The kit of libztcl.

Each binary has its segments aligned to 16 KiB, so it loads on a device with pages of 16 KiB (Android 15 and later can run so). The build links each Android binary with LLVM and LLD. The ELF linker of Zig 0.17 keeps an alignment of 4 KiB and writes no PT_PHDR, and the loader of bionic refuses such a program.

Embed ztcl in an app

Compile the C code of the app with the clang of the NDK against the kit, and put libtcl8.6.so in the lib/ARCH folder of the APK:

NDK_BIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin
$NDK_BIN/clang --target=aarch64-linux-android29 -I out/aarch64/include app.c \
    -L out/aarch64/lib -ltcl8.6 -o app

The library has the same sandbox as on each system: no command of exec, file, socket, clock, or env until the embedder turns its capability on (Limits, Embedding). puts goes to the output callback of the embedder. An app gives its own folders (getFilesDir() and getCacheDir() of its Context) as the root folder of file.

The JIT

The JIT is on, with tier 1 and tier 2, as on Linux: Android is a Linux kernel, and the machine code and its calls are the code of x86_64-linux and aarch64-linux. The region of the JIT uses mmap and mprotect to make each page writable or executable, never both. jit-probe in the emulator gives selfTest=true region=pages.

The assembler of arm64 never writes x18, the register of the shadow call stack of Android.

Run in the emulator

adb push out/x86_64/bin/ztcl /data/local/tmp/
adb shell 'cd /data/local/tmp && chmod 755 ztcl && echo "puts [info patchlevel]" > a.tcl && ./ztcl a.tcl'

The unit tests and the Tcl suite run in the emulator too:

zig build test-bin -Dtarget=x86_64-linux-android --prefix out/x86_64
adb push out/x86_64/bin/ztcl-unit /data/local/tmp/
adb shell 'cd /data/local/tmp && ./ztcl-unit'

zig build suite-runner -Doptimize=ReleaseSafe -Dtarget=x86_64-linux-android --prefix out/x86_64
adb shell mkdir -p /data/local/tmp/s/tools /data/local/tmp/s/zig-out/bin /data/local/tmp/s/tcl/library
adb push .tcl-src/core-8-6-16/tests /data/local/tmp/s/tcl/
adb push .tcl-src/core-8-6-16/library/encoding /data/local/tmp/s/tcl/library/
adb push tools/suite.sh tools/tcltest.tcl tools/tcltest-end.tcl /data/local/tmp/s/tools/
adb push out/x86_64/bin/ztcl-suite /data/local/tmp/s/zig-out/bin/
adb shell 'cd /data/local/tmp/s && TMPDIR=/data/local/tmp sh tools/suite.sh -r zig-out/bin/ztcl-suite -s tcl/tests'

tools/fetch-tcl.sh downloads the Tcl sources into .tcl-src/. Android has timeout and mktemp in toybox, so tools/suite.sh runs in its shell.

Pages of 16 KiB on x86-64

The emulator has x86-64 images with pages of 16 KiB (google_apis_ps16k) for the test of apps for arm64 devices. The standard library of Zig has 4 KiB as the largest page of x86-64, so std_options.page_size_max of each ztcl program is 16 KiB on x86-64 Android (src/sys.zig). The test runner of Zig has its own std_options, so ztcl-unit for x86-64 runs only on an image with pages of 4 KiB. An x86-64 device has pages of 4 KiB.

Not done yet

ztcl build writes no APK or AAB. ztk bundle of ztk writes an APK with the Android backend of ztk.