run_make_support/wasm.rs
1//! Wasm-specific helpers for various tests
2
3use wasmparser::{Parser, Payload};
4
5use crate::{Rustc, target};
6
7/// Returns whether the current compilation target will emit a WebAssembly
8/// component.
9pub fn target_emits_components() -> bool {
10 let target = target();
11 if target.contains("wasi") {
12 return !target.contains("wasip1");
13 }
14 if target.contains("component") {
15 return true;
16 }
17 return false;
18}
19
20/// Helper methods for the `Rustc` type.
21impl Rustc {
22 /// Helper to emit a core module for this wasm target, skipping the
23 /// component output part of compilation.
24 pub fn emit_wasm_core_module(&mut self) -> &mut Self {
25 if target_emits_components() {
26 self.arg("-Clink-arg=--skip-wit-component");
27 }
28 self
29 }
30}
31
32/// Returns the size, in bytes, of the wasm module in `bytes`.
33///
34/// This excludes custom sections to avoid counting easily strippable sections
35/// such as debuginfo.
36pub fn wasm_binary_size(bytes: &[u8]) -> usize {
37 let mut size = 0;
38 for payload in Parser::new(0).parse_all(bytes) {
39 let payload = payload.unwrap();
40 match payload {
41 // Don't count the size of custom sections as they're easily
42 // stripped, and don't double-count modules since this'll recurse
43 // into module.
44 Payload::CustomSection { .. } | Payload::ModuleSection { .. } => continue,
45 _ => {}
46 }
47 if let Some((_, range)) = payload.as_section() {
48 size += range.len();
49 }
50 }
51 size
52}