Skip to content

Commit

Permalink
Use version_check crate
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Aug 24, 2020
1 parent ec6793a commit 4d47247
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 53 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ members = ["test_suite"]
[lib]
proc-macro = true

[build-dependencies]
version_check = "0.9.2"

[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
Expand Down
64 changes: 11 additions & 53 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
use std::{env, fs, path::PathBuf, process::Command, str};
use std::{env, fs, path::PathBuf};

fn main() {
let rustc = env::var_os("RUSTC").map_or_else(|| "rustc".into(), PathBuf::from);
let output = Command::new(&rustc)
.arg("--version")
.output()
.unwrap_or_else(|e| panic!("failed to run `{} --version`: {}", rustc.display(), e));
let version = str::from_utf8(&output.stdout).unwrap_or_else(|e| {
panic!("failed to parse output of `{} --version`: {}", rustc.display(), e)
});
let version = match parse(&version) {
let version = match Version::new() {
Some(version) => format!("{:#?}\n", version),
None => panic!("unexpected output from `rustc --version`: {}", version),
None => panic!("unexpected output from `rustc --version`"),
};

let out_dir = env::var_os("OUT_DIR").map(PathBuf::from).expect("OUT_DIR not set");
Expand All @@ -21,50 +13,16 @@ fn main() {

#[derive(Debug)]
struct Version {
minor: usize,
patch: usize,
minor: u16,
patch: u16,
nightly: bool,
}

fn parse(string: &str) -> Option<Version> {
let last_line = string.lines().last().unwrap_or(&string);
let mut words = last_line.trim().split(' ');

if words.next()? != "rustc" {
return None;
}

let mut version_channel = words.next()?.split('-');
let version = version_channel.next()?;
let channel = version_channel.next();

let mut digits = version.split('.');
let major = digits.next()?;
if major != "1" {
return None;
impl Version {
fn new() -> Option<Self> {
let (version, channel, _date) = version_check::triple()?;
let (_major, minor, patch) = version.to_mmp();
let nightly = channel.is_nightly() || channel.is_dev();
Some(Version { minor, patch, nightly })
}
let minor = digits.next()?.parse().ok()?;
let patch = digits.next().unwrap_or("0").parse().ok()?;

let nightly = match channel {
None => false,
Some(channel) if channel.starts_with("beta") => false,
Some(channel) if channel == "dev" => true,
Some(channel) if channel == "nightly" => match words.next() {
Some(hash) => {
if !hash.starts_with('(') {
return None;
}
let date = words.next()?;
if !date.ends_with(')') {
return None;
}
true
}
None => true,
},
Some(_) => return None,
};

Some(Version { minor, patch, nightly })
}

0 comments on commit 4d47247

Please sign in to comment.