chinedufn / swift-bridge

swift-bridge facilitates Rust and Swift interop.
https://chinedufn.github.io/swift-bridge
Apache License 2.0
842 stars 62 forks source link

Unresolved FFI Types When Using Binary Target #294

Closed llbartekll closed 1 month ago

llbartekll commented 1 month ago

When releasing my library as a binary target, consumers encounter errors related to unresolved FFI types. Specifically, the type FFIEndpoint cannot be found in scope. However, when building and using the bindings locally, everything works as expected.

        .binaryTarget(
            name: "RustXcframework",
            url: "https://github.com/.../RustXcframework.xcframework.zip",
            checksum: "2a9b8823a8b6184e88c14ec4d6f64e0a39c57c2efb3b1"
        ),

Error in the swift framework consuming binary target: Cannot find type 'FFIEndpoint' in scope

Expected behaviour: I can depend on GH hosted xcframework not only local package.

Script for building the package:

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

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

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

echo "Lipoing for iOS..."

lipo \
    ./../../target/aarch64-apple-ios-sim/debug/lib$PACKAGE_NAME.a \
    ./../../target/x86_64-apple-ios/debug/lib$PACKAGE_NAME.a -create -output \
    ./../../target/universal-ios/debug/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/debug/lib$PACKAGE_NAME.a \
        --simulator ./../../target/universal-ios/debug/lib$PACKAGE_NAME.a \
        --name $SWIFT_PACKAGE_NAME
}

and for GH releases I zip it, compute checksum and upload the xcframework

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

Type Definition in lib.rs:

#[swift_bridge::bridge]
mod ffi {

    #[derive(Debug, Clone)]
    #[swift_bridge(swift_repr = "struct")]
    pub struct FFIEndpoint {
        pub api_key: String,
        pub base_url: String,
    }
    ...
chinedufn commented 1 month ago

What was the problem?