| 6 |
| set -Eeuo pipefail |
| 7 |
| 8 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | source "$SCRIPT_DIR/common.sh" |
| 10 |
| 11 | require_ndk |
| 12 | require_command cargo |
| 13 | require_command rustup |
| 14 |
| 15 | # gram-wallet's Cargo manifest pins its path dependency to this submodule. |
| 16 | ENGINE_DIR="$JNI_DIR/wallet-engine" |
| 17 | if [[ ! -f "$ENGINE_DIR/Cargo.toml" ]]; then |
| 18 | echo "ERROR: Wallet Engine sources not found: $ENGINE_DIR" >&2 |
| 19 | echo " Initialize the TMessagesProj/jni/wallet-engine submodule." >&2 |
| 20 | exit 1 |
| 21 | fi |
| 22 | ENGINE_DIR="$(cd "$ENGINE_DIR" && pwd)" |
| 23 | ADAPTER_DIR="$JNI_DIR/gram-wallet" |
| 24 | BUILD_DIR="${WALLET_ENGINE_BUILD_DIR:-$SCRIPT_DIR/build/wallet-engine}" |
| 25 | mkdir -p "$BUILD_DIR" "$INCLUDE_DIR/wallet-engine" |
| 26 | BUILD_DIR="$(cd "$BUILD_DIR" && pwd)" |
| 27 | INCLUDE_DIR="$(cd "$INCLUDE_DIR" && pwd)" |
| 28 | mkdir -p "$LIB_DIR" |
| 29 | LIB_DIR="$(cd "$LIB_DIR" && pwd)" |
| 30 |
| 31 | # The app adapter links the engine and reexports its UniFFI scaffolding. Build |
| 32 | # one archive to avoid linking two copies of Rust and wallet-engine into JNI. |
| 33 | cd "$ADAPTER_DIR" |
| 34 | export RUSTC="$(rustup which rustc)" |
| 35 | export CARGO_BUILD_JOBS="$JOBS" |
| 36 | export CARGO_INCREMENTAL=0 |
| 37 |
| 38 | rust_target_for_abi() { |
| 39 | case "$1" in |
| 40 | arm64-v8a) echo aarch64-linux-android ;; |
| 41 | armeabi-v7a) echo armv7-linux-androideabi ;; |
| 42 | x86_64) echo x86_64-linux-android ;; |
| 43 | x86) echo i686-linux-android ;; |
| 44 | *) echo "ERROR: unsupported ABI: $1" >&2; return 1 ;; |
| 45 | esac |
| 46 | } |
| 47 |
| 48 | installed_targets="$(rustup target list --installed)" |
| 49 | missing_targets=() |
| 50 | for abi in $ABIS; do |
| 51 | target="$(rust_target_for_abi "$abi")" |
| 52 | if [[ $'\n'"$installed_targets"$'\n' != *$'\n'"$target"$'\n'* ]]; then |
| 53 | missing_targets+=("$target") |
| 54 | fi |
| 55 | done |
| 56 | if (( ${#missing_targets[@]} )); then |
| 57 | echo "ERROR: missing Rust Android targets. From $ENGINE_DIR run:" >&2 |
| 58 | echo " rustup target add ${missing_targets[*]}" >&2 |
| 59 | exit 1 |
| 60 | fi |
| 61 |
| 62 | # Use stable virtual paths in Rust objects, as common.sh does for C/C++. |
| 63 | rust_sysroot="$(rustc --print sysroot)" |
| 64 | rust_cargo_home="${CARGO_HOME:-$HOME/.cargo}" |
| 65 | rustflags=( |
| 66 | "--remap-path-prefix=$ENGINE_DIR=/wallet-engine" |
| 67 | "--remap-path-prefix=$ADAPTER_DIR=/gram-wallet" |
| 68 | "--remap-path-prefix=$BUILD_DIR=/wallet-engine-target" |
| 69 | "--remap-path-prefix=$rust_sysroot=/rust-sysroot" |
| 70 | "--remap-path-prefix=$rust_cargo_home=/cargo-home" |
| 71 | "-Cdebuginfo=0" |
| 72 | "-Crelocation-model=pic" |
| 73 | ) |
| 74 | unset RUSTFLAGS |
| 75 | CARGO_ENCODED_RUSTFLAGS="$(IFS=$'\037'; echo "${rustflags[*]}")" |
| 76 | export CARGO_ENCODED_RUSTFLAGS |
| 77 |
| 78 | # Equivalent to `cargo xtask bindings cpp`, with build outputs kept in prebuild. |
| 79 | # The pinned generator reads UniFFI metadata from the matching host library. |
| 80 | case "$host_os" in |
| 81 | Darwin) host_library=libgram_wallet.dylib ;; |
| 82 | Linux) host_library=libgram_wallet.so ;; |
| 83 | esac |
| 84 | echo "Generating Wallet Engine C++ bindings with $(rustc --version)" |
| 85 | cargo rustc --manifest-path "$ADAPTER_DIR/Cargo.toml" \ |
| 86 | --target-dir "$BUILD_DIR/host" --locked --release --lib --crate-type cdylib |
| 87 | cargo run --manifest-path "$ENGINE_DIR/bindgen/cpp/bindgen/Cargo.toml" \ |
| 88 | --target-dir "$BUILD_DIR/bindgen" --locked -- \ |
| 89 | --library --out-dir "$BUILD_DIR/bindings" \ |
| 90 | "$BUILD_DIR/host/release/$host_library" |
| 91 |
| 92 | for generated in wallet_engine.hpp wallet_engine.cpp wallet_engine_scaffolding.hpp \ |
| 93 | gram_wallet.hpp gram_wallet.cpp gram_wallet_scaffolding.hpp; do |
| 94 | cp "$BUILD_DIR/bindings/$generated" "$INCLUDE_DIR/wallet-engine/$generated" |
| 95 | chmod 0644 "$INCLUDE_DIR/wallet-engine/$generated" |
| 96 | done |
| 97 |
| 98 | for abi in $ABIS; do |
| 99 | target="$(rust_target_for_abi "$abi")" |
| 100 | # NDK names its ARMv7 compiler armv7a, while Rust uses armv7. |
| 101 | case "$abi" in |
| 102 | armeabi-v7a) clang_prefix=armv7a-linux-androideabi ;; |
| 103 | *) clang_prefix="$target" ;; |
| 104 | esac |
| 105 | linker="$NDK_TOOLCHAIN/bin/${clang_prefix}${API}-clang" |
| 106 | if [[ ! -x "$linker" ]]; then |
| 107 | echo "ERROR: Android compiler not found: $linker" >&2 |
| 108 | exit 1 |
| 109 | fi |
| 110 | target_key="$(echo "$target" | tr '[:lower:]-' '[:upper:]_')" |
| 111 | cc_key="${target//-/_}" |
| 112 |
| 113 | echo "Building Wallet Engine: $abi (API $API)" |
| 114 | env "CARGO_TARGET_${target_key}_LINKER=$linker" \ |
| 115 | "CC_${cc_key}=$linker" \ |
| 116 | "CXX_${cc_key}=${linker}++" \ |
| 117 | "AR_${cc_key}=$NDK_TOOLCHAIN/bin/llvm-ar" \ |
| 118 | cargo rustc --manifest-path "$ADAPTER_DIR/Cargo.toml" \ |
| 119 | --target-dir "$BUILD_DIR/android" --locked --release --lib \ |
| 120 | --target "$target" --crate-type staticlib |
| 121 |
| 122 | destination="$(abi_output_dir "$abi")/libwallet_engine.a" |
| 123 | copy_archive "$BUILD_DIR/android/$target/release/libgram_wallet.a" "$destination" |
| 124 | # tlottie's no_std runtime defines its own rust_eh_personality. Namespace |
| 125 | # this archive's definition AND references (including the DWARF anchor) |
| 126 | # so both Rust libraries can be linked into the same JNI shared library. |
| 127 | require_llvm_objcopy |
| 128 | "$LLVM_OBJCOPY_BIN" \ |
| 129 | --redefine-sym rust_eh_personality=wallet_engine_rust_eh_personality \ |
| 130 | --redefine-sym DW.ref.rust_eh_personality=DW.ref.wallet_engine_rust_eh_personality \ |
| 131 | "$destination" |
| 132 | finalize_archive "$destination" |
| 133 | echo "Wrote $destination" |
| 134 | done |
| 135 |
| 136 | echo "Wallet Engine archives: $LIB_DIR/<abi>/libwallet_engine.a" |
| 137 | echo "Wallet Engine C++ bindings: $INCLUDE_DIR/wallet-engine" |
| 138 |