xref: /wasmtime-44.0.1/crates/cli-flags/src/lib.rs (revision 73ef034a)
1 //! Contains the common Wasmtime command line interface (CLI) flags.
2 
3 use clap::Parser;
4 use serde::Deserialize;
5 use std::{
6     fmt, fs,
7     path::{Path, PathBuf},
8     time::Duration,
9 };
10 use wasmtime::{Config, Result, bail, error::Context as _};
11 
12 pub mod opt;
13 
14 #[cfg(feature = "logging")]
15 fn init_file_per_thread_logger(prefix: &'static str) {
16     file_per_thread_logger::initialize(prefix);
17     file_per_thread_logger::allow_uninitialized();
18 
19     // Extending behavior of default spawner:
20     // https://docs.rs/rayon/1.1.0/rayon/struct.ThreadPoolBuilder.html#method.spawn_handler
21     // Source code says DefaultSpawner is implementation detail and
22     // shouldn't be used directly.
23     #[cfg(feature = "parallel-compilation")]
24     rayon::ThreadPoolBuilder::new()
25         .spawn_handler(move |thread| {
26             let mut b = std::thread::Builder::new();
27             if let Some(name) = thread.name() {
28                 b = b.name(name.to_owned());
29             }
30             if let Some(stack_size) = thread.stack_size() {
31                 b = b.stack_size(stack_size);
32             }
33             b.spawn(move || {
34                 file_per_thread_logger::initialize(prefix);
35                 thread.run()
36             })?;
37             Ok(())
38         })
39         .build_global()
40         .unwrap();
41 }
42 
43 wasmtime_option_group! {
44     #[derive(PartialEq, Clone, Deserialize)]
45     #[serde(rename_all = "kebab-case", deny_unknown_fields)]
46     pub struct OptimizeOptions {
47         /// Optimization level of generated code (0-2, s; default: 2)
48         #[serde(default)]
49         #[serde(deserialize_with = "crate::opt::cli_parse_wrapper")]
50         pub opt_level: Option<wasmtime::OptLevel>,
51 
52         /// Register allocator algorithm choice.
53         #[serde(default)]
54         #[serde(deserialize_with = "crate::opt::cli_parse_wrapper")]
55         pub regalloc_algorithm: Option<wasmtime::RegallocAlgorithm>,
56 
57         /// Do not allow Wasm linear memories to move in the host process's
58         /// address space.
59         pub memory_may_move: Option<bool>,
60 
61         /// Initial virtual memory allocation size for memories.
62         pub memory_reservation: Option<u64>,
63 
64         /// Bytes to reserve at the end of linear memory for growth into.
65         pub memory_reservation_for_growth: Option<u64>,
66 
67         /// Size, in bytes, of guard pages for linear memories.
68         pub memory_guard_size: Option<u64>,
69 
70         /// Indicates whether an unmapped region of memory is placed before all
71         /// linear memories.
72         pub guard_before_linear_memory: Option<bool>,
73 
74         /// Whether to initialize tables lazily, so that instantiation is
75         /// fast but indirect calls are a little slower. If no, tables are
76         /// initialized eagerly from any active element segments that apply to
77         /// them during instantiation. (default: yes)
78         pub table_lazy_init: Option<bool>,
79 
80         /// Enable the pooling allocator, in place of the on-demand allocator.
81         pub pooling_allocator: Option<bool>,
82 
83         /// The number of decommits to do per batch. A batch size of 1
84         /// effectively disables decommit batching. (default: 1)
85         pub pooling_decommit_batch_size: Option<usize>,
86 
87         /// How many bytes to keep resident between instantiations for the
88         /// pooling allocator in linear memories.
89         pub pooling_memory_keep_resident: Option<usize>,
90 
91         /// How many bytes to keep resident between instantiations for the
92         /// pooling allocator in tables.
93         pub pooling_table_keep_resident: Option<usize>,
94 
95         /// Enable memory protection keys for the pooling allocator; this can
96         /// optimize the size of memory slots.
97         #[serde(default)]
98         #[serde(deserialize_with = "crate::opt::cli_parse_wrapper")]
99         pub pooling_memory_protection_keys: Option<wasmtime::Enabled>,
100 
101         /// Sets an upper limit on how many memory protection keys (MPK) Wasmtime
102         /// will use. (default: 16)
103         pub pooling_max_memory_protection_keys: Option<usize>,
104 
105         /// Configure attempting to initialize linear memory via a
106         /// copy-on-write mapping (default: yes)
107         pub memory_init_cow: Option<bool>,
108 
109         /// Threshold below which CoW images are guaranteed to be used and be
110         /// dense.
111         pub memory_guaranteed_dense_image_size: Option<u64>,
112 
113         /// The maximum number of WebAssembly instances which can be created
114         /// with the pooling allocator.
115         pub pooling_total_core_instances: Option<u32>,
116 
117         /// The maximum number of WebAssembly components which can be created
118         /// with the pooling allocator.
119         pub pooling_total_component_instances: Option<u32>,
120 
121         /// The maximum number of WebAssembly memories which can be created with
122         /// the pooling allocator.
123         pub pooling_total_memories: Option<u32>,
124 
125         /// The maximum number of WebAssembly tables which can be created with
126         /// the pooling allocator.
127         pub pooling_total_tables: Option<u32>,
128 
129         /// The maximum number of WebAssembly stacks which can be created with
130         /// the pooling allocator.
131         pub pooling_total_stacks: Option<u32>,
132 
133         /// The maximum runtime size of each linear memory in the pooling
134         /// allocator, in bytes.
135         pub pooling_max_memory_size: Option<usize>,
136 
137         /// The maximum table elements for any table defined in a module when
138         /// using the pooling allocator.
139         pub pooling_table_elements: Option<usize>,
140 
141         /// The maximum size, in bytes, allocated for a core instance's metadata
142         /// when using the pooling allocator.
143         pub pooling_max_core_instance_size: Option<usize>,
144 
145         /// Configures the maximum number of "unused warm slots" to retain in the
146         /// pooling allocator. (default: 100)
147         pub pooling_max_unused_warm_slots: Option<u32>,
148 
149         /// How much memory, in bytes, to keep resident for async stacks allocated
150         /// with the pooling allocator. (default: 0)
151         pub pooling_async_stack_keep_resident: Option<usize>,
152 
153         /// The maximum size, in bytes, allocated for a component instance's
154         /// `VMComponentContext` metadata. (default: 1MiB)
155         pub pooling_max_component_instance_size: Option<usize>,
156 
157         /// The maximum number of core instances a single component may contain
158         /// (default is unlimited).
159         pub pooling_max_core_instances_per_component: Option<u32>,
160 
161         /// The maximum number of Wasm linear memories that a single component may
162         /// transitively contain (default is unlimited).
163         pub pooling_max_memories_per_component: Option<u32>,
164 
165         /// The maximum number of tables that a single component may transitively
166         /// contain (default is unlimited).
167         pub pooling_max_tables_per_component: Option<u32>,
168 
169         /// The maximum number of defined tables for a core module. (default: 1)
170         pub pooling_max_tables_per_module: Option<u32>,
171 
172         /// The maximum number of defined linear memories for a module. (default: 1)
173         pub pooling_max_memories_per_module: Option<u32>,
174 
175         /// The maximum number of concurrent GC heaps supported. (default: 1000)
176         pub pooling_total_gc_heaps: Option<u32>,
177 
178         /// Enable or disable the use of host signal handlers for traps.
179         pub signals_based_traps: Option<bool>,
180 
181         /// DEPRECATED: Use `-Cmemory-guard-size=N` instead.
182         pub dynamic_memory_guard_size: Option<u64>,
183 
184         /// DEPRECATED: Use `-Cmemory-guard-size=N` instead.
185         pub static_memory_guard_size: Option<u64>,
186 
187         /// DEPRECATED: Use `-Cmemory-may-move` instead.
188         pub static_memory_forced: Option<bool>,
189 
190         /// DEPRECATED: Use `-Cmemory-reservation=N` instead.
191         pub static_memory_maximum_size: Option<u64>,
192 
193         /// DEPRECATED: Use `-Cmemory-reservation-for-growth=N` instead.
194         pub dynamic_memory_reserved_for_growth: Option<u64>,
195 
196         /// Whether or not `PAGEMAP_SCAN` ioctls are used to reset linear
197         /// memory.
198         #[serde(default)]
199         #[serde(deserialize_with = "crate::opt::cli_parse_wrapper")]
200         pub pooling_pagemap_scan: Option<wasmtime::Enabled>,
201     }
202 
203     enum Optimize {
204         ...
205     }
206 }
207 
208 wasmtime_option_group! {
209     #[derive(PartialEq, Clone, Deserialize)]
210     #[serde(rename_all = "kebab-case", deny_unknown_fields)]
211     pub struct CodegenOptions {
212         /// Either `cranelift` or `winch`.
213         ///
214         /// Currently only `cranelift` and `winch` are supported, but not all
215         /// builds of Wasmtime have both built in.
216         #[serde(default)]
217         #[serde(deserialize_with = "crate::opt::cli_parse_wrapper")]
218         pub compiler: Option<wasmtime::Strategy>,
219         /// Which garbage collector to use: `drc` or `null`.
220         ///
221         /// `drc` is the deferred reference-counting collector.
222         ///
223         /// `null` is the null garbage collector, which does not collect any
224         /// garbage.
225         ///
226         /// Note that not all builds of Wasmtime will have support for garbage
227         /// collection included.
228         #[serde(default)]
229         #[serde(deserialize_with = "crate::opt::cli_parse_wrapper")]
230         pub collector: Option<wasmtime::Collector>,
231         /// Enable Cranelift's internal debug verifier (expensive)
232         pub cranelift_debug_verifier: Option<bool>,
233         /// Whether or not to enable caching of compiled modules.
234         pub cache: Option<bool>,
235         /// Configuration for compiled module caching.
236         pub cache_config: Option<String>,
237         /// Whether or not to enable parallel compilation of modules.
238         pub parallel_compilation: Option<bool>,
239         /// Whether to enable proof-carrying code (PCC)-based validation.
240         pub pcc: Option<bool>,
241         /// Controls whether native unwind information is present in compiled
242         /// object files.
243         pub native_unwind_info: Option<bool>,
244 
245         /// Whether to perform function inlining during compilation.
246         pub inlining: Option<bool>,
247 
248         #[prefixed = "cranelift"]
249         #[serde(default)]
250         /// Set a cranelift-specific option. Use `wasmtime settings` to see
251         /// all.
252         pub cranelift: Vec<(String, Option<String>)>,
253     }
254 
255     enum Codegen {
256         ...
257     }
258 }
259 
260 wasmtime_option_group! {
261     #[derive(PartialEq, Clone, Deserialize)]
262     #[serde(rename_all = "kebab-case", deny_unknown_fields)]
263     pub struct DebugOptions {
264         /// Enable generation of DWARF debug information in compiled code.
265         pub debug_info: Option<bool>,
266         /// Enable guest debugging insrumentation.
267         pub guest_debug: Option<bool>,
268         /// Configure whether compiled code can map native addresses to wasm.
269         pub address_map: Option<bool>,
270         /// Configure whether logging is enabled.
271         pub logging: Option<bool>,
272         /// Configure whether logs are emitted to files
273         pub log_to_files: Option<bool>,
274         /// Enable coredump generation to this file after a WebAssembly trap.
275         pub coredump: Option<String>,
276     }
277 
278     enum Debug {
279         ...
280     }
281 }
282 
283 wasmtime_option_group! {
284     #[derive(PartialEq, Clone, Deserialize)]
285     #[serde(rename_all = "kebab-case", deny_unknown_fields)]
286     pub struct WasmOptions {
287         /// Enable canonicalization of all NaN values.
288         pub nan_canonicalization: Option<bool>,
289         /// Enable execution fuel with N units fuel, trapping after running out
290         /// of fuel.
291         ///
292         /// Most WebAssembly instructions consume 1 unit of fuel. Some
293         /// instructions, such as `nop`, `drop`, `block`, and `loop`, consume 0
294         /// units, as any execution cost associated with them involves other
295         /// instructions which do consume fuel.
296         pub fuel: Option<u64>,
297         /// Yield when a global epoch counter changes, allowing for async
298         /// operation without blocking the executor.
299         pub epoch_interruption: Option<bool>,
300         /// Maximum stack size, in bytes, that wasm is allowed to consume before a
301         /// stack overflow is reported.
302         pub max_wasm_stack: Option<usize>,
303         /// Stack size, in bytes, that will be allocated for async stacks.
304         ///
305         /// Note that this must be larger than `max-wasm-stack` and the
306         /// difference between the two is how much stack the host has to execute
307         /// on.
308         pub async_stack_size: Option<usize>,
309         /// Configures whether or not stacks used for async futures are zeroed
310         /// before (re)use as a defense-in-depth mechanism. (default: false)
311         pub async_stack_zeroing: Option<bool>,
312         /// Allow unknown exports when running commands.
313         pub unknown_exports_allow: Option<bool>,
314         /// Allow the main module to import unknown functions, using an
315         /// implementation that immediately traps, when running commands.
316         pub unknown_imports_trap: Option<bool>,
317         /// Allow the main module to import unknown functions, using an
318         /// implementation that returns default values, when running commands.
319         pub unknown_imports_default: Option<bool>,
320         /// Enables memory error checking. (see wmemcheck.md for more info)
321         pub wmemcheck: Option<bool>,
322         /// Maximum size, in bytes, that a linear memory is allowed to reach.
323         ///
324         /// Growth beyond this limit will cause `memory.grow` instructions in
325         /// WebAssembly modules to return -1 and fail.
326         pub max_memory_size: Option<usize>,
327         /// Maximum size, in table elements, that a table is allowed to reach.
328         pub max_table_elements: Option<usize>,
329         /// Maximum number of WebAssembly instances allowed to be created.
330         pub max_instances: Option<usize>,
331         /// Maximum number of WebAssembly tables allowed to be created.
332         pub max_tables: Option<usize>,
333         /// Maximum number of WebAssembly linear memories allowed to be created.
334         pub max_memories: Option<usize>,
335         /// Force a trap to be raised on `memory.grow` and `table.grow` failure
336         /// instead of returning -1 from these instructions.
337         ///
338         /// This is not necessarily a spec-compliant option to enable but can be
339         /// useful for tracking down a backtrace of what is requesting so much
340         /// memory, for example.
341         pub trap_on_grow_failure: Option<bool>,
342         /// Maximum execution time of wasm code before timing out (1, 2s, 100ms, etc)
343         pub timeout: Option<Duration>,
344         /// Configures support for all WebAssembly proposals implemented.
345         pub all_proposals: Option<bool>,
346         /// Configure support for the bulk memory proposal.
347         pub bulk_memory: Option<bool>,
348         /// Configure support for the multi-memory proposal.
349         pub multi_memory: Option<bool>,
350         /// Configure support for the multi-value proposal.
351         pub multi_value: Option<bool>,
352         /// Configure support for the reference-types proposal.
353         pub reference_types: Option<bool>,
354         /// Configure support for the simd proposal.
355         pub simd: Option<bool>,
356         /// Configure support for the relaxed-simd proposal.
357         pub relaxed_simd: Option<bool>,
358         /// Configure forcing deterministic and host-independent behavior of
359         /// the relaxed-simd instructions.
360         ///
361         /// By default these instructions may have architecture-specific behavior as
362         /// allowed by the specification, but this can be used to force the behavior
363         /// of these instructions to match the deterministic behavior classified in
364         /// the specification. Note that enabling this option may come at a
365         /// performance cost.
366         pub relaxed_simd_deterministic: Option<bool>,
367         /// Configure support for the tail-call proposal.
368         pub tail_call: Option<bool>,
369         /// Configure support for the threads proposal.
370         pub threads: Option<bool>,
371         /// Configure the ability to create a `shared` memory.
372         pub shared_memory: Option<bool>,
373         /// Configure support for the shared-everything-threads proposal.
374         pub shared_everything_threads: Option<bool>,
375         /// Configure support for the memory64 proposal.
376         pub memory64: Option<bool>,
377         /// Configure support for the component-model proposal.
378         pub component_model: Option<bool>,
379         /// Component model support for async lifting/lowering.
380         pub component_model_async: Option<bool>,
381         /// Component model support for async lifting/lowering: this corresponds
382         /// to the �� emoji in the component model specification.
383         pub component_model_async_builtins: Option<bool>,
384         /// Component model support for async lifting/lowering: this corresponds
385         /// to the �� emoji in the component model specification.
386         pub component_model_async_stackful: Option<bool>,
387         /// Component model support for threading: this corresponds
388         /// to the �� emoji in the component model specification.
389         pub component_model_threading: Option<bool>,
390         /// Component model support for `error-context`: this corresponds
391         /// to the �� emoji in the component model specification.
392         pub component_model_error_context: Option<bool>,
393         /// GC support in the component model: this corresponds to the �� emoji
394         /// in the component model specification.
395         pub component_model_gc: Option<bool>,
396         /// Configure support for the function-references proposal.
397         pub function_references: Option<bool>,
398         /// Configure support for the stack-switching proposal.
399         pub stack_switching: Option<bool>,
400         /// Configure support for the GC proposal.
401         pub gc: Option<bool>,
402         /// Configure support for the custom-page-sizes proposal.
403         pub custom_page_sizes: Option<bool>,
404         /// Configure support for the wide-arithmetic proposal.
405         pub wide_arithmetic: Option<bool>,
406         /// Configure support for the extended-const proposal.
407         pub extended_const: Option<bool>,
408         /// Configure support for the exceptions proposal.
409         pub exceptions: Option<bool>,
410         /// Whether or not any GC infrastructure in Wasmtime is enabled or not.
411         pub gc_support: Option<bool>,
412     }
413 
414     enum Wasm {
415         ...
416     }
417 }
418 
419 wasmtime_option_group! {
420     #[derive(PartialEq, Clone, Deserialize)]
421     #[serde(rename_all = "kebab-case", deny_unknown_fields)]
422     pub struct WasiOptions {
423         /// Enable support for WASI CLI APIs, including filesystems, sockets, clocks, and random.
424         pub cli: Option<bool>,
425         /// Enable WASI APIs marked as: @unstable(feature = cli-exit-with-code)
426         pub cli_exit_with_code: Option<bool>,
427         /// Deprecated alias for `cli`
428         pub common: Option<bool>,
429         /// Enable support for WASI neural network imports (experimental)
430         pub nn: Option<bool>,
431         /// Enable support for WASI threading imports (experimental). Implies preview2=false.
432         pub threads: Option<bool>,
433         /// Enable support for WASI HTTP imports
434         pub http: Option<bool>,
435         /// Number of distinct write calls to the outgoing body's output-stream
436         /// that the implementation will buffer.
437         /// Default: 1.
438         pub http_outgoing_body_buffer_chunks: Option<usize>,
439         /// Maximum size allowed in a write call to the outgoing body's output-stream.
440         /// Default: 1024 * 1024.
441         pub http_outgoing_body_chunk_size: Option<usize>,
442         /// Enable support for WASI config imports (experimental)
443         pub config: Option<bool>,
444         /// Enable support for WASI key-value imports (experimental)
445         pub keyvalue: Option<bool>,
446         /// Inherit environment variables and file descriptors following the
447         /// systemd listen fd specification (UNIX only) (legacy wasip1
448         /// implementation only)
449         pub listenfd: Option<bool>,
450         /// Grant access to the given TCP listen socket (experimental, legacy
451         /// wasip1 implementation only)
452         #[serde(default)]
453         pub tcplisten: Vec<String>,
454         /// Enable support for WASI TLS (Transport Layer Security) imports (experimental)
455         pub tls: Option<bool>,
456         /// Implement WASI Preview1 using new Preview2 implementation (true, default) or legacy
457         /// implementation (false)
458         pub preview2: Option<bool>,
459         /// Pre-load machine learning graphs (i.e., models) for use by wasi-nn.
460         ///
461         /// Each use of the flag will preload a ML model from the host directory
462         /// using the given model encoding. The model will be mapped to the
463         /// directory name: e.g., `--wasi-nn-graph openvino:/foo/bar` will preload
464         /// an OpenVINO model named `bar`. Note that which model encodings are
465         /// available is dependent on the backends implemented in the
466         /// `wasmtime_wasi_nn` crate.
467         #[serde(skip)]
468         pub nn_graph: Vec<WasiNnGraph>,
469         /// Flag for WASI preview2 to inherit the host's network within the
470         /// guest so it has full access to all addresses/ports/etc.
471         pub inherit_network: Option<bool>,
472         /// Indicates whether `wasi:sockets/ip-name-lookup` is enabled or not.
473         pub allow_ip_name_lookup: Option<bool>,
474         /// Indicates whether `wasi:sockets` TCP support is enabled or not.
475         pub tcp: Option<bool>,
476         /// Indicates whether `wasi:sockets` UDP support is enabled or not.
477         pub udp: Option<bool>,
478         /// Enable WASI APIs marked as: @unstable(feature = network-error-code)
479         pub network_error_code: Option<bool>,
480         /// Allows imports from the `wasi_unstable` core wasm module.
481         pub preview0: Option<bool>,
482         /// Inherit all environment variables from the parent process.
483         ///
484         /// This option can be further overwritten with `--env` flags.
485         pub inherit_env: Option<bool>,
486         /// Pass a wasi config variable to the program.
487         #[serde(skip)]
488         pub config_var: Vec<KeyValuePair>,
489         /// Preset data for the In-Memory provider of WASI key-value API.
490         #[serde(skip)]
491         pub keyvalue_in_memory_data: Vec<KeyValuePair>,
492         /// Enable support for WASIp3 APIs.
493         pub p3: Option<bool>,
494     }
495 
496     enum Wasi {
497         ...
498     }
499 }
500 
501 #[derive(Debug, Clone, PartialEq)]
502 pub struct WasiNnGraph {
503     pub format: String,
504     pub dir: String,
505 }
506 
507 #[derive(Debug, Clone, PartialEq)]
508 pub struct KeyValuePair {
509     pub key: String,
510     pub value: String,
511 }
512 
513 /// Common options for commands that translate WebAssembly modules
514 #[derive(Parser, Clone, Deserialize)]
515 #[serde(deny_unknown_fields)]
516 pub struct CommonOptions {
517     // These options groups are used to parse `-O` and such options but aren't
518     // the raw form consumed by the CLI. Instead they're pushed into the `pub`
519     // fields below as part of the `configure` method.
520     //
521     // Ideally clap would support `pub opts: OptimizeOptions` and parse directly
522     // into that but it does not appear to do so for multiple `-O` flags for
523     // now.
524     /// Optimization and tuning related options for wasm performance, `-O help` to
525     /// see all.
526     #[arg(short = 'O', long = "optimize", value_name = "KEY[=VAL[,..]]")]
527     #[serde(skip)]
528     opts_raw: Vec<opt::CommaSeparated<Optimize>>,
529 
530     /// Codegen-related configuration options, `-C help` to see all.
531     #[arg(short = 'C', long = "codegen", value_name = "KEY[=VAL[,..]]")]
532     #[serde(skip)]
533     codegen_raw: Vec<opt::CommaSeparated<Codegen>>,
534 
535     /// Debug-related configuration options, `-D help` to see all.
536     #[arg(short = 'D', long = "debug", value_name = "KEY[=VAL[,..]]")]
537     #[serde(skip)]
538     debug_raw: Vec<opt::CommaSeparated<Debug>>,
539 
540     /// Options for configuring semantic execution of WebAssembly, `-W help` to see
541     /// all.
542     #[arg(short = 'W', long = "wasm", value_name = "KEY[=VAL[,..]]")]
543     #[serde(skip)]
544     wasm_raw: Vec<opt::CommaSeparated<Wasm>>,
545 
546     /// Options for configuring WASI and its proposals, `-S help` to see all.
547     #[arg(short = 'S', long = "wasi", value_name = "KEY[=VAL[,..]]")]
548     #[serde(skip)]
549     wasi_raw: Vec<opt::CommaSeparated<Wasi>>,
550 
551     // These fields are filled in by the `configure` method below via the
552     // options parsed from the CLI above. This is what the CLI should use.
553     #[arg(skip)]
554     #[serde(skip)]
555     configured: bool,
556 
557     #[arg(skip)]
558     #[serde(rename = "optimize", default)]
559     pub opts: OptimizeOptions,
560 
561     #[arg(skip)]
562     #[serde(rename = "codegen", default)]
563     pub codegen: CodegenOptions,
564 
565     #[arg(skip)]
566     #[serde(rename = "debug", default)]
567     pub debug: DebugOptions,
568 
569     #[arg(skip)]
570     #[serde(rename = "wasm", default)]
571     pub wasm: WasmOptions,
572 
573     #[arg(skip)]
574     #[serde(rename = "wasi", default)]
575     pub wasi: WasiOptions,
576 
577     /// The target triple; default is the host triple
578     #[arg(long, value_name = "TARGET")]
579     #[serde(skip)]
580     pub target: Option<String>,
581 
582     /// Use the specified TOML configuration file.
583     /// This TOML configuration file can provide same configuration options as the
584     /// `--optimize`, `--codgen`, `--debug`, `--wasm`, `--wasi` CLI options, with a couple exceptions.
585     ///
586     /// Additional options specified on the command line will take precedent over options loaded from
587     /// this TOML file.
588     #[arg(long = "config", value_name = "FILE")]
589     #[serde(skip)]
590     pub config: Option<PathBuf>,
591 }
592 
593 macro_rules! match_feature {
594     (
595         [$feat:tt : $config:expr]
596         $val:ident => $e:expr,
597         $p:pat => err,
598     ) => {
599         #[cfg(feature = $feat)]
600         {
601             if let Some($val) = $config {
602                 $e;
603             }
604         }
605         #[cfg(not(feature = $feat))]
606         {
607             if let Some($p) = $config {
608                 bail!(concat!("support for ", $feat, " disabled at compile time"));
609             }
610         }
611     };
612 }
613 
614 impl CommonOptions {
615     /// Creates a blank new set of [`CommonOptions`] that can be configured.
616     pub fn new() -> CommonOptions {
617         CommonOptions {
618             opts_raw: Vec::new(),
619             codegen_raw: Vec::new(),
620             debug_raw: Vec::new(),
621             wasm_raw: Vec::new(),
622             wasi_raw: Vec::new(),
623             configured: true,
624             opts: Default::default(),
625             codegen: Default::default(),
626             debug: Default::default(),
627             wasm: Default::default(),
628             wasi: Default::default(),
629             target: None,
630             config: None,
631         }
632     }
633 
634     fn configure(&mut self) -> Result<()> {
635         if self.configured {
636             return Ok(());
637         }
638         self.configured = true;
639         if let Some(toml_config_path) = &self.config {
640             let toml_options = CommonOptions::from_file(toml_config_path)?;
641             self.opts = toml_options.opts;
642             self.codegen = toml_options.codegen;
643             self.debug = toml_options.debug;
644             self.wasm = toml_options.wasm;
645             self.wasi = toml_options.wasi;
646         }
647         self.opts.configure_with(&self.opts_raw);
648         self.codegen.configure_with(&self.codegen_raw);
649         self.debug.configure_with(&self.debug_raw);
650         self.wasm.configure_with(&self.wasm_raw);
651         self.wasi.configure_with(&self.wasi_raw);
652         Ok(())
653     }
654 
655     pub fn init_logging(&mut self) -> Result<()> {
656         self.configure()?;
657         if self.debug.logging == Some(false) {
658             return Ok(());
659         }
660         #[cfg(feature = "logging")]
661         if self.debug.log_to_files == Some(true) {
662             let prefix = "wasmtime.dbg.";
663             init_file_per_thread_logger(prefix);
664         } else {
665             use std::io::IsTerminal;
666             use tracing_subscriber::{EnvFilter, FmtSubscriber};
667             let builder = FmtSubscriber::builder()
668                 .with_writer(std::io::stderr)
669                 .with_env_filter(EnvFilter::from_env("WASMTIME_LOG"))
670                 .with_ansi(std::io::stderr().is_terminal());
671             if std::env::var("WASMTIME_LOG_NO_CONTEXT").is_ok_and(|value| value.eq("1")) {
672                 builder
673                     .with_level(false)
674                     .with_target(false)
675                     .without_time()
676                     .init()
677             } else {
678                 builder.init();
679             }
680         }
681         #[cfg(not(feature = "logging"))]
682         if self.debug.log_to_files == Some(true) || self.debug.logging == Some(true) {
683             bail!("support for logging disabled at compile time");
684         }
685         Ok(())
686     }
687 
688     pub fn config(&mut self, pooling_allocator_default: Option<bool>) -> Result<Config> {
689         self.configure()?;
690         let mut config = Config::new();
691 
692         match_feature! {
693             ["cranelift" : self.codegen.compiler]
694             strategy => config.strategy(strategy),
695             _ => err,
696         }
697         match_feature! {
698             ["gc" : self.codegen.collector]
699             collector => config.collector(collector),
700             _ => err,
701         }
702         if let Some(target) = &self.target {
703             config.target(target)?;
704         }
705         match_feature! {
706             ["cranelift" : self.codegen.cranelift_debug_verifier]
707             enable => config.cranelift_debug_verifier(enable),
708             true => err,
709         }
710         if let Some(enable) = self.debug.debug_info {
711             config.debug_info(enable);
712         }
713         match_feature! {
714             ["debug" : self.debug.guest_debug]
715             enable => config.guest_debug(enable),
716             _ => err,
717         }
718         if self.debug.coredump.is_some() {
719             #[cfg(feature = "coredump")]
720             config.coredump_on_trap(true);
721             #[cfg(not(feature = "coredump"))]
722             bail!("support for coredumps disabled at compile time");
723         }
724         match_feature! {
725             ["cranelift" : self.opts.opt_level]
726             level => config.cranelift_opt_level(level),
727             _ => err,
728         }
729         match_feature! {
730             ["cranelift": self.opts.regalloc_algorithm]
731             algo => config.cranelift_regalloc_algorithm(algo),
732             _ => err,
733         }
734         match_feature! {
735             ["cranelift" : self.wasm.nan_canonicalization]
736             enable => config.cranelift_nan_canonicalization(enable),
737             true => err,
738         }
739         match_feature! {
740             ["cranelift" : self.codegen.pcc]
741             enable => config.cranelift_pcc(enable),
742             true => err,
743         }
744 
745         self.enable_wasm_features(&mut config)?;
746 
747         #[cfg(feature = "cranelift")]
748         for (name, value) in self.codegen.cranelift.iter() {
749             let name = name.replace('-', "_");
750             unsafe {
751                 match value {
752                     Some(val) => {
753                         config.cranelift_flag_set(&name, val);
754                     }
755                     None => {
756                         config.cranelift_flag_enable(&name);
757                     }
758                 }
759             }
760         }
761         #[cfg(not(feature = "cranelift"))]
762         if !self.codegen.cranelift.is_empty() {
763             bail!("support for cranelift disabled at compile time");
764         }
765 
766         #[cfg(feature = "cache")]
767         if self.codegen.cache != Some(false) {
768             use wasmtime::Cache;
769             let cache = match &self.codegen.cache_config {
770                 Some(path) => Cache::from_file(Some(Path::new(path)))?,
771                 None => Cache::from_file(None)?,
772             };
773             config.cache(Some(cache));
774         }
775         #[cfg(not(feature = "cache"))]
776         if self.codegen.cache == Some(true) {
777             bail!("support for caching disabled at compile time");
778         }
779 
780         match_feature! {
781             ["parallel-compilation" : self.codegen.parallel_compilation]
782             enable => config.parallel_compilation(enable),
783             true => err,
784         }
785 
786         let memory_reservation = self
787             .opts
788             .memory_reservation
789             .or(self.opts.static_memory_maximum_size);
790         if let Some(size) = memory_reservation {
791             config.memory_reservation(size);
792         }
793 
794         if let Some(enable) = self.opts.static_memory_forced {
795             config.memory_may_move(!enable);
796         }
797         if let Some(enable) = self.opts.memory_may_move {
798             config.memory_may_move(enable);
799         }
800 
801         let memory_guard_size = self
802             .opts
803             .static_memory_guard_size
804             .or(self.opts.dynamic_memory_guard_size)
805             .or(self.opts.memory_guard_size);
806         if let Some(size) = memory_guard_size {
807             config.memory_guard_size(size);
808         }
809 
810         let mem_for_growth = self
811             .opts
812             .memory_reservation_for_growth
813             .or(self.opts.dynamic_memory_reserved_for_growth);
814         if let Some(size) = mem_for_growth {
815             config.memory_reservation_for_growth(size);
816         }
817         if let Some(enable) = self.opts.guard_before_linear_memory {
818             config.guard_before_linear_memory(enable);
819         }
820         if let Some(enable) = self.opts.table_lazy_init {
821             config.table_lazy_init(enable);
822         }
823 
824         // If fuel has been configured, set the `consume fuel` flag on the config.
825         if self.wasm.fuel.is_some() {
826             config.consume_fuel(true);
827         }
828 
829         if let Some(enable) = self.wasm.epoch_interruption {
830             config.epoch_interruption(enable);
831         }
832         if let Some(enable) = self.debug.address_map {
833             config.generate_address_map(enable);
834         }
835         if let Some(enable) = self.opts.memory_init_cow {
836             config.memory_init_cow(enable);
837         }
838         if let Some(size) = self.opts.memory_guaranteed_dense_image_size {
839             config.memory_guaranteed_dense_image_size(size);
840         }
841         if let Some(enable) = self.opts.signals_based_traps {
842             config.signals_based_traps(enable);
843         }
844         if let Some(enable) = self.codegen.native_unwind_info {
845             config.native_unwind_info(enable);
846         }
847         if let Some(enable) = self.codegen.inlining {
848             config.compiler_inlining(enable);
849         }
850 
851         // async_stack_size enabled by either async or stack-switching, so
852         // cannot directly use match_feature!
853         #[cfg(any(feature = "async", feature = "stack-switching"))]
854         {
855             if let Some(size) = self.wasm.async_stack_size {
856                 config.async_stack_size(size);
857             }
858         }
859         #[cfg(not(any(feature = "async", feature = "stack-switching")))]
860         {
861             if let Some(_size) = self.wasm.async_stack_size {
862                 bail!(concat!(
863                     "support for async/stack-switching disabled at compile time"
864                 ));
865             }
866         }
867 
868         match_feature! {
869             ["pooling-allocator" : self.opts.pooling_allocator.or(pooling_allocator_default)]
870             enable => {
871                 if enable {
872                     let mut cfg = wasmtime::PoolingAllocationConfig::default();
873                     if let Some(size) = self.opts.pooling_memory_keep_resident {
874                         cfg.linear_memory_keep_resident(size);
875                     }
876                     if let Some(size) = self.opts.pooling_table_keep_resident {
877                         cfg.table_keep_resident(size);
878                     }
879                     if let Some(limit) = self.opts.pooling_total_core_instances {
880                         cfg.total_core_instances(limit);
881                     }
882                     if let Some(limit) = self.opts.pooling_total_component_instances {
883                         cfg.total_component_instances(limit);
884                     }
885                     if let Some(limit) = self.opts.pooling_total_memories {
886                         cfg.total_memories(limit);
887                     }
888                     if let Some(limit) = self.opts.pooling_total_tables {
889                         cfg.total_tables(limit);
890                     }
891                     if let Some(limit) = self.opts.pooling_table_elements
892                         .or(self.wasm.max_table_elements)
893                     {
894                         cfg.table_elements(limit);
895                     }
896                     if let Some(limit) = self.opts.pooling_max_core_instance_size {
897                         cfg.max_core_instance_size(limit);
898                     }
899                     match_feature! {
900                         ["async" : self.opts.pooling_total_stacks]
901                         limit => cfg.total_stacks(limit),
902                         _ => err,
903                     }
904                     if let Some(max) = self.opts.pooling_max_memory_size
905                         .or(self.wasm.max_memory_size)
906                     {
907                         cfg.max_memory_size(max);
908                     }
909                     if let Some(size) = self.opts.pooling_decommit_batch_size {
910                         cfg.decommit_batch_size(size);
911                     }
912                     if let Some(max) = self.opts.pooling_max_unused_warm_slots {
913                         cfg.max_unused_warm_slots(max);
914                     }
915                     match_feature! {
916                         ["async" : self.opts.pooling_async_stack_keep_resident]
917                         size => cfg.async_stack_keep_resident(size),
918                         _ => err,
919                     }
920                     if let Some(max) = self.opts.pooling_max_component_instance_size {
921                         cfg.max_component_instance_size(max);
922                     }
923                     if let Some(max) = self.opts.pooling_max_core_instances_per_component {
924                         cfg.max_core_instances_per_component(max);
925                     }
926                     if let Some(max) = self.opts.pooling_max_memories_per_component {
927                         cfg.max_memories_per_component(max);
928                     }
929                     if let Some(max) = self.opts.pooling_max_tables_per_component {
930                         cfg.max_tables_per_component(max);
931                     }
932                     if let Some(max) = self.opts.pooling_max_tables_per_module {
933                         cfg.max_tables_per_module(max);
934                     }
935                     if let Some(max) = self.opts.pooling_max_memories_per_module {
936                         cfg.max_memories_per_module(max);
937                     }
938                     match_feature! {
939                         ["memory-protection-keys" : self.opts.pooling_memory_protection_keys]
940                         enable => cfg.memory_protection_keys(enable),
941                         _ => err,
942                     }
943                     match_feature! {
944                         ["memory-protection-keys" : self.opts.pooling_max_memory_protection_keys]
945                         max => cfg.max_memory_protection_keys(max),
946                         _ => err,
947                     }
948                     match_feature! {
949                         ["gc" : self.opts.pooling_total_gc_heaps]
950                         max => cfg.total_gc_heaps(max),
951                         _ => err,
952                     }
953                     if let Some(enabled) = self.opts.pooling_pagemap_scan {
954                         cfg.pagemap_scan(enabled);
955                     }
956                     config.allocation_strategy(wasmtime::InstanceAllocationStrategy::Pooling(cfg));
957                 }
958             },
959             true => err,
960         }
961 
962         if self.opts.pooling_memory_protection_keys.is_some()
963             && !self.opts.pooling_allocator.unwrap_or(false)
964         {
965             bail!("memory protection keys require the pooling allocator");
966         }
967 
968         if self.opts.pooling_max_memory_protection_keys.is_some()
969             && !self.opts.pooling_memory_protection_keys.is_some()
970         {
971             bail!("max memory protection keys requires memory protection keys to be enabled");
972         }
973 
974         match_feature! {
975             ["async" : self.wasm.async_stack_zeroing]
976             enable => config.async_stack_zeroing(enable),
977             _ => err,
978         }
979 
980         if let Some(max) = self.wasm.max_wasm_stack {
981             config.max_wasm_stack(max);
982 
983             // If `-Wasync-stack-size` isn't passed then automatically adjust it
984             // to the wasm stack size provided here too. That prevents the need
985             // to pass both when one can generally be inferred from the other.
986             #[cfg(any(feature = "async", feature = "stack-switching"))]
987             if self.wasm.async_stack_size.is_none() {
988                 const DEFAULT_HOST_STACK: usize = 512 << 10;
989                 config.async_stack_size(max + DEFAULT_HOST_STACK);
990             }
991         }
992 
993         if let Some(enable) = self.wasm.relaxed_simd_deterministic {
994             config.relaxed_simd_deterministic(enable);
995         }
996         match_feature! {
997             ["cranelift" : self.wasm.wmemcheck]
998             enable => config.wmemcheck(enable),
999             true => err,
1000         }
1001 
1002         if let Some(enable) = self.wasm.gc_support {
1003             config.gc_support(enable);
1004         }
1005 
1006         if let Some(enable) = self.wasm.shared_memory {
1007             config.shared_memory(enable);
1008         }
1009 
1010         Ok(config)
1011     }
1012 
1013     pub fn enable_wasm_features(&self, config: &mut Config) -> Result<()> {
1014         let all = self.wasm.all_proposals;
1015 
1016         if let Some(enable) = self.wasm.simd.or(all) {
1017             config.wasm_simd(enable);
1018         }
1019         if let Some(enable) = self.wasm.relaxed_simd.or(all) {
1020             config.wasm_relaxed_simd(enable);
1021         }
1022         if let Some(enable) = self.wasm.bulk_memory.or(all) {
1023             config.wasm_bulk_memory(enable);
1024         }
1025         if let Some(enable) = self.wasm.multi_value.or(all) {
1026             config.wasm_multi_value(enable);
1027         }
1028         if let Some(enable) = self.wasm.tail_call.or(all) {
1029             config.wasm_tail_call(enable);
1030         }
1031         if let Some(enable) = self.wasm.multi_memory.or(all) {
1032             config.wasm_multi_memory(enable);
1033         }
1034         if let Some(enable) = self.wasm.memory64.or(all) {
1035             config.wasm_memory64(enable);
1036         }
1037         if let Some(enable) = self.wasm.stack_switching {
1038             config.wasm_stack_switching(enable);
1039         }
1040         if let Some(enable) = self.wasm.custom_page_sizes.or(all) {
1041             config.wasm_custom_page_sizes(enable);
1042         }
1043         if let Some(enable) = self.wasm.wide_arithmetic.or(all) {
1044             config.wasm_wide_arithmetic(enable);
1045         }
1046         if let Some(enable) = self.wasm.extended_const.or(all) {
1047             config.wasm_extended_const(enable);
1048         }
1049 
1050         macro_rules! handle_conditionally_compiled {
1051             ($(($feature:tt, $field:tt, $method:tt))*) => ($(
1052                 if let Some(enable) = self.wasm.$field.or(all) {
1053                     #[cfg(feature = $feature)]
1054                     config.$method(enable);
1055                     #[cfg(not(feature = $feature))]
1056                     if enable && all.is_none() {
1057                         bail!("support for {} was disabled at compile-time", $feature);
1058                     }
1059                 }
1060             )*)
1061         }
1062 
1063         handle_conditionally_compiled! {
1064             ("component-model", component_model, wasm_component_model)
1065             ("component-model-async", component_model_async, wasm_component_model_async)
1066             ("component-model-async", component_model_async_builtins, wasm_component_model_async_builtins)
1067             ("component-model-async", component_model_async_stackful, wasm_component_model_async_stackful)
1068             ("component-model-async", component_model_threading, wasm_component_model_threading)
1069             ("component-model", component_model_error_context, wasm_component_model_error_context)
1070             ("threads", threads, wasm_threads)
1071             ("gc", gc, wasm_gc)
1072             ("gc", reference_types, wasm_reference_types)
1073             ("gc", function_references, wasm_function_references)
1074             ("gc", exceptions, wasm_exceptions)
1075             ("stack-switching", stack_switching, wasm_stack_switching)
1076         }
1077 
1078         if let Some(enable) = self.wasm.component_model_gc {
1079             #[cfg(all(feature = "component-model", feature = "gc"))]
1080             config.wasm_component_model_gc(enable);
1081             #[cfg(not(all(feature = "component-model", feature = "gc")))]
1082             if enable && all.is_none() {
1083                 bail!("support for `component-model-gc` was disabled at compile time")
1084             }
1085         }
1086 
1087         Ok(())
1088     }
1089 
1090     pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
1091         let path_ref = path.as_ref();
1092         let file_contents = fs::read_to_string(path_ref)
1093             .with_context(|| format!("failed to read config file: {path_ref:?}"))?;
1094         toml::from_str::<CommonOptions>(&file_contents)
1095             .with_context(|| format!("failed to parse TOML config file {path_ref:?}"))
1096     }
1097 }
1098 
1099 #[cfg(test)]
1100 mod tests {
1101     use wasmtime::{OptLevel, RegallocAlgorithm};
1102 
1103     use super::*;
1104 
1105     #[test]
1106     fn from_toml() {
1107         // empty toml
1108         let empty_toml = "";
1109         let mut common_options: CommonOptions = toml::from_str(empty_toml).unwrap();
1110         common_options.config(None).unwrap();
1111 
1112         // basic toml
1113         let basic_toml = r#"
1114             [optimize]
1115             [codegen]
1116             [debug]
1117             [wasm]
1118             [wasi]
1119         "#;
1120         let mut common_options: CommonOptions = toml::from_str(basic_toml).unwrap();
1121         common_options.config(None).unwrap();
1122 
1123         // toml with custom deserialization to match CLI flag parsing
1124         for (opt_value, expected) in [
1125             ("0", Some(OptLevel::None)),
1126             ("1", Some(OptLevel::Speed)),
1127             ("2", Some(OptLevel::Speed)),
1128             ("\"s\"", Some(OptLevel::SpeedAndSize)),
1129             ("\"hello\"", None), // should fail
1130             ("3", None),         // should fail
1131         ] {
1132             let toml = format!(
1133                 r#"
1134                     [optimize]
1135                     opt-level = {opt_value}
1136                 "#,
1137             );
1138             let parsed_opt_level = toml::from_str::<CommonOptions>(&toml)
1139                 .ok()
1140                 .and_then(|common_options| common_options.opts.opt_level);
1141 
1142             assert_eq!(
1143                 parsed_opt_level, expected,
1144                 "Mismatch for input '{opt_value}'. Parsed: {parsed_opt_level:?}, Expected: {expected:?}"
1145             );
1146         }
1147 
1148         // Regalloc algorithm
1149         for (regalloc_value, expected) in [
1150             ("\"backtracking\"", Some(RegallocAlgorithm::Backtracking)),
1151             ("\"single-pass\"", Some(RegallocAlgorithm::SinglePass)),
1152             ("\"hello\"", None), // should fail
1153             ("3", None),         // should fail
1154             ("true", None),      // should fail
1155         ] {
1156             let toml = format!(
1157                 r#"
1158                     [optimize]
1159                     regalloc-algorithm = {regalloc_value}
1160                 "#,
1161             );
1162             let parsed_regalloc_algorithm = toml::from_str::<CommonOptions>(&toml)
1163                 .ok()
1164                 .and_then(|common_options| common_options.opts.regalloc_algorithm);
1165             assert_eq!(
1166                 parsed_regalloc_algorithm, expected,
1167                 "Mismatch for input '{regalloc_value}'. Parsed: {parsed_regalloc_algorithm:?}, Expected: {expected:?}"
1168             );
1169         }
1170 
1171         // Strategy
1172         for (strategy_value, expected) in [
1173             ("\"cranelift\"", Some(wasmtime::Strategy::Cranelift)),
1174             ("\"winch\"", Some(wasmtime::Strategy::Winch)),
1175             ("\"hello\"", None), // should fail
1176             ("5", None),         // should fail
1177             ("true", None),      // should fail
1178         ] {
1179             let toml = format!(
1180                 r#"
1181                     [codegen]
1182                     compiler = {strategy_value}
1183                 "#,
1184             );
1185             let parsed_strategy = toml::from_str::<CommonOptions>(&toml)
1186                 .ok()
1187                 .and_then(|common_options| common_options.codegen.compiler);
1188             assert_eq!(
1189                 parsed_strategy, expected,
1190                 "Mismatch for input '{strategy_value}'. Parsed: {parsed_strategy:?}, Expected: {expected:?}",
1191             );
1192         }
1193 
1194         // Collector
1195         for (collector_value, expected) in [
1196             (
1197                 "\"drc\"",
1198                 Some(wasmtime::Collector::DeferredReferenceCounting),
1199             ),
1200             ("\"null\"", Some(wasmtime::Collector::Null)),
1201             ("\"hello\"", None), // should fail
1202             ("5", None),         // should fail
1203             ("true", None),      // should fail
1204         ] {
1205             let toml = format!(
1206                 r#"
1207                     [codegen]
1208                     collector = {collector_value}
1209                 "#,
1210             );
1211             let parsed_collector = toml::from_str::<CommonOptions>(&toml)
1212                 .ok()
1213                 .and_then(|common_options| common_options.codegen.collector);
1214             assert_eq!(
1215                 parsed_collector, expected,
1216                 "Mismatch for input '{collector_value}'. Parsed: {parsed_collector:?}, Expected: {expected:?}",
1217             );
1218         }
1219     }
1220 }
1221 
1222 impl Default for CommonOptions {
1223     fn default() -> CommonOptions {
1224         CommonOptions::new()
1225     }
1226 }
1227 
1228 impl fmt::Display for CommonOptions {
1229     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1230         let CommonOptions {
1231             codegen_raw,
1232             codegen,
1233             debug_raw,
1234             debug,
1235             opts_raw,
1236             opts,
1237             wasm_raw,
1238             wasm,
1239             wasi_raw,
1240             wasi,
1241             configured,
1242             target,
1243             config,
1244         } = self;
1245         if let Some(target) = target {
1246             write!(f, "--target {target} ")?;
1247         }
1248         if let Some(config) = config {
1249             write!(f, "--config {} ", config.display())?;
1250         }
1251 
1252         let codegen_flags;
1253         let opts_flags;
1254         let wasi_flags;
1255         let wasm_flags;
1256         let debug_flags;
1257 
1258         if *configured {
1259             codegen_flags = codegen.to_options();
1260             debug_flags = debug.to_options();
1261             wasi_flags = wasi.to_options();
1262             wasm_flags = wasm.to_options();
1263             opts_flags = opts.to_options();
1264         } else {
1265             codegen_flags = codegen_raw
1266                 .iter()
1267                 .flat_map(|t| t.0.iter())
1268                 .cloned()
1269                 .collect();
1270             debug_flags = debug_raw.iter().flat_map(|t| t.0.iter()).cloned().collect();
1271             wasi_flags = wasi_raw.iter().flat_map(|t| t.0.iter()).cloned().collect();
1272             wasm_flags = wasm_raw.iter().flat_map(|t| t.0.iter()).cloned().collect();
1273             opts_flags = opts_raw.iter().flat_map(|t| t.0.iter()).cloned().collect();
1274         }
1275 
1276         for flag in codegen_flags {
1277             write!(f, "-C{flag} ")?;
1278         }
1279         for flag in opts_flags {
1280             write!(f, "-O{flag} ")?;
1281         }
1282         for flag in wasi_flags {
1283             write!(f, "-S{flag} ")?;
1284         }
1285         for flag in wasm_flags {
1286             write!(f, "-W{flag} ")?;
1287         }
1288         for flag in debug_flags {
1289             write!(f, "-D{flag} ")?;
1290         }
1291 
1292         Ok(())
1293     }
1294 }
1295