Skip to content

Commit a11be0a

Browse files
committed
Backport build.rs fixes to 2.x.
1 parent e0af518 commit a11be0a

File tree

4 files changed

+162
-27
lines changed

4 files changed

+162
-27
lines changed

build.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ fn main() {
1717
// https://doc.rust-lang.org/unstable-book/library-features/windows-file-type-ext.html
1818
use_feature_or_nothing("windows_file_type_ext");
1919

20+
// Cfgs that users may set.
21+
println!("cargo:rustc-check-cfg=cfg(racy_asserts)");
22+
2023
// Don't rerun this on changes other than build.rs, as we only depend on
2124
// the rustc version.
2225
println!("cargo:rerun-if-changed=build.rs");
@@ -26,6 +29,7 @@ fn use_feature_or_nothing(feature: &str) {
2629
if has_feature(feature) {
2730
use_feature(feature);
2831
}
32+
println!("cargo:rustc-check-cfg=cfg({})", feature);
2933
}
3034

3135
fn use_feature(feature: &str) {
@@ -34,7 +38,7 @@ fn use_feature(feature: &str) {
3438

3539
/// Test whether the rustc at `var("RUSTC")` supports the given feature.
3640
fn has_feature(feature: &str) -> bool {
37-
can_compile(&format!(
41+
can_compile(format!(
3842
"#![allow(stable_features)]\n#![feature({})]",
3943
feature
4044
))
@@ -44,12 +48,11 @@ fn has_feature(feature: &str) -> bool {
4448
fn can_compile<T: AsRef<str>>(test: T) -> bool {
4549
use std::process::Stdio;
4650

47-
let out_dir = var("OUT_DIR").unwrap();
4851
let rustc = var("RUSTC").unwrap();
4952
let target = var("TARGET").unwrap();
5053

51-
// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
52-
// as documented [here].
54+
// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string, as
55+
// documented [here].
5356
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
5457
let wrapper = var("RUSTC_WRAPPER")
5558
.ok()
@@ -68,8 +71,9 @@ fn can_compile<T: AsRef<str>>(test: T) -> bool {
6871
.arg("--emit=metadata") // Do as little as possible but still parse.
6972
.arg("--target")
7073
.arg(target)
71-
.arg("--out-dir")
72-
.arg(out_dir); // Put the output somewhere inconsequential.
74+
.arg("-o")
75+
.arg("-")
76+
.stdout(Stdio::null()); // We don't care about the output (only whether it builds or not)
7377

7478
// If Cargo wants to set RUSTFLAGS, use that.
7579
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {

cap-fs-ext/build.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn use_feature_or_nothing(feature: &str) {
1313
if has_feature(feature) {
1414
use_feature(feature);
1515
}
16+
println!("cargo:rustc-check-cfg=cfg({})", feature);
1617
}
1718

1819
fn use_feature(feature: &str) {
@@ -21,20 +22,60 @@ fn use_feature(feature: &str) {
2122

2223
/// Test whether the rustc at `var("RUSTC")` supports the given feature.
2324
fn has_feature(feature: &str) -> bool {
24-
let out_dir = var("OUT_DIR").unwrap();
25+
can_compile(&format!(
26+
"#![allow(stable_features)]\n#![feature({})]",
27+
feature
28+
))
29+
}
30+
31+
/// Test whether the rustc at `var("RUSTC")` can compile the given code.
32+
fn can_compile<T: AsRef<str>>(test: T) -> bool {
33+
use std::process::Stdio;
34+
2535
let rustc = var("RUSTC").unwrap();
36+
let target = var("TARGET").unwrap();
37+
38+
// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
39+
// as documented [here].
40+
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
41+
let wrapper = var("RUSTC_WRAPPER")
42+
.ok()
43+
.and_then(|w| if w.is_empty() { None } else { Some(w) });
2644

27-
let mut child = std::process::Command::new(rustc)
28-
.arg("--crate-type=rlib") // Don't require `main`.
45+
let mut cmd = if let Some(wrapper) = wrapper {
46+
let mut cmd = std::process::Command::new(wrapper);
47+
// The wrapper's first argument is supposed to be the path to rustc.
48+
cmd.arg(rustc);
49+
cmd
50+
} else {
51+
std::process::Command::new(rustc)
52+
};
53+
54+
cmd.arg("--crate-type=rlib") // Don't require `main`.
2955
.arg("--emit=metadata") // Do as little as possible but still parse.
30-
.arg("--out-dir")
31-
.arg(out_dir) // Put the output somewhere inconsequential.
56+
.arg("--target")
57+
.arg(target)
58+
.arg("-o")
59+
.arg("-")
60+
.stdout(Stdio::null()); // We don't care about the output (only whether it builds or not)
61+
62+
// If Cargo wants to set RUSTFLAGS, use that.
63+
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
64+
if !rustflags.is_empty() {
65+
for arg in rustflags.split('\x1f') {
66+
cmd.arg(arg);
67+
}
68+
}
69+
}
70+
71+
let mut child = cmd
3272
.arg("-") // Read from stdin.
33-
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
73+
.stdin(Stdio::piped()) // Stdin is a pipe.
74+
.stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
3475
.spawn()
3576
.unwrap();
3677

37-
writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
78+
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();
3879

3980
child.wait().unwrap().success()
4081
}

cap-primitives/build.rs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ fn main() {
88
use_feature_or_nothing("io_error_more"); // https://github.com/rust-lang/rust/issues/86442
99
use_feature_or_nothing("io_error_uncategorized");
1010

11+
// Cfgs that users may set.
12+
println!("cargo:rustc-check-cfg=cfg(racy_asserts)");
13+
println!("cargo:rustc-check-cfg=cfg(emulate_second_only_system)");
14+
println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)");
15+
1116
// Don't rerun this on changes other than build.rs, as we only depend on
1217
// the rustc version.
1318
println!("cargo:rerun-if-changed=build.rs");
@@ -17,6 +22,7 @@ fn use_feature_or_nothing(feature: &str) {
1722
if has_feature(feature) {
1823
use_feature(feature);
1924
}
25+
println!("cargo:rustc-check-cfg=cfg({})", feature);
2026
}
2127

2228
fn use_feature(feature: &str) {
@@ -25,20 +31,60 @@ fn use_feature(feature: &str) {
2531

2632
/// Test whether the rustc at `var("RUSTC")` supports the given feature.
2733
fn has_feature(feature: &str) -> bool {
28-
let out_dir = var("OUT_DIR").unwrap();
34+
can_compile(&format!(
35+
"#![allow(stable_features)]\n#![feature({})]",
36+
feature
37+
))
38+
}
39+
40+
/// Test whether the rustc at `var("RUSTC")` can compile the given code.
41+
fn can_compile<T: AsRef<str>>(test: T) -> bool {
42+
use std::process::Stdio;
43+
2944
let rustc = var("RUSTC").unwrap();
45+
let target = var("TARGET").unwrap();
46+
47+
// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
48+
// as documented [here].
49+
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
50+
let wrapper = var("RUSTC_WRAPPER")
51+
.ok()
52+
.and_then(|w| if w.is_empty() { None } else { Some(w) });
3053

31-
let mut child = std::process::Command::new(rustc)
32-
.arg("--crate-type=rlib") // Don't require `main`.
54+
let mut cmd = if let Some(wrapper) = wrapper {
55+
let mut cmd = std::process::Command::new(wrapper);
56+
// The wrapper's first argument is supposed to be the path to rustc.
57+
cmd.arg(rustc);
58+
cmd
59+
} else {
60+
std::process::Command::new(rustc)
61+
};
62+
63+
cmd.arg("--crate-type=rlib") // Don't require `main`.
3364
.arg("--emit=metadata") // Do as little as possible but still parse.
34-
.arg("--out-dir")
35-
.arg(out_dir) // Put the output somewhere inconsequential.
65+
.arg("--target")
66+
.arg(target)
67+
.arg("-o")
68+
.arg("-")
69+
.stdout(Stdio::null()); // We don't care about the output (only whether it builds or not)
70+
71+
// If Cargo wants to set RUSTFLAGS, use that.
72+
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
73+
if !rustflags.is_empty() {
74+
for arg in rustflags.split('\x1f') {
75+
cmd.arg(arg);
76+
}
77+
}
78+
}
79+
80+
let mut child = cmd
3681
.arg("-") // Read from stdin.
37-
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
82+
.stdin(Stdio::piped()) // Stdin is a pipe.
83+
.stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
3884
.spawn()
3985
.unwrap();
4086

41-
writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
87+
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();
4288

4389
child.wait().unwrap().success()
4490
}

cap-std/build.rs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ fn main() {
55
use_feature_or_nothing("can_vector"); // https://github.com/rust-lang/rust/issues/69941
66
use_feature_or_nothing("write_all_vectored"); // https://github.com/rust-lang/rust/issues/70436
77

8+
// Cfgs that users may set.
9+
println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)");
10+
811
// Don't rerun this on changes other than build.rs, as we only depend on
912
// the rustc version.
1013
println!("cargo:rerun-if-changed=build.rs");
@@ -14,6 +17,7 @@ fn use_feature_or_nothing(feature: &str) {
1417
if has_feature(feature) {
1518
use_feature(feature);
1619
}
20+
println!("cargo:rustc-check-cfg=cfg({})", feature);
1721
}
1822

1923
fn use_feature(feature: &str) {
@@ -22,20 +26,60 @@ fn use_feature(feature: &str) {
2226

2327
/// Test whether the rustc at `var("RUSTC")` supports the given feature.
2428
fn has_feature(feature: &str) -> bool {
25-
let out_dir = var("OUT_DIR").unwrap();
29+
can_compile(&format!(
30+
"#![allow(stable_features)]\n#![feature({})]",
31+
feature
32+
))
33+
}
34+
35+
/// Test whether the rustc at `var("RUSTC")` can compile the given code.
36+
fn can_compile<T: AsRef<str>>(test: T) -> bool {
37+
use std::process::Stdio;
38+
2639
let rustc = var("RUSTC").unwrap();
40+
let target = var("TARGET").unwrap();
41+
42+
// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
43+
// as documented [here].
44+
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
45+
let wrapper = var("RUSTC_WRAPPER")
46+
.ok()
47+
.and_then(|w| if w.is_empty() { None } else { Some(w) });
2748

28-
let mut child = std::process::Command::new(rustc)
29-
.arg("--crate-type=rlib") // Don't require `main`.
49+
let mut cmd = if let Some(wrapper) = wrapper {
50+
let mut cmd = std::process::Command::new(wrapper);
51+
// The wrapper's first argument is supposed to be the path to rustc.
52+
cmd.arg(rustc);
53+
cmd
54+
} else {
55+
std::process::Command::new(rustc)
56+
};
57+
58+
cmd.arg("--crate-type=rlib") // Don't require `main`.
3059
.arg("--emit=metadata") // Do as little as possible but still parse.
31-
.arg("--out-dir")
32-
.arg(out_dir) // Put the output somewhere inconsequential.
60+
.arg("--target")
61+
.arg(target)
62+
.arg("-o")
63+
.arg("-")
64+
.stdout(Stdio::null()); // We don't care about the output (only whether it builds or not)
65+
66+
// If Cargo wants to set RUSTFLAGS, use that.
67+
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
68+
if !rustflags.is_empty() {
69+
for arg in rustflags.split('\x1f') {
70+
cmd.arg(arg);
71+
}
72+
}
73+
}
74+
75+
let mut child = cmd
3376
.arg("-") // Read from stdin.
34-
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
77+
.stdin(Stdio::piped()) // Stdin is a pipe.
78+
.stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
3579
.spawn()
3680
.unwrap();
3781

38-
writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
82+
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();
3983

4084
child.wait().unwrap().success()
4185
}

0 commit comments

Comments
 (0)