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