Skip to content
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

wip: example binary to build swift package (#2382) #2383

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"weedle2",

"examples/app/uniffi-bindgen-cli",
"examples/app/uniffi-bindgen-swift-package-cli",
"examples/arithmetic",
"examples/arithmetic-procmacro",
"examples/async-api-client",
Expand Down
15 changes: 15 additions & 0 deletions examples/app/uniffi-bindgen-swift-package-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "uniffi-bindgen-swift-package-cli"
version = "0.1.0"
edition = "2021"
publish = false

[[bin]]
name = "uniffi-bindgen-swift-package"
path = "uniffi-bindgen-swift-package.rs"

[dependencies]
uniffi = { workspace = true, features = ["cli"] }

[features]
ffi-trace = ["uniffi/ffi-trace"]
67 changes: 67 additions & 0 deletions examples/app/uniffi-bindgen-swift-package-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# uniffi-bindgen-swift-package-cli

An example binary for building Swift packages based on Uniffi bindgen.

The `--build` and its related options are provided as convenience to build all required targets.

The resulting Swift package can be used as a local path dependnecy directly. Currently code
signing and notorization is out of scope for this application.

```
Usage: uniffi-bindgen-swift-package [OPTIONS] --package-name <SPEC> --out-dir <OUT_DIR>

Options:
-p, --package-name <SPEC>
Rust package to use for building the Swift package

Cargo metadata will be searched to determine the library name.

--library-type <LIB_TYPE>
Type of library to package, defaults to staticlib

Possible values:
- staticlib: Build an embedded XCFramework with static libraries
- dylib: Build an embedded XCFrameowrk with embedded dynamic Framework libraries

-o, --out-dir <OUT_DIR>
Directory in which to generate the Swift package, i.e., Package.swift parent dir

--swift-package-name <NAME>
Swift package name

Defaults to the package library name.

--manifest-path <MANIFEST_PATH>
Path to manifest for Rust workspace/package

Defaults to search from current working path

-c, --consolidate
Consolidate crate bindings into single Swift target.

Otherwise separate targets will be generated to help avoid name conflicts.

-b, --build
Builds package for specified targets.

Otherwise assumes all targets have been built in the default target dir.

-r, --release
Build artifacts in release mode, with optimization

Requires build flag to be set

-F, --features <FEATURES>
Space or comma separated list of features to activate

Requires build flag to be set

--target <TARGET>
Target for target triple to include

-h, --help
Print help (see a summary with '-h')

-V, --version
Print version
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
uniffi::uniffi_bindgen_swift_package()
}
8 changes: 8 additions & 0 deletions uniffi/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

mod swift;
mod swift_package;
mod uniffi_bindgen;

pub fn uniffi_bindgen_main() {
Expand All @@ -18,3 +19,10 @@ pub fn uniffi_bindgen_swift() {
std::process::exit(1);
}
}

pub fn uniffi_bindgen_swift_package() {
if let Err(e) = swift_package::run_main() {
eprintln!("{e:?}");
std::process::exit(1);
}
}
94 changes: 94 additions & 0 deletions uniffi/src/cli/swift_package.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use anyhow::{bail, Result};
use camino::Utf8PathBuf;
use clap::{command, Parser};

// TODO: Add all-features and no-default-features options
// TODO: Add univeral library override option(s)

#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Rust package to use for building the Swift package
///
/// Cargo metadata will be searched to determine the library name.
#[arg(short = 'p', long, value_name = "SPEC")]
package_name: String,

/// Type of library to package, defaults to staticlib.
#[arg(long, value_name = "LIB_TYPE")]
library_type: Option<LibraryType>,

/// Directory in which to generate the Swift package, i.e., Package.swift parent dir
#[arg(short = 'o', long)]
out_dir: Utf8PathBuf,

/// Swift package name
///
/// Defaults to the package library name.
#[arg(long, value_name = "NAME")]
swift_package_name: Option<String>,

/// Path to manifest for Rust workspace/package
///
/// Defaults to search from current working path
#[arg(long, value_name = "MANIFEST_PATH")]
manifest_path: Option<Utf8PathBuf>,

/// Consolidate crate bindings into single Swift target.
///
/// Otherwise separate Swift targets will be generated
#[arg(short = 'c', long)]
consolidate: bool,

/// Builds package for specified targets.
///
/// Otherwise assumes all targets have been built in the default target dir.
#[arg(short = 'b', long)]
build: bool,

/// Build artifacts in release mode, with optimization
///
/// Requires build flag to be set
#[arg(short = 'r', long)]
release: bool,

/// Space or comma separated list of features to activate
///
/// Requires build flag to be set
#[arg(short = 'F', long)]
features: Vec<String>,

/// Target for target triple to include
#[arg(long)]
target: Vec<String>,
}

#[derive(Clone, Debug, clap::ValueEnum)]
enum LibraryType {
/// Build an embedded XCFramework with static libraries
Staticlib,
/// Build an embedded XCFrameowrk with embedded dynamic Framework libraries
Dylib,
}

pub fn run_main() -> Result<()> {
let _ = Cli::parse();

// TODO: Specify targets from command line with default.
let _targets = [
"x86_64-apple-ios",
"aarch64-apple-ios-sim",
"aarch64-apple-ios",
"x86_64-apple-darwin",
"aarch64-apple-darwin",
];

// TODO: Override xcframework libraries from command line.
let _library_targets = [
vec!["x86_64-apple-ios", "aarch64-apple-ios-sim"], // iOS simulator
vec!["aarch64-apple-ios"], // iOS
vec!["x86_64-apple-darwin", "aarch64-apple-darwin"], // macOS
];

bail!("Building a Swift Package is unimplemented!");
}