| 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 |
#![cfg_attr(feature = "nightly", feature(proc_macro_expand))] |
| 5 |
#![warn(rust_2018_idioms, unused_qualifications)] |
| 6 |
// somewhere between 1.77 and 1.84 we got a lot of new `dead_code` warnings because |
| 7 |
// we use structs to aid in parsing but don't actually use the items otherwise. |
| 8 |
#![allow(dead_code)] |
| 9 |
|
| 10 |
//! Macros for `uniffi`. |
| 11 |
|
| 12 |
#[cfg(feature = "trybuild")] |
| 13 |
use camino::Utf8Path; |
| 14 |
use proc_macro::TokenStream; |
| 15 |
use quote::quote; |
| 16 |
use syn::{parse_macro_input, LitStr}; |
| 17 |
|
| 18 |
mod custom; |
| 19 |
mod default; |
| 20 |
mod derive; |
| 21 |
mod enum_; |
| 22 |
mod error; |
| 23 |
mod export; |
| 24 |
mod ffiops; |
| 25 |
mod fnsig; |
| 26 |
mod object; |
| 27 |
mod record; |
| 28 |
mod remote; |
| 29 |
mod setup_scaffolding; |
| 30 |
mod test; |
| 31 |
mod util; |
| 32 |
|
| 33 |
use self::{ |
| 34 |
derive::DeriveOptions, enum_::expand_enum, error::expand_error, export::expand_export, |
| 35 |
object::expand_object, record::expand_record, |
| 36 |
}; |
| 37 |
|
| 38 |
/// A macro to build testcases for a component's generated bindings. |
| 39 |
/// |
| 40 |
/// This macro provides some plumbing to write automated tests for the generated |
| 41 |
/// foreign language bindings of a component. As a component author, you can write |
| 42 |
/// script files in the target foreign language(s) that exercise you component API, |
| 43 |
/// and then call this macro to produce a `cargo test` testcase from each one. |
| 44 |
/// The generated code will execute your script file with appropriate configuration and |
| 45 |
/// environment to let it load the component bindings, and will pass iff the script |
| 46 |
/// exits successfully. |
| 47 |
/// |
| 48 |
/// To use it, invoke the macro with the name of a fixture/example crate as the first argument, |
| 49 |
/// then one or more file paths relative to the crate root directory. It will produce one `#[test]` |
| 50 |
/// function per file, in a manner designed to play nicely with `cargo test` and its test filtering |
| 51 |
/// options. |
| 52 |
#[proc_macro] |
| 53 |
pub fn build_foreign_language_testcases(tokens: TokenStream) -> TokenStream { |
| 54 |
test::build_foreign_language_testcases(tokens) |
| 55 |
} |
| 56 |
|
| 57 |
/// Top-level initialization macro |
| 58 |
/// |
| 59 |
/// The optional namespace argument is only used by the scaffolding templates to pass in the |
| 60 |
/// CI namespace. |
| 61 |
#[proc_macro] |
| 62 |
pub fn setup_scaffolding(tokens: TokenStream) -> TokenStream { |
| 63 |
let namespace = match syn::parse_macro_input!(tokens as Option<LitStr>) { |
| 64 |
Some(lit_str) => lit_str.value(), |
| 65 |
None => match util::mod_path() { |
| 66 |
Ok(v) => v, |
| 67 |
Err(e) => return e.into_compile_error().into(), |
| 68 |
}, |
| 69 |
}; |
| 70 |
setup_scaffolding::setup_scaffolding(namespace) |
| 71 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 72 |
.into() |
| 73 |
} |
| 74 |
|
| 75 |
#[proc_macro_attribute] |
| 76 |
pub fn export(attr_args: TokenStream, input: TokenStream) -> TokenStream { |
| 77 |
do_export(attr_args, input, false) |
| 78 |
} |
| 79 |
|
| 80 |
fn do_export(attr_args: TokenStream, input: TokenStream, udl_mode: bool) -> TokenStream { |
| 81 |
let copied_input = (!udl_mode).then(|| proc_macro2::TokenStream::from(input.clone())); |
| 82 |
|
| 83 |
let gen_output = || { |
| 84 |
let item = syn::parse(input)?; |
| 85 |
expand_export(item, attr_args, udl_mode) |
| 86 |
}; |
| 87 |
let output = gen_output().unwrap_or_else(syn::Error::into_compile_error); |
| 88 |
|
| 89 |
quote! { |
| 90 |
#copied_input |
| 91 |
#output |
| 92 |
} |
| 93 |
.into() |
| 94 |
} |
| 95 |
|
| 96 |
#[proc_macro_derive(Record, attributes(uniffi))] |
| 97 |
pub fn derive_record(input: TokenStream) -> TokenStream { |
| 98 |
expand_record(parse_macro_input!(input), DeriveOptions::default()) |
| 99 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 100 |
.into() |
| 101 |
} |
| 102 |
|
| 103 |
#[proc_macro_derive(Enum)] |
| 104 |
pub fn derive_enum(input: TokenStream) -> TokenStream { |
| 105 |
expand_enum(parse_macro_input!(input), DeriveOptions::default()) |
| 106 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 107 |
.into() |
| 108 |
} |
| 109 |
|
| 110 |
#[proc_macro_derive(Object)] |
| 111 |
pub fn derive_object(input: TokenStream) -> TokenStream { |
| 112 |
expand_object(parse_macro_input!(input), DeriveOptions::default()) |
| 113 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 114 |
.into() |
| 115 |
} |
| 116 |
|
| 117 |
#[proc_macro_derive(Error, attributes(uniffi))] |
| 118 |
pub fn derive_error(input: TokenStream) -> TokenStream { |
| 119 |
expand_error(parse_macro_input!(input), DeriveOptions::default()) |
| 120 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 121 |
.into() |
| 122 |
} |
| 123 |
|
| 124 |
/// Generate FFI code for a custom type |
| 125 |
#[proc_macro] |
| 126 |
pub fn custom_type(tokens: TokenStream) -> TokenStream { |
| 127 |
custom::expand_custom_type(parse_macro_input!(tokens)) |
| 128 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 129 |
.into() |
| 130 |
} |
| 131 |
|
| 132 |
/// Generate FFI code for a custom newtype |
| 133 |
#[proc_macro] |
| 134 |
pub fn custom_newtype(tokens: TokenStream) -> TokenStream { |
| 135 |
custom::expand_custom_newtype(parse_macro_input!(tokens)) |
| 136 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 137 |
.into() |
| 138 |
} |
| 139 |
|
| 140 |
/// `#[remote(<kind>)]` attribute |
| 141 |
/// |
| 142 |
/// `remote()` generates the same code that `#[derive(uniffi::<kind>)]` would, except it only |
| 143 |
/// implements the FFI traits for the local UniFfiTag. |
| 144 |
/// |
| 145 |
/// Use this to wrap the definition of an item defined in a remote crate. |
| 146 |
/// See `<https://mozilla.github.io/uniffi-rs/udl/remote_ext_types.html>` for details. |
| 147 |
#[doc(hidden)] |
| 148 |
#[proc_macro_attribute] |
| 149 |
pub fn remote(attrs: TokenStream, input: TokenStream) -> TokenStream { |
| 150 |
derive::expand_derive( |
| 151 |
parse_macro_input!(attrs), |
| 152 |
parse_macro_input!(input), |
| 153 |
DeriveOptions::remote(), |
| 154 |
) |
| 155 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 156 |
.into() |
| 157 |
} |
| 158 |
|
| 159 |
/// `#[udl_remote(<kind>)]` attribute |
| 160 |
/// |
| 161 |
/// Alternate version of `#[remote]` for UDL-based generation |
| 162 |
/// |
| 163 |
/// The difference is that it doesn't generate metadata items, since we get those from parsing the |
| 164 |
/// UDL. |
| 165 |
#[doc(hidden)] |
| 166 |
#[proc_macro_attribute] |
| 167 |
pub fn udl_remote(attrs: TokenStream, input: TokenStream) -> TokenStream { |
| 168 |
derive::expand_derive( |
| 169 |
parse_macro_input!(attrs), |
| 170 |
parse_macro_input!(input), |
| 171 |
DeriveOptions::udl_remote(), |
| 172 |
) |
| 173 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 174 |
.into() |
| 175 |
} |
| 176 |
|
| 177 |
/// Derive items for UDL mode |
| 178 |
/// |
| 179 |
/// The Askama templates generate placeholder items wrapped with the `#[udl_derive(<kind>)]` |
| 180 |
/// attribute. The macro code then generates derived items based on the input. This system ensures |
| 181 |
/// that the same code path is used for UDL-based code and proc-macros. |
| 182 |
/// |
| 183 |
/// `udl_derive` works almost exactly like the `derive_*` macros, except it doesn't generate |
| 184 |
/// metadata items, since we get those from parsing the UDL. |
| 185 |
#[doc(hidden)] |
| 186 |
#[proc_macro_attribute] |
| 187 |
pub fn udl_derive(attrs: TokenStream, input: TokenStream) -> TokenStream { |
| 188 |
derive::expand_derive( |
| 189 |
parse_macro_input!(attrs), |
| 190 |
parse_macro_input!(input), |
| 191 |
DeriveOptions::udl_derive(), |
| 192 |
) |
| 193 |
.unwrap_or_else(syn::Error::into_compile_error) |
| 194 |
.into() |
| 195 |
} |
| 196 |
|
| 197 |
// Generate export items for UDL mode |
| 198 |
// |
| 199 |
// This works similarly to `udl_derive`, but for #[export]. |
| 200 |
#[doc(hidden)] |
| 201 |
#[proc_macro_attribute] |
| 202 |
pub fn export_for_udl(attrs: TokenStream, input: TokenStream) -> TokenStream { |
| 203 |
do_export(attrs, input, true) |
| 204 |
} |
| 205 |
|
| 206 |
/// A helper macro to include generated component scaffolding. |
| 207 |
/// |
| 208 |
/// This is a simple convenience macro to include the UniFFI component |
| 209 |
/// scaffolding as built by `uniffi_build::generate_scaffolding`. |
| 210 |
/// Use it like so: |
| 211 |
/// |
| 212 |
/// ```rs |
| 213 |
/// uniffi_macros::include_scaffolding!("my_component_name"); |
| 214 |
/// ``` |
| 215 |
/// |
| 216 |
/// This will expand to the appropriate `include!` invocation to include |
| 217 |
/// the generated `my_component_name.uniffi.rs` (which it assumes has |
| 218 |
/// been successfully built by your crate's `build.rs` script). |
| 219 |
#[proc_macro] |
| 220 |
pub fn include_scaffolding(udl_stem: TokenStream) -> TokenStream { |
| 221 |
let udl_stem = syn::parse_macro_input!(udl_stem as LitStr); |
| 222 |
if std::env::var("OUT_DIR").is_err() { |
| 223 |
quote! { |
| 224 |
compile_error!("This macro assumes the crate has a build.rs script, but $OUT_DIR is not present"); |
| 225 |
} |
| 226 |
} else { |
| 227 |
let toml_path = match util::manifest_path() { |
| 228 |
Ok(path) => path.display().to_string(), |
| 229 |
Err(_) => { |
| 230 |
return quote! { |
| 231 |
compile_error!("This macro assumes the crate has a build.rs script, but $OUT_DIR is not present"); |
| 232 |
}.into(); |
| 233 |
} |
| 234 |
}; |
| 235 |
|
| 236 |
quote! { |
| 237 |
// FIXME(HACK): |
| 238 |
// Include the `Cargo.toml` file into the build. |
| 239 |
// That way cargo tracks the file and other tools relying on file |
| 240 |
// tracking see it as well. |
| 241 |
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1846223 |
| 242 |
// In the future we should handle that by using the `track_path::path` API, |
| 243 |
// see https://github.com/rust-lang/rust/pull/84029 |
| 244 |
#[allow(dead_code)] |
| 245 |
mod __unused { |
| 246 |
const _: &[u8] = include_bytes!(#toml_path); |
| 247 |
} |
| 248 |
|
| 249 |
include!(concat!(env!("OUT_DIR"), "/", #udl_stem, ".uniffi.rs")); |
| 250 |
} |
| 251 |
}.into() |
| 252 |
} |
| 253 |
|
| 254 |
/// Use the FFI trait implementations defined in another crate for a remote type |
| 255 |
/// |
| 256 |
/// See `<https://mozilla.github.io/uniffi-rs/udl/remote_ext_types.html>` for details. |
| 257 |
#[proc_macro] |
| 258 |
pub fn use_remote_type(tokens: TokenStream) -> TokenStream { |
| 259 |
remote::expand_remote_type(parse_macro_input!(tokens)).into() |
| 260 |
} |
| 261 |
|
| 262 |
/// A helper macro to generate and include component scaffolding. |
| 263 |
/// |
| 264 |
/// This is a convenience macro designed for writing `trybuild`-style tests and |
| 265 |
/// probably shouldn't be used for production code. Given the path to a `.udl` file, |
| 266 |
/// if will run `uniffi-bindgen` to produce the corresponding Rust scaffolding and then |
| 267 |
/// include it directly into the calling file. Like so: |
| 268 |
/// |
| 269 |
/// ```rs |
| 270 |
/// uniffi_macros::generate_and_include_scaffolding!("path/to/my/interface.udl"); |
| 271 |
/// ``` |
| 272 |
#[proc_macro] |
| 273 |
#[cfg(feature = "trybuild")] |
| 274 |
pub fn generate_and_include_scaffolding(udl_file: TokenStream) -> TokenStream { |
| 275 |
let udl_file = syn::parse_macro_input!(udl_file as LitStr); |
| 276 |
let udl_file_string = udl_file.value(); |
| 277 |
let udl_file_path = Utf8Path::new(&udl_file_string); |
| 278 |
if std::env::var("OUT_DIR").is_err() { |
| 279 |
quote! { |
| 280 |
compile_error!("This macro assumes the crate has a build.rs script, but $OUT_DIR is not present"); |
| 281 |
} |
| 282 |
} else if let Err(e) = uniffi_build::generate_scaffolding(udl_file_path) { |
| 283 |
let err = format!("{e:#}"); |
| 284 |
quote! { |
| 285 |
compile_error!(concat!("Failed to generate scaffolding from UDL file at ", #udl_file, ": ", #err)); |
| 286 |
} |
| 287 |
} else { |
| 288 |
// We know the filename is good because `generate_scaffolding` succeeded, |
| 289 |
// so this `unwrap` will never fail. |
| 290 |
let name = LitStr::new(udl_file_path.file_stem().unwrap(), udl_file.span()); |
| 291 |
quote! { |
| 292 |
uniffi_macros::include_scaffolding!(#name); |
| 293 |
} |
| 294 |
}.into() |
| 295 |
} |
| 296 |
|
| 297 |
/// An attribute for constructors. |
| 298 |
/// |
| 299 |
/// Constructors are in `impl` blocks which have a `#[uniffi::export]` attribute, |
| 300 |
/// |
| 301 |
/// This exists so `#[uniffi::export]` can emit its input verbatim without |
| 302 |
/// causing unexpected errors in the entire exported block. |
| 303 |
/// This happens very often when the proc-macro is run on an incomplete |
| 304 |
/// input by rust-analyzer while the developer is typing. |
| 305 |
/// |
| 306 |
/// So much better to do nothing here then let the impl block find the attribute. |
| 307 |
#[proc_macro_attribute] |
| 308 |
pub fn constructor(_attrs: TokenStream, input: TokenStream) -> TokenStream { |
| 309 |
input |
| 310 |
} |
| 311 |
|
| 312 |
/// An attribute for methods. |
| 313 |
/// |
| 314 |
/// Everything above applies here too. |
| 315 |
#[proc_macro_attribute] |
| 316 |
pub fn method(_attrs: TokenStream, input: TokenStream) -> TokenStream { |
| 317 |
input |
| 318 |
} |