]> git.proxmox.com Git - rustc.git/blob - vendor/version_check/src/lib.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / vendor / version_check / src / lib.rs
1 //! This tiny crate checks that the running or installed `rustc` meets some
2 //! version requirements. The version is queried by calling the Rust compiler
3 //! with `--version`. The path to the compiler is determined first via the
4 //! `RUSTC` environment variable. If it is not set, then `rustc` is used. If
5 //! that fails, no determination is made, and calls return `None`.
6 //!
7 //! # Examples
8 //!
9 //! Set a `cfg` flag in `build.rs` if the running compiler was determined to be
10 //! at least version `1.13.0`:
11 //!
12 //! ```rust
13 //! extern crate version_check as rustc;
14 //!
15 //! if rustc::is_min_version("1.13.0").unwrap_or(false) {
16 //! println!("cargo:rustc-cfg=question_mark_operator");
17 //! }
18 //! ```
19 //!
20 //! See [`is_max_version`] or [`is_exact_version`] to check if the compiler
21 //! is _at most_ or _exactly_ a certain version.
22 //!
23 //! Check that the running compiler was released on or after `2018-12-18`:
24 //!
25 //! ```rust
26 //! extern crate version_check as rustc;
27 //!
28 //! match rustc::is_min_date("2018-12-18") {
29 //! Some(true) => "Yep! It's recent!",
30 //! Some(false) => "No, it's older.",
31 //! None => "Couldn't determine the rustc version."
32 //! };
33 //! ```
34 //!
35 //! See [`is_max_date`] or [`is_exact_date`] to check if the compiler was
36 //! released _prior to_ or _exactly on_ a certain date.
37 //!
38 //! Check that the running compiler supports feature flags:
39 //!
40 //! ```rust
41 //! extern crate version_check as rustc;
42 //!
43 //! match rustc::is_feature_flaggable() {
44 //! Some(true) => "Yes! It's a dev or nightly release!",
45 //! Some(false) => "No, it's stable or beta.",
46 //! None => "Couldn't determine the rustc version."
47 //! };
48 //! ```
49 //!
50 //! Check that the running compiler is on the stable channel:
51 //!
52 //! ```rust
53 //! extern crate version_check as rustc;
54 //!
55 //! match rustc::Channel::read() {
56 //! Some(c) if c.is_stable() => format!("Yes! It's stable."),
57 //! Some(c) => format!("No, the channel {} is not stable.", c),
58 //! None => format!("Couldn't determine the rustc version.")
59 //! };
60 //! ```
61 //!
62 //! To interact with the version, release date, and release channel as structs,
63 //! use [`Version`], [`Date`], and [`Channel`], respectively. The [`triple()`]
64 //! function returns all three values efficiently.
65 //!
66 //! # Alternatives
67 //!
68 //! This crate is dead simple with no dependencies. If you need something more
69 //! and don't care about panicking if the version cannot be obtained, or if you
70 //! don't mind adding dependencies, see
71 //! [rustc_version](https://crates.io/crates/rustc_version).
72
73 #![allow(deprecated)]
74
75 mod version;
76 mod channel;
77 mod date;
78
79 use std::env;
80 use std::process::Command;
81
82 #[doc(inline)] pub use version::*;
83 #[doc(inline)] pub use channel::*;
84 #[doc(inline)] pub use date::*;
85
86 /// Parses (version, date) as available from rustc version string.
87 fn version_and_date_from_rustc_version(s: &str) -> (Option<String>, Option<String>) {
88 let last_line = s.lines().last().unwrap_or(s);
89 let mut components = last_line.trim().split(" ");
90 let version = components.nth(1);
91 let date = components.filter(|c| c.ends_with(')')).next()
92 .map(|s| s.trim_right().trim_right_matches(")").trim_left().trim_left_matches('('));
93 (version.map(|s| s.to_string()), date.map(|s| s.to_string()))
94 }
95
96 /// Parses (version, date) as available from rustc verbose version output.
97 fn version_and_date_from_rustc_verbose_version(s: &str) -> (Option<String>, Option<String>) {
98 let (mut version, mut date) = (None, None);
99 for line in s.lines() {
100 let split = |s: &str| s.splitn(2, ":").nth(1).map(|s| s.trim().to_string());
101 match line.trim().split(" ").nth(0) {
102 Some("rustc") => {
103 let (v, d) = version_and_date_from_rustc_version(line);
104 version = version.or(v);
105 date = date.or(d);
106 },
107 Some("release:") => version = split(line),
108 Some("commit-date:") if line.ends_with("unknown") => date = None,
109 Some("commit-date:") => date = split(line),
110 _ => continue
111 }
112 }
113
114 (version, date)
115 }
116
117 /// Returns (version, date) as available from `rustc --version`.
118 fn get_version_and_date() -> Option<(Option<String>, Option<String>)> {
119 let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
120 Command::new(rustc).arg("--verbose").arg("--version").output().ok()
121 .and_then(|output| String::from_utf8(output.stdout).ok())
122 .map(|s| version_and_date_from_rustc_verbose_version(&s))
123 }
124
125 /// Reads the triple of [`Version`], [`Channel`], and [`Date`] of the installed
126 /// or running `rustc`.
127 ///
128 /// If any attribute cannot be determined (see the [top-level
129 /// documentation](crate)), returns `None`.
130 ///
131 /// To obtain only one of three attributes, use [`Version::read()`],
132 /// [`Channel::read()`], or [`Date::read()`].
133 pub fn triple() -> Option<(Version, Channel, Date)> {
134 let (version_str, date_str) = match get_version_and_date() {
135 Some((Some(version), Some(date))) => (version, date),
136 _ => return None
137 };
138
139 // Can't use `?` or `try!` for `Option` in 1.0.0.
140 match Version::parse(&version_str) {
141 Some(version) => match Channel::parse(&version_str) {
142 Some(channel) => match Date::parse(&date_str) {
143 Some(date) => Some((version, channel, date)),
144 _ => None,
145 },
146 _ => None,
147 },
148 _ => None
149 }
150 }
151
152 /// Checks that the running or installed `rustc` was released **on or after**
153 /// some date.
154 ///
155 /// The format of `min_date` must be YYYY-MM-DD. For instance: `2016-12-20` or
156 /// `2017-01-09`.
157 ///
158 /// If the date cannot be retrieved or parsed, or if `min_date` could not be
159 /// parsed, returns `None`. Otherwise returns `true` if the installed `rustc`
160 /// was release on or after `min_date` and `false` otherwise.
161 pub fn is_min_date(min_date: &str) -> Option<bool> {
162 match (Date::read(), Date::parse(min_date)) {
163 (Some(rustc_date), Some(min_date)) => Some(rustc_date >= min_date),
164 _ => None
165 }
166 }
167
168 /// Checks that the running or installed `rustc` was released **on or before**
169 /// some date.
170 ///
171 /// The format of `max_date` must be YYYY-MM-DD. For instance: `2016-12-20` or
172 /// `2017-01-09`.
173 ///
174 /// If the date cannot be retrieved or parsed, or if `max_date` could not be
175 /// parsed, returns `None`. Otherwise returns `true` if the installed `rustc`
176 /// was release on or before `max_date` and `false` otherwise.
177 pub fn is_max_date(max_date: &str) -> Option<bool> {
178 match (Date::read(), Date::parse(max_date)) {
179 (Some(rustc_date), Some(max_date)) => Some(rustc_date <= max_date),
180 _ => None
181 }
182 }
183
184 /// Checks that the running or installed `rustc` was released **exactly** on
185 /// some date.
186 ///
187 /// The format of `date` must be YYYY-MM-DD. For instance: `2016-12-20` or
188 /// `2017-01-09`.
189 ///
190 /// If the date cannot be retrieved or parsed, or if `date` could not be parsed,
191 /// returns `None`. Otherwise returns `true` if the installed `rustc` was
192 /// release on `date` and `false` otherwise.
193 pub fn is_exact_date(date: &str) -> Option<bool> {
194 match (Date::read(), Date::parse(date)) {
195 (Some(rustc_date), Some(date)) => Some(rustc_date == date),
196 _ => None
197 }
198 }
199
200 /// Checks that the running or installed `rustc` is **at least** some minimum
201 /// version.
202 ///
203 /// The format of `min_version` is a semantic version: `1.3.0`, `1.15.0-beta`,
204 /// `1.14.0`, `1.16.0-nightly`, etc.
205 ///
206 /// If the version cannot be retrieved or parsed, or if `min_version` could not
207 /// be parsed, returns `None`. Otherwise returns `true` if the installed `rustc`
208 /// is at least `min_version` and `false` otherwise.
209 pub fn is_min_version(min_version: &str) -> Option<bool> {
210 match (Version::read(), Version::parse(min_version)) {
211 (Some(rustc_ver), Some(min_ver)) => Some(rustc_ver >= min_ver),
212 _ => None
213 }
214 }
215
216 /// Checks that the running or installed `rustc` is **at most** some maximum
217 /// version.
218 ///
219 /// The format of `max_version` is a semantic version: `1.3.0`, `1.15.0-beta`,
220 /// `1.14.0`, `1.16.0-nightly`, etc.
221 ///
222 /// If the version cannot be retrieved or parsed, or if `max_version` could not
223 /// be parsed, returns `None`. Otherwise returns `true` if the installed `rustc`
224 /// is at most `max_version` and `false` otherwise.
225 pub fn is_max_version(max_version: &str) -> Option<bool> {
226 match (Version::read(), Version::parse(max_version)) {
227 (Some(rustc_ver), Some(max_ver)) => Some(rustc_ver <= max_ver),
228 _ => None
229 }
230 }
231
232 /// Checks that the running or installed `rustc` is **exactly** some version.
233 ///
234 /// The format of `version` is a semantic version: `1.3.0`, `1.15.0-beta`,
235 /// `1.14.0`, `1.16.0-nightly`, etc.
236 ///
237 /// If the version cannot be retrieved or parsed, or if `version` could not be
238 /// parsed, returns `None`. Otherwise returns `true` if the installed `rustc` is
239 /// exactly `version` and `false` otherwise.
240 pub fn is_exact_version(version: &str) -> Option<bool> {
241 match (Version::read(), Version::parse(version)) {
242 (Some(rustc_ver), Some(version)) => Some(rustc_ver == version),
243 _ => None
244 }
245 }
246
247 /// Checks whether the running or installed `rustc` supports feature flags.
248 ///
249 /// In other words, if the channel is either "nightly" or "dev".
250 ///
251 /// If the version could not be determined, returns `None`. Otherwise returns
252 /// `true` if the running version supports feature flags and `false` otherwise.
253 pub fn is_feature_flaggable() -> Option<bool> {
254 Channel::read().map(|c| c.supports_features())
255 }
256
257 #[cfg(test)]
258 mod tests {
259 use super::version_and_date_from_rustc_version;
260 use super::version_and_date_from_rustc_verbose_version;
261
262 macro_rules! check_parse {
263 (@ $f:expr, $s:expr => $v:expr, $d:expr) => ({
264 if let (Some(v), d) = $f($s) {
265 let e_d: Option<&str> = $d.into();
266 assert_eq!((v, d), ($v.into(), e_d.map(|s| s.into())));
267 } else {
268 panic!("{:?} didn't parse for version testing.", $s);
269 }
270 });
271 ($f:expr, $s:expr => $v:expr, $d:expr) => ({
272 let warn = "warning: invalid logging spec 'warning', ignoring it";
273 let warn2 = "warning: sorry, something went wrong :(sad)";
274 check_parse!(@ $f, $s => $v, $d);
275 check_parse!(@ $f, &format!("{}\n{}", warn, $s) => $v, $d);
276 check_parse!(@ $f, &format!("{}\n{}", warn2, $s) => $v, $d);
277 check_parse!(@ $f, &format!("{}\n{}\n{}", warn, warn2, $s) => $v, $d);
278 check_parse!(@ $f, &format!("{}\n{}\n{}", warn2, warn, $s) => $v, $d);
279 })
280 }
281
282 macro_rules! check_terse_parse {
283 ($($s:expr => $v:expr, $d:expr,)+) => {$(
284 check_parse!(version_and_date_from_rustc_version, $s => $v, $d);
285 )+}
286 }
287
288 macro_rules! check_verbose_parse {
289 ($($s:expr => $v:expr, $d:expr,)+) => {$(
290 check_parse!(version_and_date_from_rustc_verbose_version, $s => $v, $d);
291 )+}
292 }
293
294 #[test]
295 fn test_version_parse() {
296 check_terse_parse! {
297 "rustc 1.18.0" => "1.18.0", None,
298 "rustc 1.8.0" => "1.8.0", None,
299 "rustc 1.20.0-nightly" => "1.20.0-nightly", None,
300 "rustc 1.20" => "1.20", None,
301 "rustc 1.3" => "1.3", None,
302 "rustc 1" => "1", None,
303 "rustc 1.5.1-beta" => "1.5.1-beta", None,
304 "rustc 1.20.0 (2017-07-09)" => "1.20.0", Some("2017-07-09"),
305 "rustc 1.20.0-dev (2017-07-09)" => "1.20.0-dev", Some("2017-07-09"),
306 "rustc 1.20.0-nightly (d84693b93 2017-07-09)" => "1.20.0-nightly", Some("2017-07-09"),
307 "rustc 1.20.0 (d84693b93 2017-07-09)" => "1.20.0", Some("2017-07-09"),
308 "rustc 1.30.0-nightly (3bc2ca7e4 2018-09-20)" => "1.30.0-nightly", Some("2018-09-20"),
309 };
310 }
311
312 #[test]
313 fn test_verbose_version_parse() {
314 check_verbose_parse! {
315 "rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)\n\
316 binary: rustc\n\
317 commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e\n\
318 commit-date: 2015-05-13\n\
319 build-date: 2015-05-14\n\
320 host: x86_64-unknown-linux-gnu\n\
321 release: 1.0.0" => "1.0.0", Some("2015-05-13"),
322
323 "rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)\n\
324 commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e\n\
325 commit-date: 2015-05-13\n\
326 build-date: 2015-05-14\n\
327 host: x86_64-unknown-linux-gnu\n\
328 release: 1.0.0" => "1.0.0", Some("2015-05-13"),
329
330 "rustc 1.50.0 (cb75ad5db 2021-02-10)\n\
331 binary: rustc\n\
332 commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\n\
333 commit-date: 2021-02-10\n\
334 host: x86_64-unknown-linux-gnu\n\
335 release: 1.50.0" => "1.50.0", Some("2021-02-10"),
336
337 "rustc 1.52.0-nightly (234781afe 2021-03-07)\n\
338 binary: rustc\n\
339 commit-hash: 234781afe33d3f339b002f85f948046d8476cfc9\n\
340 commit-date: 2021-03-07\n\
341 host: x86_64-unknown-linux-gnu\n\
342 release: 1.52.0-nightly\n\
343 LLVM version: 12.0.0" => "1.52.0-nightly", Some("2021-03-07"),
344
345 "rustc 1.41.1\n\
346 binary: rustc\n\
347 commit-hash: unknown\n\
348 commit-date: unknown\n\
349 host: x86_64-unknown-linux-gnu\n\
350 release: 1.41.1\n\
351 LLVM version: 7.0" => "1.41.1", None,
352
353 "rustc 1.49.0\n\
354 binary: rustc\n\
355 commit-hash: unknown\n\
356 commit-date: unknown\n\
357 host: x86_64-unknown-linux-gnu\n\
358 release: 1.49.0" => "1.49.0", None,
359
360 "rustc 1.50.0 (Fedora 1.50.0-1.fc33)\n\
361 binary: rustc\n\
362 commit-hash: unknown\n\
363 commit-date: unknown\n\
364 host: x86_64-unknown-linux-gnu\n\
365 release: 1.50.0" => "1.50.0", None,
366 };
367 }
368 }