]> git.lizzy.rs Git - rust.git/blob - build.rs
Format code
[rust.git] / build.rs
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.
4 //!
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.
8 //!
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.
12 //!
13 //! This build script was originally taken from the Rocket web framework:
14 //! https://github.com/SergioBenitez/Rocket
15
16 extern crate ansi_term;
17 extern crate rustc_version;
18
19 use ansi_term::Colour::Red;
20 use rustc_version::{version_meta, version_meta_for, Channel, Version, VersionMeta};
21 use std::env;
22
23 fn main() {
24     check_rustc_version();
25
26     // Forward the profile to the main compilation
27     println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
28     // Don't rebuild even if nothing changed
29     println!("cargo:rerun-if-changed=build.rs");
30 }
31
32 fn check_rustc_version() {
33     let string = include_str!("min_version.txt");
34     let min_version_meta = version_meta_for(string).expect("Could not parse version string in min_version.txt");
35     let current_version_meta = version_meta().expect("Could not retrieve current rustc version information from ENV");
36
37     let min_version = min_version_meta.clone().semver;
38     let min_date_str = min_version_meta
39         .clone()
40         .commit_date
41         .expect("min_version.txt does not contain a rustc commit date");
42
43     // Dev channel (rustc built from git) does not have any date or commit information in rustc -vV
44     // `current_version_meta.commit_date` would crash, so we return early here.
45     if current_version_meta.channel == Channel::Dev {
46         return;
47     }
48
49     let current_version = current_version_meta.clone().semver;
50     let current_date_str = current_version_meta
51         .clone()
52         .commit_date
53         .expect("current rustc version information does not contain a rustc commit date");
54
55     let print_version_err = |version: &Version, date: &str| {
56         eprintln!(
57             "> {} {}. {} {}.\n",
58             "Installed rustc version is:",
59             format!("{} ({})", version, date),
60             "Minimum required rustc version:",
61             format!("{} ({})", min_version, min_date_str)
62         );
63     };
64
65     if !correct_channel(&current_version_meta) {
66         eprintln!(
67             "\n{} {}",
68             Red.bold().paint("error:"),
69             "clippy requires a nightly version of Rust."
70         );
71         print_version_err(&current_version, &*current_date_str);
72         eprintln!(
73             "{}{}{}",
74             "See the README (", "https://github.com/rust-lang-nursery/rust-clippy#usage", ") for more information."
75         );
76         panic!("Aborting compilation due to incompatible compiler.")
77     }
78
79     let current_date = str_to_ymd(&current_date_str).unwrap();
80     let min_date = str_to_ymd(&min_date_str).unwrap();
81
82     if current_date < min_date {
83         eprintln!(
84             "\n{} {}",
85             Red.bold().paint("error:"),
86             "clippy does not support this version of rustc nightly."
87         );
88         eprintln!(
89             "> {}{}{}",
90             "Use `", "rustup update", "` or your preferred method to update Rust."
91         );
92         print_version_err(&current_version, &*current_date_str);
93         panic!("Aborting compilation due to incompatible compiler.")
94     }
95 }
96
97 fn correct_channel(version_meta: &VersionMeta) -> bool {
98     match version_meta.channel {
99         Channel::Stable | Channel::Beta => false,
100         Channel::Nightly | Channel::Dev => true,
101     }
102 }
103
104 /// Convert a string of %Y-%m-%d to a single u32 maintaining ordering.
105 fn str_to_ymd(ymd: &str) -> Option<u32> {
106     let ymd: Vec<u32> = ymd.split("-").filter_map(|s| s.parse::<u32>().ok()).collect();
107     if ymd.len() != 3 {
108         return None;
109     }
110
111     let (y, m, d) = (ymd[0], ymd[1], ymd[2]);
112     Some((y << 9) | (m << 5) | d)
113 }