Skip to main content

wasmtime_cli_flags/
lib.rs

1//! Contains the common Wasmtime command line interface (CLI) flags.
2
3use anyhow::Result;
4use clap::Parser;
5use std::time::Duration;
6use wasmtime::Config;
7
8pub mod opt;
9
10#[cfg(feature = "logging")]
11fn init_file_per_thread_logger(prefix: &'static str) {
12    file_per_thread_logger::initialize(prefix);
13    file_per_thread_logger::allow_uninitialized();
14
15    // Extending behavior of default spawner:
16    // https://docs.rs/rayon/1.1.0/rayon/struct.ThreadPoolBuilder.html#method.spawn_handler
17    // Source code says DefaultSpawner is implementation detail and
18    // shouldn't be used directly.
19    #[cfg(feature = "parallel-compilation")]
20    rayon::ThreadPoolBuilder::new()
21        .spawn_handler(move |thread| {
22            let mut b = std::thread::Builder::new();
23            if let Some(name) = thread.name() {
24                b = b.name(name.to_owned());
25            }
26            if let Some(stack_size) = thread.stack_size() {
27                b = b.stack_size(stack_size);
28            }
29            b.spawn(move || {
30                file_per_thread_logger::initialize(prefix);
31                thread.run()
32            })?;
33            Ok(())
34        })
35        .build_global()
36        .unwrap();
37}
38
39wasmtime_option_group! {
40    #[derive(PartialEq, Clone)]
41    pub struct OptimizeOptions {
42        /// Optimization level of generated code (0-2, s; default: 2)
43        pub opt_level: Option<wasmtime::OptLevel>,
44
45        /// Byte size of the guard region after dynamic memories are allocated
46        pub dynamic_memory_guard_size: Option<u64>,
47
48        /// Force using a "static" style for all wasm memories
49        pub static_memory_forced: Option<bool>,
50
51        /// Maximum size in bytes of wasm memory before it becomes dynamically
52        /// relocatable instead of up-front-reserved.
53        pub static_memory_maximum_size: Option<u64>,
54
55        /// Byte size of the guard region after static memories are allocated
56        pub static_memory_guard_size: Option<u64>,
57
58        /// Bytes to reserve at the end of linear memory for growth for dynamic
59        /// memories.
60        pub dynamic_memory_reserved_for_growth: Option<u64>,
61
62        /// Indicates whether an unmapped region of memory is placed before all
63        /// linear memories.
64        pub guard_before_linear_memory: Option<bool>,
65
66        /// Whether to initialize tables lazily, so that instantiation is
67        /// fast but indirect calls are a little slower. If no, tables are
68        /// initialized eagerly from any active element segments that apply to
69        /// them during instantiation. (default: yes)
70        pub table_lazy_init: Option<bool>,
71
72        /// Enable the pooling allocator, in place of the on-demand allocator.
73        pub pooling_allocator: Option<bool>,
74
75        /// The number of decommits to do per batch. A batch size of 1
76        /// effectively disables decommit batching. (default: 1)
77        pub pooling_decommit_batch_size: Option<u32>,
78
79        /// How many bytes to keep resident between instantiations for the
80        /// pooling allocator in linear memories.
81        pub pooling_memory_keep_resident: Option<usize>,
82
83        /// How many bytes to keep resident between instantiations for the
84        /// pooling allocator in tables.
85        pub pooling_table_keep_resident: Option<usize>,
86
87        /// Enable memory protection keys for the pooling allocator; this can
88        /// optimize the size of memory slots.
89        pub memory_protection_keys: Option<bool>,
90
91        /// Configure attempting to initialize linear memory via a
92        /// copy-on-write mapping (default: yes)
93        pub memory_init_cow: Option<bool>,
94
95        /// The maximum number of WebAssembly instances which can be created
96        /// with the pooling allocator.
97        pub pooling_total_core_instances: Option<u32>,
98
99        /// The maximum number of WebAssembly components which can be created
100        /// with the pooling allocator.
101        pub pooling_total_component_instances: Option<u32>,
102
103        /// The maximum number of WebAssembly memories which can be created with
104        /// the pooling allocator.
105        pub pooling_total_memories: Option<u32>,
106
107        /// The maximum number of WebAssembly tables which can be created with
108        /// the pooling allocator.
109        pub pooling_total_tables: Option<u32>,
110
111        /// The maximum number of WebAssembly stacks which can be created with
112        /// the pooling allocator.
113        pub pooling_total_stacks: Option<u32>,
114
115        /// The maximum runtime size of each linear memory in the pooling
116        /// allocator, in bytes.
117        pub pooling_max_memory_size: Option<usize>,
118    }
119
120    enum Optimize {
121        ...
122    }
123}
124
125wasmtime_option_group! {
126    #[derive(PartialEq, Clone)]
127    pub struct CodegenOptions {
128        /// Either `cranelift` or `winch`.
129        ///
130        /// Currently only `cranelift` and `winch` are supported, but not all
131        /// builds of Wasmtime have both built in.
132        pub compiler: Option<wasmtime::Strategy>,
133        /// Enable Cranelift's internal debug verifier (expensive)
134        pub cranelift_debug_verifier: Option<bool>,
135        /// Whether or not to enable caching of compiled modules.
136        pub cache: Option<bool>,
137        /// Configuration for compiled module caching.
138        pub cache_config: Option<String>,
139        /// Whether or not to enable parallel compilation of modules.
140        pub parallel_compilation: Option<bool>,
141        /// Whether to enable proof-carrying code (PCC)-based validation.
142        pub pcc: Option<bool>,
143
144        #[prefixed = "cranelift"]
145        /// Set a cranelift-specific option. Use `wasmtime settings` to see
146        /// all.
147        pub cranelift: Vec<(String, Option<String>)>,
148    }
149
150    enum Codegen {
151        ...
152    }
153}
154
155wasmtime_option_group! {
156    #[derive(PartialEq, Clone)]
157    pub struct DebugOptions {
158        /// Enable generation of DWARF debug information in compiled code.
159        pub debug_info: Option<bool>,
160        /// Configure whether compiled code can map native addresses to wasm.
161        pub address_map: Option<bool>,
162        /// Configure whether logging is enabled.
163        pub logging: Option<bool>,
164        /// Configure whether logs are emitted to files
165        pub log_to_files: Option<bool>,
166        /// Enable coredump generation to this file after a WebAssembly trap.
167        pub coredump: Option<String>,
168    }
169
170    enum Debug {
171        ...
172    }
173}
174
175wasmtime_option_group! {
176    #[derive(PartialEq, Clone)]
177    pub struct WasmOptions {
178        /// Enable canonicalization of all NaN values.
179        pub nan_canonicalization: Option<bool>,
180        /// Enable execution fuel with N units fuel, trapping after running out
181        /// of fuel.
182        ///
183        /// Most WebAssembly instructions consume 1 unit of fuel. Some
184        /// instructions, such as `nop`, `drop`, `block`, and `loop`, consume 0
185        /// units, as any execution cost associated with them involves other
186        /// instructions which do consume fuel.
187        pub fuel: Option<u64>,
188        /// Yield when a global epoch counter changes, allowing for async
189        /// operation without blocking the executor.
190        pub epoch_interruption: Option<bool>,
191        /// Maximum stack size, in bytes, that wasm is allowed to consume before a
192        /// stack overflow is reported.
193        pub max_wasm_stack: Option<usize>,
194        /// Allow unknown exports when running commands.
195        pub unknown_exports_allow: Option<bool>,
196        /// Allow the main module to import unknown functions, using an
197        /// implementation that immediately traps, when running commands.
198        pub unknown_imports_trap: Option<bool>,
199        /// Allow the main module to import unknown functions, using an
200        /// implementation that returns default values, when running commands.
201        pub unknown_imports_default: Option<bool>,
202        /// Enables memory error checking. (see wmemcheck.md for more info)
203        pub wmemcheck: Option<bool>,
204        /// Maximum size, in bytes, that a linear memory is allowed to reach.
205        ///
206        /// Growth beyond this limit will cause `memory.grow` instructions in
207        /// WebAssembly modules to return -1 and fail.
208        pub max_memory_size: Option<usize>,
209        /// Maximum size, in table elements, that a table is allowed to reach.
210        pub max_table_elements: Option<u32>,
211        /// Maximum number of WebAssembly instances allowed to be created.
212        pub max_instances: Option<usize>,
213        /// Maximum number of WebAssembly tables allowed to be created.
214        pub max_tables: Option<usize>,
215        /// Maximum number of WebAssembly linear memories allowed to be created.
216        pub max_memories: Option<usize>,
217        /// Force a trap to be raised on `memory.grow` and `table.grow` failure
218        /// instead of returning -1 from these instructions.
219        ///
220        /// This is not necessarily a spec-compliant option to enable but can be
221        /// useful for tracking down a backtrace of what is requesting so much
222        /// memory, for example.
223        pub trap_on_grow_failure: Option<bool>,
224        /// Maximum execution time of wasm code before timing out (1, 2s, 100ms, etc)
225        pub timeout: Option<Duration>,
226        /// Configures support for all WebAssembly proposals implemented.
227        pub all_proposals: Option<bool>,
228        /// Configure support for the bulk memory proposal.
229        pub bulk_memory: Option<bool>,
230        /// Configure support for the multi-memory proposal.
231        pub multi_memory: Option<bool>,
232        /// Configure support for the multi-value proposal.
233        pub multi_value: Option<bool>,
234        /// Configure support for the reference-types proposal.
235        pub reference_types: Option<bool>,
236        /// Configure support for the simd proposal.
237        pub simd: Option<bool>,
238        /// Configure support for the relaxed-simd proposal.
239        pub relaxed_simd: Option<bool>,
240        /// Configure forcing deterministic and host-independent behavior of
241        /// the relaxed-simd instructions.
242        ///
243        /// By default these instructions may have architecture-specific behavior as
244        /// allowed by the specification, but this can be used to force the behavior
245        /// of these instructions to match the deterministic behavior classified in
246        /// the specification. Note that enabling this option may come at a
247        /// performance cost.
248        pub relaxed_simd_deterministic: Option<bool>,
249        /// Configure support for the tail-call proposal.
250        pub tail_call: Option<bool>,
251        /// Configure support for the threads proposal.
252        pub threads: Option<bool>,
253        /// Configure support for the memory64 proposal.
254        pub memory64: Option<bool>,
255        /// Configure support for the component-model proposal.
256        pub component_model: Option<bool>,
257        /// Configure support for 33+ flags in the component model.
258        pub component_model_more_flags: Option<bool>,
259        /// Component model support for more than one return value.
260        pub component_model_multiple_returns: Option<bool>,
261        /// Configure support for the function-references proposal.
262        pub function_references: Option<bool>,
263        /// Configure support for the GC proposal.
264        pub gc: Option<bool>,
265        /// Configure support for the custom-page-sizes proposal.
266        pub custom_page_sizes: Option<bool>,
267    }
268
269    enum Wasm {
270        ...
271    }
272}
273
274wasmtime_option_group! {
275    #[derive(PartialEq, Clone)]
276    pub struct WasiOptions {
277        /// Enable support for WASI CLI APIs, including filesystems, sockets, clocks, and random.
278        pub cli: Option<bool>,
279        /// Deprecated alias for `cli`
280        pub common: Option<bool>,
281        /// Enable support for WASI neural network API (experimental)
282        pub nn: Option<bool>,
283        /// Enable support for WASI threading API (experimental)
284        pub threads: Option<bool>,
285        /// Enable support for WASI HTTP API (experimental)
286        pub http: Option<bool>,
287        /// Enable support for WASI runtime config API (experimental)
288        pub runtime_config: Option<bool>,
289        /// Enable support for WASI key-value API (experimental)
290        pub keyvalue: Option<bool>,
291        /// Inherit environment variables and file descriptors following the
292        /// systemd listen fd specification (UNIX only)
293        pub listenfd: Option<bool>,
294        /// Grant access to the given TCP listen socket
295        pub tcplisten: Vec<String>,
296        /// Implement WASI CLI APIs with preview2 primitives (experimental).
297        ///
298        /// Indicates that the implementation of WASI preview1 should be backed by
299        /// the preview2 implementation for components.
300        ///
301        /// This will become the default in the future and this option will be
302        /// removed. For now this is primarily here for testing.
303        pub preview2: Option<bool>,
304        /// Pre-load machine learning graphs (i.e., models) for use by wasi-nn.
305        ///
306        /// Each use of the flag will preload a ML model from the host directory
307        /// using the given model encoding. The model will be mapped to the
308        /// directory name: e.g., `--wasi-nn-graph openvino:/foo/bar` will preload
309        /// an OpenVINO model named `bar`. Note that which model encodings are
310        /// available is dependent on the backends implemented in the
311        /// `wasmtime_wasi_nn` crate.
312        pub nn_graph: Vec<WasiNnGraph>,
313        /// Flag for WASI preview2 to inherit the host's network within the
314        /// guest so it has full access to all addresses/ports/etc.
315        pub inherit_network: Option<bool>,
316        /// Indicates whether `wasi:sockets/ip-name-lookup` is enabled or not.
317        pub allow_ip_name_lookup: Option<bool>,
318        /// Indicates whether `wasi:sockets` TCP support is enabled or not.
319        pub tcp: Option<bool>,
320        /// Indicates whether `wasi:sockets` UDP support is enabled or not.
321        pub udp: Option<bool>,
322        /// Allows imports from the `wasi_unstable` core wasm module.
323        pub preview0: Option<bool>,
324        /// Inherit all environment variables from the parent process.
325        ///
326        /// This option can be further overwritten with `--env` flags.
327        pub inherit_env: Option<bool>,
328        /// Pass a wasi runtime config variable to the program.
329        pub runtime_config_var: Vec<KeyValuePair>,
330        /// Preset data for the In-Memory provider of WASI key-value API.
331        pub keyvalue_in_memory_data: Vec<KeyValuePair>,
332        /// Maximum resources the guest is allowed to create simultaneously.
333        pub max_resources: Option<usize>,
334        /// Fuel to use for all hostcalls to limit guest<->host data transfer.
335        pub hostcall_fuel: Option<usize>,
336        /// Maximum value, in bytes, for a wasi-random 0.2
337        /// `get{,-insecure}-random-bytes` `len` parameter. Calls with a value
338        /// exceeding this limit will trap.
339        pub max_random_size: Option<u64>,
340        /// Maximum value, in bytes, for the contents of a wasi-http 0.2
341        /// `fields` resource (aka `headers` and `trailers`). `fields` methods
342        /// which cause the contents to exceed this size limit will trap.
343        pub max_http_fields_size: Option<usize>,
344    }
345
346    enum Wasi {
347        ...
348    }
349}
350
351#[derive(Debug, Clone, PartialEq)]
352pub struct WasiNnGraph {
353    pub format: String,
354    pub dir: String,
355}
356
357#[derive(Debug, Clone, PartialEq)]
358pub struct KeyValuePair {
359    pub key: String,
360    pub value: String,
361}
362
363/// Common options for commands that translate WebAssembly modules
364#[derive(Parser, Clone)]
365pub struct CommonOptions {
366    // These options groups are used to parse `-O` and such options but aren't
367    // the raw form consumed by the CLI. Instead they're pushed into the `pub`
368    // fields below as part of the `configure` method.
369    //
370    // Ideally clap would support `pub opts: OptimizeOptions` and parse directly
371    // into that but it does not appear to do so for multiple `-O` flags for
372    // now.
373    /// Optimization and tuning related options for wasm performance, `-O help` to
374    /// see all.
375    #[arg(short = 'O', long = "optimize", value_name = "KEY[=VAL[,..]]")]
376    opts_raw: Vec<opt::CommaSeparated<Optimize>>,
377
378    /// Codegen-related configuration options, `-C help` to see all.
379    #[arg(short = 'C', long = "codegen", value_name = "KEY[=VAL[,..]]")]
380    codegen_raw: Vec<opt::CommaSeparated<Codegen>>,
381
382    /// Debug-related configuration options, `-D help` to see all.
383    #[arg(short = 'D', long = "debug", value_name = "KEY[=VAL[,..]]")]
384    debug_raw: Vec<opt::CommaSeparated<Debug>>,
385
386    /// Options for configuring semantic execution of WebAssembly, `-W help` to see
387    /// all.
388    #[arg(short = 'W', long = "wasm", value_name = "KEY[=VAL[,..]]")]
389    wasm_raw: Vec<opt::CommaSeparated<Wasm>>,
390
391    /// Options for configuring WASI and its proposals, `-S help` to see all.
392    #[arg(short = 'S', long = "wasi", value_name = "KEY[=VAL[,..]]")]
393    wasi_raw: Vec<opt::CommaSeparated<Wasi>>,
394
395    // These fields are filled in by the `configure` method below via the
396    // options parsed from the CLI above. This is what the CLI should use.
397    #[arg(skip)]
398    configured: bool,
399    #[arg(skip)]
400    pub opts: OptimizeOptions,
401    #[arg(skip)]
402    pub codegen: CodegenOptions,
403    #[arg(skip)]
404    pub debug: DebugOptions,
405    #[arg(skip)]
406    pub wasm: WasmOptions,
407    #[arg(skip)]
408    pub wasi: WasiOptions,
409}
410
411macro_rules! match_feature {
412    (
413        [$feat:tt : $config:expr]
414        $val:ident => $e:expr,
415        $p:pat => err,
416    ) => {
417        #[cfg(feature = $feat)]
418        {
419            if let Some($val) = $config {
420                $e;
421            }
422        }
423        #[cfg(not(feature = $feat))]
424        {
425            if let Some($p) = $config {
426                anyhow::bail!(concat!("support for ", $feat, " disabled at compile time"));
427            }
428        }
429    };
430}
431
432impl CommonOptions {
433    fn configure(&mut self) {
434        if self.configured {
435            return;
436        }
437        self.configured = true;
438        self.opts.configure_with(&self.opts_raw);
439        self.codegen.configure_with(&self.codegen_raw);
440        self.debug.configure_with(&self.debug_raw);
441        self.wasm.configure_with(&self.wasm_raw);
442        self.wasi.configure_with(&self.wasi_raw);
443    }
444
445    pub fn init_logging(&mut self) -> Result<()> {
446        self.configure();
447        if self.debug.logging == Some(false) {
448            return Ok(());
449        }
450        #[cfg(feature = "logging")]
451        if self.debug.log_to_files == Some(true) {
452            let prefix = "wasmtime.dbg.";
453            init_file_per_thread_logger(prefix);
454        } else {
455            use std::io::IsTerminal;
456            use tracing_subscriber::{EnvFilter, FmtSubscriber};
457            let b = FmtSubscriber::builder()
458                .with_writer(std::io::stderr)
459                .with_env_filter(EnvFilter::from_env("WASMTIME_LOG"))
460                .with_ansi(std::io::stderr().is_terminal());
461            b.init();
462        }
463        #[cfg(not(feature = "logging"))]
464        if self.debug.log_to_files == Some(true) || self.debug.logging == Some(true) {
465            anyhow::bail!("support for logging disabled at compile time");
466        }
467        Ok(())
468    }
469
470    pub fn config(
471        &mut self,
472        target: Option<&str>,
473        pooling_allocator_default: Option<bool>,
474    ) -> Result<Config> {
475        self.configure();
476        let mut config = Config::new();
477
478        match_feature! {
479            ["cranelift" : self.codegen.compiler]
480            strategy => config.strategy(strategy),
481            _ => err,
482        }
483        match_feature! {
484            ["cranelift" : target]
485            target => config.target(target)?,
486            _ => err,
487        }
488        match_feature! {
489            ["cranelift" : self.codegen.cranelift_debug_verifier]
490            enable => config.cranelift_debug_verifier(enable),
491            true => err,
492        }
493        if let Some(enable) = self.debug.debug_info {
494            config.debug_info(enable);
495        }
496        if self.debug.coredump.is_some() {
497            #[cfg(feature = "coredump")]
498            config.coredump_on_trap(true);
499            #[cfg(not(feature = "coredump"))]
500            anyhow::bail!("support for coredumps disabled at compile time");
501        }
502        match_feature! {
503            ["cranelift" : self.opts.opt_level]
504            level => config.cranelift_opt_level(level),
505            _ => err,
506        }
507        match_feature! {
508            ["cranelift" : self.wasm.nan_canonicalization]
509            enable => config.cranelift_nan_canonicalization(enable),
510            true => err,
511        }
512        match_feature! {
513            ["cranelift" : self.codegen.pcc]
514            enable => config.cranelift_pcc(enable),
515            true => err,
516        }
517
518        self.enable_wasm_features(&mut config)?;
519
520        #[cfg(feature = "cranelift")]
521        for (name, value) in self.codegen.cranelift.iter() {
522            let name = name.replace('-', "_");
523            unsafe {
524                match value {
525                    Some(val) => {
526                        config.cranelift_flag_set(&name, val);
527                    }
528                    None => {
529                        config.cranelift_flag_enable(&name);
530                    }
531                }
532            }
533        }
534        #[cfg(not(feature = "cranelift"))]
535        if !self.codegen.cranelift.is_empty() {
536            anyhow::bail!("support for cranelift disabled at compile time");
537        }
538
539        #[cfg(feature = "cache")]
540        if self.codegen.cache != Some(false) {
541            match &self.codegen.cache_config {
542                Some(path) => {
543                    config.cache_config_load(path)?;
544                }
545                None => {
546                    config.cache_config_load_default()?;
547                }
548            }
549        }
550        #[cfg(not(feature = "cache"))]
551        if self.codegen.cache == Some(true) {
552            anyhow::bail!("support for caching disabled at compile time");
553        }
554
555        match_feature! {
556            ["parallel-compilation" : self.codegen.parallel_compilation]
557            enable => config.parallel_compilation(enable),
558            true => err,
559        }
560
561        if let Some(max) = self.opts.static_memory_maximum_size {
562            config.static_memory_maximum_size(max);
563        }
564
565        if let Some(enable) = self.opts.static_memory_forced {
566            config.static_memory_forced(enable);
567        }
568
569        if let Some(size) = self.opts.static_memory_guard_size {
570            config.static_memory_guard_size(size);
571        }
572
573        if let Some(size) = self.opts.dynamic_memory_guard_size {
574            config.dynamic_memory_guard_size(size);
575        }
576        if let Some(size) = self.opts.dynamic_memory_reserved_for_growth {
577            config.dynamic_memory_reserved_for_growth(size);
578        }
579        if let Some(enable) = self.opts.guard_before_linear_memory {
580            config.guard_before_linear_memory(enable);
581        }
582        if let Some(enable) = self.opts.table_lazy_init {
583            config.table_lazy_init(enable);
584        }
585
586        // If fuel has been configured, set the `consume fuel` flag on the config.
587        if self.wasm.fuel.is_some() {
588            config.consume_fuel(true);
589        }
590
591        if let Some(enable) = self.wasm.epoch_interruption {
592            config.epoch_interruption(enable);
593        }
594        if let Some(enable) = self.debug.address_map {
595            config.generate_address_map(enable);
596        }
597        if let Some(enable) = self.opts.memory_init_cow {
598            config.memory_init_cow(enable);
599        }
600
601        match_feature! {
602            ["pooling-allocator" : self.opts.pooling_allocator.or(pooling_allocator_default)]
603            enable => {
604                if enable {
605                    let mut cfg = wasmtime::PoolingAllocationConfig::default();
606                    if let Some(size) = self.opts.pooling_memory_keep_resident {
607                        cfg.linear_memory_keep_resident(size);
608                    }
609                    if let Some(size) = self.opts.pooling_table_keep_resident {
610                        cfg.table_keep_resident(size);
611                    }
612                    if let Some(limit) = self.opts.pooling_total_core_instances {
613                        cfg.total_core_instances(limit);
614                    }
615                    if let Some(limit) = self.opts.pooling_total_component_instances {
616                        cfg.total_component_instances(limit);
617                    }
618                    if let Some(limit) = self.opts.pooling_total_memories {
619                        cfg.total_memories(limit);
620                    }
621                    if let Some(limit) = self.opts.pooling_total_tables {
622                        cfg.total_tables(limit);
623                    }
624                    match_feature! {
625                        ["async" : self.opts.pooling_total_stacks]
626                        limit => cfg.total_stacks(limit),
627                        _ => err,
628                    }
629                    if let Some(limit) = self.opts.pooling_max_memory_size {
630                        cfg.max_memory_size(limit);
631                    }
632                    match_feature! {
633                        ["memory-protection-keys" : self.opts.memory_protection_keys]
634                        enable => cfg.memory_protection_keys(if enable {
635                            wasmtime::MpkEnabled::Enable
636                        } else {
637                            wasmtime::MpkEnabled::Disable
638                        }),
639                        _ => err,
640                    }
641                    config.allocation_strategy(wasmtime::InstanceAllocationStrategy::Pooling(cfg));
642                }
643            },
644            true => err,
645        }
646
647        if self.opts.memory_protection_keys.unwrap_or(false)
648            && !self.opts.pooling_allocator.unwrap_or(false)
649        {
650            anyhow::bail!("memory protection keys require the pooling allocator");
651        }
652
653        if let Some(max) = self.wasm.max_wasm_stack {
654            config.max_wasm_stack(max);
655        }
656
657        if let Some(enable) = self.wasm.relaxed_simd_deterministic {
658            config.relaxed_simd_deterministic(enable);
659        }
660        match_feature! {
661            ["cranelift" : self.wasm.wmemcheck]
662            enable => config.wmemcheck(enable),
663            true => err,
664        }
665
666        Ok(config)
667    }
668
669    pub fn enable_wasm_features(&self, config: &mut Config) -> Result<()> {
670        let all = self.wasm.all_proposals;
671
672        if let Some(enable) = self.wasm.simd.or(all) {
673            config.wasm_simd(enable);
674        }
675        if let Some(enable) = self.wasm.relaxed_simd.or(all) {
676            config.wasm_relaxed_simd(enable);
677        }
678        if let Some(enable) = self.wasm.bulk_memory.or(all) {
679            config.wasm_bulk_memory(enable);
680        }
681        if let Some(enable) = self.wasm.multi_value.or(all) {
682            config.wasm_multi_value(enable);
683        }
684        if let Some(enable) = self.wasm.tail_call.or(all) {
685            config.wasm_tail_call(enable);
686        }
687        if let Some(enable) = self.wasm.multi_memory.or(all) {
688            config.wasm_multi_memory(enable);
689        }
690        if let Some(enable) = self.wasm.memory64.or(all) {
691            config.wasm_memory64(enable);
692        }
693        if let Some(enable) = self.wasm.custom_page_sizes.or(all) {
694            config.wasm_custom_page_sizes(enable);
695        }
696
697        macro_rules! handle_conditionally_compiled {
698            ($(($feature:tt, $field:tt, $method:tt))*) => ($(
699                if let Some(enable) = self.wasm.$field.or(all) {
700                    #[cfg(feature = $feature)]
701                    config.$method(enable);
702                    #[cfg(not(feature = $feature))]
703                    if enable && all.is_none() {
704                        anyhow::bail!("support for {} was disabled at compile-time", $feature);
705                    }
706                }
707            )*)
708        }
709
710        handle_conditionally_compiled! {
711            ("component-model", component_model, wasm_component_model)
712            ("component-model", component_model_more_flags, wasm_component_model_more_flags)
713            ("component-model", component_model_multiple_returns, wasm_component_model_multiple_returns)
714            ("threads", threads, wasm_threads)
715            ("gc", gc, wasm_gc)
716            ("gc", reference_types, wasm_reference_types)
717            ("gc", function_references, wasm_function_references)
718        }
719        Ok(())
720    }
721}
722
723impl PartialEq for CommonOptions {
724    fn eq(&self, other: &CommonOptions) -> bool {
725        let mut me = self.clone();
726        me.configure();
727        let mut other = other.clone();
728        other.configure();
729        let CommonOptions {
730            opts_raw: _,
731            codegen_raw: _,
732            debug_raw: _,
733            wasm_raw: _,
734            wasi_raw: _,
735            configured: _,
736
737            opts,
738            codegen,
739            debug,
740            wasm,
741            wasi,
742        } = me;
743        opts == other.opts
744            && codegen == other.codegen
745            && debug == other.debug
746            && wasm == other.wasm
747            && wasi == other.wasi
748    }
749}