/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use anyhow::{bail, Context, Result}; use camino::Utf8PathBuf; use clap::{Parser, Subcommand}; use std::fmt; use uniffi_bindgen::bindings::*;
/// Enumeration of all foreign language targets currently supported by our CLI. /// #[derive(Copy, Clone, Eq, PartialEq, Hash, clap::ValueEnum)] enum TargetLanguage {
Kotlin,
Swift,
Python,
Ruby,
}
#[derive(Subcommand)] enum Commands { /// Generate foreign language bindings
Generate { /// Foreign language(s) for which to build bindings. #[clap(long, short, value_enum)]
language: Vec<TargetLanguage>,
/// Directory in which to write generated files. Default is same folder as .udl file. #[clap(long, short)]
out_dir: Option<Utf8PathBuf>,
/// Do not try to format the generated bindings. #[clap(long, short)]
no_format: bool,
/// Path to optional uniffi config file. This config is merged with the `uniffi.toml` config present in each crate, with its values taking precedence. #[clap(long, short)]
config: Option<Utf8PathBuf>,
/// Extract proc-macro metadata from a native lib (cdylib or staticlib) for this crate. #[clap(long)]
lib_file: Option<Utf8PathBuf>,
/// Pass in a cdylib path rather than a UDL file #[clap(long = "library")]
library_mode: bool,
/// When `--library` is passed, only generate bindings for one crate. /// When `--library` is not passed, use this as the crate name instead of attempting to /// locate and parse Cargo.toml. #[clap(long = "crate")]
crate_name: Option<String>,
/// Path to the UDL file, or cdylib if `library-mode` is specified
source: Utf8PathBuf,
/// 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,
},
/// Generate Rust scaffolding code
Scaffolding { /// Directory in which to write generated files. Default is same folder as .udl file. #[clap(long, short)]
out_dir: Option<Utf8PathBuf>,
/// Do not try to format the generated bindings. #[clap(long, short)]
no_format: bool,
/// Path to the UDL file.
udl_file: Utf8PathBuf,
},
/// Print a debug representation of the interface from a dynamic library
PrintRepr { /// Path to the library file (.so, .dll, .dylib, or .a)
path: Utf8PathBuf,
},
}
#[cfg(feature = "cargo-metadata")] let config_supplier = { use uniffi_bindgen::cargo_metadata::CrateConfigSupplier; letmut cmd = cargo_metadata::MetadataCommand::new(); if metadata_no_deps {
cmd.no_deps();
} let metadata = cmd.exec().context("error running cargo metadata")?;
CrateConfigSupplier::from(metadata)
}; #[cfg(not(feature = "cargo-metadata"))] let config_supplier = uniffi_bindgen::EmptyCrateConfigSupplier;
for language in languages { // to help avoid mistakes we check the library is actually a cdylib, except // for swift where static libs are often used to extract the metadata. if !matches!(language, TargetLanguage::Swift) && !uniffi_bindgen::is_cdylib(library_path) {
anyhow::bail!( "Generate bindings for {language} requires a cdylib, but {library_path} was given"
);
}
// Type-bounds on trait implementations makes selecting between languages a bit tedious. match language {
TargetLanguage::Kotlin => generate_bindings(
library_path,
crate_name.clone(),
&KotlinBindingGenerator,
&config_supplier,
cfo,
out_dir,
fmt,
)?
.len(),
TargetLanguage::Python => generate_bindings(
library_path,
crate_name.clone(),
&PythonBindingGenerator,
&config_supplier,
cfo,
out_dir,
fmt,
)?
.len(),
TargetLanguage::Ruby => generate_bindings(
library_path,
crate_name.clone(),
&RubyBindingGenerator,
&config_supplier,
cfo,
out_dir,
fmt,
)?
.len(),
TargetLanguage::Swift => generate_bindings(
library_path,
crate_name.clone(),
&SwiftBindingGenerator,
&config_supplier,
cfo,
out_dir,
fmt,
)?
.len(),
};
}
Ok(())
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.