-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a `uniffi-bindgen` variant dedicated to Swift code, with options specialized for Swift. I'm currently thinking that we should consider doing this for all languages. In addition to allowing specialized options, it also makes the in-tree bindings less special-cased compared to external bindings.
- Loading branch information
Showing
13 changed files
with
299 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# uniffi-bindgen-swift | ||
|
||
Swift bindings can be generated like other languages using `uniffi-bindgen -l swift`. However, you | ||
can also use the `uniffi-bindgen-swift` binary which gives greater control over Swift-specific | ||
features: | ||
|
||
* Select which kind of files to generate: headers, modulemaps, and/or Swift sources. | ||
* Generate a single modulemap for a library. | ||
* Generate XCFramework-compatible modulemaps. | ||
* Customize the modulemap module name. | ||
* Customize the modulemap filename. | ||
|
||
`uniffi-bindgen-swift` can be added to your project using the same general steps as `uniffi-bindgen`. | ||
See https://mozilla.github.io/uniffi-rs/latest/tutorial/foreign_language_bindings.html#creating-the-bindgen-binary. | ||
The Rust source for the binary should be: | ||
|
||
``` | ||
fn main() { | ||
uniffi::uniffi_bindgen_swift() | ||
} | ||
``` | ||
|
||
`uniffi-bindgen-swift` always inputs a library path and runs in "library mode". This means | ||
proc-macro-based bindings generation is always supported. | ||
|
||
## Examples: | ||
|
||
|
||
Generate .swift source files for a library | ||
``` | ||
cargo run -p uniffi-bindgen-swift -- target/release/mylibrary.a build/swift --swift-sources | ||
``` | ||
|
||
Generate .h files for a library | ||
``` | ||
cargo run -p uniffi-bindgen-swift -- target/release/mylibrary.a build/swift/Headers --headers | ||
``` | ||
|
||
|
||
Generate a modulemap | ||
``` | ||
cargo run -p uniffi-bindgen-swift -- target/release/mylibrary.a build/swift/Modules --modulemap --modulemap-filename mymodule.modulemap | ||
``` | ||
|
||
Generate a Xcframework-compatible modulemap | ||
``` | ||
cargo run -p uniffi-bindgen-swift -- target/release/mylibrary.a build/swift/Modules --xcframework --modulemap --modulemap-filename mymodule.modulemap | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
mod swift; | ||
mod uniffi_bindgen; | ||
|
||
pub fn uniffi_bindgen_main() { | ||
if let Err(e) = uniffi_bindgen::run_main() { | ||
eprintln!("{e}"); | ||
std::process::exit(1); | ||
} | ||
} | ||
|
||
pub fn uniffi_bindgen_swift() { | ||
if let Err(e) = swift::run_main() { | ||
eprintln!("{e}"); | ||
std::process::exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use anyhow::Result; | ||
use camino::Utf8PathBuf; | ||
use clap::{Args, Parser}; | ||
|
||
use uniffi_bindgen::bindings::{generate_swift_bindings, SwiftBindingsOptions}; | ||
|
||
#[derive(Debug, Parser)] | ||
#[command(version, about, long_about = None)] | ||
struct Cli { | ||
#[command(flatten)] | ||
kinds: Kinds, | ||
/// Library path to generate bindings for | ||
library_path: Utf8PathBuf, | ||
/// Directory to generate files in | ||
out_dir: Utf8PathBuf, | ||
/// Generate a XCFramework-compatible modulemap | ||
#[arg(long)] | ||
xcframework: bool, | ||
/// module name for the generated modulemap | ||
#[arg(long)] | ||
module_name: Option<String>, | ||
/// filename for the generate modulemap | ||
#[arg(long)] | ||
modulemap_filename: Option<String>, | ||
/// Whether we should exclude dependencies when running "cargo metadata". | ||
/// This will mean external types may not be resolved if they are implemented in crates | ||
/// outside of this workspace. | ||
/// This can be used in environments when all types are in the namespace and fetching | ||
/// all sub-dependencies causes obscure platform specific problems. | ||
#[clap(long)] | ||
metadata_no_deps: bool, | ||
} | ||
|
||
#[derive(Debug, Args)] | ||
#[group(required = true, multiple = true)] | ||
struct Kinds { | ||
/// Generate swift files | ||
#[arg(long)] | ||
swift_sources: bool, | ||
|
||
/// Generate header files | ||
#[arg(long)] | ||
headers: bool, | ||
|
||
/// Generate modulemap | ||
#[arg(long)] | ||
modulemap: bool, | ||
} | ||
|
||
pub fn run_main() -> Result<()> { | ||
let cli = Cli::parse(); | ||
generate_swift_bindings(cli.into()) | ||
} | ||
|
||
impl From<Cli> for SwiftBindingsOptions { | ||
fn from(cli: Cli) -> Self { | ||
Self { | ||
generate_swift_sources: cli.kinds.swift_sources, | ||
generate_headers: cli.kinds.headers, | ||
generate_modulemap: cli.kinds.modulemap, | ||
library_path: cli.library_path, | ||
out_dir: cli.out_dir, | ||
xcframework: cli.xcframework, | ||
module_name: cli.module_name, | ||
modulemap_filename: cli.modulemap_filename, | ||
metadata_no_deps: cli.metadata_no_deps, | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.