From b5966e0d28309219f9dc657097b5b83d638e78d1 Mon Sep 17 00:00:00 2001 From: Michael Bisgaard Olesen Date: Tue, 30 Jan 2024 10:57:05 +0100 Subject: [PATCH] Replace cargo-swift with equivalent script (#15) 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. --- .github/workflows/build+test.yml | 7 +- README.md | 9 +- lib/crypto/.gitignore | 1 - lib/crypto/Cargo.lock | 113 ++++++++++++++++++ lib/crypto/Cargo.toml | 8 +- lib/crypto/ConcordiumWalletCrypto/.gitignore | 3 + .../ConcordiumWalletCrypto/Package.swift | 29 +++++ lib/crypto/build.sh | 39 +++++- lib/crypto/uniffi-bindgen.rs | 3 + 9 files changed, 198 insertions(+), 14 deletions(-) create mode 100644 lib/crypto/ConcordiumWalletCrypto/.gitignore create mode 100644 lib/crypto/ConcordiumWalletCrypto/Package.swift create mode 100644 lib/crypto/uniffi-bindgen.rs diff --git a/.github/workflows/build+test.yml b/.github/workflows/build+test.yml index 12f9dc3..39bafe6 100644 --- a/.github/workflows/build+test.yml +++ b/.github/workflows/build+test.yml @@ -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 diff --git a/README.md b/README.md index a9d86b9..2235d7f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/crypto/.gitignore b/lib/crypto/.gitignore index d4f0acb..56fca14 100644 --- a/lib/crypto/.gitignore +++ b/lib/crypto/.gitignore @@ -1,3 +1,2 @@ /target /generated -/ConcordiumWalletCrypto diff --git a/lib/crypto/Cargo.lock b/lib/crypto/Cargo.lock index df59453..b556099 100644 --- a/lib/crypto/Cargo.lock +++ b/lib/crypto/Cargo.lock @@ -49,6 +49,54 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.79" @@ -419,6 +467,52 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "clap" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "concordium-contracts-common" version = "9.0.0" @@ -1765,6 +1859,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21345172d31092fd48c47fd56c53d4ae9e41c4b1f559fb8c38c1ab1685fd919f" dependencies = [ "anyhow", + "camino", + "clap", + "uniffi_bindgen", "uniffi_build", "uniffi_core", "uniffi_macros", @@ -1780,6 +1877,7 @@ dependencies = [ "askama", "camino", "cargo_metadata", + "clap", "fs-err", "glob", "goblin", @@ -1886,6 +1984,12 @@ dependencies = [ "weedle2", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "uuid" version = "1.7.0" @@ -1990,6 +2094,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.0" diff --git a/lib/crypto/Cargo.toml b/lib/crypto/Cargo.toml index 64d9b28..3b558eb 100644 --- a/lib/crypto/Cargo.toml +++ b/lib/crypto/Cargo.toml @@ -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" diff --git a/lib/crypto/ConcordiumWalletCrypto/.gitignore b/lib/crypto/ConcordiumWalletCrypto/.gitignore new file mode 100644 index 0000000..43a034f --- /dev/null +++ b/lib/crypto/ConcordiumWalletCrypto/.gitignore @@ -0,0 +1,3 @@ +* +!*ignore +!/Package.swift diff --git a/lib/crypto/ConcordiumWalletCrypto/Package.swift b/lib/crypto/ConcordiumWalletCrypto/Package.swift new file mode 100644 index 0000000..d81bc02 --- /dev/null +++ b/lib/crypto/ConcordiumWalletCrypto/Package.swift @@ -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"), + ] + ), + ] +) diff --git a/lib/crypto/build.sh b/lib/crypto/build.sh index 8840c4f..1e01ce9 100755 --- a/lib/crypto/build.sh +++ b/lib/crypto/build.sh @@ -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 diff --git a/lib/crypto/uniffi-bindgen.rs b/lib/crypto/uniffi-bindgen.rs new file mode 100644 index 0000000..f6cff6c --- /dev/null +++ b/lib/crypto/uniffi-bindgen.rs @@ -0,0 +1,3 @@ +fn main() { + uniffi::uniffi_bindgen_main() +}