Debian Codegraph @

third_party/rust/uniffi_build/src/lib.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use anyhow::{Context, Result};
6 use camino::Utf8Path;
7 use std::env;
8
9 /// Generate the rust "scaffolding" required to build a uniffi component.
10 ///
11 /// Given the path to an UDL file, this function will call the `uniffi-bindgen`
12 /// command-line tool to generate the `pub extern "C"` functions and other supporting
13 /// code required to expose the defined interface from Rust. The expectation is that
14 /// this will be called from a crate's build script, and the resulting file will
15 /// be `include!()`ed into the build.
16 ///
17 /// This function will attempt to locate and parse a corresponding Cargo.toml to
18 /// determine the crate name which will host this UDL.
19 ///
20 /// Given an UDL file named `example.udl`, the generated scaffolding will be written
21 /// into a file named `example.uniffi.rs` in the `$OUT_DIR` directory.
22 pub fn generate_scaffolding(udl_file: impl AsRef<Utf8Path>) -> Result<()> {
23 let udl_file = udl_file.as_ref();
24 println!("cargo:rerun-if-changed={udl_file}");
25 println!("cargo:rerun-if-env-changed=UNIFFI_TESTS_DISABLE_EXTENSIONS");
26 let out_dir = env::var("OUT_DIR").context("$OUT_DIR missing?!")?;
27 uniffi_bindgen::generate_component_scaffolding(udl_file, Some(out_dir.as_ref()), false)
28 }
29
30 /// Like generate_scaffolding, but uses the specified crate_name instead of locating and parsing
31 /// Cargo.toml.
32 pub fn generate_scaffolding_for_crate(
33 udl_file: impl AsRef<Utf8Path>,
34 crate_name: &str,
35 ) -> Result<()> {
36 let udl_file = udl_file.as_ref();
37
38 println!("cargo:rerun-if-changed={udl_file}");
39 // The UNIFFI_TESTS_DISABLE_EXTENSIONS variable disables some bindings, but it is evaluated
40 // at *build* time, so we need to rebuild when it changes.
41 println!("cargo:rerun-if-env-changed=UNIFFI_TESTS_DISABLE_EXTENSIONS");
42 let out_dir = env::var("OUT_DIR").context("$OUT_DIR missing?!")?;
43 uniffi_bindgen::generate_component_scaffolding_for_crate(
44 udl_file,
45 crate_name,
46 Some(out_dir.as_ref()),
47 false,
48 )
49 }