Skip to content

Commit 47eefac

Browse files
committed
Port print-cfg run-make to Rust-based rmake.rs
1 parent 91d5e4a commit 47eefac

File tree

4 files changed

+166
-38
lines changed

4 files changed

+166
-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

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
fn main() {
18+
check(
19+
/*target*/ "x86_64-pc-windows-gnu",
20+
/*includes*/ &["windows", "target_arch=\"x86_64\""],
21+
/*disallow*/ &["unix"],
22+
);
23+
check(
24+
/*target*/ "i686-pc-windows-msvc",
25+
/*includes*/ &["windows", "target_env=\"msvc\""],
26+
/*disallow*/ &["unix"],
27+
);
28+
check(
29+
/*target*/ "i686-apple-darwin",
30+
/*includes*/ &["unix", "target_os=\"macos\"", "target_vendor=\"apple\""],
31+
/*disallow*/ &["windows"],
32+
);
33+
check(
34+
/*target*/ "i686-unknown-linux-gnu",
35+
/*includes*/ &["unix", "target_env=\"gnu\""],
36+
/*disallow*/ &["windows"],
37+
);
38+
check(
39+
/*target*/ "arm-unknown-linux-gnueabihf",
40+
/*includes*/ &["unix", "target_abi=\"eabihf\""],
41+
/*disallow*/ &["windows"],
42+
);
43+
}
44+
45+
fn check(target: &str, includes: &[&str], disallow: &[&str]) {
46+
fn _inner(output: &str, includes: &[&str], disallow: &[&str]) {
47+
let mut found = HashSet::<String>::new();
48+
let mut recorded = HashSet::<String>::new();
49+
50+
for l in output.lines() {
51+
assert!(l == l.trim());
52+
if let Some((left, right)) = l.split_once('=') {
53+
assert!(right.starts_with("\""));
54+
assert!(right.ends_with("\""));
55+
assert!(!left.contains("\""));
56+
} else {
57+
assert!(!l.contains("\""));
58+
}
59+
60+
assert!(recorded.insert(l.to_string()), "duplicated: {}", &l);
61+
assert!(!disallow.contains(&l), "found disallowed: {}", &l);
62+
if includes.contains(&l) {
63+
assert!(found.insert(l.to_string()), "duplicated (includes): {}", &l);
64+
}
65+
}
66+
67+
let should_found = HashSet::<String>::from_iter(includes.iter().map(|s| s.to_string()));
68+
let diff: Vec<_> = should_found.difference(&found).collect();
69+
70+
assert!(
71+
diff.is_empty(),
72+
"expected: {:?}, found: {:?} (~ {:?})",
73+
&should_found,
74+
&found,
75+
&diff
76+
);
77+
}
78+
79+
// --print=cfg
80+
{
81+
let output = rustc().target(target).print("cfg").run();
82+
83+
let stdout = String::from_utf8(output.stdout).unwrap();
84+
85+
_inner(&stdout, includes, disallow);
86+
}
87+
88+
// --print=cfg=PATH
89+
{
90+
let tmp_path = tmp_dir().join(format!("{target}.cfg"));
91+
let mut print_arg = OsString::from("--print=cfg=");
92+
print_arg.push(tmp_path.as_os_str());
93+
94+
let output = rustc().target(target).arg(print_arg).run();
95+
96+
let output = std::fs::read_to_string(&tmp_path).unwrap();
97+
98+
_inner(&output, includes, disallow);
99+
}
100+
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
fn main() {
11+
// Printed from CodegenBackend trait impl in rustc_codegen_llvm/src/lib.rs
12+
check(
13+
/*target*/ &target(),
14+
/*option*/ "relocation-models",
15+
/*includes*/ &["dynamic-no-pic"],
16+
);
17+
18+
// Printed by compiler/rustc_codegen_llvm/src/llvm_util.rs
19+
check(
20+
/*target*/ "wasm32-unknown-unknown",
21+
/*option*/ "target-features",
22+
/*includes*/ &["reference-types"],
23+
);
24+
25+
// Printed by C++ code in rustc_llvm/llvm-wrapper/PassWrapper.cpp
26+
check(
27+
/*target*/ "wasm32-unknown-unknown",
28+
/*option*/ "target-cpus",
29+
/*includes*/ &["generic"],
30+
);
31+
}
32+
33+
fn check(target: &str, option: &str, includes: &[&str]) {
34+
fn _inner(output: &str, includes: &[&str]) {
35+
for i in includes {
36+
assert!(output.contains(i), "output doesn't contains: {}", i);
37+
}
38+
}
39+
40+
let stdout: String;
41+
let output: String;
42+
43+
// --print={option}
44+
{
45+
let output = rustc().target(target).print(option).run();
46+
47+
stdout = String::from_utf8(output.stdout).unwrap();
48+
49+
_inner(&stdout, includes);
50+
}
51+
52+
// --print={option}=PATH
53+
{
54+
let tmp_path = tmp_dir().join(format!("{option}.txt"));
55+
let mut print_arg = OsString::from(format!("--print={option}="));
56+
print_arg.push(tmp_path.as_os_str());
57+
58+
let _output = rustc().target(target).arg(print_arg).run();
59+
60+
output = std::fs::read_to_string(&tmp_path).unwrap();
61+
62+
_inner(&output, includes);
63+
}
64+
65+
assert_eq!(&stdout, &output);
66+
}

0 commit comments

Comments
 (0)