|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
| 4 | +TARGET="desktop" |
| 5 | +while [[ $# -gt 0 ]]; do |
| 6 | + case "$1" in |
| 7 | + --target) |
| 8 | + TARGET="${2:-desktop}" |
| 9 | + shift 2 |
| 10 | + ;; |
| 11 | + --help|-h) |
| 12 | + echo "Usage: $0 [--target desktop|ios|android]" |
| 13 | + exit 0 |
| 14 | + ;; |
| 15 | + *) |
| 16 | + echo "Unknown option: $1" >&2 |
| 17 | + exit 1 |
| 18 | + ;; |
| 19 | + esac |
| 20 | +done |
| 21 | + |
4 | 22 | OS=$(uname -s) |
5 | | -echo "Running on $OS" |
| 23 | +ARCH=$(uname -m) |
| 24 | +echo "Running on $OS ($ARCH)" |
6 | 25 |
|
7 | 26 | dart --version |
8 | 27 | dart pub get |
9 | 28 |
|
10 | 29 | mkdir -p lib |
11 | 30 | rm -f lib/bdk.dart |
12 | 31 |
|
13 | | -# Install Rust targets if on macOS |
14 | 32 | if [[ "$OS" == "Darwin" ]]; then |
15 | 33 | LIBNAME=libbdkffi.dylib |
16 | 34 | elif [[ "$OS" == "Linux" ]]; then |
17 | 35 | LIBNAME=libbdkffi.so |
18 | 36 | else |
19 | | - echo "Unsupported os: $OS" |
| 37 | + echo "Unsupported os: $OS" >&2 |
20 | 38 | exit 1 |
21 | 39 | fi |
22 | 40 |
|
23 | 41 | # Run from the specific crate inside the embedded submodule |
24 | 42 | cd ./bdk-ffi/bdk-ffi/ |
25 | | -echo "Building bdk-ffi crate and generating Dart bindings..." |
26 | | -cargo build --profile dev -p bdk-ffi |
27 | 43 |
|
28 | | -# Generate Dart bindings using local uniffi-bindgen wrapper |
29 | | -(cd ../../ && cargo run --profile dev --bin uniffi-bindgen -- --language dart --library bdk-ffi/bdk-ffi/target/debug/$LIBNAME --out-dir lib/) |
| 44 | +generate_bindings() { |
| 45 | + echo "Building bdk-ffi crate and generating Dart bindings..." |
| 46 | + cargo build --profile dev -p bdk-ffi |
| 47 | + (cd ../../ && cargo run --profile dev --bin uniffi-bindgen -- --language dart --library bdk-ffi/bdk-ffi/target/debug/$LIBNAME --out-dir lib/) |
| 48 | +} |
30 | 49 |
|
31 | | -if [[ "$OS" == "Darwin" ]]; then |
32 | | - echo "Generating native binaries..." |
33 | | - rustup target add aarch64-apple-darwin x86_64-apple-darwin |
34 | | - # This is a test script the actual release should not include the test utils feature |
35 | | - cargo build --profile dev -p bdk-ffi --target aarch64-apple-darwin & |
36 | | - cargo build --profile dev -p bdk-ffi --target x86_64-apple-darwin & |
37 | | - wait |
38 | | - |
39 | | - echo "Building macOS fat library" |
40 | | - lipo -create -output ../../$LIBNAME \ |
41 | | - target/aarch64-apple-darwin/debug/$LIBNAME \ |
42 | | - target/x86_64-apple-darwin/debug/$LIBNAME |
43 | | -else |
44 | | - echo "Generating native binaries..." |
45 | | - rustup target add x86_64-unknown-linux-gnu |
46 | | - # This is a test script the actual release should not include the test utils feature |
47 | | - cargo build --profile dev -p bdk-ffi --target x86_64-unknown-linux-gnu |
| 50 | +build_ios() { |
| 51 | + echo "Building iOS static libraries..." |
| 52 | + rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim >/dev/null |
48 | 53 |
|
49 | | - echo "Copying bdk-ffi binary" |
50 | | - cp target/x86_64-unknown-linux-gnu/debug/$LIBNAME ../../$LIBNAME |
51 | | -fi |
| 54 | + PROFILE="release-smaller" |
| 55 | + OUT_ROOT="../../build/ios" |
| 56 | + mkdir -p "$OUT_ROOT" |
| 57 | + |
| 58 | + for TARGET_TRIPLE in aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim; do |
| 59 | + echo " -> $TARGET_TRIPLE" |
| 60 | + cargo build --profile "$PROFILE" -p bdk-ffi --target "$TARGET_TRIPLE" |
| 61 | + ARTIFACT="target/${TARGET_TRIPLE}/${PROFILE}/libbdkffi.a" |
| 62 | + DEST_DIR="$OUT_ROOT/${TARGET_TRIPLE}" |
| 63 | + mkdir -p "$DEST_DIR" |
| 64 | + cp "$ARTIFACT" "$DEST_DIR/" |
| 65 | + done |
| 66 | +} |
| 67 | + |
| 68 | +build_android() { |
| 69 | + if [[ -z "${ANDROID_NDK_ROOT:-}" ]]; then |
| 70 | + echo "ANDROID_NDK_ROOT must be set to build Android artifacts" >&2 |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + echo "Building Android shared libraries..." |
| 75 | + rustup target add aarch64-linux-android armv7-linux-androideabi x86_64-linux-android >/dev/null |
| 76 | + |
| 77 | + API_LEVEL=24 |
| 78 | + case "$OS" in |
| 79 | + Darwin) |
| 80 | + HOST_OS=darwin |
| 81 | + ;; |
| 82 | + Linux) |
| 83 | + HOST_OS=linux |
| 84 | + ;; |
| 85 | + *) |
| 86 | + echo "Unsupported host for Android builds: $OS" >&2 |
| 87 | + exit 1 |
| 88 | + ;; |
| 89 | + esac |
| 90 | + |
| 91 | + case "$ARCH" in |
| 92 | + x86_64) |
| 93 | + HOST_ARCH=x86_64 |
| 94 | + ;; |
| 95 | + arm64|aarch64) |
| 96 | + HOST_ARCH=arm64 |
| 97 | + ;; |
| 98 | + *) |
| 99 | + echo "Unsupported architecture for Android builds: $ARCH" >&2 |
| 100 | + exit 1 |
| 101 | + ;; |
| 102 | + esac |
| 103 | + |
| 104 | + TOOLCHAIN="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/${HOST_OS}-${HOST_ARCH}/bin" |
| 105 | + if [[ ! -d "$TOOLCHAIN" ]]; then |
| 106 | + echo "Unable to locate NDK toolchain at $TOOLCHAIN" >&2 |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + |
| 110 | + OUT_ROOT="../../build/android" |
| 111 | + mkdir -p "$OUT_ROOT" |
| 112 | + |
| 113 | + for TARGET_TRIPLE in aarch64-linux-android armv7-linux-androideabi x86_64-linux-android; do |
| 114 | + case "$TARGET_TRIPLE" in |
| 115 | + aarch64-linux-android) |
| 116 | + ABI="arm64-v8a" |
| 117 | + CLANG="${TOOLCHAIN}/aarch64-linux-android${API_LEVEL}-clang" |
| 118 | + TARGET_ENV_LOWER="aarch64_linux_android" |
| 119 | + ;; |
| 120 | + armv7-linux-androideabi) |
| 121 | + ABI="armeabi-v7a" |
| 122 | + CLANG="${TOOLCHAIN}/armv7a-linux-androideabi${API_LEVEL}-clang" |
| 123 | + TARGET_ENV_LOWER="armv7_linux_androideabi" |
| 124 | + ;; |
| 125 | + x86_64-linux-android) |
| 126 | + ABI="x86_64" |
| 127 | + CLANG="${TOOLCHAIN}/x86_64-linux-android${API_LEVEL}-clang" |
| 128 | + TARGET_ENV_LOWER="x86_64_linux_android" |
| 129 | + ;; |
| 130 | + esac |
| 131 | + |
| 132 | + TARGET_ENV=$(echo "$TARGET_TRIPLE" | tr '[:lower:]' '[:upper:]' | tr '-' '_') |
| 133 | + |
| 134 | + export CARGO_TARGET_${TARGET_ENV}_LINKER="$CLANG" |
| 135 | + export CARGO_TARGET_${TARGET_ENV}_AR="${TOOLCHAIN}/llvm-ar" |
| 136 | + |
| 137 | + export CC_${TARGET_ENV_LOWER}="$CLANG" |
| 138 | + export AR_${TARGET_ENV_LOWER}="${TOOLCHAIN}/llvm-ar" |
| 139 | + |
| 140 | + echo " -> $TARGET_TRIPLE ($ABI)" |
| 141 | + cargo build --profile release-smaller -p bdk-ffi --target "$TARGET_TRIPLE" |
| 142 | + ARTIFACT="target/${TARGET_TRIPLE}/release-smaller/libbdkffi.so" |
| 143 | + DEST_DIR="$OUT_ROOT/$ABI" |
| 144 | + mkdir -p "$DEST_DIR" |
| 145 | + cp "$ARTIFACT" "$DEST_DIR/" |
| 146 | + done |
| 147 | +} |
| 148 | + |
| 149 | +case "$TARGET" in |
| 150 | + ios) |
| 151 | + generate_bindings |
| 152 | + build_ios |
| 153 | + ;; |
| 154 | + android) |
| 155 | + generate_bindings |
| 156 | + build_android |
| 157 | + ;; |
| 158 | + desktop|*) |
| 159 | + generate_bindings |
| 160 | + if [[ "$OS" == "Darwin" ]]; then |
| 161 | + echo "Generating native macOS binaries..." |
| 162 | + rustup target add aarch64-apple-darwin x86_64-apple-darwin >/dev/null |
| 163 | + cargo build --profile dev -p bdk-ffi --target aarch64-apple-darwin & |
| 164 | + cargo build --profile dev -p bdk-ffi --target x86_64-apple-darwin & |
| 165 | + wait |
| 166 | + |
| 167 | + echo "Building macOS fat library" |
| 168 | + lipo -create -output ../../$LIBNAME \ |
| 169 | + target/aarch64-apple-darwin/debug/$LIBNAME \ |
| 170 | + target/x86_64-apple-darwin/debug/$LIBNAME |
| 171 | + else |
| 172 | + echo "Generating native Linux binaries..." |
| 173 | + rustup target add x86_64-unknown-linux-gnu >/dev/null |
| 174 | + cargo build --profile dev -p bdk-ffi --target x86_64-unknown-linux-gnu |
| 175 | + |
| 176 | + echo "Copying bdk-ffi binary" |
| 177 | + cp target/x86_64-unknown-linux-gnu/debug/$LIBNAME ../../$LIBNAME |
| 178 | + fi |
| 179 | + ;; |
| 180 | +esac |
52 | 181 |
|
53 | 182 | echo "All done!" |
0 commit comments