Skip to content

Commit 2c0250f

Browse files
authored
Merge pull request #3910 from tgross35/simplify-wrapper
Simplify the RUSTC_WRAPPER check
2 parents 1b11393 + bdce2b2 commit 2c0250f

File tree

2 files changed

+376
-2
lines changed

2 files changed

+376
-2
lines changed

build-tmp.rs

+374
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
use std::env;
2+
use std::ffi::{OsStr, OsString};
3+
use std::process::{Command, Output};
4+
use std::str;
5+
6+
// List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we
7+
// need to know all the possible cfgs that this script will set. If you need to set another cfg
8+
// make sure to add it to this list as well.
9+
const ALLOWED_CFGS: &'static [&'static str] = &[
10+
"emscripten_new_stat_abi",
11+
"espidf_time64",
12+
"freebsd10",
13+
"freebsd11",
14+
"freebsd12",
15+
"freebsd13",
16+
"freebsd14",
17+
"freebsd15",
18+
"libc_align",
19+
"libc_cfg_target_vendor",
20+
"libc_const_extern_fn",
21+
"libc_const_extern_fn_unstable",
22+
"libc_const_size_of",
23+
"libc_core_cvoid",
24+
"libc_deny_warnings",
25+
"libc_int128",
26+
"libc_long_array",
27+
"libc_non_exhaustive",
28+
"libc_packedN",
29+
"libc_priv_mod_use",
30+
"libc_ptr_addr_of",
31+
"libc_thread_local",
32+
"libc_underscore_const_names",
33+
"libc_union",
34+
"libc_ctest",
35+
];
36+
37+
// Extra values to allow for check-cfg.
38+
const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
39+
("target_os", &["switch", "aix", "ohos", "hurd", "visionos"]),
40+
("target_env", &["illumos", "wasi", "aix", "ohos"]),
41+
(
42+
"target_arch",
43+
&["loongarch64", "mips32r6", "mips64r6", "csky"],
44+
),
45+
];
46+
47+
fn main() {
48+
// Avoid unnecessary re-building.
49+
println!("cargo:rerun-if-changed=build.rs");
50+
51+
let (rustc_minor_ver, is_nightly) = rustc_minor_nightly();
52+
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
53+
let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok();
54+
let const_extern_fn_cargo_feature = env::var("CARGO_FEATURE_CONST_EXTERN_FN").is_ok();
55+
let libc_ci = env::var("LIBC_CI").is_ok();
56+
let libc_check_cfg = env::var("LIBC_CHECK_CFG").is_ok() || rustc_minor_ver >= 80;
57+
58+
if env::var("CARGO_FEATURE_USE_STD").is_ok() {
59+
println!(
60+
"cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
61+
please consider using the `std` cargo feature instead\""
62+
);
63+
}
64+
65+
// The ABI of libc used by std is backward compatible with FreeBSD 12.
66+
// The ABI of libc from crates.io is backward compatible with FreeBSD 11.
67+
//
68+
// On CI, we detect the actual FreeBSD version and match its ABI exactly,
69+
// running tests to ensure that the ABI is correct.
70+
let which_freebsd = if libc_ci {
71+
which_freebsd().unwrap_or(11)
72+
} else if rustc_dep_of_std {
73+
12
74+
} else {
75+
11
76+
};
77+
match which_freebsd {
78+
x if x < 10 => panic!("FreeBSD older than 10 is not supported"),
79+
10 => set_cfg("freebsd10"),
80+
11 => set_cfg("freebsd11"),
81+
12 => set_cfg("freebsd12"),
82+
13 => set_cfg("freebsd13"),
83+
14 => set_cfg("freebsd14"),
84+
_ => set_cfg("freebsd15"),
85+
}
86+
87+
match emcc_version_code() {
88+
Some(v) if (v >= 30142) => set_cfg("emscripten_new_stat_abi"),
89+
// Non-Emscripten or version < 3.1.42.
90+
Some(_) | None => (),
91+
}
92+
93+
// On CI: deny all warnings
94+
if libc_ci {
95+
set_cfg("libc_deny_warnings");
96+
}
97+
98+
// Rust >= 1.15 supports private module use:
99+
if rustc_minor_ver >= 15 || rustc_dep_of_std {
100+
set_cfg("libc_priv_mod_use");
101+
}
102+
103+
// Rust >= 1.19 supports unions:
104+
if rustc_minor_ver >= 19 || rustc_dep_of_std {
105+
set_cfg("libc_union");
106+
}
107+
108+
// Rust >= 1.24 supports const mem::size_of:
109+
if rustc_minor_ver >= 24 || rustc_dep_of_std {
110+
set_cfg("libc_const_size_of");
111+
}
112+
113+
// Rust >= 1.25 supports repr(align):
114+
if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature {
115+
set_cfg("libc_align");
116+
}
117+
118+
// Rust >= 1.26 supports i128 and u128:
119+
if rustc_minor_ver >= 26 || rustc_dep_of_std {
120+
set_cfg("libc_int128");
121+
}
122+
123+
// Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it.
124+
// Otherwise, it defines an incompatible type to retaining
125+
// backwards-compatibility.
126+
if rustc_minor_ver >= 30 || rustc_dep_of_std {
127+
set_cfg("libc_core_cvoid");
128+
}
129+
130+
// Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor).
131+
if rustc_minor_ver >= 33 || rustc_dep_of_std {
132+
set_cfg("libc_packedN");
133+
set_cfg("libc_cfg_target_vendor");
134+
}
135+
136+
// Rust >= 1.40 supports #[non_exhaustive].
137+
if rustc_minor_ver >= 40 || rustc_dep_of_std {
138+
set_cfg("libc_non_exhaustive");
139+
}
140+
141+
// Rust >= 1.47 supports long array:
142+
if rustc_minor_ver >= 47 || rustc_dep_of_std {
143+
set_cfg("libc_long_array");
144+
}
145+
146+
if rustc_minor_ver >= 51 || rustc_dep_of_std {
147+
set_cfg("libc_ptr_addr_of");
148+
}
149+
150+
// Rust >= 1.37.0 allows underscores as anonymous constant names.
151+
if rustc_minor_ver >= 37 || rustc_dep_of_std {
152+
set_cfg("libc_underscore_const_names");
153+
}
154+
155+
// #[thread_local] is currently unstable
156+
if rustc_dep_of_std {
157+
set_cfg("libc_thread_local");
158+
}
159+
160+
// Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C".
161+
if rustc_minor_ver >= 62 {
162+
set_cfg("libc_const_extern_fn");
163+
} else {
164+
// Rust < 1.62.0 requires a crate feature and feature gate.
165+
if const_extern_fn_cargo_feature {
166+
if !is_nightly || rustc_minor_ver < 40 {
167+
panic!("const-extern-fn requires a nightly compiler >= 1.40");
168+
}
169+
set_cfg("libc_const_extern_fn_unstable");
170+
set_cfg("libc_const_extern_fn");
171+
}
172+
}
173+
174+
// check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the
175+
// codebase. libc can configure it if the appropriate environment variable is passed. Since
176+
// rust-lang/rust enforces it, this is useful when using a custom libc fork there.
177+
//
178+
// https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
179+
if libc_check_cfg {
180+
for cfg in ALLOWED_CFGS {
181+
if rustc_minor_ver >= 75 {
182+
println!("cargo:rustc-check-cfg=cfg({})", cfg);
183+
} else {
184+
println!("cargo:rustc-check-cfg=values({})", cfg);
185+
}
186+
}
187+
for &(name, values) in CHECK_CFG_EXTRA {
188+
let values = values.join("\",\"");
189+
if rustc_minor_ver >= 75 {
190+
println!("cargo:rustc-check-cfg=cfg({},values(\"{}\"))", name, values);
191+
} else {
192+
println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values);
193+
}
194+
}
195+
}
196+
}
197+
198+
fn rustc_version_cmd(is_clippy_driver: bool) -> Output {
199+
let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty());
200+
let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env");
201+
202+
let mut cmd = if let Some(wrapper) = rustc_wrapper {
203+
let mut cmd = Command::new(wrapper);
204+
cmd.arg(rustc);
205+
if is_clippy_driver {
206+
cmd.arg("--rustc");
207+
}
208+
209+
cmd
210+
} else {
211+
Command::new(rustc)
212+
};
213+
214+
cmd.arg("--version");
215+
216+
let output = cmd.output().expect("Failed to get rustc version");
217+
218+
if !output.status.success() {
219+
panic!(
220+
"failed to run rustc: {}",
221+
String::from_utf8_lossy(output.stderr.as_slice())
222+
);
223+
}
224+
225+
output
226+
}
227+
228+
fn rustc_minor_nightly() -> (u32, bool) {
229+
macro_rules! otry {
230+
($e:expr) => {
231+
match $e {
232+
Some(e) => e,
233+
None => panic!("Failed to get rustc version"),
234+
}
235+
};
236+
}
237+
238+
<<<<<<< HEAD
239+
let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env");
240+
let mut cmd = match env::var_os("RUSTC_WRAPPER").as_ref() {
241+
Some(wrapper) if !wrapper.is_empty() => {
242+
let mut cmd = Command::new(wrapper);
243+
cmd.arg(rustc);
244+
cmd
245+
}
246+
_ => Command::new(rustc),
247+
};
248+
||||||| parent of 18b8da967 (Handle rustc version output correctly when `clippy-driver` used)
249+
let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env");
250+
let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty()) {
251+
let mut cmd = Command::new(wrapper);
252+
cmd.arg(rustc);
253+
cmd
254+
} else {
255+
Command::new(rustc)
256+
};
257+
=======
258+
let mut output = rustc_version_cmd(false);
259+
>>>>>>> 18b8da967 (Handle rustc version output correctly when `clippy-driver` used)
260+
261+
<<<<<<< HEAD
262+
let output = cmd
263+
.arg("--version")
264+
.output()
265+
.ok()
266+
.expect("Failed to get rustc version");
267+
if !output.status.success() {
268+
panic!(
269+
"failed to run rustc: {}",
270+
String::from_utf8_lossy(output.stderr.as_slice())
271+
);
272+
||||||| parent of 18b8da967 (Handle rustc version output correctly when `clippy-driver` used)
273+
let output = cmd
274+
.arg("--version")
275+
.output()
276+
.expect("Failed to get rustc version");
277+
if !output.status.success() {
278+
panic!(
279+
"failed to run rustc: {}",
280+
String::from_utf8_lossy(output.stderr.as_slice())
281+
);
282+
=======
283+
if otry!(str::from_utf8(&output.stdout).ok()).starts_with("clippy") {
284+
output = rustc_version_cmd(true);
285+
>>>>>>> 18b8da967 (Handle rustc version output correctly when `clippy-driver` used)
286+
}
287+
288+
let version = otry!(str::from_utf8(&output.stdout).ok());
289+
290+
let mut pieces = version.split('.');
291+
292+
if pieces.next() != Some("rustc 1") {
293+
panic!("Failed to get rustc version");
294+
}
295+
296+
let minor = pieces.next();
297+
298+
// If `rustc` was built from a tarball, its version string
299+
// will have neither a git hash nor a commit date
300+
// (e.g. "rustc 1.39.0"). Treat this case as non-nightly,
301+
// since a nightly build should either come from CI
302+
// or a git checkout
303+
let nightly_raw = otry!(pieces.next()).split('-').nth(1);
304+
let nightly = nightly_raw
305+
.map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
306+
.unwrap_or(false);
307+
let minor = otry!(otry!(minor).parse().ok());
308+
309+
(minor, nightly)
310+
}
311+
312+
fn which_freebsd() -> Option<i32> {
313+
let output = std::process::Command::new("freebsd-version").output().ok();
314+
if output.is_none() {
315+
return None;
316+
}
317+
let output = output.unwrap();
318+
if !output.status.success() {
319+
return None;
320+
}
321+
322+
let stdout = String::from_utf8(output.stdout).ok();
323+
if stdout.is_none() {
324+
return None;
325+
}
326+
let stdout = stdout.unwrap();
327+
328+
match &stdout {
329+
s if s.starts_with("10") => Some(10),
330+
s if s.starts_with("11") => Some(11),
331+
s if s.starts_with("12") => Some(12),
332+
s if s.starts_with("13") => Some(13),
333+
s if s.starts_with("14") => Some(14),
334+
s if s.starts_with("15") => Some(15),
335+
_ => None,
336+
}
337+
}
338+
339+
fn emcc_version_code() -> Option<u64> {
340+
let output = std::process::Command::new("emcc")
341+
.arg("-dumpversion")
342+
.output()
343+
.ok();
344+
if output.is_none() {
345+
return None;
346+
}
347+
let output = output.unwrap();
348+
if !output.status.success() {
349+
return None;
350+
}
351+
352+
let stdout = String::from_utf8(output.stdout).ok();
353+
if stdout.is_none() {
354+
return None;
355+
}
356+
let version = stdout.unwrap();
357+
358+
// Some Emscripten versions come with `-git` attached, so split the
359+
// version string also on the `-` char.
360+
let mut pieces = version.trim().split(|c| c == '.' || c == '-');
361+
362+
let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
363+
let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
364+
let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
365+
366+
Some(major * 10000 + minor * 100 + patch)
367+
}
368+
369+
fn set_cfg(cfg: &str) {
370+
if !ALLOWED_CFGS.contains(&cfg) {
371+
panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg);
372+
}
373+
println!("cargo:rustc-cfg={}", cfg);
374+
}

build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ fn main() {
114114
/// Run `rustc --version` and capture the output, adjusting arguments as needed if `clippy-driver`
115115
/// is used instead.
116116
fn rustc_version_cmd(is_clippy_driver: bool) -> Output {
117-
let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty());
118117
let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env");
119118

120-
let mut cmd = match rustc_wrapper {
119+
let mut cmd = match env::var_os("RUSTC_WRAPPER") {
120+
Some(ref wrapper) if wrapper.is_empty() => Command::new(rustc),
121121
Some(wrapper) => {
122122
let mut cmd = Command::new(wrapper);
123123
cmd.arg(rustc);

0 commit comments

Comments
 (0)