2018-07-05 10:37:50 +00:00
|
|
|
//! This build script ensures that Clippy is not compiled with an
|
2018-05-07 19:01:23 +00:00
|
|
|
//! incompatible version of rust. It will panic with a descriptive
|
|
|
|
//! error message instead.
|
|
|
|
//!
|
2018-07-05 10:37:50 +00:00
|
|
|
//! We specifially want to ensure that Clippy is only built with a
|
2018-05-07 19:01:23 +00:00
|
|
|
//! rustc version that is newer or equal to the one specified in the
|
|
|
|
//! `min_version.txt` file.
|
|
|
|
//!
|
|
|
|
//! `min_version.txt` is in the repo but also in the `.gitignore` to
|
|
|
|
//! make sure that it is not updated manually by accident. Only CI
|
|
|
|
//! should update that file.
|
|
|
|
//!
|
|
|
|
//! This build script was originally taken from the Rocket web framework:
|
|
|
|
//! https://github.com/SergioBenitez/Rocket
|
|
|
|
|
|
|
|
use ansi_term::Colour::Red;
|
2018-05-22 08:21:42 +00:00
|
|
|
use rustc_version::{version_meta, version_meta_for, Channel, Version, VersionMeta};
|
|
|
|
use std::env;
|
2017-09-18 10:47:33 +00:00
|
|
|
|
|
|
|
fn main() {
|
2018-05-13 08:11:52 +00:00
|
|
|
check_rustc_version();
|
|
|
|
|
|
|
|
// Forward the profile to the main compilation
|
|
|
|
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
|
|
|
|
// Don't rebuild even if nothing changed
|
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_rustc_version() {
|
2018-05-07 19:01:23 +00:00
|
|
|
let string = include_str!("min_version.txt");
|
2018-05-22 08:21:42 +00:00
|
|
|
let min_version_meta = version_meta_for(string).expect("Could not parse version string in min_version.txt");
|
|
|
|
let current_version_meta = version_meta().expect("Could not retrieve current rustc version information from ENV");
|
2018-05-07 19:01:23 +00:00
|
|
|
|
|
|
|
let min_version = min_version_meta.clone().semver;
|
2018-05-22 08:21:42 +00:00
|
|
|
let min_date_str = min_version_meta
|
|
|
|
.clone()
|
|
|
|
.commit_date
|
2018-05-07 19:01:23 +00:00
|
|
|
.expect("min_version.txt does not contain a rustc commit date");
|
|
|
|
|
2018-05-13 08:11:52 +00:00
|
|
|
// Dev channel (rustc built from git) does not have any date or commit information in rustc -vV
|
|
|
|
// `current_version_meta.commit_date` would crash, so we return early here.
|
|
|
|
if current_version_meta.channel == Channel::Dev {
|
2018-05-22 08:21:42 +00:00
|
|
|
return;
|
2018-05-13 08:11:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 19:01:23 +00:00
|
|
|
let current_version = current_version_meta.clone().semver;
|
2018-05-22 08:21:42 +00:00
|
|
|
let current_date_str = current_version_meta
|
|
|
|
.clone()
|
|
|
|
.commit_date
|
2018-05-07 19:01:23 +00:00
|
|
|
.expect("current rustc version information does not contain a rustc commit date");
|
|
|
|
|
|
|
|
let print_version_err = |version: &Version, date: &str| {
|
2018-05-22 08:21:42 +00:00
|
|
|
eprintln!(
|
|
|
|
"> {} {}. {} {}.\n",
|
|
|
|
"Installed rustc version is:",
|
|
|
|
format!("{} ({})", version, date),
|
|
|
|
"Minimum required rustc version:",
|
|
|
|
format!("{} ({})", min_version, min_date_str)
|
|
|
|
);
|
2018-05-07 19:01:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if !correct_channel(¤t_version_meta) {
|
2018-05-22 08:21:42 +00:00
|
|
|
eprintln!(
|
|
|
|
"\n{} {}",
|
|
|
|
Red.bold().paint("error:"),
|
2018-07-05 10:37:50 +00:00
|
|
|
"Clippy requires a nightly version of Rust."
|
2018-05-22 08:21:42 +00:00
|
|
|
);
|
2018-05-07 19:01:23 +00:00
|
|
|
print_version_err(¤t_version, &*current_date_str);
|
2018-05-22 08:21:42 +00:00
|
|
|
eprintln!(
|
|
|
|
"{}{}{}",
|
|
|
|
"See the README (", "https://github.com/rust-lang-nursery/rust-clippy#usage", ") for more information."
|
|
|
|
);
|
2018-05-07 19:01:23 +00:00
|
|
|
panic!("Aborting compilation due to incompatible compiler.")
|
|
|
|
}
|
|
|
|
|
|
|
|
let current_date = str_to_ymd(¤t_date_str).unwrap();
|
|
|
|
let min_date = str_to_ymd(&min_date_str).unwrap();
|
|
|
|
|
|
|
|
if current_date < min_date {
|
2018-05-22 08:21:42 +00:00
|
|
|
eprintln!(
|
|
|
|
"\n{} {}",
|
|
|
|
Red.bold().paint("error:"),
|
2018-07-05 10:37:50 +00:00
|
|
|
"Clippy does not support this version of rustc nightly."
|
2018-05-22 08:21:42 +00:00
|
|
|
);
|
|
|
|
eprintln!(
|
|
|
|
"> {}{}{}",
|
|
|
|
"Use `", "rustup update", "` or your preferred method to update Rust."
|
|
|
|
);
|
2018-05-07 19:01:23 +00:00
|
|
|
print_version_err(¤t_version, &*current_date_str);
|
|
|
|
panic!("Aborting compilation due to incompatible compiler.")
|
|
|
|
}
|
2017-09-18 10:47:33 +00:00
|
|
|
}
|
2018-05-07 19:01:23 +00:00
|
|
|
|
|
|
|
fn correct_channel(version_meta: &VersionMeta) -> bool {
|
|
|
|
match version_meta.channel {
|
2018-05-22 08:21:42 +00:00
|
|
|
Channel::Stable | Channel::Beta => false,
|
|
|
|
Channel::Nightly | Channel::Dev => true,
|
2018-05-07 19:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a string of %Y-%m-%d to a single u32 maintaining ordering.
|
|
|
|
fn str_to_ymd(ymd: &str) -> Option<u32> {
|
|
|
|
let ymd: Vec<u32> = ymd.split("-").filter_map(|s| s.parse::<u32>().ok()).collect();
|
|
|
|
if ymd.len() != 3 {
|
2018-05-22 08:21:42 +00:00
|
|
|
return None;
|
2018-05-07 19:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let (y, m, d) = (ymd[0], ymd[1], ymd[2]);
|
|
|
|
Some((y << 9) | (m << 5) | d)
|
|
|
|
}
|