File: rmake.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,245,360 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (52 lines) | stat: -rw-r--r-- 1,750 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
extern crate run_make_support;

use run_make_support::{out_dir, rustc, wasmparser};
use std::collections::{HashMap, HashSet};

fn main() {
    if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
        return;
    }

    test_file("foo.rs", &[("a", &["foo"]), ("b", &["foo"])]);
    test_file("bar.rs", &[("m1", &["f", "g"]), ("m2", &["f"])]);
    test_file("baz.rs", &[("sqlite", &["allocate", "deallocate"])]);
    test_file("log.rs", &[("test", &["log"])]);
}

fn test_file(file: &str, expected_imports: &[(&str, &[&str])]) {
    test(file, &[], expected_imports);
    test(file, &["-Clto"], expected_imports);
    test(file, &["-O"], expected_imports);
    test(file, &["-Clto", "-O"], expected_imports);
}

fn test(file: &str, args: &[&str], expected_imports: &[(&str, &[&str])]) {
    println!("test {file:?} {args:?} for {expected_imports:?}");

    rustc().arg(file).arg("--target=wasm32-wasip1").args(args).run();

    let file = std::fs::read(&out_dir().join(file).with_extension("wasm")).unwrap();

    let mut imports = HashMap::new();
    for payload in wasmparser::Parser::new(0).parse_all(&file) {
        let payload = payload.unwrap();
        if let wasmparser::Payload::ImportSection(s) = payload {
            for i in s {
                let i = i.unwrap();
                imports.entry(i.module).or_insert(HashSet::new()).insert(i.name);
            }
        }
    }

    eprintln!("imports {imports:?}");

    for (expected_module, expected_names) in expected_imports {
        let names = imports.remove(expected_module).unwrap();
        assert_eq!(names.len(), expected_names.len());
        for name in *expected_names {
            assert!(names.contains(name));
        }
    }
    assert!(imports.is_empty());
}