Skip to content

Commit 6edb721

Browse files
authored
Rollup merge of #124473 - Urgau:port-print-cfg, r=jieyouxu
Port `print-cfg` run-make test to Rust-based rmake.rs This PR port the `print-cfg` run-make test to Rust-based rmake.rs tests. The actual test is now split in two: - the first part for the `--print=cfg` part - and the second part for the `=PATH` part of `--print` Part of #121876. r? `@jieyouxu`
2 parents 1b567de + 006c94c commit 6edb721

File tree

4 files changed

+174
-38
lines changed

4 files changed

+174
-38
lines changed

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ run-make/prefer-rlib/Makefile
219219
run-make/pretty-print-to-file/Makefile
220220
run-make/pretty-print-with-dep-file/Makefile
221221
run-make/print-calling-conventions/Makefile
222-
run-make/print-cfg/Makefile
223222
run-make/print-target-list/Makefile
224223
run-make/profile/Makefile
225224
run-make/prune-link-args/Makefile

tests/run-make/print-cfg/Makefile

-37
This file was deleted.

tests/run-make/print-cfg/rmake.rs

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//! This checks the output of `--print=cfg`
2+
//!
3+
//! Specifically it checks that output is correctly formatted
4+
//! (ie. no duplicated cfgs, values are between "", names are not).
5+
//!
6+
//! It also checks that some targets have the correct set cfgs.
7+
8+
extern crate run_make_support;
9+
10+
use std::collections::HashSet;
11+
use std::ffi::OsString;
12+
use std::io::BufRead;
13+
use std::iter::FromIterator;
14+
15+
use run_make_support::{rustc, tmp_dir};
16+
17+
struct PrintCfg {
18+
target: &'static str,
19+
includes: &'static [&'static str],
20+
disallow: &'static [&'static str],
21+
}
22+
23+
fn main() {
24+
check(PrintCfg {
25+
target: "x86_64-pc-windows-gnu",
26+
includes: &["windows", "target_arch=\"x86_64\""],
27+
disallow: &["unix"],
28+
});
29+
check(PrintCfg {
30+
target: "i686-pc-windows-msvc",
31+
includes: &["windows", "target_env=\"msvc\""],
32+
disallow: &["unix"],
33+
});
34+
check(PrintCfg {
35+
target: "i686-apple-darwin",
36+
includes: &["unix", "target_os=\"macos\"", "target_vendor=\"apple\""],
37+
disallow: &["windows"],
38+
});
39+
check(PrintCfg {
40+
target: "i686-unknown-linux-gnu",
41+
includes: &["unix", "target_env=\"gnu\""],
42+
disallow: &["windows"],
43+
});
44+
check(PrintCfg {
45+
target: "arm-unknown-linux-gnueabihf",
46+
includes: &["unix", "target_abi=\"eabihf\""],
47+
disallow: &["windows"],
48+
});
49+
}
50+
51+
fn check(PrintCfg { target, includes, disallow }: PrintCfg) {
52+
fn check_(output: &str, includes: &[&str], disallow: &[&str]) {
53+
let mut found = HashSet::<String>::new();
54+
let mut recorded = HashSet::<String>::new();
55+
56+
for l in output.lines() {
57+
assert!(l == l.trim());
58+
if let Some((left, right)) = l.split_once('=') {
59+
assert!(right.starts_with("\""));
60+
assert!(right.ends_with("\""));
61+
assert!(!left.contains("\""));
62+
} else {
63+
assert!(!l.contains("\""));
64+
}
65+
66+
assert!(recorded.insert(l.to_string()), "duplicated: {}", &l);
67+
assert!(!disallow.contains(&l), "found disallowed: {}", &l);
68+
if includes.contains(&l) {
69+
assert!(found.insert(l.to_string()), "duplicated (includes): {}", &l);
70+
}
71+
}
72+
73+
let should_found = HashSet::<String>::from_iter(includes.iter().map(|s| s.to_string()));
74+
let diff: Vec<_> = should_found.difference(&found).collect();
75+
76+
assert!(
77+
diff.is_empty(),
78+
"expected: {:?}, found: {:?} (~ {:?})",
79+
&should_found,
80+
&found,
81+
&diff
82+
);
83+
}
84+
85+
// --print=cfg
86+
{
87+
let output = rustc().target(target).print("cfg").run();
88+
89+
let stdout = String::from_utf8(output.stdout).unwrap();
90+
91+
check_(&stdout, includes, disallow);
92+
}
93+
94+
// --print=cfg=PATH
95+
{
96+
let tmp_path = tmp_dir().join(format!("{target}.cfg"));
97+
let mut print_arg = OsString::from("--print=cfg=");
98+
print_arg.push(tmp_path.as_os_str());
99+
100+
let output = rustc().target(target).arg(print_arg).run();
101+
102+
let output = std::fs::read_to_string(&tmp_path).unwrap();
103+
104+
check_(&output, includes, disallow);
105+
}
106+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! This checks the output of some `--print` options when
2+
//! output to a file (instead of stdout)
3+
4+
extern crate run_make_support;
5+
6+
use std::ffi::OsString;
7+
8+
use run_make_support::{rustc, target, tmp_dir};
9+
10+
struct Option<'a> {
11+
target: &'a str,
12+
option: &'static str,
13+
includes: &'static [&'static str],
14+
}
15+
16+
fn main() {
17+
// Printed from CodegenBackend trait impl in rustc_codegen_llvm/src/lib.rs
18+
check(Option {
19+
target: &target(),
20+
option: "relocation-models",
21+
includes: &["dynamic-no-pic"],
22+
});
23+
24+
// Printed by compiler/rustc_codegen_llvm/src/llvm_util.rs
25+
check(Option {
26+
target: "wasm32-unknown-unknown",
27+
option: "target-features",
28+
includes: &["reference-types"],
29+
});
30+
31+
// Printed by C++ code in rustc_llvm/llvm-wrapper/PassWrapper.cpp
32+
check(Option {
33+
target: "wasm32-unknown-unknown",
34+
option: "target-cpus",
35+
includes: &["generic"],
36+
});
37+
}
38+
39+
fn check(args: Option) {
40+
fn check_(output: &str, includes: &[&str]) {
41+
for i in includes {
42+
assert!(output.contains(i), "output doesn't contains: {}", i);
43+
}
44+
}
45+
46+
// --print={option}
47+
let stdout = {
48+
let output = rustc().target(args.target).print(args.option).run();
49+
50+
String::from_utf8(output.stdout).unwrap()
51+
};
52+
53+
// --print={option}=PATH
54+
let output = {
55+
let tmp_path = tmp_dir().join(format!("{}.txt", args.option));
56+
let mut print_arg = OsString::from(format!("--print={}=", args.option));
57+
print_arg.push(tmp_path.as_os_str());
58+
59+
let _output = rustc().target(args.target).arg(print_arg).run();
60+
61+
std::fs::read_to_string(&tmp_path).unwrap()
62+
};
63+
64+
check_(&stdout, args.includes);
65+
check_(&output, args.includes);
66+
67+
assert_eq!(&stdout, &output);
68+
}

0 commit comments

Comments
 (0)