Debian Codegraph @

debian/rust-vendor/portable-atomic/version.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 use std::{env, iter, process::Command, str};
4
5 pub(crate) fn rustc_version() -> Option<Version> {
6 let rustc = env::var_os("RUSTC")?;
7 let rustc_wrapper = if env::var_os("CARGO_ENCODED_RUSTFLAGS").is_some() {
8 env::var_os("RUSTC_WRAPPER").filter(|v| !v.is_empty())
9 } else {
10 // Cargo sets environment variables for wrappers correctly only since https://github.com/rust-lang/cargo/pull/9601.
11 None
12 };
13 // Do not apply RUSTC_WORKSPACE_WRAPPER: https://github.com/cuviper/autocfg/issues/58#issuecomment-2067625980
14 let mut rustc = rustc_wrapper.into_iter().chain(iter::once(rustc));
15 let mut cmd = Command::new(rustc.next().unwrap());
16 cmd.args(rustc);
17 // Use verbose version output because the packagers add extra strings to the normal version output.
18 // Do not use long flags (--version --verbose) because clippy-deriver doesn't handle them properly.
19 // -vV is also matched with that cargo internally uses: https://github.com/rust-lang/cargo/blob/0.80.0/src/cargo/util/rustc.rs#L65
20 let output = cmd.arg("-vV").output().ok()?;
21 let verbose_version = str::from_utf8(&output.stdout).ok()?;
22 Version::parse(verbose_version)
23 }
24
25 #[cfg_attr(test, derive(Debug, PartialEq))]
26 pub(crate) struct Version {
27 pub(crate) minor: u32,
28 pub(crate) nightly: bool,
29 commit_date: Date,
30 pub(crate) llvm: u32,
31 }
32
33 impl Version {
34 // The known latest stable version. If we unable to determine
35 // the rustc version, we assume this is the current version.
36 // It is no problem if this is older than the actual latest stable.
37 // LLVM version is assumed to be the minimum external LLVM version:
38 // https://github.com/rust-lang/rust/blob/1.97.0/src/bootstrap/src/core/build_steps/llvm.rs#L638
39 pub(crate) const LATEST: Self = Self::stable(97, 21);
40
41 pub(crate) const fn stable(rustc_minor: u32, llvm_major: u32) -> Self {
42 Self { minor: rustc_minor, nightly: false, commit_date: Date::UNKNOWN, llvm: llvm_major }
43 }
44
45 pub(crate) fn probe(&self, minor: u32, year: u16, month: u8, day: u8) -> bool {
46 if self.nightly {
47 self.minor > minor
48 || self.minor == minor && self.commit_date >= Date::new(year, month, day)
49 } else {
50 self.minor >= minor
51 }
52 }
53
54 #[cfg(test)]
55 pub(crate) fn commit_date(&self) -> &Date {
56 &self.commit_date
57 }
58
59 pub(crate) fn parse(verbose_version: &str) -> Option<Self> {
60 let mut release = verbose_version
61 .lines()
62 .find(|line| line.starts_with("release: "))
63 .map(|line| &line["release: ".len()..])?
64 .splitn(2, '-');
65 let version = release.next().unwrap();
66 let channel = release.next().unwrap_or_default();
67 let mut digits = version.splitn(3, '.');
68 let major = digits.next()?;
69 if major != "1" {
70 return None;
71 }
72 let minor = digits.next()?.parse::<u32>().ok()?;
73 let _patch = digits.next().unwrap_or("0").parse::<u32>().ok()?;
74 let nightly = match env::var_os("RUSTC_BOOTSTRAP") {
75 // When -1 is passed rustc works like stable, e.g., cfg(target_feature = "unstable_target_feature") will never be set. https://github.com/rust-lang/rust/pull/132993
76 Some(ref v) if v == "-1" => false,
77 // When 1 is passed stable rustc works like nightly, but we ignore it for now.
78 _ => channel == "nightly" || channel == "dev",
79 };
80
81 // Note that rustc 1.49-1.50 (and 1.13 or older) don't print LLVM version.
82 let llvm_major = (|| {
83 let version = verbose_version
84 .lines()
85 .find(|line| line.starts_with("LLVM version: "))
86 .map(|line| &line["LLVM version: ".len()..])?;
87 let mut digits = version.splitn(3, '.');
88 let major = digits.next()?.parse::<u32>().ok()?;
89 let _minor = digits.next()?.parse::<u32>().ok()?;
90 let _patch = digits.next().unwrap_or("0").parse::<u32>().ok()?;
91 Some(major)
92 })()
93 .unwrap_or(0);
94
95 // we don't refer commit date on stable/beta.
96 if nightly {
97 let commit_date = (|| {
98 let mut commit_date = verbose_version
99 .lines()
100 .find(|line| line.starts_with("commit-date: "))
101 .map(|line| &line["commit-date: ".len()..])?
102 .splitn(3, '-');
103 let year = commit_date.next()?.parse::<u16>().ok()?;
104 let month = commit_date.next()?.parse::<u8>().ok()?;
105 let day = commit_date.next()?.parse::<u8>().ok()?;
106 if month > 12 || day > 31 {
107 return None;
108 }
109 Some(Date::new(year, month, day))
110 })();
111 Some(Self {
112 minor,
113 nightly,
114 commit_date: commit_date.unwrap_or(Date::UNKNOWN),
115 llvm: llvm_major,
116 })
117 } else {
118 Some(Self::stable(minor, llvm_major))
119 }
120 }
121 }
122
123 #[derive(PartialEq, PartialOrd)]
124 #[cfg_attr(test, derive(Debug))]
125 pub(crate) struct Date {
126 pub(crate) year: u16,
127 pub(crate) month: u8,
128 pub(crate) day: u8,
129 }
130
131 impl Date {
132 const UNKNOWN: Self = Self::new(0, 0, 0);
133
134 const fn new(year: u16, month: u8, day: u8) -> Self {
135 Self { year, month, day }
136 }
137 }