From ea9cdfbc6dbee90ec8852b0745625610592453c2 Mon Sep 17 00:00:00 2001 From: sigsegv0x0b <6982462+sigsegv0x0b@users.noreply.github.com> Date: Sat, 31 Oct 2026 10:22:48 -0400 Subject: [PATCH] feat(cli): add native Android support Build the CLI natively for Termux with Bun's android target instead of requiring glibc-runner, and publish an @opencode/cli-android-arm64 package. - Add the android-arm64 target and map it to bun-linux-arm64-android, with the matching bun-linux-aarch64-android release asset. - Publish android in the distribution package os list and resolve @opencode/pty-android-arm64 from the CLI build. - Build opencode-pty for aarch64-linux-android, pin the Zig API level to 28 so the Ghostty build does not query getprop on Termux, and use TMPDIR for the service socket on Android because /tmp is not writable there. - Embed the Android fff native library so file search works in compiled binaries. The TUI additionally needs an @opentui/core-android-arm64 prebuild; that change lives in anomalyco/opentui. --- .github/workflows/release-process-daemon.yml | 29 ++++++++++++++++++- packages/cli/script/build.ts | 12 ++++++-- packages/cli/script/opencode-pty.ts | 2 +- packages/cli/script/pack.ts | 2 +- packages/process-daemon/build.rs | 6 +++- packages/process-daemon/npm/index.js | 9 ++++-- packages/process-daemon/npm/package.json | 3 +- .../process-daemon/script/npm-packages.mjs | 1 + packages/process-daemon/src/daemon/unix.rs | 15 +++++++++- patches/@ff-labs%2Ffff-bun@0.10.5.patch | 19 ++++++++++++ 10 files changed, 88 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release-process-daemon.yml b/.github/workflows/release-process-daemon.yml index 147f3ddf58b2..4b7574afe1b2 100644 --- a/.github/workflows/release-process-daemon.yml +++ b/.github/workflows/release-process-daemon.yml @@ -78,6 +78,10 @@ jobs: target: x86_64-apple-darwin - runner: macos-15 target: aarch64-apple-darwin + - runner: ubuntu-22.04 + target: aarch64-linux-android + android: true + api-level: "28" runs-on: ${{ matrix.runner }} env: MACOSX_DEPLOYMENT_TARGET: "11.0" @@ -95,15 +99,36 @@ jobs: - name: Install musl toolchain if: matrix.musl run: sudo apt-get update && sudo apt-get install -y musl-tools + - name: Configure Android NDK + if: matrix.android + run: | + ndk="${ANDROID_NDK_LATEST_HOME:-$ANDROID_NDK_HOME}" + test -n "$ndk" || { echo "Android NDK is not installed" >&2; exit 1; } + sysroot="$ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot" + lib="$sysroot/usr/lib/aarch64-linux-android/${{ matrix.api-level }}" + cat > "$RUNNER_TEMP/android-libc.txt" <> "$GITHUB_ENV" - name: Install Rust target run: rustup toolchain install 1.90.0 --profile minimal --target "${{ matrix.target }}" - name: Install glibc baseline linker if: endsWith(matrix.target, '-linux-gnu') run: cargo install cargo-zigbuild --locked --version "$CARGO_ZIGBUILD_VERSION" - name: Test service + if: matrix.android != true run: cargo test --locked --target "${{ matrix.target }}" --lib --test service --test ownership - name: Test CLI integration - if: runner.os != 'macOS' + if: runner.os != 'macOS' && matrix.android != true run: cargo test --locked --target "${{ matrix.target }}" --test playground - name: Build run: | @@ -120,6 +145,8 @@ jobs: echo "Highest required glibc version: $required" test "$(printf '%s\n' "$required" "$GNU_GLIBC_VERSION" | sort -V | tail -n 1)" = "$GNU_GLIBC_VERSION" - name: Smoke test + # Android binaries only run on a device or emulator. + if: matrix.android != true run: | version="$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name == "opencode-pty") | .version')" binary="target/${{ matrix.target }}/release/opencode-pty" diff --git a/packages/cli/script/build.ts b/packages/cli/script/build.ts index 80a67fe75589..1539c8e2cb8c 100755 --- a/packages/cli/script/build.ts +++ b/packages/cli/script/build.ts @@ -49,6 +49,7 @@ const allTargets: { { os: "linux", arch: "arm64", abi: "musl" }, { os: "linux", arch: "x64", abi: "musl" }, { os: "linux", arch: "x64", abi: "musl", avx2: false }, + { os: "android", arch: "arm64" }, { os: "darwin", arch: "arm64" }, { os: "darwin", arch: "x64" }, { os: "darwin", arch: "x64", avx2: false }, @@ -180,7 +181,7 @@ export default { path: file, version: ${JSON.stringify(opencodePty.version)}, sh autoloadDotenv: false, autoloadTsconfig: true, autoloadPackageJson: true, - target: target.replace(binary, "bun") as Bun.Build.CompileTarget, + target: compileTarget(item) as Bun.Build.CompileTarget, ...(executablePath ? { executablePath } : {}), outfile: path.join(outdir, name, "bin", binary), execArgv: [ @@ -234,12 +235,13 @@ async function compileExecutable(item: (typeof allTargets)[number]) { const release = process.env.BUN_COMPILE_RELEASE if (!release) return - const platform = item.os === "win32" ? "windows" : item.os + const platform = item.os === "win32" ? "windows" : item.os === "android" ? "linux" : item.os const name = [ "bun", platform, item.arch === "arm64" ? "aarch64" : item.arch, item.abi, + item.os === "android" ? "android" : undefined, item.avx2 === false ? "baseline" : undefined, ] .filter(Boolean) @@ -330,3 +332,9 @@ function targetName(item: (typeof allTargets)[number]) { .filter(Boolean) .join("-") } + +// Bun names the Android target as a Linux one with an Android suffix, unlike the npm package name. +function compileTarget(item: (typeof allTargets)[number]) { + if (item.os === "android") return "bun-linux-arm64-android" + return targetName(item).replace(binary, "bun") +} diff --git a/packages/cli/script/opencode-pty.ts b/packages/cli/script/opencode-pty.ts index a40070c1c40c..f85449968792 100644 --- a/packages/cli/script/opencode-pty.ts +++ b/packages/cli/script/opencode-pty.ts @@ -19,7 +19,7 @@ const core = createRequire(path.resolve(import.meta.dirname, "../../core/package const pty = createRequire(core.resolve("@opencode/pty/package.json")) export async function resolveOpencodePty(target: Target): Promise { - if (target.platform !== "darwin" && target.platform !== "linux" && target.platform !== "win32") return undefined + if (!["darwin", "linux", "win32", "android"].includes(target.platform)) return undefined if (target.arch !== "arm64" && target.arch !== "x64") return undefined const suffix = [target.platform, target.arch, target.platform === "linux" ? (target.libc ?? "glibc") : undefined] diff --git a/packages/cli/script/pack.ts b/packages/cli/script/pack.ts index b66cfb2e3490..56837e9c4558 100755 --- a/packages/cli/script/pack.ts +++ b/packages/cli/script/pack.ts @@ -82,7 +82,7 @@ async function packDistribution(input: { version, license: pkg.license, repository: { type: "git", url: "git+https://github.com/anomalyco/opencode.git" }, - os: ["darwin", "linux", "win32"], + os: ["darwin", "linux", "win32", "android"], cpu: ["arm64", "x64"], optionalDependencies: binaries, }, diff --git a/packages/process-daemon/build.rs b/packages/process-daemon/build.rs index 1dbf8428f370..f2d9a1ce55ec 100644 --- a/packages/process-daemon/build.rs +++ b/packages/process-daemon/build.rs @@ -104,7 +104,9 @@ fn main() { } build.arg("--prefix").arg(&install); build.arg("--cache-dir").arg(out.join("zig-cache")); - if env::var("HOST").as_deref() != Ok(target.as_str()) { + // Android builds always pin the target: the host is Android on Termux, but the + // API level must stay explicit so Zig does not consult getprop. + if env::var("HOST").as_deref() != Ok(target.as_str()) || target == "aarch64-linux-android" { let zig_target = match target.as_str() { "x86_64-unknown-linux-gnu" => "x86_64-linux-gnu", "aarch64-unknown-linux-gnu" => "aarch64-linux-gnu", @@ -114,6 +116,8 @@ fn main() { "aarch64-apple-darwin" => "aarch64-macos", "x86_64-pc-windows-msvc" => "x86_64-windows-msvc", "aarch64-pc-windows-msvc" => "aarch64-windows-msvc", + // Pin the API level so Zig does not shell out to getprop, matching Bun's Android target. + "aarch64-linux-android" => "aarch64-linux-android.28", _ => panic!("unsupported Ghostty target: {target}"), }; build.arg(format!("-Dtarget={zig_target}")); diff --git a/packages/process-daemon/npm/index.js b/packages/process-daemon/npm/index.js index d645177f75ea..b244c4f00de2 100644 --- a/packages/process-daemon/npm/index.js +++ b/packages/process-daemon/npm/index.js @@ -2,9 +2,14 @@ import { createRequire } from "node:module" import path from "node:path" const require = createRequire(import.meta.url) -const supported = process.platform === "darwin" || process.platform === "linux" || process.platform === "win32" +const supported = + process.platform === "darwin" || + process.platform === "linux" || + process.platform === "win32" || + process.platform === "android" const arch = process.arch === "arm64" || process.arch === "x64" ? process.arch : undefined -const libc = process.platform === "linux" ? (process.report.getReport().header.glibcVersionRuntime ? "gnu" : "musl") : "" +const libc = + process.platform === "linux" ? (process.report.getReport().header.glibcVersionRuntime ? "gnu" : "musl") : "" const suffix = [process.platform, arch, libc].filter(Boolean).join("-") export const binaryPath = diff --git a/packages/process-daemon/npm/package.json b/packages/process-daemon/npm/package.json index 8e851b1dcc0e..7d7d3cc09090 100644 --- a/packages/process-daemon/npm/package.json +++ b/packages/process-daemon/npm/package.json @@ -32,7 +32,8 @@ "@opencode/pty-linux-x64-gnu": "0.4.0", "@opencode/pty-linux-x64-musl": "0.4.0", "@opencode/pty-win32-arm64": "0.4.0", - "@opencode/pty-win32-x64": "0.4.0" + "@opencode/pty-win32-x64": "0.4.0", + "@opencode/pty-android-arm64": "0.4.0" }, "publishConfig": { "access": "public" diff --git a/packages/process-daemon/script/npm-packages.mjs b/packages/process-daemon/script/npm-packages.mjs index 5f37f4a4366b..cb68abab216c 100644 --- a/packages/process-daemon/script/npm-packages.mjs +++ b/packages/process-daemon/script/npm-packages.mjs @@ -19,6 +19,7 @@ const targets = [ { target: "x86_64-unknown-linux-musl", platform: "linux", arch: "x64", libc: "musl", suffix: "musl" }, { target: "aarch64-pc-windows-msvc", platform: "win32", arch: "arm64" }, { target: "x86_64-pc-windows-msvc", platform: "win32", arch: "x64" }, + { target: "aarch64-linux-android", platform: "android", arch: "arm64" }, ] await rm(output, { recursive: true, force: true }) diff --git a/packages/process-daemon/src/daemon/unix.rs b/packages/process-daemon/src/daemon/unix.rs index 8867ea7fb964..e4bc2078ba35 100644 --- a/packages/process-daemon/src/daemon/unix.rs +++ b/packages/process-daemon/src/daemon/unix.rs @@ -173,7 +173,7 @@ fn socket_root() -> Result { fn resolve_socket_root(directory: Option, uid: u32) -> Result { let base = match directory { - None => PathBuf::from("/tmp"), + None => default_socket_base(), Some(directory) if !directory.is_absolute() => { bail!("OPENCODE_PTY_SOCKET_DIR must be an absolute path") } @@ -191,6 +191,19 @@ fn resolve_socket_root(directory: Option, uid: u32) -> Result Ok(base.join(format!("opencode-pty-{uid}"))) } +fn default_socket_base() -> PathBuf { + // Android runtimes do not provide a writable /tmp; TMPDIR points at the app's + // own temporary directory (Termux sets it to $PREFIX/tmp). + #[cfg(target_os = "android")] + { + std::env::temp_dir() + } + #[cfg(not(target_os = "android"))] + { + PathBuf::from("/tmp") + } +} + fn socket_file(root: &Path, canonical_directory: &Path) -> PathBuf { let digest = Sha256::digest(canonical_directory.as_os_str().as_bytes()); let name = digest[..16] diff --git a/patches/@ff-labs%2Ffff-bun@0.10.5.patch b/patches/@ff-labs%2Ffff-bun@0.10.5.patch index f370ce39da57..fc763b662a21 100644 --- a/patches/@ff-labs%2Ffff-bun@0.10.5.patch +++ b/patches/@ff-labs%2Ffff-bun@0.10.5.patch @@ -88,3 +88,22 @@ index 3b54b82d03bd5d37f3665d59c91273dc62118c1b..17bf456ea78d6b1f432d514f12eb430c getTriple, -} from "./platform"; +} from "./platform.js"; + +diff --git a/src/embedded.ts b/src/embedded.ts +--- a/src/embedded.ts ++++ b/src/embedded.ts +@@ -46,6 +46,14 @@ + ); + } + ++ if (process.platform === "android") { ++ return importFile( ++ import(`@ff-labs/fff-bin-android-arm64/libfff_c.so`, { ++ with: { type: "file" }, ++ }), ++ ); ++ } ++ + return null; + } +