#!/usr/bin/env bash # # Reproduce the bundled `monero-wallet-rpc` for Android (arm64-v8a). # # This is the command behind `android/app/src/main/jniLibs/arm64-v8a/ # libmonerowalletrpc.so` and `android/MONERO_BINARIES.md`. It is deliberately # NOT part of the CI path: building Monero's C++ tree for Android is a # ~30-45 min job on 12 cores and CI here runs on an arm64 Raspberry Pi. CI # re-verifies the committed blob's checksum instead (see # `scripts/build-android-local.sh`); this script is how that blob is produced # and how a reviewer re-derives it. # # Monero ships no Android binaries and no Android CMake preset, so this uses # upstream's own cross-compile path: `contrib/depends` (which builds boost, # openssl, libunbound, libzmq, libsodium, ncurses and readline for the target) # plus the toolchain.cmake it emits. # # Usage: # scripts/build-monero-wallet-rpc-android.sh [workdir] # # Output: # /monero-src/build/aarch64-linux-android/release/bin/monero-wallet-rpc # # Requirements (all host-side): git, make, cmake 3.2x (NOT 4.x — see below), # unzip, curl, python3, a JDK-free C/C++ host toolchain (gcc/clang), ~15 GB free. set -euo pipefail MONERO_TAG="${MONERO_TAG:-v0.18.4.4}" WORKDIR="${1:-${HOME}/.hermes/tmp/monero-src}" JOBS="${JOBS:-$(nproc)}" # NDK r17b is what `contrib/depends/packages/android_ndk.mk` pins; it downloads # and unpacks it itself. Nothing here uses the Gradle-side NDK. NDK_ABI_DIR="aarch64-linux-android" root="$(cd "$(dirname "$0")/.." && pwd)" cd "$root" echo "==> monero $MONERO_TAG -> $WORKDIR ($JOBS jobs)" # --------------------------------------------------------------------------- # 0. cmake 3.x. CMake 4.x removed the standalone-NDK-toolchain handling that # `contrib/depends`' generated toolchain.cmake relies on; with 4.x the # configure step picks the HOST clang and dies on `--target=` link flags. # --------------------------------------------------------------------------- CMAKE_BIN="${CMAKE_BIN:-cmake}" if ! "$CMAKE_BIN" --version | head -1 | grep -qE 'version 3\.'; then echo "==> host cmake is not 3.x; installing 3.22.6 into $WORKDIR/../cmakevenv" python3 -m venv "$WORKDIR/../cmakevenv" >/dev/null 2>&1 || true "$WORKDIR/../cmakevenv/bin/pip" install -q "cmake==3.22.6" CMAKE_BIN="$WORKDIR/../cmakevenv/bin/cmake" fi "$CMAKE_BIN" --version | head -1 # --------------------------------------------------------------------------- # 1. Source, at the tag. Submodules are mandatory: the configure step refuses # to run without external/miniupnp (and randomx/rapidjson/supercop build). # --------------------------------------------------------------------------- if [ ! -d "$WORKDIR/.git" ]; then git clone --depth 1 --branch "$MONERO_TAG" \ https://github.com/monero-project/monero.git "$WORKDIR" fi git -C "$WORKDIR" submodule update --init --force --depth 1 # --------------------------------------------------------------------------- # 2. Host quirk: the NDK r17b standalone toolchain's prebuilt `clang60` is # linked against ncurses 5 (`libtinfo.so.5`). Debian bookworm, Arch and # everything else current ships ncurses 6 only, so the compiler refuses to # start with "error while loading shared libraries: libtinfo.so.5". ncurses # 6 is ABI-compatible for what clang uses terminfo for; a shim directory is # enough, and it is scoped to this build via LD_LIBRARY_PATH. # --------------------------------------------------------------------------- compat="$WORKDIR/../ndk17-compat" mkdir -p "$compat" if [ ! -e "$compat/libtinfo.so.5" ]; then for cand in /usr/lib/libtinfo.so.6 \ /usr/lib/x86_64-linux-gnu/libtinfo.so.6 \ /usr/lib64/libtinfo.so.6 \ /lib/x86_64-linux-gnu/libtinfo.so.6; do if [ -e "$cand" ]; then ln -sf "$cand" "$compat/libtinfo.so.5" break fi done fi if [ ! -e "$compat/libtinfo.so.5" ]; then echo "!! no libtinfo.so.6 found to shim libtinfo.so.5; NDK r17b clang will not run" >&2 exit 1 fi export LD_LIBRARY_PATH="$compat${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" # The card's rule for this host: never build C in /tmp (16 GB tmpfs with a # per-user quota — "Disk quota exceeded" mid-build looks like a protocol bug). export TMPDIR="${TMPDIR:-$WORKDIR/../monero-build-tmp}" mkdir -p "$TMPDIR" # --------------------------------------------------------------------------- # 3. The dependencies, cross-built for the target. ~20 min. # --------------------------------------------------------------------------- make -C "$WORKDIR/contrib/depends" HOST="$NDK_ABI_DIR" -j"$JOBS" # --------------------------------------------------------------------------- # 3b. The toolchain.cmake depends emits sets # SET(CMAKE_FIND_ROOT_PATH /usr) # and /usr is the HOST. `CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY` does not # save you: /usr is a root, so `find_library()` happily returns host # libraries. That bites twice in this tree: # * monero's optional zmq extras (pgm, norm, gssapi_krb5, protolib, bsd, # md, protokit) get host paths appended to ZMQ_LIB, and the final link # dies with `/usr/lib/libpgm.so: error adding symbols: File in wrong # format` — a host x86-64 .so in an aarch64 link line; # * `find_package(HIDAPI)` succeeds against the host hidapi, defines # HAVE_HIDAPI, and src/device then fails to compile `hidapi/hidapi.h`. # Nothing under /usr is a valid Android target library, so drop it. This is # also what upstream's `make depends target=aarch64-linux-android` silently # relies on: it only works on a build host that happens to have none of # these installed. # --------------------------------------------------------------------------- prefix="$WORKDIR/contrib/depends/$NDK_ABI_DIR" tc="$prefix/share/toolchain.cmake" cp "$tc" "$tc.orig" sed -i -E "s|^SET\(CMAKE_FIND_ROOT_PATH .*|SET(CMAKE_FIND_ROOT_PATH ${prefix})|" "$tc" grep -qF "SET(CMAKE_FIND_ROOT_PATH ${prefix})" "$tc" \ || { echo "could not pin CMAKE_FIND_ROOT_PATH in $tc" >&2; exit 1; } # --------------------------------------------------------------------------- # 4. Configure with the toolchain depends just emitted, then build ONLY the # wallet RPC. The CMake target is `wallet_rpc_server`; the binary it emits is # `monero-wallet-rpc`. (`make monero-wallet-rpc` is not a target and fails # with "No rule to make target".) # --------------------------------------------------------------------------- builddir="$WORKDIR/build/$NDK_ABI_DIR/release" rm -rf "$builddir" mkdir -p "$builddir" ( cd "$builddir" # `-U` on the optional-library cache entries is belt-and-braces for a reused # build dir: a cache written before the toolchain patch still holds the host # paths, and a stale cache is invisible until the link step. "$CMAKE_BIN" \ -DCMAKE_TOOLCHAIN_FILE="$tc" \ -DBUILD_TESTS=OFF \ -DBUILD_DOCUMENTATION=OFF \ -DCMAKE_BUILD_TYPE=Release \ -U PGM_LIBRARY -U NORM_LIBRARY -U GSSAPI_LIBRARY -U PROTOLIB_LIBRARY \ -U SODIUM_LIBRARY -U BSD_LIBRARY -U MD_LIBRARY -U PROTOKIT_LIBRARY \ -U ZMQ_LIB -U ZMQ_INCLUDE_PATH -U HIDAPI_LIBRARY -U HIDAPI_INCLUDE_DIR \ "$WORKDIR" make wallet_rpc_server -j"$JOBS" ) bin="$builddir/bin/monero-wallet-rpc" [ -s "$bin" ] || { echo "missing $bin" >&2; exit 1; } # --------------------------------------------------------------------------- # 5. Strip with the target's own strip, then verify the shape. # --------------------------------------------------------------------------- strip_bin="$WORKDIR/contrib/depends/$NDK_ABI_DIR/native/bin/${NDK_ABI_DIR}-strip" [ -x "$strip_bin" ] && "$strip_bin" "$bin" || true echo "==> $bin" file "$bin" wc -c "$bin" sha256sum "$bin" echo echo "==> to package it into the APK:" echo " cp '$bin' android/app/src/main/jniLibs/arm64-v8a/libmonerowalletrpc.so" echo " # then update android/monero-binaries.sha256 and android/MONERO_BINARIES.md" echo echo "NOTE: a Monero build is not bit-reproducible (build paths and timestamps" echo " land in the binary), so a fresh run will NOT match the committed" echo " sha256. That hash is a provenance record of the shipped blob and the" echo " thing CI verifies — not a reproducible-build assertion."