Skip to content

Commit

Permalink
ffi: add build-xcframework.sh
Browse files Browse the repository at this point in the history
Rework Swift Package script

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 16, 2024
1 parent 0765b0c commit 7c2ef36
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 118 deletions.
42 changes: 2 additions & 40 deletions bindings/nostr-sdk-ffi/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,47 +71,9 @@ aar: sdk-root kotlin assemble-aar
publish-android: aar
cd android && ./gradlew publishAndReleaseToMavenCentral --no-configuration-cache

[private]
swift-deps:
cargo lipo --version || cargo install cargo-lipo
cbindgen --version || cargo install cbindgen

[private]
ios-universal: swift-deps
rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim
mkdir -p ../../target/ios-universal/release
mkdir -p ../../target/ios-universal-sim/release
cargo build --release --target aarch64-apple-ios
cargo build --release --target x86_64-apple-ios
cargo build --release --target aarch64-apple-ios-sim
lipo -create -output ../../target/ios-universal/release/libnostr_sdk_ffi.a ../../target/aarch64-apple-ios/release/libnostr_sdk_ffi.a ../../target/x86_64-apple-ios/release/libnostr_sdk_ffi.a
lipo -create -output ../../target/ios-universal-sim/release/libnostr_sdk_ffi.a ../../target/aarch64-apple-ios-sim/release/libnostr_sdk_ffi.a ../../target/x86_64-apple-ios/release/libnostr_sdk_ffi.a

[private]
darwin-universal: swift-deps
rustup target add aarch64-apple-darwin x86_64-apple-darwin
mkdir -p ../../target/darwin-universal/release
cargo lipo --release --targets aarch64-apple-darwin
cargo lipo --release --targets x86_64-apple-darwin
lipo -create -output ../../target/darwin-universal/release/libnostr_sdk_ffi.dylib ../../target/aarch64-apple-darwin/release/libnostr_sdk_ffi.dylib ../../target/x86_64-apple-darwin/release/libnostr_sdk_ffi.dylib
lipo -create -output ../../target/darwin-universal/release/libnostr_sdk_ffi.a ../../target/aarch64-apple-darwin/release/libnostr_sdk_ffi.a ../../target/x86_64-apple-darwin/release/libnostr_sdk_ffi.a

[private]
assemble-swift-package:
mkdir -p swift/Sources/NostrSDK
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-apple-ios/release/libnostr_sdk_ffi.a --no-format --language swift --out-dir swift/Sources/NostrSDK
mv swift/Sources/NostrSDK/nostr_sdk.swift swift/Sources/NostrSDK/NostrSDK.swift
cp swift/Sources/NostrSDK/nostr_sdkFFI.h swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Headers
cp swift/Sources/NostrSDK/nostr_sdkFFI.h swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Headers
cp swift/Sources/NostrSDK/nostr_sdkFFI.h swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Headers
cp ../../target/aarch64-apple-ios/release/libnostr_sdk_ffi.a swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/nostr_sdkFFI
cp ../../target/ios-universal-sim/release/libnostr_sdk_ffi.a swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/nostr_sdkFFI
cp ../../target/darwin-universal/release/libnostr_sdk_ffi.a swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/nostr_sdkFFI
rm swift/Sources/NostrSDK/nostr_sdkFFI.h
rm swift/Sources/NostrSDK/nostr_sdkFFI.modulemap

# Compile and build Swift Package
swift: ios-universal darwin-universal assemble-swift-package
swift:
@cd swift && bash build-xcframework.sh

[linux]
python:
Expand Down
10 changes: 6 additions & 4 deletions bindings/nostr-sdk-ffi/swift/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
.build/
*.xcodeproj
*.podspec
Sources/NostrSDK/NostrSDK.swift
**/nostr_sdkFFI.h
**/nostr_sdkFFI
**/nostr_sdkFFI.modulemap
Sources/
nostr_sdkFFI
nostr_sdkFFI.h
nostr_sdkFFI.a
nostr_sdkFFI.modulemap
nostr_sdkFFI.xcframework
62 changes: 62 additions & 0 deletions bindings/nostr-sdk-ffi/swift/build-xcframework.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

# Inspired by https://github.com/bitcoindevkit/bdk-ffi/blob/014504ee8b6c12234592b68628ab0be888099d45/bdk-swift/build-xcframework.sh

set -exuo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TARGET_DIR="${SCRIPT_DIR}/../../../target"
TEMP_FFI_DIR="${SCRIPT_DIR}/../ffi/swift"
CDY_LIB="libnostr_sdk_ffi.dylib"
STATIC_LIB="libnostr_sdk_ffi.a"

# Install and targets
rustup target add aarch64-apple-ios # iOS arm64
rustup target add x86_64-apple-ios # iOS x86_64
rustup target add aarch64-apple-ios-sim # simulator mac M1
rustup target add aarch64-apple-darwin # mac M1
rustup target add x86_64-apple-darwin # mac x86_64

# Build for apple targets
cargo build -p nostr-sdk-ffi --release --target x86_64-apple-darwin
cargo build -p nostr-sdk-ffi --release --target aarch64-apple-darwin
cargo build -p nostr-sdk-ffi --release --target x86_64-apple-ios
cargo build -p nostr-sdk-ffi --release --target aarch64-apple-ios
cargo build -p nostr-sdk-ffi --release --target aarch64-apple-ios-sim

# Make Sources dir
mkdir -p Sources/NostrSDK

# build bdk-ffi Swift bindings and put in bdk-swift Sources
cargo run -p uniffi-bindgen generate --library "${TARGET_DIR}/aarch64-apple-ios/release/${CDY_LIB}" --no-format --language swift --out-dir Sources/NostrSDK

# Make universal dirs
mkdir -p "${TARGET_DIR}/darwin-universal/release" # macOS
mkdir -p "${TARGET_DIR}/ios-universal/release" # iOS
mkdir -p "${TARGET_DIR}/ios-universal-sim/release" # iOS Simulator

# Combine static libs for aarch64 and x86_64 targets via lipo
lipo -create -output "${TARGET_DIR}/darwin-universal/release/${STATIC_LIB}" \
"${TARGET_DIR}/aarch64-apple-darwin/release/${STATIC_LIB}" \
"${TARGET_DIR}/x86_64-apple-darwin/release/${STATIC_LIB}" # macOS
lipo -create -output "${TARGET_DIR}/ios-universal-sim/release/${STATIC_LIB}" \
"${TARGET_DIR}/aarch64-apple-ios-sim/release/${STATIC_LIB}" \
"${TARGET_DIR}/x86_64-apple-ios/release/${STATIC_LIB}" # iOS Simulator

# Move static lib header files to temporary directory
mkdir -p "${TEMP_FFI_DIR}"
mv "Sources/NostrSDK/nostr_sdkFFI.h" "${TEMP_FFI_DIR}/nostr_sdkFFI.h"
mv "Sources/NostrSDK/nostr_sdkFFI.modulemap" "${TEMP_FFI_DIR}/module.modulemap"

# Remove old xcframework dir
rm -rf "nostr_sdkFFI.xcframework"

# Create new xcframework directory from static libs and headers
xcodebuild -create-xcframework \
-library "${TARGET_DIR}/darwin-universal/release/${STATIC_LIB}" \
-headers "${TEMP_FFI_DIR}" \
-library "${TARGET_DIR}/aarch64-apple-ios/release/${STATIC_LIB}" \
-headers "${TEMP_FFI_DIR}" \
-library "${TARGET_DIR}/ios-universal-sim/release/${STATIC_LIB}" \
-headers "${TEMP_FFI_DIR}" \
-output "nostr_sdkFFI.xcframework"
53 changes: 0 additions & 53 deletions bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/Info.plist

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 7c2ef36

Please sign in to comment.