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::path::Path;
13 use std::process::{self, Command};
14
main()15 fn main() {
16 println!("cargo:rerun-if-changed=build/build.rs");
17
18 let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
19
20 let mut is_clippy_driver = false;
21 let version = loop {
22 let mut command = Command::new(&rustc);
23 if is_clippy_driver {
24 command.arg("--rustc");
25 }
26 command.arg("--version");
27
28 let output = match command.output() {
29 Ok(output) => output,
30 Err(e) => {
31 let rustc = rustc.to_string_lossy();
32 eprintln!("Error: failed to run `{} --version`: {}", rustc, e);
33 process::exit(1);
34 }
35 };
36
37 let string = match String::from_utf8(output.stdout) {
38 Ok(string) => string,
39 Err(e) => {
40 let rustc = rustc.to_string_lossy();
41 eprintln!(
42 "Error: failed to parse output of `{} --version`: {}",
43 rustc, e,
44 );
45 process::exit(1);
46 }
47 };
48
49 break match rustc::parse(&string) {
50 rustc::ParseResult::Success(version) => version,
51 rustc::ParseResult::OopsClippy if !is_clippy_driver => {
52 is_clippy_driver = true;
53 continue;
54 }
55 rustc::ParseResult::Unrecognized | rustc::ParseResult::OopsClippy => {
56 eprintln!(
57 "Error: unexpected output from `rustc --version`: {:?}\n\n\
58 Please file an issue in https://github.com/dtolnay/rustversion",
59 string
60 );
61 process::exit(1);
62 }
63 };
64 };
65
66 if version.minor < 38 {
67 // Prior to 1.38, a #[proc_macro] is not allowed to be named `cfg`.
68 println!("cargo:rustc-cfg=cfg_macro_not_allowed");
69 }
70
71 let version = format!("{:#?}\n", version);
72 let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR not set");
73 let out_file = Path::new(&out_dir).join("version.expr");
74 fs::write(out_file, version).expect("failed to write version.expr");
75 }
76