Skip to main content

wit_bindgen_rust_macro/
lib.rs

1use proc_macro2::{Span, TokenStream};
2use quote::ToTokens;
3use std::collections::HashMap;
4use std::path::{Path, PathBuf};
5use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
6use syn::parse::{Error, Parse, ParseStream, Result};
7use syn::punctuated::Punctuated;
8use syn::{Token, braced, token};
9use wit_bindgen_core::AsyncFilterSet;
10use wit_bindgen_core::WorldGenerator;
11use wit_bindgen_core::wit_parser::{PackageId, Resolve, UnresolvedPackageGroup, WorldId};
12use wit_bindgen_rust::{Opts, Ownership, WithOption};
13
14#[proc_macro]
15pub fn generate(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
16    syn::parse_macro_input!(input as Config)
17        .expand()
18        .unwrap_or_else(Error::into_compile_error)
19        .into()
20}
21
22fn anyhow_to_syn(span: Span, err: anyhow::Error) -> Error {
23    let err = attach_with_context(err);
24    let mut msg = err.to_string();
25    for cause in err.chain().skip(1) {
26        msg.push_str(&format!("\n\nCaused by:\n  {cause}"));
27    }
28    Error::new(span, msg)
29}
30
31fn attach_with_context(err: anyhow::Error) -> anyhow::Error {
32    if let Some(e) = err.downcast_ref::<wit_bindgen_rust::MissingWith>() {
33        let option = e.0.clone();
34        return err.context(format!(
35            "missing one of:\n\
36            * `generate_all` option\n\
37            * `with: {{ \"{option}\": path::to::bindings, }}`\n\
38            * `with: {{ \"{option}\": generate, }}`\
39            "
40        ));
41    }
42    err
43}
44
45struct Config {
46    opts: Opts,
47    resolve: Resolve,
48    world: WorldId,
49    files: Vec<PathBuf>,
50    debug: bool,
51}
52
53/// The source of the wit package definition
54enum Source {
55    /// A list of paths to wit directories
56    Paths(Vec<PathBuf>),
57    /// Inline sources have an optional path to a directory of their dependencies
58    Inline(String, Option<Vec<PathBuf>>),
59}
60
61impl Parse for Config {
62    fn parse(input: ParseStream<'_>) -> Result<Self> {
63        let call_site = Span::call_site();
64        let mut opts = Opts::default();
65        let mut world = None;
66        let mut source = None;
67        let mut features = Vec::new();
68        let mut async_configured = false;
69        let mut debug = false;
70
71        if input.peek(token::Brace) {
72            let content;
73            syn::braced!(content in input);
74            let fields = Punctuated::<Opt, Token![,]>::parse_terminated(&content)?;
75            for field in fields.into_pairs() {
76                match field.into_value() {
77                    Opt::Path(span, p) => {
78                        let paths = p
79                            .into_iter()
80                            .map(|f| f.evaluate_string())
81                            .collect::<Result<Vec<_>>>()?
82                            .into_iter()
83                            .map(PathBuf::from)
84                            .collect();
85
86                        source = Some(match source {
87                            Some(Source::Paths(_)) | Some(Source::Inline(_, Some(_))) => {
88                                return Err(Error::new(span, "cannot specify second source"));
89                            }
90                            Some(Source::Inline(i, None)) => Source::Inline(i, Some(paths)),
91                            None => Source::Paths(paths),
92                        })
93                    }
94                    Opt::World(s) => {
95                        if world.is_some() {
96                            return Err(Error::new(s.span(), "cannot specify second world"));
97                        }
98                        world = Some(s.value());
99                    }
100                    Opt::Inline(s) => {
101                        source = Some(match source {
102                            Some(Source::Inline(_, _)) => {
103                                return Err(Error::new(s.span(), "cannot specify second source"));
104                            }
105                            Some(Source::Paths(p)) => Source::Inline(s.value(), Some(p)),
106                            None => Source::Inline(s.value(), None),
107                        })
108                    }
109                    Opt::UseStdFeature => opts.std_feature = true,
110                    Opt::RawStrings => opts.raw_strings = true,
111                    Opt::Ownership(ownership) => opts.ownership = ownership,
112                    Opt::Skip(list) => opts.skip.extend(list.iter().map(|i| i.value())),
113                    Opt::RuntimePath(path) => opts.runtime_path = Some(path.value()),
114                    Opt::MapType(path) => opts.map_type = Some(path.value()),
115                    Opt::BitflagsPath(path) => opts.bitflags_path = Some(path.value()),
116                    Opt::Stubs => {
117                        opts.stubs = true;
118                    }
119                    Opt::ExportPrefix(prefix) => opts.export_prefix = Some(prefix.value()),
120                    Opt::AdditionalDerives(paths) => {
121                        opts.additional_derive_attributes = paths
122                            .into_iter()
123                            .map(|p| p.into_token_stream().to_string())
124                            .collect()
125                    }
126                    Opt::AdditionalDerivesIgnore(list) => {
127                        opts.additional_derive_ignore =
128                            list.into_iter().map(|i| i.value()).collect()
129                    }
130                    Opt::With(with) => opts.with.extend(with),
131                    Opt::GenerateAll => {
132                        opts.generate_all = true;
133                    }
134                    Opt::TypeSectionSuffix(suffix) => {
135                        opts.type_section_suffix = Some(suffix.value());
136                    }
137                    Opt::DisableRunCtorsOnceWorkaround(enable) => {
138                        opts.disable_run_ctors_once_workaround = enable.value();
139                    }
140                    Opt::DefaultBindingsModule(enable) => {
141                        opts.default_bindings_module = Some(enable.value());
142                    }
143                    Opt::ExportMacroName(name) => {
144                        opts.export_macro_name = Some(name.value());
145                    }
146                    Opt::PubExportMacro(enable) => {
147                        opts.pub_export_macro = enable.value();
148                    }
149                    Opt::GenerateUnusedTypes(enable) => {
150                        opts.generate_unused_types = enable.value();
151                    }
152                    Opt::Features(f) => {
153                        features.extend(f.into_iter().map(|f| f.value()));
154                    }
155                    Opt::DisableCustomSectionLinkHelpers(disable) => {
156                        opts.disable_custom_section_link_helpers = disable.value();
157                    }
158                    Opt::Debug(enable) => {
159                        debug = enable.value();
160                    }
161                    Opt::Async(val, span) => {
162                        if async_configured {
163                            return Err(Error::new(span, "cannot specify second async config"));
164                        }
165                        async_configured = true;
166                        if val.any_enabled() && !cfg!(feature = "async") {
167                            return Err(Error::new(
168                                span,
169                                "must enable `async` feature to enable async imports and/or exports",
170                            ));
171                        }
172                        opts.async_ = val;
173                    }
174                }
175            }
176        } else {
177            world = input.parse::<Option<syn::LitStr>>()?.map(|s| s.value());
178            if input.parse::<Option<syn::token::In>>()?.is_some() {
179                source = Some(Source::Paths(vec![PathBuf::from(
180                    input.parse::<syn::LitStr>()?.value(),
181                )]));
182            }
183        }
184        let (resolve, main_packages, files) =
185            parse_source(&source, &features).map_err(|err| anyhow_to_syn(call_site, err))?;
186        let world = resolve
187            .select_world(&main_packages, world.as_deref())
188            .map_err(|e| anyhow_to_syn(call_site, e))?;
189        Ok(Config {
190            opts,
191            resolve,
192            world,
193            files,
194            debug,
195        })
196    }
197}
198
199/// Parse the source
200fn parse_source(
201    source: &Option<Source>,
202    features: &[String],
203) -> anyhow::Result<(Resolve, Vec<PackageId>, Vec<PathBuf>)> {
204    let mut resolve = Resolve::default();
205    resolve.features.extend(features.iter().cloned());
206    let mut files = Vec::new();
207    let mut pkgs = Vec::new();
208    let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
209    let mut parse = |paths: &[PathBuf]| -> anyhow::Result<()> {
210        for path in paths {
211            let p = root.join(path);
212            // Try to normalize the path to make the error message more understandable when
213            // the path is not correct. Fallback to the original path if normalization fails
214            // (probably return an error somewhere else).
215            let normalized_path = match std::fs::canonicalize(&p) {
216                Ok(p) => p,
217                Err(_) => p.to_path_buf(),
218            };
219            let (pkg, sources) = resolve.push_path(normalized_path)?;
220            pkgs.push(pkg);
221            files.extend(sources.paths().map(|p| p.to_owned()));
222        }
223        Ok(())
224    };
225    let default = root.join("wit");
226    match source {
227        Some(Source::Inline(s, path)) => {
228            match path {
229                Some(p) => parse(p)?,
230                // If no `path` is explicitly specified still parse the default
231                // `wit` directory if it exists. Don't require its existence,
232                // however, as `inline` can be used in lieu of a folder. Test
233                // whether it exists and only if there is it parsed.
234                None => {
235                    if default.exists() {
236                        parse(&[default])?;
237                    }
238                }
239            }
240            pkgs.truncate(0);
241            pkgs.push(resolve.push_group(UnresolvedPackageGroup::parse("macro-input", s)?)?);
242        }
243        Some(Source::Paths(p)) => parse(p)?,
244        None => parse(&[default])?,
245    };
246
247    Ok((resolve, pkgs, files))
248}
249
250impl Config {
251    fn expand(mut self) -> Result<TokenStream> {
252        let mut files = Default::default();
253        let mut generator = self.opts.build();
254        generator
255            .generate(&mut self.resolve, self.world, &mut files)
256            .map_err(|e| anyhow_to_syn(Span::call_site(), e))?;
257        let (_, src) = files.iter().next().unwrap();
258        let mut src = std::str::from_utf8(src).unwrap().to_string();
259
260        // If a magical `WIT_BINDGEN_DEBUG` environment variable is set then
261        // place a formatted version of the expanded code into a file. This file
262        // will then show up in rustc error messages for any codegen issues and can
263        // be inspected manually.
264        if std::env::var("WIT_BINDGEN_DEBUG").is_ok() || self.debug {
265            static INVOCATION: AtomicUsize = AtomicUsize::new(0);
266            let root = Path::new(env!("DEBUG_OUTPUT_DIR"));
267            let world_name = &self.resolve.worlds[self.world].name;
268            let n = INVOCATION.fetch_add(1, Relaxed);
269            let path = root.join(format!("{world_name}{n}.rs"));
270
271            // optimistically format the code but don't require success
272            let contents = match fmt(&src) {
273                Ok(formatted) => formatted,
274                Err(_) => src.clone(),
275            };
276            std::fs::write(&path, contents.as_bytes()).unwrap();
277
278            src = format!("include!({path:?});");
279        }
280        let mut contents = src.parse::<TokenStream>().unwrap();
281
282        // Include a dummy `include_bytes!` for any files we read so rustc knows that
283        // we depend on the contents of those files.
284        for file in self.files.iter() {
285            contents.extend(
286                format!(
287                    "const _: &[u8] = include_bytes!(r#\"{}\"#);\n",
288                    file.display()
289                )
290                .parse::<TokenStream>()
291                .unwrap(),
292            );
293        }
294
295        Ok(contents)
296    }
297}
298
299mod kw {
300    syn::custom_keyword!(std_feature);
301    syn::custom_keyword!(raw_strings);
302    syn::custom_keyword!(skip);
303    syn::custom_keyword!(world);
304    syn::custom_keyword!(path);
305    syn::custom_keyword!(inline);
306    syn::custom_keyword!(ownership);
307    syn::custom_keyword!(runtime_path);
308    syn::custom_keyword!(map_type);
309    syn::custom_keyword!(bitflags_path);
310    syn::custom_keyword!(exports);
311    syn::custom_keyword!(stubs);
312    syn::custom_keyword!(export_prefix);
313    syn::custom_keyword!(additional_derives);
314    syn::custom_keyword!(additional_derives_ignore);
315    syn::custom_keyword!(with);
316    syn::custom_keyword!(generate_all);
317    syn::custom_keyword!(type_section_suffix);
318    syn::custom_keyword!(disable_run_ctors_once_workaround);
319    syn::custom_keyword!(default_bindings_module);
320    syn::custom_keyword!(export_macro_name);
321    syn::custom_keyword!(pub_export_macro);
322    syn::custom_keyword!(generate_unused_types);
323    syn::custom_keyword!(features);
324    syn::custom_keyword!(disable_custom_section_link_helpers);
325    syn::custom_keyword!(imports);
326    syn::custom_keyword!(debug);
327}
328
329#[derive(Clone)]
330enum ExportKey {
331    World,
332    Name(syn::LitStr),
333}
334
335impl Parse for ExportKey {
336    fn parse(input: ParseStream<'_>) -> Result<Self> {
337        let l = input.lookahead1();
338        Ok(if l.peek(kw::world) {
339            input.parse::<kw::world>()?;
340            Self::World
341        } else {
342            Self::Name(input.parse()?)
343        })
344    }
345}
346
347impl From<ExportKey> for wit_bindgen_rust::ExportKey {
348    fn from(key: ExportKey) -> Self {
349        match key {
350            ExportKey::World => Self::World,
351            ExportKey::Name(s) => Self::Name(s.value()),
352        }
353    }
354}
355
356#[cfg(feature = "macro-string")]
357type PathType = macro_string::MacroString;
358#[cfg(not(feature = "macro-string"))]
359type PathType = syn::LitStr;
360
361trait EvaluateString {
362    fn evaluate_string(&self) -> Result<String>;
363}
364
365#[cfg(feature = "macro-string")]
366impl EvaluateString for macro_string::MacroString {
367    fn evaluate_string(&self) -> Result<String> {
368        self.eval()
369    }
370}
371
372#[cfg(not(feature = "macro-string"))]
373impl EvaluateString for syn::LitStr {
374    fn evaluate_string(&self) -> Result<String> {
375        Ok(self.value())
376    }
377}
378
379enum Opt {
380    World(syn::LitStr),
381    Path(Span, Vec<PathType>),
382    Inline(syn::LitStr),
383    UseStdFeature,
384    RawStrings,
385    Skip(Vec<syn::LitStr>),
386    Ownership(Ownership),
387    RuntimePath(syn::LitStr),
388    MapType(syn::LitStr),
389    BitflagsPath(syn::LitStr),
390    Stubs,
391    ExportPrefix(syn::LitStr),
392    // Parse as paths so we can take the concrete types/macro names rather than raw strings
393    AdditionalDerives(Vec<syn::Path>),
394    AdditionalDerivesIgnore(Vec<syn::LitStr>),
395    With(HashMap<String, WithOption>),
396    GenerateAll,
397    TypeSectionSuffix(syn::LitStr),
398    DisableRunCtorsOnceWorkaround(syn::LitBool),
399    DefaultBindingsModule(syn::LitStr),
400    ExportMacroName(syn::LitStr),
401    PubExportMacro(syn::LitBool),
402    GenerateUnusedTypes(syn::LitBool),
403    Features(Vec<syn::LitStr>),
404    DisableCustomSectionLinkHelpers(syn::LitBool),
405    Async(AsyncFilterSet, Span),
406    Debug(syn::LitBool),
407}
408
409impl Parse for Opt {
410    fn parse(input: ParseStream<'_>) -> Result<Self> {
411        let l = input.lookahead1();
412        if l.peek(kw::path) {
413            input.parse::<kw::path>()?;
414            input.parse::<Token![:]>()?;
415            // the `path` supports two forms:
416            // * path: "xxx"
417            // * path: ["aaa", "bbb"]
418            if input.peek(token::Bracket) {
419                let contents;
420                syn::bracketed!(contents in input);
421                let span = input.span();
422                let list = Punctuated::<PathType, Token![,]>::parse_terminated(&contents)?;
423                Ok(Opt::Path(span, list.into_iter().collect()))
424            } else {
425                let span = input.span();
426                let path: PathType = input.parse()?;
427                Ok(Opt::Path(span, vec![path]))
428            }
429        } else if l.peek(kw::inline) {
430            input.parse::<kw::inline>()?;
431            input.parse::<Token![:]>()?;
432            Ok(Opt::Inline(input.parse()?))
433        } else if l.peek(kw::world) {
434            input.parse::<kw::world>()?;
435            input.parse::<Token![:]>()?;
436            Ok(Opt::World(input.parse()?))
437        } else if l.peek(kw::std_feature) {
438            input.parse::<kw::std_feature>()?;
439            Ok(Opt::UseStdFeature)
440        } else if l.peek(kw::raw_strings) {
441            input.parse::<kw::raw_strings>()?;
442            Ok(Opt::RawStrings)
443        } else if l.peek(kw::ownership) {
444            input.parse::<kw::ownership>()?;
445            input.parse::<Token![:]>()?;
446            let ownership = input.parse::<syn::Ident>()?;
447            Ok(Opt::Ownership(match ownership.to_string().as_str() {
448                "Owning" => Ownership::Owning,
449                "Borrowing" => Ownership::Borrowing {
450                    duplicate_if_necessary: {
451                        let contents;
452                        braced!(contents in input);
453                        let field = contents.parse::<syn::Ident>()?;
454                        match field.to_string().as_str() {
455                            "duplicate_if_necessary" => {
456                                contents.parse::<Token![:]>()?;
457                                contents.parse::<syn::LitBool>()?.value
458                            }
459                            name => {
460                                return Err(Error::new(
461                                    field.span(),
462                                    format!(
463                                        "unrecognized `Ownership::Borrowing` field: `{name}`; \
464                                         expected `duplicate_if_necessary`"
465                                    ),
466                                ));
467                            }
468                        }
469                    },
470                },
471                name => {
472                    return Err(Error::new(
473                        ownership.span(),
474                        format!(
475                            "unrecognized ownership: `{name}`; \
476                             expected `Owning` or `Borrowing`"
477                        ),
478                    ));
479                }
480            }))
481        } else if l.peek(kw::skip) {
482            input.parse::<kw::skip>()?;
483            input.parse::<Token![:]>()?;
484            let contents;
485            syn::bracketed!(contents in input);
486            let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
487            Ok(Opt::Skip(list.iter().cloned().collect()))
488        } else if l.peek(kw::runtime_path) {
489            input.parse::<kw::runtime_path>()?;
490            input.parse::<Token![:]>()?;
491            Ok(Opt::RuntimePath(input.parse()?))
492        } else if l.peek(kw::map_type) {
493            input.parse::<kw::map_type>()?;
494            input.parse::<Token![:]>()?;
495            Ok(Opt::MapType(input.parse()?))
496        } else if l.peek(kw::bitflags_path) {
497            input.parse::<kw::bitflags_path>()?;
498            input.parse::<Token![:]>()?;
499            Ok(Opt::BitflagsPath(input.parse()?))
500        } else if l.peek(kw::stubs) {
501            input.parse::<kw::stubs>()?;
502            Ok(Opt::Stubs)
503        } else if l.peek(kw::export_prefix) {
504            input.parse::<kw::export_prefix>()?;
505            input.parse::<Token![:]>()?;
506            Ok(Opt::ExportPrefix(input.parse()?))
507        } else if l.peek(kw::additional_derives) {
508            input.parse::<kw::additional_derives>()?;
509            input.parse::<Token![:]>()?;
510            let contents;
511            syn::bracketed!(contents in input);
512            let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
513            Ok(Opt::AdditionalDerives(list.iter().cloned().collect()))
514        } else if l.peek(kw::additional_derives_ignore) {
515            input.parse::<kw::additional_derives_ignore>()?;
516            input.parse::<Token![:]>()?;
517            let contents;
518            syn::bracketed!(contents in input);
519            let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
520            Ok(Opt::AdditionalDerivesIgnore(list.iter().cloned().collect()))
521        } else if l.peek(kw::with) {
522            input.parse::<kw::with>()?;
523            input.parse::<Token![:]>()?;
524            let contents;
525            let _lbrace = braced!(contents in input);
526            let fields: Punctuated<_, Token![,]> =
527                contents.parse_terminated(with_field_parse, Token![,])?;
528            Ok(Opt::With(HashMap::from_iter(fields)))
529        } else if l.peek(kw::generate_all) {
530            input.parse::<kw::generate_all>()?;
531            Ok(Opt::GenerateAll)
532        } else if l.peek(kw::type_section_suffix) {
533            input.parse::<kw::type_section_suffix>()?;
534            input.parse::<Token![:]>()?;
535            Ok(Opt::TypeSectionSuffix(input.parse()?))
536        } else if l.peek(kw::disable_run_ctors_once_workaround) {
537            input.parse::<kw::disable_run_ctors_once_workaround>()?;
538            input.parse::<Token![:]>()?;
539            Ok(Opt::DisableRunCtorsOnceWorkaround(input.parse()?))
540        } else if l.peek(kw::default_bindings_module) {
541            input.parse::<kw::default_bindings_module>()?;
542            input.parse::<Token![:]>()?;
543            Ok(Opt::DefaultBindingsModule(input.parse()?))
544        } else if l.peek(kw::export_macro_name) {
545            input.parse::<kw::export_macro_name>()?;
546            input.parse::<Token![:]>()?;
547            Ok(Opt::ExportMacroName(input.parse()?))
548        } else if l.peek(kw::pub_export_macro) {
549            input.parse::<kw::pub_export_macro>()?;
550            input.parse::<Token![:]>()?;
551            Ok(Opt::PubExportMacro(input.parse()?))
552        } else if l.peek(kw::generate_unused_types) {
553            input.parse::<kw::generate_unused_types>()?;
554            input.parse::<Token![:]>()?;
555            Ok(Opt::GenerateUnusedTypes(input.parse()?))
556        } else if l.peek(kw::features) {
557            input.parse::<kw::features>()?;
558            input.parse::<Token![:]>()?;
559            let contents;
560            syn::bracketed!(contents in input);
561            let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
562            Ok(Opt::Features(list.into_iter().collect()))
563        } else if l.peek(kw::disable_custom_section_link_helpers) {
564            input.parse::<kw::disable_custom_section_link_helpers>()?;
565            input.parse::<Token![:]>()?;
566            Ok(Opt::DisableCustomSectionLinkHelpers(input.parse()?))
567        } else if l.peek(kw::debug) {
568            input.parse::<kw::debug>()?;
569            input.parse::<Token![:]>()?;
570            Ok(Opt::Debug(input.parse()?))
571        } else if l.peek(Token![async]) {
572            let span = input.parse::<Token![async]>()?.span;
573            input.parse::<Token![:]>()?;
574            if input.peek(syn::LitBool) {
575                let enabled = input.parse::<syn::LitBool>()?.value;
576                Ok(Opt::Async(AsyncFilterSet::all(enabled), span))
577            } else {
578                let mut set = AsyncFilterSet::default();
579                let contents;
580                syn::bracketed!(contents in input);
581                for val in contents.parse_terminated(|p| p.parse::<syn::LitStr>(), Token![,])? {
582                    set.push(&val.value());
583                }
584                Ok(Opt::Async(set, span))
585            }
586        } else {
587            Err(l.error())
588        }
589    }
590}
591
592fn with_field_parse(input: ParseStream<'_>) -> Result<(String, WithOption)> {
593    let interface = input.parse::<syn::LitStr>()?.value();
594    input.parse::<Token![:]>()?;
595    let start = input.span();
596    let path = input.parse::<syn::Path>()?;
597
598    // It's not possible for the segments of a path to be empty
599    let span = start
600        .join(path.segments.last().unwrap().ident.span())
601        .unwrap_or(start);
602
603    if path.is_ident("generate") {
604        return Ok((interface, WithOption::Generate));
605    }
606
607    let mut buf = String::new();
608    let append = |buf: &mut String, segment: syn::PathSegment| -> Result<()> {
609        if !segment.arguments.is_none() {
610            return Err(Error::new(
611                span,
612                "Module path must not contain angles or parens",
613            ));
614        }
615
616        buf.push_str(&segment.ident.to_string());
617
618        Ok(())
619    };
620
621    if path.leading_colon.is_some() {
622        buf.push_str("::");
623    }
624
625    let mut segments = path.segments.into_iter();
626
627    if let Some(segment) = segments.next() {
628        append(&mut buf, segment)?;
629    }
630
631    for segment in segments {
632        buf.push_str("::");
633        append(&mut buf, segment)?;
634    }
635
636    Ok((interface, WithOption::Path(buf)))
637}
638
639/// Format a valid Rust string
640fn fmt(input: &str) -> Result<String> {
641    let syntax_tree = syn::parse_file(input)?;
642    Ok(prettyplease::unparse(&syntax_tree))
643}