Skip to content

Commit 4824f39

Browse files
committed
feat: add mobile platform build support
1 parent f845653 commit 4824f39

File tree

3 files changed

+239
-27
lines changed

3 files changed

+239
-27
lines changed

scripts/build-android.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
5+
PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
6+
ANDROID_BUILD_ROOT="$PROJECT_ROOT/build/android"
7+
OUTPUT_ROOT="$PROJECT_ROOT/android/libs"
8+
9+
if [[ -z "${ANDROID_NDK_ROOT:-}" ]]; then
10+
echo "ANDROID_NDK_ROOT must be set" >&2
11+
exit 1
12+
fi
13+
14+
if [[ ! -d "$ANDROID_BUILD_ROOT" ]]; then
15+
echo "Android build artifacts not found in $ANDROID_BUILD_ROOT" >&2
16+
echo "Run scripts/generate_bindings.sh --target android first." >&2
17+
exit 1
18+
fi
19+
20+
mkdir -p "$OUTPUT_ROOT"
21+
22+
for ABI in arm64-v8a armeabi-v7a x86_64; do
23+
ARTIFACT="$ANDROID_BUILD_ROOT/${ABI}/libbdkffi.so"
24+
if [[ ! -f "$ARTIFACT" ]]; then
25+
echo "Missing artifact for $ABI at $ARTIFACT" >&2
26+
exit 1
27+
fi
28+
29+
DEST_DIR="$OUTPUT_ROOT/$ABI"
30+
mkdir -p "$DEST_DIR"
31+
cp "$ARTIFACT" "$DEST_DIR/"
32+
echo "Copied $ABI artifact to $DEST_DIR"
33+
done
34+
35+
echo "Android libraries staged under $OUTPUT_ROOT"

scripts/build-ios-xcframework.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
5+
PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
6+
IOS_BUILD_ROOT="$PROJECT_ROOT/build/ios"
7+
OUTPUT_ROOT="$PROJECT_ROOT/ios/Release"
8+
XCFRAMEWORK_NAME="bdkffi.xcframework"
9+
10+
DEVICE_LIB="$IOS_BUILD_ROOT/aarch64-apple-ios/libbdkffi.a"
11+
SIM_ARM64_LIB="$IOS_BUILD_ROOT/aarch64-apple-ios-sim/libbdkffi.a"
12+
SIM_X86_LIB="$IOS_BUILD_ROOT/x86_64-apple-ios/libbdkffi.a"
13+
SIM_UNIVERSAL_DIR="$IOS_BUILD_ROOT/simulator-universal"
14+
SIM_UNIVERSAL_LIB="$SIM_UNIVERSAL_DIR/libbdkffi.a"
15+
16+
if [[ ! -f "$DEVICE_LIB" ]]; then
17+
echo "Missing device library: $DEVICE_LIB" >&2
18+
echo "Run scripts/generate_bindings.sh --target ios first." >&2
19+
exit 1
20+
fi
21+
22+
if [[ ! -f "$SIM_ARM64_LIB" || ! -f "$SIM_X86_LIB" ]]; then
23+
echo "Missing simulator libraries: $SIM_ARM64_LIB or $SIM_X86_LIB" >&2
24+
echo "Run scripts/generate_bindings.sh --target ios first." >&2
25+
exit 1
26+
fi
27+
28+
if ! command -v xcodebuild >/dev/null 2>&1; then
29+
echo "xcodebuild is required to create an XCFramework" >&2
30+
exit 1
31+
fi
32+
33+
mkdir -p "$SIM_UNIVERSAL_DIR"
34+
35+
echo "Combining simulator slices with lipo..."
36+
lipo -create "$SIM_ARM64_LIB" "$SIM_X86_LIB" -output "$SIM_UNIVERSAL_LIB"
37+
38+
mkdir -p "$OUTPUT_ROOT"
39+
OUTPUT_PATH="$OUTPUT_ROOT/$XCFRAMEWORK_NAME"
40+
rm -rf "$OUTPUT_PATH"
41+
42+
echo "Creating XCFramework at $OUTPUT_PATH..."
43+
xcodebuild -create-xcframework \
44+
-library "$DEVICE_LIB" \
45+
-library "$SIM_UNIVERSAL_LIB" \
46+
-output "$OUTPUT_PATH"
47+
48+
echo "XCFramework created: $OUTPUT_PATH"

scripts/generate_bindings.sh

Lines changed: 156 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,182 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

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+
422
OS=$(uname -s)
5-
echo "Running on $OS"
23+
ARCH=$(uname -m)
24+
echo "Running on $OS ($ARCH)"
625

726
dart --version
827
dart pub get
928

1029
mkdir -p lib
1130
rm -f lib/bdk.dart
1231

13-
# Install Rust targets if on macOS
1432
if [[ "$OS" == "Darwin" ]]; then
1533
LIBNAME=libbdkffi.dylib
1634
elif [[ "$OS" == "Linux" ]]; then
1735
LIBNAME=libbdkffi.so
1836
else
19-
echo "Unsupported os: $OS"
37+
echo "Unsupported os: $OS" >&2
2038
exit 1
2139
fi
2240

2341
# Run from the specific crate inside the embedded submodule
2442
cd ./bdk-ffi/bdk-ffi/
25-
echo "Building bdk-ffi crate and generating Dart bindings..."
26-
cargo build --profile dev -p bdk-ffi
2743

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+
}
3049

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
4853

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
52181

53182
echo "All done!"

0 commit comments

Comments
 (0)