Debian Codegraph @

third_party/rust/rustversion/build/build.rs

raw
1 #![allow(
2 clippy::enum_glob_use,
3 clippy::must_use_candidate,
4 clippy::single_match_else
5 )]
6
7 mod rustc;
8
9 use std::env;
10 use std::ffi::OsString;
11 use std::fs;
12 use std::iter;
13 use std::path::Path;
14 use std::process::{self, Command};
15
16 fn main() {
17 println!("cargo:rerun-if-changed=build/build.rs");
18
19 let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
20 let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty());
21 let wrapped_rustc = rustc_wrapper.iter().chain(iter::once(&rustc));
22
23 let mut is_clippy_driver = false;
24 let mut is_mirai = false;
25 let version = loop {
26 let mut command;
27 if is_mirai {
28 command = Command::new(&rustc);
29 } else {
30 let mut wrapped_rustc = wrapped_rustc.clone();
31 command = Command::new(wrapped_rustc.next().unwrap());
32 command.args(wrapped_rustc);
33 }
34 if is_clippy_driver {
35 command.arg("--rustc");
36 }
37 command.arg("--version");
38
39 let output = match command.output() {
40 Ok(output) => output,
41 Err(e) => {
42 let rustc = rustc.to_string_lossy();
43 eprintln!("Error: failed to run `{} --version`: {}", rustc, e);
44 process::exit(1);
45 }
46 };
47
48 let string = match String::from_utf8(output.stdout) {
49 Ok(string) => string,
50 Err(e) => {
51 let rustc = rustc.to_string_lossy();
52 eprintln!(
53 "Error: failed to parse output of `{} --version`: {}",
54 rustc, e,
55 );
56 process::exit(1);
57 }
58 };
59
60 break match rustc::parse(&string) {
61 rustc::ParseResult::Success(version) => version,
62 rustc::ParseResult::OopsClippy if !is_clippy_driver => {
63 is_clippy_driver = true;
64 continue;
65 }
66 rustc::ParseResult::OopsMirai if !is_mirai && rustc_wrapper.is_some() => {
67 is_mirai = true;
68 continue;
69 }
70 rustc::ParseResult::Unrecognized
71 | rustc::ParseResult::OopsClippy
72 | rustc::ParseResult::OopsMirai => {
73 eprintln!(
74 "Error: unexpected output from `rustc --version`: {:?}\n\n\
75 Please file an issue in https://github.com/dtolnay/rustversion",
76 string
77 );
78 process::exit(1);
79 }
80 };
81 };
82
83 if version.minor < 38 {
84 // Prior to 1.38, a #[proc_macro] is not allowed to be named `cfg`.
85 println!("cargo:rustc-cfg=cfg_macro_not_allowed");
86 }
87
88 if version.minor >= 80 {
89 println!("cargo:rustc-check-cfg=cfg(cfg_macro_not_allowed)");
90 println!("cargo:rustc-check-cfg=cfg(host_os, values(\"windows\"))");
91 }
92
93 let version = format!("{:#?}\n", version);
94 let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR not set");
95 let out_file = Path::new(&out_dir).join("version.expr");
96 fs::write(out_file, version).expect("failed to write version.expr");
97
98 let host = env::var_os("HOST").expect("HOST not set");
99 if let Some("windows") = host.to_str().unwrap().split('-').nth(2) {
100 println!("cargo:rustc-cfg=host_os=\"windows\"");
101 }
102 }