Skip to content

Commit

Permalink
chore(xcframework): generate xcframework
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpooleywc committed Sep 17, 2024
1 parent dfe0f88 commit 545959e
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 5 deletions.
77 changes: 77 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Build and Release Yttrium

on:
# workflow_dispatch:
# inputs:
# version:
# description: 'Version number for the release (e.g., 0.1.0)'
# required: true
push:
branches:
- 'jack/xcframework'

env:
CARGO_TERM_COLOR: always
SKIP_RELEASE: true
TARGET_BRANCH: ${{ github.head_ref }} # ${{ github.head_ref || 'main' }}

jobs:
release-swift-package:
runs-on: macos-latest
strategy:
matrix:
config:
- debug
steps:
- uses: actions/checkout@v4
- name: Run sccache-cache
uses: mozilla-actions/[email protected]
- uses: pnpm/action-setup@v4
with:
version: 9
- name: Build and Package Rust XCFramework
env:
GITHUB_VERSION: ${{ github.event.inputs.version }}
run: |
rustup update stable && rustup default stable
git submodule update --init --recursive
make setup-thirdparty
make build-ios-bindings-release
make zip-rust-xcframework
make compute-rust-checksum
make generate-package-swift
- name: Commit and Push Package.swift
if: ${{ env.SKIP_RELEASE != 'true' }}
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git checkout ${{ env.TARGET_BRANCH }}
git add Package.swift
git commit -m "chore: update package.swift for version ${{ github.event.inputs.version }}"
git push origin HEAD:${{ env.TARGET_BRANCH }}
- name: Create Release
if: ${{ env.SKIP_RELEASE != 'true' }}
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
with:
tag_name: ${{ github.event.inputs.version }}-test
release_name: Yttrium ${{ github.event.inputs.version }}-test
draft: false
prerelease: true

- name: Upload Rust XCFramework to Release
if: ${{ env.SKIP_RELEASE != 'true' }}
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./Output/RustXcframework.xcframework.zip
asset_name: RustXcframework.xcframework.zip
asset_content_type: application/zip
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ setup-thirdparty:
build-ios-bindings:
sh crates/ffi/build-rust-ios.sh

build-ios-bindings-release:
sh crates/ffi/build-rust-ios-release.sh

test:
cargo test --workspace

Expand All @@ -66,6 +69,20 @@ local-infra-forked:
local-infra-7702:
cd test/scripts/7702 && sh local-infra.sh

.PHONY: zip-rust-xcframework
zip-rust-xcframework:
mkdir -p Output
cd crates/ffi/YttriumCore/ && zip -r ../../../Output/RustXcframework.xcframework.zip RustXcframework.xcframework

.PHONY: compute-rust-checksum
compute-rust-checksum:
swift package compute-checksum Output/RustXcframework.xcframework.zip > rust_checksum.txt
echo "Rust XCFramework checksum: $$(cat rust_checksum.txt)"

.PHONY: generate-package-swift
generate-package-swift:
./scripts/generate_package_swift.sh

.PHONY: build setup build-ios-bindings build-swift-apple-platforms test-swift-apple-platforms fetch-thirdparty setup-thirdparty test format clean local-infra local-infra-forked local-infra-7702

define udid_for
Expand Down
36 changes: 31 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// swift-tools-version: 5.10
import PackageDescription
import Foundation

let isDevelopment = ProcessInfo.processInfo.environment["YTTRIUM_DEVELOPMENT"] == "false"

let rustBinaryTarget: Target = {
guard isDevelopment else {
return rustBinaryTarget = .binaryTarget(
name: "RustXcframework",
url: "$REPO_URL/releases/download/$PACKAGE_VERSION/$RUST_XCFRAMEWORK_ZIP",
checksum: "$RUST_CHECKSUM"
)
}
return .binaryTarget(
name: "RustXcframework",
path: "crates/ffi/YttriumCore/RustXcframework.xcframework"
)
}()

let package = Package(
name: "yttrium",
Expand All @@ -12,10 +29,10 @@ let package = Package(
products: [
.library(
name: "Yttrium",
targets: ["Yttrium"]),
targets: ["Yttrium"]
),
],
dependencies: [
.package(path: "crates/ffi/YttriumCore"),
.package(url: "https://github.com/thebarndog/swift-dotenv.git", from: "2.0.0")
],
targets: [
Expand All @@ -25,14 +42,23 @@ let package = Package(
"YttriumCore",
.product(name: "SwiftDotenv", package: "swift-dotenv")
],
path: "platforms/swift/Sources/Yttrium")
,
path: "platforms/swift/Sources/Yttrium"
),
.target(
name: "YttriumCore",
dependencies: [
"RustXcframework"
],
path: "crates/ffi/YttriumCore/Sources/YttriumCore"
),
rustBinaryTarget,
.testTarget(
name: "YttriumTests",
dependencies: [
"Yttrium" ,
.product(name: "SwiftDotenv", package: "swift-dotenv")
],
path: "platforms/swift/Tests/YttriumTests"),
path: "platforms/swift/Tests/YttriumTests"
),
]
)
54 changes: 54 additions & 0 deletions crates/ffi/build-rust-ios-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# build-rust-ios.sh

#!/bin/bash

PACKAGE_NAME=ffi
SWIFT_PACKAGE_NAME=YttriumCore

set -e

THISDIR=$(dirname $0)
cd $THISDIR

echo "Building for iOS..."

rustup target add aarch64-apple-ios
rustup target add x86_64-apple-ios
rustup target add aarch64-apple-ios-sim

cargo build --release --target aarch64-apple-ios
cargo build --release --target x86_64-apple-ios
cargo build --release --target aarch64-apple-ios-sim

mkdir -p ./../../target/universal-ios/release

echo "Lipoing for iOS..."

lipo \
./../../target/aarch64-apple-ios-sim/release/lib$PACKAGE_NAME.a \
./../../target/x86_64-apple-ios/release/lib$PACKAGE_NAME.a -create -output \
./../../target/universal-ios/release/lib$PACKAGE_NAME.a

function create_package {
cargo install -f swift-bridge-cli
swift-bridge-cli create-package \
--bridges-dir ./generated \
--out-dir $SWIFT_PACKAGE_NAME \
--ios ./../../target/aarch64-apple-ios/release/lib$PACKAGE_NAME.a \
--simulator ./../../target/universal-ios/release/lib$PACKAGE_NAME.a \
--name $SWIFT_PACKAGE_NAME
}

# Check if Package.swift file exists
if [ -f $SWIFT_PACKAGE_NAME/Package.swift ]; then
echo "Package.swift already exists. Copying existing file to backup..."
rm -f $SWIFT_PACKAGE_NAME/Package.swift.bak
cp $SWIFT_PACKAGE_NAME/Package.swift $SWIFT_PACKAGE_NAME/Package.swift.bak
echo "Creating Swift package..."
create_package
cp $SWIFT_PACKAGE_NAME/Package.swift.bak $SWIFT_PACKAGE_NAME/Package.swift
rm -f $SWIFT_PACKAGE_NAME/Package.swift.bak
else
echo "Creating Swift package..."
create_package
fi
80 changes: 80 additions & 0 deletions scripts/generate_package_swift.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

set -e

# Variables
# PACKAGE_VERSION="${GITHUB_VERSION:-0.0.1-alpha}"
PACKAGE_VERSION="${GITHUB_VERSION:-0.0.1-alpha}-test"
RUST_CHECKSUM=$(cat rust_checksum.txt)
RUST_XCFRAMEWORK_ZIP="RustXcframework.xcframework.zip"
REPO_URL="https://github.com/WalletConnect/yttrium"

# Generate Package.swift
cat > Package.swift <<EOF
// swift-tools-version: 5.10
import PackageDescription
import Foundation
let isDevelopment = ProcessInfo.processInfo.environment["YTTRIUM_DEVELOPMENT"] == "false"
let rustBinaryTarget: Target = {
guard isDevelopment else {
return .binaryTarget(
name: "RustXcframework",
url: "$REPO_URL/releases/download/$PACKAGE_VERSION/$RUST_XCFRAMEWORK_ZIP",
checksum: "$RUST_CHECKSUM"
),
}
return .binaryTarget(
name: "RustXcframework",
path: "crates/ffi/YttriumCore/RustXcframework.xcframework"
)
}()
let package = Package(
name: "yttrium",
platforms: [
.macOS(.v14),
.iOS(.v13),
.watchOS(.v10),
.tvOS(.v17)
],
products: [
.library(
name: "Yttrium",
targets: ["Yttrium"]
),
],
dependencies: [
.package(url: "https://github.com/thebarndog/swift-dotenv.git", from: "2.0.0")
],
targets: [
.target(
name: "Yttrium",
dependencies: [
"YttriumCore",
.product(name: "SwiftDotenv", package: "swift-dotenv")
],
path: "platforms/swift/Sources/Yttrium"
),
.target(
name: "YttriumCore",
dependencies: [
"RustXcframework"
],
path: "crates/ffi/YttriumCore/Sources/YttriumCore"
),
rustBinaryTarget,
.testTarget(
name: "YttriumTests",
dependencies: [
"Yttrium" ,
.product(name: "SwiftDotenv", package: "swift-dotenv")
],
path: "platforms/swift/Tests/YttriumTests"
),
]
)
EOF

echo "Package.swift generated with Rust XCFramework checksum: $RUST_CHECKSUM"

0 comments on commit 545959e

Please sign in to comment.