-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation on creating a Swift Package #23
Comments
Hey! If I'm understanding, you're saying that you'd like to be able to have the Rust side of things get packaged up into a Swift package that you can then import from your Swift project? Or are you saying something else? I'll read up on Swift packages since I don't know much about them at the moment. Would using Swift packages be strictly better to what we're currently doing, or are there any downsides? Also, if you could explain your Swift+Rust use case that would be great - just so that I can better understand when and how Swift packages are typically used (I'm new to Swift). Thanks for opening this issue! |
Alright I read up on Swift packages on modules and have a basic understanding. It seems like if I had to find the closest Rust ecosystem version of them..: Swift Package == Cargo crate I know they aren't the same.. I'm just going for a rough understanding of what these Swift code organization tools are. Ok so I'd sum up my current thoughts as:
|
I do think that compiling to a Swift package could be a huge plus. Right now we can compile Swift code using If our Swift code is in package form, we can import it in every Swift project (as long as we have compiled our rust code to the right targets, of course). Swift packages are similar to Rust crates, you can create one with Other projects or packages can then depend on the package by specifying the package in the I guess this would be similar to any other Swift package that wraps a C api (Can't remember a good, simple example, but here is one for LLVM: https://github.com/llvm-swift/llvmswift). Really love this project, if you need any help, I would love to contribute where I can. EDIT: I'll also do some research myself and will let you know if I get some working prototype |
So far I've had no luck, my knowledge of C and headers is quite limited, though. So far I have found the following:
|
Thanks the gist and for all of the links. I've read through them and they were helpful as I start to get familiar with Swift packages / modules / etc. Awesome work and research so far..
I'd love for you to contribute! Awesome, going forwards I'll start creating more issues and you can get involved wherever you like. Figuring out this Swift Package stuff might be a great place to start. Alright.. I did some more reading and thinking and here's where I'm at.
So, here's a potential path forwards (which I think you've already made progress on in your gist.)
On the flip side.. we'll also want to eventually explore how to expose |
Awesome. I'll be looking through the code of this package to get a better understanding of how this all works. After that I can have another attempt at converting to a Swift package. |
Nice! Just let me know whenever you have any questions and I'll try to add answers to the currently empty internal design chapter. |
Ok, I have started looking at the internals of the project. I just wanted to finish a few of my own projects first before I tackled this. So if I understand correctly, the With |
Awesome!! I'm not familiar with XCFramework but in general that all sounds about right. |
So far, I have packaged a system library and a c source file into a Swift package. I've had no luck with including a static library, though. If you have any ideas on including the rust library, let me know, because so far I've had no luck. |
I think this might walk you through the entire process of going from Rust code to Swift package -> https://betterprogramming.pub/from-rust-to-swift-df9bde59b7cd Let me know if that helps? |
A quote from Apple's documentation https://developer.apple.com/documentation/swift_packages/distributing_binary_frameworks_as_swift_packages :
At the very least I think we'll want our own documentation to make it very clear to people that if they're creating a Swift package it can only be used on Apple hardware. Which I'd imagine that the vast majority of users will be just fine with. Not important right now.. Just dropping it here for us to keep in mind as we think through this stuff. |
Judging by the linked repo, I’m almost there with linking an XCFramework to Swift.
I’ve come across that as well, so unfortunately this approach will indeed not work for non-Apple devices. I haven’t come across any other solutions to this. I guess we will have to wait until some cross-platform solution comes around. |
Aha! Seems like all I was missing was a modulemap file. I'll clean up what I have now and try it from scratch again. I'll update you soon |
Ok, here's what I have. Source code on this branch. 1. RustI have a Rust project with these functions: fn print_hello_rust() {
println!("Hello from Rust!");
}
fn is_from_rust() -> bool {
true
}
fn get_hello_rust() -> String {
String::from("Hello Rust!")
} Build the project like you would do usually with the build.rs file specified in the book. 2. XCFrameworkThe next step is to make the XCFramework.
We have a folder for each target we build for our rust project and copied both header files as well. We can then build the XCFramework
The order of the targets is important Note: we could also build the framework without this command I think, as it is just a folder containing the different targets and a 3. Swift packageNow, we build the swift package that incorporates the XCFramework. We copy the let package = Package(
name: "package",
products: [
.library(
name: "package",
targets: ["package"]),
],
dependencies: [
],
targets: [
.binaryTarget(
name: "Rust",
path: "rust.xcframework"
),
.target(
name: "package",
dependencies: ["Rust"]),
]
) Then, we copy the generated swift files to the Now, we should also make the methods in 4. TestingAfter the previous steps, we now have a Swift package that can be used in any project targeting MacOs or iOS (we can of course target other Apple devices as well if we build those targets).
Tested on an executable on MacOS and on the simulator for iOS, and a physical device. Next stepsI can add a chapter to the book explaining the whole process in detail (basically the above, but a little more "cleaned up"). Let me know if you have any comments on the above. Next would be generating a Swift package with the build file? I think you know best on how we should implement this into swift-bridge-build. |
Awesome work! Adding a chapter to the book sounds great. My main feedback would be to call the crate something other than In terms of generating a package from programatically.. let's create a new issue for that. But yeah I'm happy to help figure out a good starting point for that. Thanks for making so much progress already! |
Closed by #30 |
Hey! just want to say thankyou so much for putting this together. Packaging my code as a swift package is way cleaner for my use case, and I appreciate the effort making it easy! Lots of gratitude for swift-bridge and this PR! |
Hi, I'm the one who asked about using Rust in Swift on Reddit.
I've made a Gist that follows your steps, but slightly modified to set up a Swift project with Swift Package Manager (https://gist.github.com/Jomy10/a4873dd43942ed1bf54d387dbc888795).
I've tried to add dependencies to
Package.swift
, but I don't know how to include them in the compiling process. Swiftc documentation seems to be rather scarce.Here's an example of the
Package.swift
file:Folder structure is also in the gist.
I think it would be great if we could also use Swift Package Manager in the Swift project using the Rust library. Or maybe import the Rust library as a package in
Package.Swift
.The text was updated successfully, but these errors were encountered: