Debian Codegraph @

third_party/rust/uniffi_bindgen/src/pipeline/initial/mod.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 std::fs;
6
7 mod from_uniffi_meta;
8 mod nodes;
9 pub use nodes::*;
10
11 use anyhow::Result;
12 use camino::Utf8Path;
13
14 use crate::{crate_name_from_cargo_toml, interface, macro_metadata, BindgenCrateConfigSupplier};
15 use from_uniffi_meta::UniffiMetaConverter;
16
17 impl Root {
18 pub fn from_library(
19 config_supplier: impl BindgenCrateConfigSupplier,
20 path: &Utf8Path,
21 crate_name: Option<String>,
22 ) -> Result<Root> {
23 let mut metadata_converter = UniffiMetaConverter::default();
24 let mut all_metadata = macro_metadata::extract_from_library(path)?;
25 if let Some(crate_name) = crate_name {
26 all_metadata.retain(|meta| meta.module_path().split("::").next() == Some(&crate_name));
27 }
28
29 let mut udl_to_load = vec![];
30
31 for meta in macro_metadata::extract_from_library(path)? {
32 match meta {
33 uniffi_meta::Metadata::UdlFile(udl) => {
34 udl_to_load.push((
35 config_supplier.get_udl(&udl.module_path, &udl.file_stub)?,
36 udl.module_path,
37 ));
38 }
39 uniffi_meta::Metadata::Namespace(namespace) => {
40 if let Some(path) = config_supplier.get_toml_path(&namespace.crate_name) {
41 metadata_converter.add_module_config_toml(namespace.name.clone(), &path)?;
42 }
43 metadata_converter
44 .add_metadata_item(uniffi_meta::Metadata::Namespace(namespace))?;
45 }
46 meta => metadata_converter.add_metadata_item(meta)?,
47 }
48 }
49
50 for (udl, module_path) in udl_to_load {
51 Self::add_metadata_from_udl(
52 &mut metadata_converter,
53 &config_supplier,
54 &udl,
55 &module_path,
56 true,
57 )?;
58 }
59 let mut root = metadata_converter.try_into_initial_ir()?;
60 root.cdylib = Some(path.to_string());
61 Ok(root)
62 }
63
64 pub fn from_udl(
65 config_supplier: impl BindgenCrateConfigSupplier,
66 path: &Utf8Path,
67 crate_name: Option<String>,
68 ) -> Result<Root> {
69 let mut metadata_converter = UniffiMetaConverter::default();
70 let crate_name = match crate_name {
71 Some(c) => c,
72 None => crate_name_from_cargo_toml(path)?,
73 };
74 Self::add_metadata_from_udl(
75 &mut metadata_converter,
76 &config_supplier,
77 &fs::read_to_string(path)?,
78 &crate_name,
79 false,
80 )?;
81 metadata_converter.try_into_initial_ir()
82 }
83
84 fn add_metadata_from_udl(
85 metadata_converter: &mut UniffiMetaConverter,
86 config_supplier: &impl BindgenCrateConfigSupplier,
87 udl: &str,
88 crate_name: &str,
89 library_mode: bool,
90 ) -> Result<()> {
91 let metadata_group = uniffi_udl::parse_udl(udl, crate_name)?;
92 // parse_udl returns a metadata group, which is nice for the CI, but we actually want to
93 // start with a raw metadata list
94 if let Some(docstring) = metadata_group.namespace_docstring {
95 metadata_converter
96 .add_module_docstring(metadata_group.namespace.name.clone(), docstring);
97 }
98 if !library_mode {
99 if let Some(path) = config_supplier.get_toml_path(&metadata_group.namespace.crate_name)
100 {
101 metadata_converter
102 .add_module_config_toml(metadata_group.namespace.name.clone(), &path)?;
103 }
104 }
105 metadata_converter
106 .add_metadata_item(uniffi_meta::Metadata::Namespace(metadata_group.namespace))?;
107 for mut meta in metadata_group.items {
108 // some items are both in UDL and library metadata. For many that's fine but
109 // uniffi-traits aren't trivial to compare meaning we end up with dupes.
110 // We filter out such problematic items here.
111 if library_mode && matches!(meta, uniffi_meta::Metadata::UniffiTrait { .. }) {
112 continue;
113 }
114 // Make sure metadata checksums are set
115 match &mut meta {
116 uniffi_meta::Metadata::Func(func) => {
117 func.checksum = Some(uniffi_meta::checksum(&interface::Function::from(
118 func.clone(),
119 )));
120 }
121 uniffi_meta::Metadata::Method(meth) => {
122 meth.checksum = Some(uniffi_meta::checksum(&interface::Method::from(
123 meth.clone(),
124 )));
125 }
126 uniffi_meta::Metadata::Constructor(cons) => {
127 cons.checksum = Some(uniffi_meta::checksum(&interface::Constructor::from(
128 cons.clone(),
129 )));
130 }
131 // Note: UDL-based callbacks don't have checksum functions, don't set the
132 // checksum for those.
133 _ => (),
134 }
135 metadata_converter.add_metadata_item(meta)?;
136 }
137 Ok(())
138 }
139 }