diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/uniffi_build | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/uniffi_build')
-rw-r--r-- | third_party/rust/uniffi_build/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/uniffi_build/Cargo.toml | 39 | ||||
-rw-r--r-- | third_party/rust/uniffi_build/src/lib.rs | 62 |
3 files changed, 102 insertions, 0 deletions
diff --git a/third_party/rust/uniffi_build/.cargo-checksum.json b/third_party/rust/uniffi_build/.cargo-checksum.json new file mode 100644 index 0000000000..24e9426bc1 --- /dev/null +++ b/third_party/rust/uniffi_build/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"30736876caf953bd0040b6c0fc824744556bf52fe23d656c7d01442d4c180674","src/lib.rs":"485a0c0ab99077a1d4037f4deec9801e87a248196e7589a002556fb84973528a"},"package":"d035e50433ee3d52ab0dcdcf3b9b26f2cc2ec39294b3c07d95865a518709455f"}
\ No newline at end of file diff --git a/third_party/rust/uniffi_build/Cargo.toml b/third_party/rust/uniffi_build/Cargo.toml new file mode 100644 index 0000000000..aeb48fe328 --- /dev/null +++ b/third_party/rust/uniffi_build/Cargo.toml @@ -0,0 +1,39 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2021" +name = "uniffi_build" +version = "0.21.1" +authors = ["Firefox Sync Team <sync-team@mozilla.com>"] +description = "a multi-language bindings generator for rust (build script helpers)" +homepage = "https://mozilla.github.io/uniffi-rs" +documentation = "https://mozilla.github.io/uniffi-rs" +keywords = [ + "ffi", + "bindgen", +] +license = "MPL-2.0" +repository = "https://github.com/mozilla/uniffi-rs" + +[dependencies.anyhow] +version = "1" + +[dependencies.camino] +version = "1.0.8" + +[dependencies.uniffi_bindgen] +version = "=0.21.1" +optional = true + +[features] +builtin-bindgen = ["uniffi_bindgen"] +default = [] diff --git a/third_party/rust/uniffi_build/src/lib.rs b/third_party/rust/uniffi_build/src/lib.rs new file mode 100644 index 0000000000..2532b4be67 --- /dev/null +++ b/third_party/rust/uniffi_build/src/lib.rs @@ -0,0 +1,62 @@ +/* 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::{Context, Result}; +use camino::Utf8Path; +use std::env; + +/// Generate the rust "scaffolding" required to build a uniffi component. +/// +/// Given the path to an UDL file, this function will call the `uniffi-bindgen` +/// command-line tool to generate the `pub extern "C"` functions and other supporting +/// code required to expose the defined interface from Rust. The expectation is that +/// this will be called from a crate's build script, and the resulting file will +/// be `include!()`ed into the build. +/// +/// Given an UDL file named `example.udl`, the generated scaffolding will be written +/// into a file named `example.uniffi.rs` in the `$OUT_DIR` directory. +/// +/// If the "builtin-bindgen" feature is enabled then this will take a dependency on +/// the `uniffi_bindgen` crate and call its methods directly, rather than using the +/// command-line tool. This is mostly useful for developers who are working on uniffi +/// itself and need to test out their changes to the bindings generator. +pub fn generate_scaffolding(udl_file: impl AsRef<Utf8Path>) -> Result<()> { + let udl_file = udl_file.as_ref(); + + println!("cargo:rerun-if-changed={udl_file}"); + // The UNIFFI_TESTS_DISABLE_EXTENSIONS variable disables some bindings, but it is evaluated + // at *build* time, so we need to rebuild when it changes. + println!("cargo:rerun-if-env-changed=UNIFFI_TESTS_DISABLE_EXTENSIONS"); + // Why don't we just depend on uniffi-bindgen and call the public functions? + // Calling the command line helps making sure that the generated swift/Kotlin/whatever + // bindings were generated with the same version of uniffi as the Rust scaffolding code. + let out_dir = env::var("OUT_DIR").context("$OUT_DIR missing?!")?; + run_uniffi_bindgen_scaffolding(out_dir.as_ref(), udl_file) +} + +#[cfg(not(feature = "builtin-bindgen"))] +fn run_uniffi_bindgen_scaffolding(out_dir: &Utf8Path, udl_file: &Utf8Path) -> Result<()> { + use anyhow::bail; + use std::process::Command; + + let status = Command::new("uniffi-bindgen") + .arg("scaffolding") + .arg("--out-dir") + .arg(out_dir) + .arg(udl_file) + .status() + .context( + "failed to run `uniffi-bindgen` - \ + have you installed it via `cargo install uniffi_bindgen`?", + )?; + if !status.success() { + bail!("Error while generating scaffolding code"); + } + Ok(()) +} + +#[cfg(feature = "builtin-bindgen")] +fn run_uniffi_bindgen_scaffolding(out_dir: &Utf8Path, udl_file: &Utf8Path) -> Result<()> { + uniffi_bindgen::generate_component_scaffolding(udl_file, None, Some(out_dir), false) +} |