Skip to content

Commit

Permalink
Update generate swift bindings script to create swift package
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory authored and tnull committed May 8, 2023
1 parent 0126e67 commit 47d8059
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# Ignore generated swift related files
.swiftpm/
ldk_nodeFFI.*
ldk_node.swift
libldk_node.dylib
LightningDevKitNode.swift
ldk_node.swiftmodule
swift.swiftsourceinfo
swift.abi.json
swift.swiftdoc

# Ignore ldk_nodeFFI.xcframework files
/bindings/swift/ldk_nodeFFI.xcframework
37 changes: 37 additions & 0 deletions bindings/swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "ldk-node",
platforms: [
.macOS(.v12),
.iOS(.v15)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "LightningDevKitNode",
targets: ["ldk_nodeFFI", "LightningDevKitNode"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
// .binaryTarget(
// name: "ldk_nodeFFI",
// url: "https://github.com/lightningdevkit/ldk-node/releases/download/0.3.0/ldk_nodeFFI.xcframework.zip",
// checksum: "<TBD>"),
.binaryTarget(name: "ldk_nodeFFI", path: "./ldk_nodeFFI.xcframework"),
.target(
name: "LightningDevKitNode",
dependencies: ["ldk_nodeFFI"]),
// .testTarget(
// name: "LightningDevKitNodeTests",
// dependencies: ["LightningDevKitNode"]),
]
)
53 changes: 53 additions & 0 deletions bindings/swift/ldk_nodeFFI.xcframework/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AvailableLibraries</key>
<array>
<dict>
<key>LibraryIdentifier</key>
<string>macos-arm64_x86_64</string>
<key>LibraryPath</key>
<string>ldk_nodeFFI.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>macos</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>ldk_nodeFFI.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>ldk_nodeFFI.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
</dict>
</array>
<key>CFBundlePackageType</key>
<string>XFWK</string>
<key>XCFrameworkFormatVersion</key>
<string>1.0</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This is the "umbrella header" for our combined Rust code library.
// It needs to import all of the individual headers.

#import "ldk_nodeFFI.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
framework module ldk_nodeFFI {
umbrella header "ldk_nodeFFI-umbrella.h"

export *
module * { export * }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This is the "umbrella header" for our combined Rust code library.
// It needs to import all of the individual headers.

#import "ldk_nodeFFI.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
framework module ldk_nodeFFI {
umbrella header "ldk_nodeFFI-umbrella.h"

export *
module * { export * }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This is the "umbrella header" for our combined Rust code library.
// It needs to import all of the individual headers.

#import "ldk_nodeFFI.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
framework module ldk_nodeFFI {
umbrella header "ldk_nodeFFI-umbrella.h"

export *
module * { export * }
}
38 changes: 37 additions & 1 deletion scripts/uniffi_bindgen_generate_swift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,40 @@ $UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language swift -o "$BINDING

mkdir -p $BINDINGS_DIR

swiftc -module-name ldk_node -emit-library -o "$BINDINGS_DIR"/libldk_node.dylib -emit-module -emit-module-path "$BINDINGS_DIR" -parse-as-library -L ./target/release -lldk_node -Xcc -fmodule-map-file="$BINDINGS_DIR"/ldk_nodeFFI.modulemap "$BINDINGS_DIR"/ldk_node.swift -v
# Install rust target toolchains
rustup install nightly-x86_64-apple-darwin
rustup component add rust-src --toolchain nightly-x86_64-apple-darwin
rustup target add aarch64-apple-ios x86_64-apple-ios
rustup target add aarch64-apple-ios-sim --toolchain nightly
rustup target add aarch64-apple-darwin x86_64-apple-darwin

# Build rust target libs
cargo build --profile release-smaller || exit 1
cargo build --profile release-smaller --target x86_64-apple-darwin || exit 1
cargo build --profile release-smaller --target aarch64-apple-darwin || exit 1
cargo build --profile release-smaller --target x86_64-apple-ios || exit 1
cargo build --profile release-smaller --target aarch64-apple-ios || exit 1
cargo +nightly build --release --target aarch64-apple-ios-sim || exit 1

# Combine ios-sim and apple-darwin (macos) libs for x86_64 and aarch64 (m1)
mkdir -p target/lipo-ios-sim/release-smaller || exit 1
lipo target/aarch64-apple-ios-sim/release/libldk_node.a target/x86_64-apple-ios/release-smaller/libldk_node.a -create -output target/lipo-ios-sim/release-smaller/libldk_node.a || exit 1
mkdir -p target/lipo-macos/release-smaller || exit 1
lipo target/aarch64-apple-darwin/release-smaller/libldk_node.a target/x86_64-apple-darwin/release-smaller/libldk_node.a -create -output target/lipo-macos/release-smaller/libldk_node.a || exit 1

$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language swift -o "$BINDINGS_DIR" || exit 1

swiftc -module-name ldk_node -emit-library -o "$BINDINGS_DIR"/libldk_node.dylib -emit-module -emit-module-path "$BINDINGS_DIR" -parse-as-library -L ./target/release-smaller -lldk_node -Xcc -fmodule-map-file="$BINDINGS_DIR"/ldk_nodeFFI.modulemap "$BINDINGS_DIR"/ldk_node.swift -v || exit 1

# Create xcframework from bindings Swift file and libs
mkdir -p "$BINDINGS_DIR"/Sources/LightningDevKitNode || exit 1
mv "$BINDINGS_DIR"/ldk_node.swift "$BINDINGS_DIR"/Sources/LightningDevKitNode/LightningDevKitNode.swift || exit 1
cp "$BINDINGS_DIR"/ldk_nodeFFI.h "$BINDINGS_DIR"/ldk_nodeFFI.xcframework/ios-arm64/ldk_nodeFFI.framework/Headers || exit 1
cp "$BINDINGS_DIR"/ldk_nodeFFI.h "$BINDINGS_DIR"/ldk_nodeFFI.xcframework/ios-arm64_x86_64-simulator/ldk_nodeFFI.framework/Headers || exit 1
cp "$BINDINGS_DIR"/ldk_nodeFFI.h "$BINDINGS_DIR"/ldk_nodeFFI.xcframework/macos-arm64_x86_64/ldk_nodeFFI.framework/Headers || exit 1
cp target/aarch64-apple-ios/release-smaller/libldk_node.a "$BINDINGS_DIR"/ldk_nodeFFI.xcframework/ios-arm64/ldk_nodeFFI.framework/ldk_nodeFFI || exit 1
cp target/lipo-ios-sim/release-smaller/libldk_node.a "$BINDINGS_DIR"/ldk_nodeFFI.xcframework/ios-arm64_x86_64-simulator/ldk_nodeFFI.framework/ldk_nodeFFI || exit 1
cp target/lipo-macos/release-smaller/libldk_node.a "$BINDINGS_DIR"/ldk_nodeFFI.xcframework/macos-arm64_x86_64/ldk_nodeFFI.framework/ldk_nodeFFI || exit 1
# rm "$BINDINGS_DIR"/ldk_nodeFFI.h || exit 1
# rm "$BINDINGS_DIR"/ldk_nodeFFI.modulemap || exit 1
echo finished successfully!

0 comments on commit 47d8059

Please sign in to comment.