File: rmake.rs

package info (click to toggle)
rustc 1.89.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 906,624 kB
  • sloc: xml: 158,148; python: 34,888; javascript: 19,595; sh: 19,221; ansic: 13,046; cpp: 7,144; asm: 4,376; makefile: 692; lisp: 174; sql: 15
file content (105 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (15)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! This checks the output of `--print=cfg`
//!
//! Specifically it checks that output is correctly formatted
//! (ie. no duplicated cfgs, values are between "", names are not).
//!
//! It also checks that some targets have the correct set cfgs.

// ignore-tidy-linelength
//@ needs-llvm-components: arm x86
// Note: without the needs-llvm-components it will fail on LLVM built without the required
// components listed above.

use std::collections::HashSet;
use std::iter::FromIterator;
use std::path::PathBuf;

use run_make_support::{rfs, rustc};

struct PrintCfg {
    target: &'static str,
    includes: &'static [&'static str],
    disallow: &'static [&'static str],
}

fn main() {
    check(PrintCfg {
        target: "x86_64-pc-windows-gnu",
        includes: &["windows", "target_arch=\"x86_64\""],
        disallow: &["unix"],
    });
    check(PrintCfg {
        target: "i686-pc-windows-msvc",
        includes: &["windows", "target_env=\"msvc\""],
        disallow: &["unix"],
    });
    check(PrintCfg {
        target: "i686-apple-darwin",
        includes: &["unix", "target_os=\"macos\"", "target_vendor=\"apple\""],
        disallow: &["windows"],
    });
    check(PrintCfg {
        target: "i686-unknown-linux-gnu",
        includes: &["unix", "target_env=\"gnu\""],
        disallow: &["windows"],
    });
    check(PrintCfg {
        target: "arm-unknown-linux-gnueabihf",
        includes: &["unix", "target_abi=\"eabihf\""],
        disallow: &["windows"],
    });
}

fn check(PrintCfg { target, includes, disallow }: PrintCfg) {
    fn check_(output: &str, includes: &[&str], disallow: &[&str]) {
        let mut found = HashSet::<String>::new();
        let mut recorded = HashSet::<String>::new();

        for l in output.lines() {
            assert!(l == l.trim());
            if let Some((left, right)) = l.split_once('=') {
                assert!(right.starts_with("\""));
                assert!(right.ends_with("\""));
                assert!(!left.contains("\""));
            } else {
                assert!(!l.contains("\""));
            }

            assert!(recorded.insert(l.to_string()), "duplicated: {}", &l);
            assert!(!disallow.contains(&l), "found disallowed: {}", &l);
            if includes.contains(&l) {
                assert!(found.insert(l.to_string()), "duplicated (includes): {}", &l);
            }
        }

        let should_found = HashSet::<String>::from_iter(includes.iter().map(|s| s.to_string()));
        let diff: Vec<_> = should_found.difference(&found).collect();

        assert!(
            diff.is_empty(),
            "expected: {:?}, found: {:?} (~ {:?})",
            &should_found,
            &found,
            &diff
        );
    }

    // --print=cfg
    {
        let output = rustc().target(target).print("cfg").run();
        let stdout = output.stdout_utf8();

        check_(&stdout, includes, disallow);
    }

    // --print=cfg=PATH
    {
        let tmp_path = PathBuf::from(format!("{target}.cfg"));

        rustc().target(target).print(&format!("cfg={}", tmp_path.display())).run();

        let output = rfs::read_to_string(&tmp_path);

        check_(&output, includes, disallow);
    }
}