1 mod rustc;
2
3 use std::env;
4 use std::ffi::OsString;
5 use std::fs;
6 use std::path::Path;
7 use std::process::{self, Command};
8
main()9 fn main() {
10 let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
11 let output = match Command::new(&rustc).arg("--version").output() {
12 Ok(output) => output,
13 Err(e) => {
14 let rustc = rustc.to_string_lossy();
15 eprintln!("Error: failed to run `{} --version`: {}", rustc, e);
16 process::exit(1);
17 }
18 };
19
20 let string = match String::from_utf8(output.stdout) {
21 Ok(string) => string,
22 Err(e) => {
23 let rustc = rustc.to_string_lossy();
24 eprintln!(
25 "Error: failed to parse output of `{} --version`: {}",
26 rustc, e,
27 );
28 process::exit(1);
29 }
30 };
31
32 let version = match rustc::parse(&string) {
33 Some(version) => format!("{:#?}\n", version),
34 None => {
35 eprintln!(
36 "Error: unexpected output from `rustc --version`: {:?}\n\n\
37 Please file an issue in https://github.com/dtolnay/rustversion",
38 string
39 );
40 process::exit(1);
41 }
42 };
43
44 let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR not set");
45 let out_file = Path::new(&out_dir).join("version.rs");
46 fs::write(out_file, version).expect("failed to write version.rs");
47 }
48