Skip to content

Commit

Permalink
Replace cargo-swift with equivalent script (#15)
Browse files Browse the repository at this point in the history
Building the framework with a shell script will (hopefully) make it clearer what's actually going on.

Going forward it will also give us more control over the exact procedure like for instance whether to build universal binaries or support arm64 only. For example it's only necessary to build a single target on the CI when running unit tests (for now we build for all platforms on the CI).

Last but not least it will allow us to remove a dependency that we don't actually need.
  • Loading branch information
bisgardo authored Jan 30, 2024
1 parent e9acd00 commit b5966e0
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 14 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/build+test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ jobs:
- name: Setup Rust
run: |
rustup default "${{env.rust_version}}"
rustup target install aarch64-apple-darwin x86_64-apple-darwin
cargo install cargo-swift
rustup target install aarch64-apple-darwin aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-darwin x86_64-apple-ios
- name: Check out sources
uses: actions/checkout@v4
with:
submodules: recursive
- name: Build rust bindings (macOS only)
run: cargo swift package --name=ConcordiumWalletCrypto --platforms=macos --release
- name: Build rust bindings
run: ./build.sh
working-directory: ./lib/crypto
- name: Check formatting
run: swift package plugin --allow-writing-to-package-directory swiftformat --lint
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ This SDK includes a thin wrapper for providing bindings to the Rust library
[`wallet_library`](https://github.com/Concordium/concordium-base/tree/main/rust-src/wallet_library)
which exposes functions specifically relevant for wallets.

These bindings are located in `./lib/crypto` and built using [`cargo-swift`](https://github.com/antoniusnaumann/cargo-swift/)
into a [XCFramework](https://developer.apple.com/documentation/xcode/distributing-binary-frameworks-as-swift-packages).
These bindings are located in `./lib/crypto` and compiled into an
[XCFramework](https://developer.apple.com/documentation/xcode/distributing-binary-frameworks-as-swift-packages).
The SDK pulls in this framework from a local path, so the bindings have to built manually before the SDK can be used.

Building is only a matter of installing `cargo-swift` and invoking a Make target:
Building is only a matter of invoking the Make target

```shell
cargo install cargo-swift
make build-crypto
```

This will place the target framework at `./lib/crypto/ConcordiumWalletCrypto`.
This will place the target framework in `./lib/crypto/ConcordiumWalletCrypto`.

### Build and test SDK

Expand Down
1 change: 0 additions & 1 deletion lib/crypto/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/target
/generated
/ConcordiumWalletCrypto
113 changes: 113 additions & 0 deletions lib/crypto/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions lib/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ crate-type = ["staticlib", "lib"]
name = "crypto"

[dependencies]
thiserror = "1.0.56"
uniffi = "0.25"
thiserror = "1"
uniffi = { version = "0.25", features = ["cli"] }
wallet_library = { path = "./concordium-base/rust-src/wallet_library" }

[build-dependencies]
uniffi = { version = "0.25", features = ["build"] }

[[bin]]
name = "uniffi-bindgen"
path = "uniffi-bindgen.rs"
3 changes: 3 additions & 0 deletions lib/crypto/ConcordiumWalletCrypto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!*ignore
!/Package.swift
29 changes: 29 additions & 0 deletions lib/crypto/ConcordiumWalletCrypto/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// swift-tools-version:5.6

import PackageDescription

let package = Package(
name: "ConcordiumWalletCrypto",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
],
products: [
.library(
name: "ConcordiumWalletCrypto",
targets: ["ConcordiumWalletCrypto"]
),
],
targets: [
.binaryTarget(
name: "RustFramework",
path: "./RustFramework.xcframework"
),
.target(
name: "ConcordiumWalletCrypto",
dependencies: [
.target(name: "RustFramework"),
]
),
]
)
39 changes: 37 additions & 2 deletions lib/crypto/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,40 @@

set -eux

rm -rf ./ConcordiumWalletCrypto/
cargo swift package --name=ConcordiumWalletCrypto --platforms=ios --platforms=macos --release
# Generate bindings/bridge code.
mkdir -p ./ConcordiumWalletCrypto/Sources/ConcordiumWalletCrypto
cargo run --bin uniffi-bindgen generate src/lib.udl --language=swift --out-dir=./generated
mv ./generated/crypto.swift ./ConcordiumWalletCrypto/Sources/ConcordiumWalletCrypto/crypto.swift
mv ./generated/cryptoFFI.modulemap ./generated/module.modulemap

# Compile for Darwin (macOS) as universal binary.
cargo build --target=x86_64-apple-darwin --release
cargo build --target=aarch64-apple-darwin --release
mkdir -p ./target/universal/darwin/release
lipo \
./target/x86_64-apple-darwin/release/libcrypto.a \
./target/aarch64-apple-darwin/release/libcrypto.a \
-create -output ./target/universal/darwin/release/libcrypto.a

# Compile for iOS.
cargo build --target=aarch64-apple-ios --release

# Compile for iOS Simulator as universal binary.
cargo build --target=x86_64-apple-ios --release
cargo build --target=aarch64-apple-ios-sim --release
mkdir -p ./target/universal/ios/release
lipo \
./target/x86_64-apple-ios/release/libcrypto.a \
./target/aarch64-apple-ios-sim/release/libcrypto.a \
-create -output ./target/universal/ios/release/libcrypto.a

# Build binary framework.
rm -rf ./ConcordiumWalletCrypto/RustFramework.xcframework
xcodebuild -create-xcframework \
-library ./target/universal/darwin/release/libcrypto.a -headers ./generated \
-library ./target/aarch64-apple-ios/release/libcrypto.a -headers ./generated \
-library ./target/universal/ios/release/libcrypto.a -headers ./generated \
-output ./ConcordiumWalletCrypto/RustFramework.xcframework

# Clean up.
rm -rf ./generated
3 changes: 3 additions & 0 deletions lib/crypto/uniffi-bindgen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
uniffi::uniffi_bindgen_main()
}

0 comments on commit b5966e0

Please sign in to comment.