1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#![warn(missing_docs)]
extern crate semver;
use semver::{Version, VersionReq, Identifier};
use std::process::Command;
use std::env;
use std::ffi::OsString;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Channel {
Dev,
Nightly,
Beta,
Stable,
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct VersionMeta {
pub semver: Version,
pub commit_hash: Option<String>,
pub commit_date: Option<String>,
pub build_date: Option<String>,
pub channel: Channel,
pub host: String,
pub short_version_string: String,
}
pub fn version() -> Version {
version_meta().semver
}
pub fn version_meta() -> VersionMeta {
let cmd = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
let out = Command::new(&cmd)
.arg("-vV")
.output()
.unwrap_or_else(|e| { panic!("failed to execute `RUSTC -vV`: {}", e) });
let out = String::from_utf8(out.stdout)
.ok()
.expect("non utf8 output from RUSTC -vV");
version_meta_for(&out)
}
pub fn version_meta_for(verbose_version_string: &str) -> VersionMeta {
let out: Vec<_> = verbose_version_string.lines().collect();
const ERR_MSG: &'static str = "unexpected -vV format";
assert!(out.len() == 6 || out.len() == 7, ERR_MSG);
let short_version_string = out[0];
fn expect_prefix<'a>(line: &'a str, prefix: &str) -> &'a str {
assert!(line.starts_with(prefix), ERR_MSG);
&line[prefix.len()..]
}
let commit_hash = match expect_prefix(out[2], "commit-hash: ") {
"unknown" => None,
hash => Some(hash.to_owned()),
};
let commit_date = match expect_prefix(out[3], "commit-date: ") {
"unknown" => None,
hash => Some(hash.to_owned()),
};
let mut idx = 4;
let mut build_date = None;
if out[idx].starts_with("build-date") {
build_date = match expect_prefix(out[idx], "build-date: ") {
"unknown" => None,
s => Some(s.to_owned()),
};
idx = idx + 1;
}
let host = expect_prefix(out[idx], "host: ");
idx = idx +1;
let release = expect_prefix(out[idx], "release: ");
let semver = Version::parse(release).unwrap();
let channel = if semver.pre.is_empty() {
Channel::Stable
} else {
match semver.pre[0] {
Identifier::AlphaNumeric(ref s)
if s == "dev" => Channel::Dev,
Identifier::AlphaNumeric(ref s)
if s == "beta" => Channel::Beta,
Identifier::AlphaNumeric(ref s)
if s == "nightly" => Channel::Nightly,
ref x => panic!("Unknown pre-release tag {}", x),
}
};
VersionMeta {
semver: semver,
commit_hash: commit_hash,
commit_date: commit_date,
build_date: build_date,
channel: channel,
host: host.into(),
short_version_string: short_version_string.into(),
}
}
pub fn version_matches(req: &str) -> bool {
VersionReq::parse(req).unwrap().matches(&version())
}
#[test]
fn smoketest() {
let v = version();
assert!(v.major >= 1);
let v = version_meta();
assert!(v.semver.major >= 1);
assert!(version_matches(">= 1.0.0"));
}
#[test]
#[should_panic(expected = "unexpected")]
fn parse_unexpected() {
version_meta_for(
"rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
binary: rustc
commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e
commit-date: 2015-05-13
rust-birthday: 2015-05-14
host: x86_64-unknown-linux-gnu
release: 1.0.0");
}
#[test]
fn parse_1_0_0() {
let version = version_meta_for(
"rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
binary: rustc
commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e
commit-date: 2015-05-13
build-date: 2015-05-14
host: x86_64-unknown-linux-gnu
release: 1.0.0");
assert_eq!(version.semver, Version::parse("1.0.0").unwrap());
assert_eq!(version.commit_hash, Some("a59de37e99060162a2674e3ff45409ac73595c0e".into()));
assert_eq!(version.commit_date, Some("2015-05-13".into()));
assert_eq!(version.build_date, Some("2015-05-14".into()));
assert_eq!(version.channel, Channel::Stable);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)");
}
#[test]
fn parse_unknown() {
let version = version_meta_for(
"rustc 1.3.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.3.0");
assert_eq!(version.semver, Version::parse("1.3.0").unwrap());
assert_eq!(version.commit_hash, None);
assert_eq!(version.commit_date, None);
assert_eq!(version.channel, Channel::Stable);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.3.0");
}
#[test]
fn parse_nightly() {
let version = version_meta_for(
"rustc 1.5.0-nightly (65d5c0833 2015-09-29)
binary: rustc
commit-hash: 65d5c083377645a115c4ac23a620d3581b9562b6
commit-date: 2015-09-29
host: x86_64-unknown-linux-gnu
release: 1.5.0-nightly");
assert_eq!(version.semver, Version::parse("1.5.0-nightly").unwrap());
assert_eq!(version.commit_hash, Some("65d5c083377645a115c4ac23a620d3581b9562b6".into()));
assert_eq!(version.commit_date, Some("2015-09-29".into()));
assert_eq!(version.channel, Channel::Nightly);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.5.0-nightly (65d5c0833 2015-09-29)");
}
#[test]
fn parse_stable() {
let version = version_meta_for(
"rustc 1.3.0 (9a92aaf19 2015-09-15)
binary: rustc
commit-hash: 9a92aaf19a64603b02b4130fe52958cc12488900
commit-date: 2015-09-15
host: x86_64-unknown-linux-gnu
release: 1.3.0");
assert_eq!(version.semver, Version::parse("1.3.0").unwrap());
assert_eq!(version.commit_hash, Some("9a92aaf19a64603b02b4130fe52958cc12488900".into()));
assert_eq!(version.commit_date, Some("2015-09-15".into()));
assert_eq!(version.channel, Channel::Stable);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.3.0 (9a92aaf19 2015-09-15)");
}