include-wasm-rs 0.2.0

Builds a Rust WebAssembly module at compile time and returns the bytes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Provides a macro for including a Rust project as Wasm bytecode,
//! by compiling it at build time of the invoking module.

#![cfg_attr(feature = "proc_macro_span", feature(proc_macro_span))]

use std::{fmt::Display, path::PathBuf, process::Command, sync::Mutex};

use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse::ParseStream, parse_macro_input, spanned::Spanned};

// Hacky polyfill for `proc_macro::Span::source_file`
#[cfg(not(feature = "proc_macro_span"))]
fn find_me(root: &str, pattern: &str) -> PathBuf {
    let mut options = Vec::new();

    for path in glob::glob(&std::path::Path::new(root).join("**/*.rs").to_string_lossy())
        .unwrap()
        .flatten()
    {
        if let Ok(mut f) = std::fs::File::open(&path) {
            let mut contents = String::new();
            std::io::Read::read_to_string(&mut f, &mut contents).ok();
            if contents.contains(pattern) {
                options.push(path.to_owned());
            }
        }
    }

    match options.as_slice() {
        [] => panic!(
            "could not find invocation point - maybe it was in a macro? \
            If you are on nightly (or in the future), enable the `proc_macro_span` \
            feature on `include-wasm-rs` to use advanced call site resolution, \
            but until then each instance of the `build_wasm` must be present \
            in the source text, and each must have a unique argument."
        ),
        [v] => v.clone(),
        _ => panic!(
            "found more than one contender for macro invocation location. \
            If you are on nightly (or in the future), enable the `proc_macro_span` \
            feature on `include-wasm-rs` to use advanced call site resolution, \
            but until then each instance of the `build_wasm` must be present \
            in the source text, and each must have a unique argument. \
            Found potential invocation locations: {:?}",
            options
                .into_iter()
                .map(|path| format!("`{}`", path.display()))
                .collect::<Vec<String>>()
        ),
    }
}

#[derive(Default)]
struct TargetFeatures {
    atomics: bool,
    bulk_memory: bool,
    mutable_globals: bool,
}

impl TargetFeatures {
    fn from_list_of_exprs(
        elems: syn::punctuated::Punctuated<syn::Expr, syn::Token![,]>,
    ) -> syn::parse::Result<Self> {
        let mut res = Self::default();

        for elem in elems {
            let span = elem.span();
            let name = match elem {
                syn::Expr::Path(ident)
                    if ident.attrs.is_empty()
                        && ident.qself.is_none()
                        && ident.path.leading_colon.is_none()
                        && ident.path.segments.len() == 1
                        && ident.path.segments[0].arguments.is_empty() =>
                {
                    ident.path.segments[0].ident.to_string()
                }
                _ => {
                    return Err(syn::Error::new(
                        span,
                        "expected a single token giving a feature",
                    ))
                }
            };

            match name.as_str() {
                "atomics" => res.atomics = true,
                "bulk_memory" => res.bulk_memory = true,
                "mutable_globals" => res.mutable_globals = true,
                _ => return Err(syn::Error::new(span, "unknown feature")),
            }
        }

        Ok(res)
    }
}

fn degroup_expr(expr: syn::Expr) -> syn::Expr {
    match expr {
        syn::Expr::Group(syn::ExprGroup {
            attrs,
            group_token: _,
            expr,
        }) if attrs.is_empty() => degroup_expr(*expr),
        expr => expr,
    }
}

#[derive(Default)]
struct Args {
    module_dir: PathBuf,
    features: TargetFeatures,
    env_vars: Vec<(String, String)>,
    release: bool,
}

impl syn::parse::Parse for Args {
    fn parse(input: ParseStream) -> syn::parse::Result<Self> {
        // Just a string gives a path, with default options
        if input.peek(syn::LitStr) {
            let path = input.parse::<syn::LitStr>()?;
            return Ok(Self {
                module_dir: PathBuf::from(path.value()),
                ..Self::default()
            });
        }

        // Else we expect a json-like dict of options
        let mut res = Self::default();

        let dict =
            syn::punctuated::Punctuated::<syn::FieldValue, syn::Token![,]>::parse_terminated(
                input,
            )?;
        for mut value in dict {
            if !value.attrs.is_empty() {
                return Err(syn::Error::new(value.attrs[0].span(), "unexpected element"));
            }
            let name = match &value.member {
                syn::Member::Named(name) => name.to_string(),
                syn::Member::Unnamed(unnamed) => unnamed.index.to_string(),
            };

            value.expr = degroup_expr(value.expr);

            // Parse value depending on key
            match name.as_str() {
                "path" => {
                    // String as PathBuf
                    res.module_dir = match value.expr {
                        syn::Expr::Lit(syn::ExprLit {
                            attrs,
                            lit: syn::Lit::Str(path),
                        }) if attrs.is_empty() => PathBuf::from(path.value()),
                        _ => {
                            return Err(syn::Error::new(
                                value.expr.span(),
                                format!("expected literal string, got {:?}", value.expr),
                            ))
                        }
                    };
                }
                "release" => {
                    // Boolean
                    res.release = match value.expr {
                        syn::Expr::Lit(syn::ExprLit {
                            attrs,
                            lit: syn::Lit::Bool(release),
                        }) if attrs.is_empty() => release.value,
                        _ => return Err(syn::Error::new(value.expr.span(), "expected boolean")),
                    };
                }
                "features" => {
                    // Array of identifiers
                    match value.expr {
                        syn::Expr::Array(syn::ExprArray {
                            attrs,
                            bracket_token: _,
                            elems,
                        }) if attrs.is_empty() => {
                            res.features = TargetFeatures::from_list_of_exprs(elems)?
                        }
                        _ => return Err(syn::Error::new(value.expr.span(), "expected boolean")),
                    };
                }
                "env" => {
                    // Dictionary of key value pairs
                    match value.expr {
                        syn::Expr::Struct(syn::ExprStruct {
                            attrs,
                            qself: None,
                            path:
                                syn::Path {
                                    leading_colon: None,
                                    segments,
                                },
                            brace_token: _,
                            fields,
                            dot2_token: None,
                            rest: None,
                        }) if attrs.is_empty()
                            && segments.len() == 1
                            && segments[0].arguments.is_empty()
                            && segments[0].ident == "Env" =>
                        {
                            for field in fields {
                                let span = field.span();
                                if !field.attrs.is_empty() || field.colon_token.is_none() {
                                    return Err(syn::Error::new(span, "expected key value pair"));
                                }

                                let env_name = match &field.member {
                                    syn::Member::Named(name) => name.to_string(),
                                    _ => {
                                        return Err(syn::Error::new(
                                            span,
                                            "expected env variable name",
                                        ))
                                    }
                                };

                                let mut expr = &field.expr;
                                while let syn::Expr::Group(syn::ExprGroup {
                                    attrs,
                                    group_token: _,
                                    expr: inner_expr,
                                }) = expr
                                {
                                    if !attrs.is_empty() {
                                        return Err(syn::Error::new(
                                            attrs[0].span(),
                                            "expected a string, int, float or bool",
                                        ));
                                    }

                                    expr = inner_expr;
                                }

                                let env_val = match expr {
                                    syn::Expr::Lit(syn::ExprLit { attrs, lit })
                                        if attrs.is_empty() =>
                                    {
                                        match lit {
                                            syn::Lit::Str(v) => v.value(),
                                            syn::Lit::Int(i) => i.to_string(),
                                            syn::Lit::Float(f) => f.to_string(),
                                            syn::Lit::Bool(b) => b.value.to_string(),
                                            _ => {
                                                return Err(syn::Error::new(
                                                    lit.span(),
                                                    format!("expected a string, int, float or bool, found literal `{}`", lit.into_token_stream()),
                                                ))
                                            }
                                        }
                                    }
                                    _ => {
                                        return Err(syn::Error::new(
                                            field.expr.span(),
                                            format!("expected a string, int, float or bool, found `{}`", field.expr.into_token_stream()),
                                        ))
                                    }
                                };

                                res.env_vars.push((env_name, env_val));
                            }
                        }
                        _ => {
                            return Err(syn::Error::new(
                                value.expr.span(),
                                "expected key value pairs",
                            ))
                        }
                    }
                }
                option => {
                    return Err(syn::Error::new(
                        value.member.span(),
                        format!("unknown option `{}`", option),
                    ))
                }
            }
        }

        Ok(res)
    }
}

impl Display for TargetFeatures {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.atomics {
            write!(f, "+atomics,")?
        }
        if self.bulk_memory {
            write!(f, "+bulk-memory,")?
        }
        if self.mutable_globals {
            write!(f, "+mutable-globals,")?
        }

        Ok(())
    }
}

/// Only allow one build job at a time, in case we are building one module many times.
static GLOBAL_LOCK: Mutex<()> = Mutex::new(());

/// Builds a cargo project as a webassembly module, returning the bytes of the module produced.
fn do_build_wasm(args: &Args) -> Result<PathBuf, String> {
    let Args {
        module_dir,
        features,
        env_vars,
        release,
    } = args;

    // Acquire global lock
    let mut lock = GLOBAL_LOCK.lock();
    while lock.is_err() {
        GLOBAL_LOCK.clear_poison();
        lock = GLOBAL_LOCK.lock();
    }

    // Check target path points to a module
    let cargo_config = module_dir.join("Cargo.toml");
    if !cargo_config.is_file() {
        return Err(format!(
            "target directory `{}` does not contain a `Cargo.toml` file",
            module_dir.display()
        ));
    }
    match std::fs::read_to_string(cargo_config) {
        Ok(cfg) => {
            if cfg.contains("[workspace]\n") {
                return Err("provided directory points to a workspace, not a module".to_owned());
            }
        }
        Err(e) => return Err(format!("failed to read target `Cargo.toml`: {e}")),
    }

    // Build output path, taking env vars into account
    let mut target_dir = "target/".to_owned();
    for (key, val) in env_vars.iter() {
        target_dir += &format!("{}_{}", key, val);
    }

    // Run `cargo update` before building
    let mut command = Command::new("cargo");

    let out = command
        .arg("update")
        .current_dir(module_dir.clone())
        .output();
    match out {
        Ok(out) => {
            if !out.status.success() {
                return Err(format!(
                    "failed to update module `{}`: \n{}",
                    module_dir.display(),
                    String::from_utf8_lossy(&out.stderr).replace('\n', "\n\t")
                ));
            }
        }
        Err(e) => {
            return Err(format!(
                "failed to update module `{}`: {e}",
                module_dir.display()
            ))
        }
    }

    // Construct build command
    let mut command = Command::new("cargo");

    // Treat `RUSTFLAGS` as special in env vars
    const RUSTFLAGS: &str = "RUSTFLAGS";
    let mut rustflags_value = format!("--cfg=web_sys_unstable_apis -C target-feature={features}");
    command.env(RUSTFLAGS, &rustflags_value);

    for (key, val) in env_vars.iter() {
        if key == RUSTFLAGS {
            rustflags_value += " ";
            rustflags_value += val;
            command.env(RUSTFLAGS, &rustflags_value);
        } else {
            command.env(key, val);
        }
    }

    // Set args
    let mut args = vec![
        "+nightly",
        "build",
        "--target",
        "wasm32-unknown-unknown",
        "-Z",
        "build-std=panic_abort,std",
        "--target-dir",
        &target_dir,
    ];
    if *release {
        args.push("--release");
    }

    let command = command.args(args).current_dir(module_dir.clone());
    let command_debug = format!("{command:?}");
    let out = command.output();
    match out {
        Ok(out) => {
            if !out.status.success() {
                return Err(format!(
                    "failed to build module `{}`: \nrunning `{}`\n{}",
                    module_dir.display(),
                    command_debug,
                    String::from_utf8_lossy(&out.stderr).replace('\n', "\n\t")
                ));
            }
        }
        Err(e) => {
            return Err(format!(
                "failed to build module `{}`: \nrunning `{}`\n{e}",
                command_debug,
                module_dir.display()
            ))
        }
    }

    // Find output with glob
    let root_output = module_dir.join(target_dir).join("wasm32-unknown-unknown/");
    let glob = if *release {
        root_output.join("release/")
    } else {
        root_output.join("debug/")
    }
    .join("*.wasm");
    let mut glob_paths = glob::glob(
        glob.as_os_str()
            .to_str()
            .expect("output path should be unicode compliant"),
    )
    .expect("glob should be valid");

    let output = match glob_paths.next() {
        Some(Ok(output)) => output,
        Some(Err(err)) => {
            return Err(format!(
                "failed to find output file matching `{glob:?}`: {err} - this is probably a bug",
            ))
        }
        None => {
            return Err(format!(
                "failed to find output file matching `{}` - this is probably a bug",
                glob.display()
            ))
        }
    };

    // Check only one output to avoid hidden bugs
    if let Some(Ok(_)) = glob_paths.next() {
        return Err(format!("multiple output files matching `{}` were found - this may be because you recently changed the name of your module; try deleting the folder `{}` and rebuilding", glob.display(), root_output.display()));
    }

    drop(lock);

    Ok(output)
}

fn all_module_files(path: PathBuf) -> Vec<String> {
    let glob_paths = glob::glob(
        path.as_os_str()
            .to_str()
            .expect("output path should be unicode compliant"),
    )
    .expect("glob should be valid");

    glob_paths
        .into_iter()
        .filter_map(|path| {
            let path = path.ok()?;
            if !path.is_file() {
                None
            } else {
                Some(path.to_string_lossy().to_string())
            }
        })
        .collect()
}

/// Invokes `cargo build` at compile time on another module, replacing this macro invocation
/// with the bytes contained in the output `.wasm` file.
///
/// # Usage
///
/// ```ignore
/// let module = build_wasm!("relative/path/to/module");
/// ```
///
/// # Arguments
///
/// This macro can take a number of additional arguments to control how the WebAssembly should be generated.
/// These options are passed to `cargo build`:
///
/// ```ignore
/// let module = build_wasm!{
///     path: "relative/path/to/module",
///     features: [
///         atomics, // Controls if the `atomics` proposal is enabled
///         bulk_memory, // Controls if the `bulk-memory` proposal is enabled
///         mutable_globals, // Controls if the `mutable-globals` proposal is enabled
///     ],
///     // Allows additional environment variables to be set while compiling the module.
///     env: Env {
///         FOO: "bar",
///         BAX: 7,
///     },
///     // Controls if the module should be built in debug or release mode.
///     release: true
/// };
/// ```
#[proc_macro]
pub fn build_wasm(args: TokenStream) -> TokenStream {
    // Parse args
    let mut args = parse_macro_input!(args as Args);

    #[cfg(not(feature = "proc_macro_span"))]
    let invocation_file = {
        let root =
            std::env::var("CARGO_MANIFEST_DIR").expect("proc macros should be run using cargo");
        find_me(&root, &format!("\"{}\"", args.module_dir.to_string_lossy()))
    };
    #[cfg(feature = "proc_macro_span")]
    let invocation_file = proc_macro::Span::call_site().source_file().path();
    let invocation_file = invocation_file
        .parent()
        .unwrap()
        .to_path_buf()
        .canonicalize()
        .unwrap();
    args.module_dir = invocation_file.join(args.module_dir);

    // Build
    let result = do_build_wasm(&args);

    // Output
    match result {
        Ok(bytes_path) => {
            let bytes_path = bytes_path.to_string_lossy().to_string();
            // Register rebuild on files changed
            let module_paths = all_module_files(args.module_dir);

            quote! {
                {
                    #(
                        let _ = include_str!(#module_paths);
                    )*
                    include_bytes!(#bytes_path) as &'static [u8]
                }
            }
        }
        Err(err) => quote! {
            {
                compile_error!(#err);
                const BS: &'static [u8] = &[0u8];
                BS
            }
        },
    }
    .into()
}