# SPDX-License-Identifier: AGPL-3.0-or-later # # Set up the Android NDK toolchain for one Rust target. Source it from bash # (macOS's bash 3.2 is fine) or zsh: # # . scripts/android-env.sh aarch64-linux-android # phones # . scripts/android-env.sh armv7-linux-androideabi # Android TV # # It mirrors mach's AndroidTarget.configure_build_environment # (source/python/servo/platform/build_target.py), so the C and C++ parts of the # build (mozjs, jemalloc, aws-lc, freetype, ...) find what they expect. # # Optional inputs: # ANDROID_SDK_ROOT or ANDROID_HOME the SDK, otherwise the usual locations # ANDROID_NDK_ROOT an NDK r29, otherwise the newest $SDK/ndk/29.* # # The API level comes from packaging/android/gradle/libs.versions.toml # (android-sdk-min), the same value as the APK's minSdk. # # Sourcing it twice in one shell for two targets is fine: it sets every # per-target variable instead of appending to it. _beaver_android_env() { local triple="$1" local root root="$(cd "$(dirname "${BASH_SOURCE[0]:-${(%):-%x}}")/.." && pwd)" || return 1 local clang_target sysroot_lib abi case "$triple" in aarch64-linux-android) clang_target=aarch64-linux-android sysroot_lib=aarch64-linux-android abi=arm64-v8a ;; armv7-linux-androideabi) clang_target=armv7a-linux-androideabi sysroot_lib=arm-linux-androideabi abi=armeabi-v7a ;; *) echo "android-env: usage: . scripts/android-env.sh aarch64-linux-android|armv7-linux-androideabi" >&2 return 1 ;; esac local catalog="$root/packaging/android/gradle/libs.versions.toml" local api api="$(sed -n 's/^android-sdk-min *= *"\([0-9][0-9]*\)".*/\1/p' "$catalog" 2>/dev/null)" if [ -z "$api" ]; then echo "android-env: no android-sdk-min in $catalog" >&2 return 1 fi local sdk="${ANDROID_SDK_ROOT:-${ANDROID_HOME:-}}" if [ -z "$sdk" ]; then # Android Studio's defaults on macOS and Linux, then the Homebrew # android-commandlinetools cask (Apple Silicon and Intel) and Arch's package. local candidate for candidate in "$HOME/Library/Android/sdk" "$HOME/Android/Sdk" \ /opt/homebrew/share/android-commandlinetools \ /usr/local/share/android-commandlinetools /opt/android-sdk; do if [ -d "$candidate/ndk" ] || [ -d "$candidate/platform-tools" ]; then sdk="$candidate" break fi done fi if [ -z "$sdk" ] || [ ! -d "$sdk" ]; then echo "android-env: no Android SDK found; set ANDROID_HOME" >&2 return 1 fi local ndk="${ANDROID_NDK_ROOT:-}" if [ -z "$ndk" ] && [ -d "$sdk/ndk" ]; then local newest newest="$(find "$sdk/ndk" -mindepth 1 -maxdepth 1 -name '29.*' -exec basename {} \; | sort -t. -k1,1n -k2,2n -k3,3n | tail -n 1)" [ -n "$newest" ] && ndk="$sdk/ndk/$newest" fi if [ -z "$ndk" ] || [ ! -f "$ndk/source.properties" ]; then echo "android-env: no NDK r29 found in $sdk/ndk; install one with sdkmanager or set ANDROID_NDK_ROOT" >&2 return 1 fi if ! grep -q '^Pkg\.Revision *= *29\.' "$ndk/source.properties"; then echo "android-env: $ndk is not an NDK r29, which Servo requires" >&2 return 1 fi # Host tag, as mach picks it: x86_64 hosts have their own prebuilt; other # CPUs use the only one the NDK ships for the OS. On Apple Silicon that is # darwin-x86_64, a universal build. There is no Linux arm64 NDK. local os host_tag os="$(uname -s | tr '[:upper:]' '[:lower:]')" case "$os" in darwin | linux) ;; *) echo "android-env: Android builds need a macOS or Linux host" >&2 return 1 ;; esac local prebuilt="$ndk/toolchains/llvm/prebuilt" case "$(uname -m)" in x86_64 | amd64) host_tag="$os-x86_64" ;; *) local found found="$(find "$prebuilt" -mindepth 1 -maxdepth 1 -name "$os-*" -exec basename {} \;)" if [ -z "$found" ] || [ "$(echo "$found" | wc -l | tr -d ' ')" != 1 ]; then echo "android-env: can't pick an NDK prebuilt for $os on $(uname -m) in $prebuilt" >&2 return 1 fi host_tag="$found" ;; esac local toolchain="$prebuilt/$host_tag" local bin="$toolchain/bin" if [ ! -x "$bin/clang" ]; then echo "android-env: no clang in $bin" >&2 return 1 fi local host_cc="${HOST_CC:-$(command -v clang)}" local host_cxx="${HOST_CXX:-$(command -v clang++)}" if [ -z "$host_cc" ] || [ -z "$host_cxx" ]; then echo "android-env: the host needs clang and clang++ on PATH (or HOST_CC and HOST_CXX)" >&2 return 1 fi export ANDROID_SDK_ROOT="$sdk" export ANDROID_HOME="$sdk" export ANDROID_NDK_ROOT="$ndk" # mozjs reads these two. export ANDROID_NDK_HOME="$ndk" export ANDROID_API_LEVEL="$api" # Appended, as mach does: prepending would hand the NDK's clang to the host # build scripts. case ":$PATH:" in *":$bin:"*) ;; *) export PATH="$PATH:$bin" ;; esac export RUST_TARGET="$triple" export HOST_CC="$host_cc" export HOST_CXX="$host_cxx" export HOST_CFLAGS="" export HOST_CXXFLAGS="" export TARGET_CC="$bin/clang" export TARGET_CPP="$bin/clang -E" export TARGET_CXX="$bin/clang++" export TARGET_AR="$bin/llvm-ar" export TARGET_RANLIB="$bin/llvm-ranlib" export TARGET_OBJCOPY="$bin/llvm-objcopy" export TARGET_YASM="$bin/yasm" export TARGET_STRIP="$bin/llvm-strip" # cc-rs gives clang rustc's triple for armv7, arm-none-linux-android, which # clang reads as ARMv4T. These flags come after cc-rs's own and set the # real architecture, so keep them. export TARGET_CFLAGS="--target=$clang_target$api" export TARGET_CXXFLAGS="--target=$clang_target$api" export TARGET_PKG_CONFIG_SYSROOT_DIR="$toolchain/sysroot" # tikv-jemalloc-sys ignores TARGET_AR: on macOS it would fall back to the # host's ar and lose the jemalloc symbols. export AR="$bin/llvm-ar" export RANLIB="$bin/llvm-ranlib" export RUST_FONTCONFIG_DLOPEN=on export LIBCLANG_PATH="$toolchain/lib" export CLANG_PATH="$bin/clang" export BINDGEN_EXTRA_CLANG_ARGS="--target=$clang_target$api --sysroot=$toolchain/sysroot" # The linker follows the API level above. This overrides the API-30 # wrappers named in .cargo/config.toml. local linker_var linker_var="CARGO_TARGET_$(echo "$triple" | tr '[:lower:]-' '[:upper:]_')_LINKER" export "$linker_var=$bin/$clang_target$api-clang" # A cross-compiled jemalloc assumes the build host's 4 KiB pages and aborts # on 16 KiB-page kernels (Android 15+ on arm64). A page size larger than the # kernel's is fine. if [ "$triple" = aarch64-linux-android ]; then export JEMALLOC_SYS_WITH_LG_PAGE=14 else unset JEMALLOC_SYS_WITH_LG_PAGE fi # mozjs's prebuilt armv7 archive has its C++ glue (libjsglue.a, libjsapi.a) # built for ARMv4T, which calls libgcc atomics the NDK no longer has. So # SpiderMonkey and its glue are built from source there; arm64 keeps the # prebuilt archive. if [ "$triple" = armv7-linux-androideabi ]; then export MOZJS_FROM_SOURCE=1 else unset MOZJS_FROM_SOURCE fi # For scripts/build-android.sh, which stages the libraries per ABI. export BEAVER_ANDROID_ABI="$abi" export BEAVER_NDK_LIBCXX="$toolchain/sysroot/usr/lib/$sysroot_lib/libc++_shared.so" echo "android-env: $triple ($abi, API $api), NDK $(basename "$ndk") ($host_tag)" } if _beaver_android_env "$@"; then _beaver_android_env_result=0 else _beaver_android_env_result=1 fi unset -f _beaver_android_env return "$_beaver_android_env_result"