Skip to content

Commit 9cb3789

Browse files
committed
Sync build.rs with rustix
Sync cap-std's build.rs with the changes in rustix in bytecodealliance/rustix#511
1 parent 4f009bd commit 9cb3789

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

build.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,53 @@ fn use_feature(feature: &str) {
3636

3737
/// Test whether the rustc at `var("RUSTC")` supports the given feature.
3838
fn has_feature(feature: &str) -> bool {
39+
can_compile(&format!(
40+
"#![allow(stable_features)]\n#![feature({})]",
41+
feature
42+
))
43+
}
44+
45+
/// Test whether the rustc at `var("RUSTC")` can compile the given code.
46+
fn can_compile<T: AsRef<str>>(test: T) -> bool {
47+
use std::process::Stdio;
48+
3949
let out_dir = var("OUT_DIR").unwrap();
4050
let rustc = var("RUSTC").unwrap();
51+
let target = var("TARGET").unwrap();
52+
53+
let mut cmd = if let Ok(wrapper) = var("CARGO_RUSTC_WRAPPER") {
54+
let mut cmd = std::process::Command::new(wrapper);
55+
// The wrapper's first argument is supposed to be the path to rustc.
56+
cmd.arg(rustc);
57+
cmd
58+
} else {
59+
std::process::Command::new(rustc)
60+
};
4161

42-
let mut child = std::process::Command::new(rustc)
43-
.arg("--crate-type=rlib") // Don't require `main`.
62+
cmd.arg("--crate-type=rlib") // Don't require `main`.
4463
.arg("--emit=metadata") // Do as little as possible but still parse.
64+
.arg("--target")
65+
.arg(target)
4566
.arg("--out-dir")
46-
.arg(out_dir) // Put the output somewhere inconsequential.
67+
.arg(out_dir); // Put the output somewhere inconsequential.
68+
69+
// If Cargo wants to set RUSTFLAGS, use that.
70+
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
71+
if !rustflags.is_empty() {
72+
for arg in rustflags.split('\x1f') {
73+
cmd.arg(arg);
74+
}
75+
}
76+
}
77+
78+
let mut child = cmd
4779
.arg("-") // Read from stdin.
48-
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
80+
.stdin(Stdio::piped()) // Stdin is a pipe.
81+
.stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
4982
.spawn()
5083
.unwrap();
5184

52-
writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
85+
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();
5386

5487
child.wait().unwrap().success()
5588
}

0 commit comments

Comments
 (0)