| 6 | tlottie_dir=${TLOTTIE_SOURCE_DIR:-"$script_dir/../tlottie"} |
| 7 | manifest="$tlottie_dir/Cargo.toml" |
| 8 | profile=release-nostd |
| 9 | features=cpu,no-std,c-api |
| 10 |
| 11 | if [ ! -f "$manifest" ]; then |
| 12 | echo "error: tlottie Cargo.toml not found at $manifest" >&2 |
| 13 | echo "set TLOTTIE_SOURCE_DIR if the source is not next to tlottie_lib" >&2 |
| 14 | exit 1 |
| 15 | fi |
| 16 |
| 17 | rustup_toolchain= |
| 18 | if command -v rustup >/dev/null 2>&1; then |
| 19 | rustup_toolchain=${TLOTTIE_RUST_TOOLCHAIN:-stable} |
| 20 | fi |
| 21 |
| 22 | run_cargo() { |
| 23 | if [ -n "$rustup_toolchain" ]; then |
| 24 | rustup run "$rustup_toolchain" cargo "$@" |
| 25 | else |
| 26 | cargo "$@" |
| 27 | fi |
| 28 | } |
| 29 |
| 30 | run_rustc() { |
| 31 | if [ -n "$rustup_toolchain" ]; then |
| 32 | rustup run "$rustup_toolchain" rustc "$@" |
| 33 | else |
| 34 | rustc "$@" |
| 35 | fi |
| 36 | } |
| 37 |
| 38 | if [ -n "$rustup_toolchain" ]; then |
| 39 | missing_targets= |
| 40 | for target in \ |
| 41 | aarch64-linux-android \ |
| 42 | armv7-linux-androideabi \ |
| 43 | i686-linux-android \ |
| 44 | x86_64-linux-android |
| 45 | do |
| 46 | if ! rustup target list --toolchain "$rustup_toolchain" --installed | grep -qx "$target"; then |
| 47 | missing_targets="$missing_targets $target" |
| 48 | fi |
| 49 | done |
| 50 | if [ -n "$missing_targets" ]; then |
| 51 | echo "error: Rust Android targets are not installed:$missing_targets" >&2 |
| 52 | echo "install them with: rustup target add --toolchain $rustup_toolchain$missing_targets" >&2 |
| 53 | exit 1 |
| 54 | fi |
| 55 | fi |
| 56 |
| 57 | target_dir=${CARGO_TARGET_DIR:-"$tlottie_dir/target/no-std-android"} |
| 58 | export CARGO_TARGET_DIR="$target_dir" |
| 59 |
| 60 | echo "Building tlottie Android archives" |
| 61 | echo " source: $tlottie_dir" |
| 62 | echo " rustc: $(run_rustc --version)" |
| 63 | echo " profile: $profile" |
| 64 | echo " features: $features" |
| 65 | echo " target dir: $target_dir" |
| 66 |
| 67 | temporary_archive= |
| 68 | cleanup() { |
| 69 | if [ -n "$temporary_archive" ] && [ -f "$temporary_archive" ]; then |
| 70 | unlink "$temporary_archive" |
| 71 | fi |
| 72 | } |
| 73 | trap cleanup EXIT HUP INT TERM |
| 74 |
| 75 | for target_spec in \ |
| 76 | arm64-v8a:aarch64-linux-android \ |
| 77 | armeabi-v7a:armv7-linux-androideabi \ |
| 78 | x86:i686-linux-android \ |
| 79 | x86_64:x86_64-linux-android |
| 80 | do |
| 81 | abi=${target_spec%%:*} |
| 82 | rust_target=${target_spec#*:} |
| 83 | destination="$script_dir/$abi/libtlottie.a" |
| 84 |
| 85 | echo "Building $abi ($rust_target)" |
| 86 | run_cargo rustc \ |
| 87 | --manifest-path "$manifest" \ |
| 88 | --locked \ |
| 89 | --profile "$profile" \ |
| 90 | --target "$rust_target" \ |
| 91 | --lib \ |
| 92 | --no-default-features \ |
| 93 | --features "$features" \ |
| 94 | --crate-type staticlib |
| 95 |
| 96 | artifact="$target_dir/$rust_target/$profile/libtlottie.a" |
| 97 | if [ ! -f "$artifact" ]; then |
| 98 | echo "error: Cargo did not emit $artifact" >&2 |
| 99 | exit 1 |
| 100 | fi |
| 101 | if [ ! -d "$script_dir/$abi" ]; then |
| 102 | echo "error: output directory does not exist: $script_dir/$abi" >&2 |
| 103 | exit 1 |
| 104 | fi |
| 105 |
| 106 | # Copy through a file in the destination directory so replacement is atomic. |
| 107 | temporary_archive=$(mktemp "$script_dir/$abi/.libtlottie.a.XXXXXX") |
| 108 | cp "$artifact" "$temporary_archive" |
| 109 | chmod 0644 "$temporary_archive" |
| 110 | mv -f "$temporary_archive" "$destination" |
| 111 | temporary_archive= |
| 112 |
| 113 | bytes=$(wc -c < "$destination" | tr -d ' ') |
| 114 | echo "Wrote $destination ($bytes bytes)" |
| 115 | done |
| 116 |
| 117 | echo "All tlottie Android archives are ready." |
| 118 |