From 7c2ef36f3b5c0a00ef7247b5a342bbc7ae699b39 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Sat, 16 Nov 2024 15:54:17 +0100 Subject: [PATCH] ffi: add `build-xcframework.sh` Rework Swift Package script Signed-off-by: Yuki Kishimoto --- bindings/nostr-sdk-ffi/justfile | 42 +------------ bindings/nostr-sdk-ffi/swift/.gitignore | 10 +-- .../nostr-sdk-ffi/swift/build-xcframework.sh | 62 +++++++++++++++++++ .../swift/nostr_sdkFFI.xcframework/Info.plist | 53 ---------------- .../Headers/nostr_sdkFFI-umbrella.h | 1 - .../Modules/module.modulemap | 6 -- .../Headers/nostr_sdkFFI-umbrella.h | 1 - .../Modules/module.modulemap | 6 -- .../Headers/nostr_sdkFFI-umbrella.h | 1 - .../Modules/module.modulemap | 6 -- 10 files changed, 70 insertions(+), 118 deletions(-) create mode 100644 bindings/nostr-sdk-ffi/swift/build-xcframework.sh delete mode 100644 bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/Info.plist delete mode 100644 bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h delete mode 100644 bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Modules/module.modulemap delete mode 100644 bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h delete mode 100644 bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Modules/module.modulemap delete mode 100644 bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h delete mode 100644 bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Modules/module.modulemap diff --git a/bindings/nostr-sdk-ffi/justfile b/bindings/nostr-sdk-ffi/justfile index 01e632c3b..e1c9508d0 100755 --- a/bindings/nostr-sdk-ffi/justfile +++ b/bindings/nostr-sdk-ffi/justfile @@ -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: diff --git a/bindings/nostr-sdk-ffi/swift/.gitignore b/bindings/nostr-sdk-ffi/swift/.gitignore index 3c5eabbb7..5210c0435 100644 --- a/bindings/nostr-sdk-ffi/swift/.gitignore +++ b/bindings/nostr-sdk-ffi/swift/.gitignore @@ -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 diff --git a/bindings/nostr-sdk-ffi/swift/build-xcframework.sh b/bindings/nostr-sdk-ffi/swift/build-xcframework.sh new file mode 100644 index 000000000..cbac356b3 --- /dev/null +++ b/bindings/nostr-sdk-ffi/swift/build-xcframework.sh @@ -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" diff --git a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/Info.plist b/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/Info.plist deleted file mode 100644 index 0d20856d4..000000000 --- a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/Info.plist +++ /dev/null @@ -1,53 +0,0 @@ - - - - - AvailableLibraries - - - LibraryIdentifier - macos-arm64_x86_64 - LibraryPath - nostr_sdkFFI.framework - SupportedArchitectures - - arm64 - x86_64 - - SupportedPlatform - macos - - - LibraryIdentifier - ios-arm64_x86_64-simulator - LibraryPath - nostr_sdkFFI.framework - SupportedArchitectures - - arm64 - x86_64 - - SupportedPlatform - ios - SupportedPlatformVariant - simulator - - - LibraryIdentifier - ios-arm64 - LibraryPath - nostr_sdkFFI.framework - SupportedArchitectures - - arm64 - - SupportedPlatform - ios - - - CFBundlePackageType - XFWK - XCFrameworkFormatVersion - 1.0 - - diff --git a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h b/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h deleted file mode 100644 index dfe1f5549..000000000 --- a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h +++ /dev/null @@ -1 +0,0 @@ -#import "nostr_sdkFFI.h" diff --git a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Modules/module.modulemap b/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Modules/module.modulemap deleted file mode 100644 index d6eeb611a..000000000 --- a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Modules/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ -framework module nostr_sdkFFI { - umbrella header "nostr_sdkFFI-umbrella.h" - - export * - module * { export * } -} diff --git a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h b/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h deleted file mode 100644 index dfe1f5549..000000000 --- a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h +++ /dev/null @@ -1 +0,0 @@ -#import "nostr_sdkFFI.h" diff --git a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Modules/module.modulemap b/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Modules/module.modulemap deleted file mode 100644 index d6eeb611a..000000000 --- a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Modules/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ -framework module nostr_sdkFFI { - umbrella header "nostr_sdkFFI-umbrella.h" - - export * - module * { export * } -} diff --git a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h b/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h deleted file mode 100644 index dfe1f5549..000000000 --- a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Headers/nostr_sdkFFI-umbrella.h +++ /dev/null @@ -1 +0,0 @@ -#import "nostr_sdkFFI.h" diff --git a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Modules/module.modulemap b/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Modules/module.modulemap deleted file mode 100644 index d6eeb611a..000000000 --- a/bindings/nostr-sdk-ffi/swift/nostr_sdkFFI.xcframework/macos-arm64_x86_64/nostr_sdkFFI.framework/Modules/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ -framework module nostr_sdkFFI { - umbrella header "nostr_sdkFFI-umbrella.h" - - export * - module * { export * } -}