cargo-swift icon indicating copy to clipboard operation
cargo-swift copied to clipboard

Handling varying APIs by OS

Open coolbluewater opened this issue 1 year ago • 1 comments

Hi, thanks for this tool! I was building something similar and found this.

I'm moving from cbindgen and simple xcframeworks to using uniffi and cargo-swift to generate the swift package.

My Rust crate has some differences between the API's exposed for iOS and macOS. This works fine with xcframeworks, since each target platform has its own set of headers and library within the xcframework.

The generated Greeter swift package for the hello-world example only a single header file, but does contain an xcframework. This code in package.rs seems to generate bindings for the first provided architecture only:

        let archs = targets
            .first()
            .ok_or("Could not generate UniFFI bindings: No target platform selected!")?
            .architectures();
        let arch = archs.first();
        let lib_path: Utf8PathBuf = format!("{target}/{arch}/{mode}/{lib_file}").into();

Question: Is this due to a limitation of swift packages, or can the generated swift package have multiple swift files, with conditional expressions to indicate which target platform each file is for?

Update: I found this document which indicates that conditional target dependencies are supported by swift packages.

In light of the above, could we please change cargo-swift to emit swift files with conditional dependencies per target os?

coolbluewater avatar Feb 25 '24 16:02 coolbluewater

Switching features dependent on target os would be a very useful feature indeed!

I'll look into it, but it will take a while until I can work on this myself.

antoniusnaumann avatar Feb 25 '24 18:02 antoniusnaumann