#swift #packages #cargo #building #cargo-subcommand #framework #generated

bin+lib swift-package

Cargo plugin and library for building Apple Swift Packages

2 releases

new 0.1.0 Nov 28, 2024
0.1.0-alpha.0 Apr 5, 2023

#24 in #packages

Download history 6/week @ 2024-09-19 9/week @ 2024-09-26 3/week @ 2024-10-03

70 downloads per month

MIT license

23KB
504 lines

⚠️ WARNING

This has not yet been thoroughly tested. Use at your own risk.

swift-package

This is a Cargo command for building Swift packages.

This crate uses uniffi to generate swift bindings and xcframework to build the binary framework, and then wraps it up in a Swift package together with any resources and a resource accessor.

See the end-to-end example for how to set it up.

Using the generated swift package:

import Foundation
import SwiftMath

@main
public struct swift_cmd {

    public static func main() {
        // the SwiftMath.resources(name:) func is generated by swift-package.
        let helloFile = SwiftMath.resources(name: "hello.txt")
        let hello = try! String(contentsOf: helloFile)

        let sum = SwiftMath.rustAdd(a: 4, b: 2)
        print(
            "SwiftMath.swift_add(4 + 2) = \(sum); from resource file: \(hello)"
        )
    }
}

It uses the xcframework configuration options except the include-dir one plus it's own configuration options:

[package.metadata.swift-package]
# Any options that are valid for the xcframework command line utility
# can be set here. The options are passed to the xcframework command.
macOS = true

#### swift-package specific options ####

# The name of the generated package
package-name = "SwiftMath"

# Any resource directories. The files will be included
# in the swift package and a convenience accessor extesion
# of the same name as the dir is created
resource-dirs = ["resources"]

Another alternative is cargo-swift

Dependencies

~8–16MB
~212K SLoC