diff --git a/.gitignore b/.gitignore index becaffb..dcdaf7d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ __debug_bin* /third_party/libeuicc/ /docs/ -.o +/.mygo/ diff --git a/mygo.json b/mygo.json index 88ef3b1..d400bd7 100644 --- a/mygo.json +++ b/mygo.json @@ -11,6 +11,10 @@ "dir": "locales", "source": "en" }, + "android": { + "minSdk": 26, + "versionCode": 1 + }, "linux": { "depends": { "deb": ["libpcsclite1", "pcscd"], diff --git a/scripts/android-version-code.sh b/scripts/android-version-code.sh new file mode 100755 index 0000000..0a77d5f --- /dev/null +++ b/scripts/android-version-code.sh @@ -0,0 +1,55 @@ +#!/bin/sh +# Prints Android's versionCode of a version of scripts/version.sh, which +# grows as the version does: +# +# (major*10000 + minor*100 + patch) * 10000 + slot +# +# and the slot, of 10000 for each x.y.z: +# +# alpha.N, beta.N, rc.N 0, 2000, 4000 + N*100 + snapshot (N up to 19) +# x.y.z itself 6000 +# x.y.z.rN.g... 6000 + N +# +# Snapshots past a slot's room (99 commits after a pre-release, 3999 after +# a release) share its last code, which still installs over itself. +set -eu + +printf '%s\n' "${1:?usage: android-version-code.sh }" | awk ' +function fail(why) { + printf "android-version-code.sh: %s: %s\n", orig, why > "/dev/stderr" + exit 1 +} +{ + orig = v = $0 + sub(/\.dirty$/, "", v) + r = 0 + if (match(v, /\.r[0-9]+\.g[0-9a-f]+$/)) { + s = substr(v, RSTART + 2) + sub(/\..*/, "", s) + r = s + 0 + v = substr(v, 1, RSTART - 1) + } + pre = "" + if (i = index(v, "-")) { + pre = substr(v, i + 1) + v = substr(v, 1, i - 1) + } + if (v !~ /^[0-9]+\.[0-9]+\.[0-9]+$/) fail("not x.y.z[-pre][.rN.gHASH]") + split(v, p, ".") + if (p[1] > 20 || p[2] > 99 || p[3] > 99) fail("too large for a versionCode") + if (pre == "") { + slot = 6000 + (r > 3999 ? 3999 : r) + } else { + if (pre ~ /^alpha(\.?[0-9]+)?$/) slot = 0 + else if (pre ~ /^beta(\.?[0-9]+)?$/) slot = 2000 + else if (pre ~ /^rc(\.?[0-9]+)?$/) slot = 4000 + else fail("pre-releases are alpha.N, beta.N or rc.N") + n = pre + gsub(/[^0-9]/, "", n) + if (n + 0 > 19) fail("pre-release number over 19") + slot += (n + 0) * 100 + (r > 99 ? 99 : r) + } + code = (p[1] * 10000 + p[2] * 100 + p[3]) * 10000 + slot + if (code > 2100000000) fail("too large for a versionCode") + printf "%d\n", (code > 0 ? code : 1) +}' diff --git a/scripts/build-android.sh b/scripts/build-android.sh new file mode 100755 index 0000000..9ab43cd --- /dev/null +++ b/scripts/build-android.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# Builds the Android APK and AAB (arm64): libeuicc with the NDK, then the +# app with MyGo and Gradle. Extra arguments go to `mygo build`. +# +# ./scripts/build-android.sh # build/android/ +# +# Needs the Android SDK (Platform 36, Build-Tools 35.0.0, an NDK) and a +# JDK 17, which MyGo's Gradle build asks for: Android Studio's own JDK is +# too new for its Gradle (8.14, which runs on Java 24 at most). The JDK is +# JAVA_HOME if that is a 17, or else the system's or Homebrew's. +set -euo pipefail +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +cd "$ROOT" + +is_jdk17() { + [ -x "$1/bin/java" ] || return 1 + local out + out="$("$1/bin/java" -version 2>&1)" || return 1 + [[ "$out" == *'version "17'* ]] +} + +JDK= +for h in "${JAVA_HOME:-}" \ + /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home \ + /usr/local/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home \ + /Library/Java/JavaVirtualMachines/*17*/Contents/Home \ + /usr/lib/jvm/*17*; do + if [ -n "$h" ] && is_jdk17 "$h"; then + JDK="$h" + break + fi +done +if [ -z "$JDK" ]; then + echo "build-android.sh: no JDK 17: install one, or set JAVA_HOME to it" >&2 + exit 1 +fi +export JAVA_HOME="$JDK" + +if [ ! -f third_party/libeuicc/android-arm64/libeuicc.a ]; then + ./scripts/build-libeuicc.sh android/arm64 +fi +EASYLPAC_ANDROID=1 \ + ./scripts/with-version.sh go run github.com/egoist/mygo/cmd/mygo@v0.10.0 build -platform android/arm64 "$@" diff --git a/scripts/build-libeuicc.sh b/scripts/build-libeuicc.sh index 73f1e54..17f04d7 100755 --- a/scripts/build-libeuicc.sh +++ b/scripts/build-libeuicc.sh @@ -1,7 +1,8 @@ #!/bin/sh # Builds the static libeuicc (+cJSON) from the lpac submodule for # one target, with zig as the C compiler (scripts/zcc), so that any host -# builds for every platform. +# builds for every platform. Android is built with the NDK's clang, which +# mygo links Go with: the NDK of ANDROID_NDK_HOME, or the SDK's newest. # # Usage: scripts/build-libeuicc.sh [GOOS/GOARCH] (default: the host's) # @@ -27,6 +28,7 @@ case "$GOOS_T" in darwin) SYSTEM=Darwin ;; linux) SYSTEM=Linux ;; windows) SYSTEM=Windows ;; +android) SYSTEM=Android ;; *) echo "build-libeuicc: unsupported GOOS $GOOS_T" >&2; exit 1 ;; esac @@ -37,6 +39,37 @@ esac # objects are position-independent, as the Go programs that link them # may be PIE (always so on macOS; Windows has no PIC flag). export GOOS="$GOOS_T" GOARCH="$GOARCH_T" +if [ "$GOOS_T" = android ]; then + case "$GOARCH_T" in + arm64) ABI=arm64-v8a ;; + arm) ABI=armeabi-v7a ;; + amd64) ABI=x86_64 ;; + *) echo "build-libeuicc: unsupported Android GOARCH $GOARCH_T" >&2; exit 1 ;; + esac + NDK="${ANDROID_NDK_HOME:-}" + if [ -z "$NDK" ]; then + for sdk in "${ANDROID_HOME:-}" "$HOME/Library/Android/sdk" "$HOME/Android/Sdk"; do + if [ -n "$sdk" ] && [ -d "$sdk/ndk" ]; then + NDK="$sdk/ndk/$(ls "$sdk/ndk" | sort -V | tail -n 1)" + break + fi + done + fi + if [ ! -f "$NDK/build/cmake/android.toolchain.cmake" ]; then + echo "build-libeuicc: no Android NDK: install one, or set ANDROID_NDK_HOME" >&2 + exit 1 + fi + for AR in "$NDK"/toolchains/llvm/prebuilt/*/bin/llvm-ar; do break; done + # API 26 is mygo.json's android.minSdk. + set -- -DCMAKE_TOOLCHAIN_FILE="$NDK/build/cmake/android.toolchain.cmake" \ + -DANDROID_ABI="$ABI" -DANDROID_PLATFORM=android-26 +else + AR="$ROOT/scripts/zar" + set -- -DCMAKE_SYSTEM_NAME="$SYSTEM" \ + -DCMAKE_C_COMPILER="$ROOT/scripts/zcc" \ + -DCMAKE_AR="$AR" \ + -DCMAKE_RANLIB="$ROOT/scripts/zranlib" +fi cmake -S "$SRC" -B "$BUILD" \ -DLPAC_BUILD_LIBEUICC_ONLY=ON \ -DLPAC_DYNAMIC_LIBEUICC=OFF \ @@ -44,16 +77,13 @@ cmake -S "$SRC" -B "$BUILD" \ -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF \ -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE:-Release}" \ - -DCMAKE_SYSTEM_NAME="$SYSTEM" \ - -DCMAKE_C_COMPILER="$ROOT/scripts/zcc" \ - -DCMAKE_AR="$ROOT/scripts/zar" \ - -DCMAKE_RANLIB="$ROOT/scripts/zranlib" + "$@" cmake --build "$BUILD" --parallel mkdir -p "$OUT" cp "$BUILD/euicc/libeuicc.a" "$OUT/libeuicc.a" cp "$BUILD/_deps/cjson-build/libcjson.a" "$OUT/libcjson.a" rm -f "$OUT/libcjsonext.a" -zig ar rcs "$OUT/libcjsonext.a" "$(find "$BUILD/cjson-ext" -name 'cJSON_ex.c.o' -o -name 'cJSON_ex.c.obj' | head -n 1)" +"$AR" rcs "$OUT/libcjsonext.a" "$(find "$BUILD/cjson-ext" -name 'cJSON_ex.c.o' -o -name 'cJSON_ex.c.obj' | head -n 1)" echo "libeuicc for $PLATFORM:" ls -l "$OUT" diff --git a/scripts/with-version.sh b/scripts/with-version.sh index 524f729..bf29988 100755 --- a/scripts/with-version.sh +++ b/scripts/with-version.sh @@ -26,4 +26,17 @@ if ! grep -qxF " \"version\": \"$VERSION\"," "$CONFIG"; then fi echo "with-version.sh: version $VERSION" >&2 +# Android's versionCode, which mygo would take wrongly from a snapshot +# (0.9.2.r12.g... as 900): scripts/build-android.sh asks for it. +if [ -n "${EASYLPAC_ANDROID:-}" ]; then + CODE=$("$ROOT/scripts/android-version-code.sh" "$VERSION") + VERSIONED=$(sed -E "s/^( \"versionCode\": )[0-9]+/\\1$CODE/" "$CONFIG") + printf '%s\n' "$VERSIONED" >"$CONFIG" + if ! grep -Eq "^ \"versionCode\": $CODE,?\$" "$CONFIG"; then + echo "with-version.sh: no android.versionCode in mygo.json to set" >&2 + exit 1 + fi + echo "with-version.sh: versionCode $CODE" >&2 +fi + "$@"