| 6 | # The toolchain itself (rustc, cargo, Android targets, build-essential) |
| 7 | # is already baked into the image at `docker build` time - see Dockerfile. |
| 8 |
| 9 | set -eu |
| 10 |
| 11 | tlottie_dir=/tlottie |
| 12 | manifest="$tlottie_dir/Cargo.toml" |
| 13 | profile=release-nostd |
| 14 | features=cpu,no-std,c-api |
| 15 | out_root=/work |
| 16 |
| 17 | if [ ! -f "$manifest" ]; then |
| 18 | echo "error: tlottie Cargo.toml not found at $manifest" >&2 |
| 19 | exit 1 |
| 20 | fi |
| 21 |
| 22 | # Keep cargo's target dir inside the container, not on the bind-mounted |
| 23 | # host repo - it's build-only scratch space and would otherwise leave a |
| 24 | # large, host-owned cache sitting in the checkout. |
| 25 | target_dir=/build/target |
| 26 | mkdir -p "$target_dir" |
| 27 | export CARGO_TARGET_DIR="$target_dir" |
| 28 |
| 29 | rust_sysroot=$(rustc --print sysroot) |
| 30 |
| 31 | # Do not inherit per-machine compiler flags. CARGO_ENCODED_RUSTFLAGS takes |
| 32 | # precedence over RUSTFLAGS and safely supports spaces in paths. |
| 33 | unset RUSTFLAGS |
| 34 | CARGO_ENCODED_RUSTFLAGS= |
| 35 | append_rustflag() { |
| 36 | if [ -n "$CARGO_ENCODED_RUSTFLAGS" ]; then |
| 37 | CARGO_ENCODED_RUSTFLAGS="$CARGO_ENCODED_RUSTFLAGS$(printf '\037')" |
| 38 | fi |
| 39 | CARGO_ENCODED_RUSTFLAGS="$CARGO_ENCODED_RUSTFLAGS$1" |
| 40 | } |
| 41 | append_rustflag "--remap-path-prefix=$tlottie_dir=/tlottie" |
| 42 | append_rustflag "--remap-path-prefix=$target_dir=/tlottie-target" |
| 43 | append_rustflag "--remap-path-prefix=$rust_sysroot=/rust-sysroot" |
| 44 | append_rustflag "--remap-path-prefix=$CARGO_HOME=/cargo-home" |
| 45 | export CARGO_ENCODED_RUSTFLAGS |
| 46 |
| 47 | echo "Building tlottie Android archives" |
| 48 | echo " source: $tlottie_dir" |
| 49 | echo " rustc: $(rustc --version)" |
| 50 | echo " profile: $profile" |
| 51 | echo " features: $features" |
| 52 |
| 53 | for target_spec in \ |
| 54 | arm64-v8a:aarch64-linux-android \ |
| 55 | armeabi-v7a:armv7-linux-androideabi \ |
| 56 | x86:i686-linux-android \ |
| 57 | x86_64:x86_64-linux-android |
| 58 | do |
| 59 | abi=${target_spec%%:*} |
| 60 | rust_target=${target_spec#*:} |
| 61 | destination="$out_root/$abi/libtlottie.a" |
| 62 |
| 63 | echo "Building $abi ($rust_target)" |
| 64 | cargo rustc \ |
| 65 | --manifest-path "$manifest" \ |
| 66 | --locked \ |
| 67 | --profile "$profile" \ |
| 68 | --target "$rust_target" \ |
| 69 | --lib \ |
| 70 | --no-default-features \ |
| 71 | --features "$features" \ |
| 72 | --crate-type staticlib \ |
| 73 | -- \ |
| 74 | -C metadata=tlottie-staticlib |
| 75 |
| 76 | artifact="$target_dir/$rust_target/$profile/libtlottie.a" |
| 77 | if [ ! -f "$artifact" ]; then |
| 78 | echo "error: cargo did not emit $artifact" >&2 |
| 79 | exit 1 |
| 80 | fi |
| 81 | if [ ! -d "$out_root/$abi" ]; then |
| 82 | echo "error: output directory does not exist: $out_root/$abi" >&2 |
| 83 | exit 1 |
| 84 | fi |
| 85 |
| 86 | # Copy through a file in the destination directory so replacement is atomic. |
| 87 | tmp=$(mktemp "$out_root/$abi/.libtlottie.a.XXXXXX") |
| 88 | cp "$artifact" "$tmp" |
| 89 | chmod 0644 "$tmp" |
| 90 | mv -f "$tmp" "$destination" |
| 91 |
| 92 | # The container runs as root, so without this the archive would land in |
| 93 | # the host checkout owned by root on native Linux hosts. HOST_UID/GID are |
| 94 | # passed in by the top-level build.sh. |
| 95 | if [ -n "${HOST_UID:-}" ] && [ -n "${HOST_GID:-}" ]; then |
| 96 | chown "$HOST_UID:$HOST_GID" "$destination" |
| 97 | fi |
| 98 |
| 99 | bytes=$(wc -c < "$destination" | tr -d ' ') |
| 100 | echo "Wrote $destination ($bytes bytes)" |
| 101 | done |
| 102 |
| 103 | echo "All tlottie Android archives are ready." |