#![allow( clippy::enum_glob_use, clippy::must_use_candidate, clippy::single_match_else )] use std::{ env, ffi::OsString, fs, iter, path::Path, process::{self, Command}, }; mod rustc; fn main() { println!("cargo:rerun-if-changed=build/build.rs"); let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty()); let wrapped_rustc = rustc_wrapper.iter().chain(iter::once(&rustc)); let mut is_clippy_driver = false; let version = loop { let mut wrapped_rustc = wrapped_rustc.clone(); let mut command = Command::new(wrapped_rustc.next().unwrap()); command.args(wrapped_rustc); if is_clippy_driver { command.arg("--rustc"); } command.arg("--version"); let output = match command.output() { Ok(output) => output, Err(e) => { let rustc = rustc.to_string_lossy(); eprintln!("Error: failed to run `{} --version`: {}", rustc, e); process::exit(1); } }; let string = match String::from_utf8(output.stdout) { Ok(string) => string, Err(err) => { let rustc = rustc.to_string_lossy(); eprintln!( "Error: failed to parse output of `{} --version`: {}", rustc, err, ); process::exit(1); } }; break match rustc::parse(&string) { rustc::ParseResult::Success(version) => version, rustc::ParseResult::OopsClippy if !is_clippy_driver => { is_clippy_driver = true; continue; } rustc::ParseResult::Unrecognized | rustc::ParseResult::OopsClippy => { eprintln!( "Error: unexpected output from `rustc --version`: {:?}\n\n\ Please file an issue in https://github.com/robjtede/rustversion-msrv", string ); process::exit(1); } }; }; let version = format!("{:#?}\n", version); let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR not set"); let out_file = Path::new(&out_dir).join("version.expr"); fs::write(out_file, version).expect("failed to write version.expr"); }