| 6 | source "$SCRIPT_DIR/common.sh" |
| 7 | script_dir="$SCRIPT_DIR" |
| 8 | tlottie_dir=${TLOTTIE_SOURCE_DIR:-"$SCRIPT_DIR/../tlottie"} |
| 9 | tlottie_dir=$(CDPATH= cd -- "$tlottie_dir" && pwd) |
| 10 | manifest="$tlottie_dir/Cargo.toml" |
| 11 | profile=release-nostd |
| 12 | features=cpu,no-std,c-api |
| 13 |
| 14 | if [ ! -f "$manifest" ]; then |
| 15 | echo "error: tlottie Cargo.toml not found at $manifest" >&2 |
| 16 | echo "set TLOTTIE_SOURCE_DIR if the source is not next to tlottie_lib" >&2 |
| 17 | exit 1 |
| 18 | fi |
| 19 |
| 20 | rustup_toolchain= |
| 21 | if command -v rustup >/dev/null 2>&1; then |
| 22 | rustup_toolchain=${TLOTTIE_RUST_TOOLCHAIN:-stable} |
| 23 | fi |
| 24 |
| 25 | run_cargo() { |
| 26 | if [ -n "$rustup_toolchain" ]; then |
| 27 | rustup run "$rustup_toolchain" cargo "$@" |
| 28 | else |
| 29 | cargo "$@" |
| 30 | fi |
| 31 | } |
| 32 |
| 33 | run_rustc() { |
| 34 | if [ -n "$rustup_toolchain" ]; then |
| 35 | rustup run "$rustup_toolchain" rustc "$@" |
| 36 | else |
| 37 | rustc "$@" |
| 38 | fi |
| 39 | } |
| 40 |
| 41 | # Cargo otherwise resolves rustc through PATH. That can mix a rustup Cargo with |
| 42 | # a Homebrew/system rustc whose sysroot does not contain the checked targets. |
| 43 | if [ -n "$rustup_toolchain" ]; then |
| 44 | RUSTC=$(rustup which --toolchain "$rustup_toolchain" rustc) |
| 45 | else |
| 46 | RUSTC=$(command -v rustc) |
| 47 | fi |
| 48 | export RUSTC |
| 49 |
| 50 | if [ -n "$rustup_toolchain" ]; then |
| 51 | missing_targets= |
| 52 | for target in \ |
| 53 | aarch64-linux-android \ |
| 54 | armv7-linux-androideabi \ |
| 55 | i686-linux-android \ |
| 56 | x86_64-linux-android |
| 57 | do |
| 58 | if ! rustup target list --toolchain "$rustup_toolchain" --installed | grep -qx "$target"; then |
| 59 | missing_targets="$missing_targets $target" |
| 60 | fi |
| 61 | done |
| 62 | if [ -n "$missing_targets" ]; then |
| 63 | echo "error: Rust Android targets are not installed:$missing_targets" >&2 |
| 64 | echo "install them with: rustup target add --toolchain $rustup_toolchain$missing_targets" >&2 |
| 65 | exit 1 |
| 66 | fi |
| 67 | fi |
| 68 |
| 69 | target_dir=${CARGO_TARGET_DIR:-"$tlottie_dir/target/no-std-android"} |
| 70 | mkdir -p "$target_dir" |
| 71 | target_dir=$(CDPATH= cd -- "$target_dir" && pwd) |
| 72 | export CARGO_TARGET_DIR="$target_dir" |
| 73 |
| 74 | # rustc records source, dependency, sysroot, and OUT_DIR paths in object files. |
| 75 | # Give every machine the same virtual paths so those details do not make the |
| 76 | # resulting static archives depend on the checkout location or user account. |
| 77 | rust_sysroot=$(run_rustc --print sysroot) |
| 78 | cargo_home=${CARGO_HOME:-} |
| 79 | if [ -z "$cargo_home" ]; then |
| 80 | cargo_home=$(dirname -- "$(dirname -- "$(command -v cargo)")") |
| 81 | if [ -d "$cargo_home/registry" ]; then |
| 82 | : |
| 83 | elif [ -n "${HOME:-}" ] && [ -d "$HOME/.cargo" ]; then |
| 84 | cargo_home="$HOME/.cargo" |
| 85 | else |
| 86 | cargo_home= |
| 87 | fi |
| 88 | fi |
| 89 |
| 90 | # Do not inherit per-machine compiler flags. CARGO_ENCODED_RUSTFLAGS takes |
| 91 | # precedence over RUSTFLAGS and, unlike RUSTFLAGS, safely supports spaces in |
| 92 | # paths. Its separator is ASCII unit separator (0x1f). |
| 93 | unset RUSTFLAGS |
| 94 | CARGO_ENCODED_RUSTFLAGS= |
| 95 | append_rustflag() { |
| 96 | if [ -n "$CARGO_ENCODED_RUSTFLAGS" ]; then |
| 97 | CARGO_ENCODED_RUSTFLAGS="$CARGO_ENCODED_RUSTFLAGS$(printf '\037')" |
| 98 | fi |
| 99 | CARGO_ENCODED_RUSTFLAGS="$CARGO_ENCODED_RUSTFLAGS$1" |
| 100 | } |
| 101 | append_rustflag "--remap-path-prefix=$tlottie_dir=/tlottie" |
| 102 | append_rustflag "--remap-path-prefix=$target_dir=/tlottie-target" |
| 103 | append_rustflag "--remap-path-prefix=$rust_sysroot=/rust-sysroot" |
| 104 | append_rustflag "-Cdebuginfo=0" |
| 105 | if [ -n "$cargo_home" ]; then |
| 106 | cargo_home=$(CDPATH= cd -- "$cargo_home" && pwd) |
| 107 | append_rustflag "--remap-path-prefix=$cargo_home=/cargo-home" |
| 108 | fi |
| 109 | export CARGO_ENCODED_RUSTFLAGS |
| 110 | export CARGO_INCREMENTAL=0 |
| 111 | export SOURCE_DATE_EPOCH=1 |
| 112 | export TZ=UTC |
| 113 | export LC_ALL=C |
| 114 |
| 115 | echo "Building tlottie Android archives" |
| 116 | echo " source: $tlottie_dir" |
| 117 | echo " rustc: $(run_rustc --version)" |
| 118 | echo " profile: $profile" |
| 119 | echo " features: $features" |
| 120 | echo " target dir: $target_dir" |
| 121 |
| 122 | temporary_archive= |
| 123 | cleanup() { |
| 124 | if [ -n "$temporary_archive" ] && [ -f "$temporary_archive" ]; then |
| 125 | unlink "$temporary_archive" |
| 126 | fi |
| 127 | } |
| 128 | trap cleanup EXIT HUP INT TERM |
| 129 |
| 130 | for abi in $ABIS; do |
| 131 | case "$abi" in |
| 132 | arm64-v8a) rust_target=aarch64-linux-android ;; |
| 133 | armeabi-v7a) rust_target=armv7-linux-androideabi ;; |
| 134 | x86) rust_target=i686-linux-android ;; |
| 135 | x86_64) rust_target=x86_64-linux-android ;; |
| 136 | *) echo "error: unsupported ABI: $abi" >&2; exit 1 ;; |
| 137 | esac |
| 138 |
| 139 | destination="$(abi_output_dir "$abi")/libtlottie.a" |
| 140 |
| 141 | echo "Building $abi ($rust_target)" |
| 142 | run_cargo rustc \ |
| 143 | --manifest-path "$manifest" \ |
| 144 | --locked \ |
| 145 | --profile "$profile" \ |
| 146 | --target "$rust_target" \ |
| 147 | --lib \ |
| 148 | --no-default-features \ |
| 149 | --features "$features" \ |
| 150 | --crate-type staticlib \ |
| 151 | -- \ |
| 152 | -C metadata=tlottie-staticlib |
| 153 |
| 154 | artifact="$target_dir/$rust_target/$profile/libtlottie.a" |
| 155 | if [ ! -f "$artifact" ]; then |
| 156 | echo "error: Cargo did not emit $artifact" >&2 |
| 157 | exit 1 |
| 158 | fi |
| 159 |
| 160 | mkdir -p "$(dirname "$destination")" |
| 161 | temporary_archive=$(mktemp "$(dirname "$destination")/.libtlottie.a.XXXXXX") |
| 162 | cp "$artifact" "$temporary_archive" |
| 163 | chmod 0644 "$temporary_archive" |
| 164 | mv -f "$temporary_archive" "$destination" |
| 165 | temporary_archive= |
| 166 | finalize_archive "$destination" |
| 167 |
| 168 | bytes=$(wc -c < "$destination" | tr -d ' ') |
| 169 | echo "Wrote $destination ($bytes bytes)" |
| 170 | done |
| 171 |
| 172 | echo "All tlottie Android archives are ready." |
| 173 |