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