1 //! This build script ensures that Clippy is not compiled with an
2 //! incompatible version of rust. It will panic with a descriptive
3 //! error message instead.
5 //! We specifially want to ensure that Clippy is only built with a
6 //! rustc version that is newer or equal to the one specified in the
7 //! `min_version.txt` file.
9 //! `min_version.txt` is in the repo but also in the `.gitignore` to
10 //! make sure that it is not updated manually by accident. Only CI
11 //! should update that file.
13 //! This build script was originally taken from the Rocket web framework:
14 //! https://github.com/SergioBenitez/Rocket
16 use ansi_term::Colour::Red;
17 use rustc_version::{version_meta, version_meta_for, Channel, Version, VersionMeta};
21 check_rustc_version();
23 // Forward the profile to the main compilation
24 println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
25 // Don't rebuild even if nothing changed
26 println!("cargo:rerun-if-changed=build.rs");
29 fn check_rustc_version() {
30 let string = include_str!("min_version.txt");
31 let min_version_meta = version_meta_for(string).expect("Could not parse version string in min_version.txt");
32 let current_version_meta = version_meta().expect("Could not retrieve current rustc version information from ENV");
34 let min_version = min_version_meta.clone().semver;
35 let min_date_str = min_version_meta
38 .expect("min_version.txt does not contain a rustc commit date");
40 // Dev channel (rustc built from git) does not have any date or commit information in rustc -vV
41 // `current_version_meta.commit_date` would crash, so we return early here.
42 if current_version_meta.channel == Channel::Dev {
46 let current_version = current_version_meta.clone().semver;
47 let current_date_str = current_version_meta
50 .expect("current rustc version information does not contain a rustc commit date");
52 let print_version_err = |version: &Version, date: &str| {
55 "Installed rustc version is:",
56 format!("{} ({})", version, date),
57 "Minimum required rustc version:",
58 format!("{} ({})", min_version, min_date_str)
62 if !correct_channel(¤t_version_meta) {
65 Red.bold().paint("error:"),
66 "Clippy requires a nightly version of Rust."
68 print_version_err(¤t_version, &*current_date_str);
71 "See the README (", "https://github.com/rust-lang-nursery/rust-clippy#usage", ") for more information."
73 panic!("Aborting compilation due to incompatible compiler.")
76 let current_date = str_to_ymd(¤t_date_str).unwrap();
77 let min_date = str_to_ymd(&min_date_str).unwrap();
79 if current_date < min_date {
82 Red.bold().paint("error:"),
83 "Clippy does not support this version of rustc nightly."
87 "Use `", "rustup update", "` or your preferred method to update Rust."
89 print_version_err(¤t_version, &*current_date_str);
90 panic!("Aborting compilation due to incompatible compiler.")
94 fn correct_channel(version_meta: &VersionMeta) -> bool {
95 match version_meta.channel {
96 Channel::Stable | Channel::Beta => false,
97 Channel::Nightly | Channel::Dev => true,
101 /// Convert a string of %Y-%m-%d to a single u32 maintaining ordering.
102 fn str_to_ymd(ymd: &str) -> Option<u32> {
103 let ymd: Vec<u32> = ymd.split("-").filter_map(|s| s.parse::<u32>().ok()).collect();
108 let (y, m, d) = (ymd[0], ymd[1], ymd[2]);
109 Some((y << 9) | (m << 5) | d)