-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathrustc.rs
406 lines (367 loc) · 12.5 KB
/
rustc.rs
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::path::{Path, PathBuf};
use std::process::Command;
use rustc_version::{Version, VersionMeta};
use serde::Deserialize;
use crate::docker::ImagePlatform;
use crate::errors::*;
use crate::extensions::{env_program, CommandExt};
use crate::shell::MessageInfo;
use crate::TargetTriple;
#[derive(Debug)]
pub struct TargetList {
pub triples: Vec<String>,
}
impl TargetList {
#[must_use]
pub fn contains(&self, triple: &str) -> bool {
self.triples.iter().any(|t| t == triple)
}
}
pub trait VersionMetaExt {
fn host(&self) -> TargetTriple;
fn needs_interpreter(&self) -> bool;
fn commit_hash(&self) -> String;
}
impl VersionMetaExt for VersionMeta {
fn host(&self) -> TargetTriple {
TargetTriple::from(&*self.host)
}
fn needs_interpreter(&self) -> bool {
self.semver < Version::new(1, 19, 0)
}
fn commit_hash(&self) -> String {
self.commit_hash.as_ref().map_or_else(
|| hash_from_version_string(&self.short_version_string, 2),
|x| short_commit_hash(x),
)
}
}
fn short_commit_hash(hash: &str) -> String {
// short version hashes are always 9 digits
// https://github.com/rust-lang/cargo/pull/10579
const LENGTH: usize = 9;
hash.get(..LENGTH)
.unwrap_or_else(|| panic!("commit hash must be at least {LENGTH} characters long"))
.to_owned()
}
#[must_use]
pub fn hash_from_version_string(version: &str, index: usize) -> String {
let is_hash = |x: &str| x.chars().all(|c| c.is_ascii_hexdigit());
let is_date = |x: &str| x.chars().all(|c| matches!(c, '-' | '0'..='9'));
// the version can be one of two forms:
// multirust channel string: `"1.61.0 (fe5b13d68 2022-05-18)"`
// short version string: `"rustc 1.61.0 (fe5b13d68 2022-05-18)"`
// want to extract the commit hash if we can, if not, just hash the string.
if let Some((commit, date)) = version
.splitn(index + 1, ' ')
.nth(index)
.and_then(|meta| meta.strip_prefix('('))
.and_then(|meta| meta.strip_suffix(')'))
.and_then(|meta| meta.split_once(' '))
{
if is_hash(commit) && is_date(date) {
return short_commit_hash(commit);
}
}
// fallback: can't extract the hash. just create a hash of the version string.
let buffer = const_sha1::ConstBuffer::from_slice(version.as_bytes());
short_commit_hash(&const_sha1::sha1(&buffer).to_string())
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct QualifiedToolchain {
pub channel: String,
pub date: Option<String>,
pub(self) host: ImagePlatform,
pub is_custom: bool,
pub full: String,
pub(self) sysroot: PathBuf,
}
impl QualifiedToolchain {
pub fn new(
channel: &str,
date: &Option<String>,
host: &ImagePlatform,
sysroot: &Path,
is_custom: bool,
) -> Self {
let mut this = Self {
channel: channel.to_owned(),
date: date.clone(),
host: host.clone(),
is_custom,
full: if let Some(date) = date {
format!("{}-{}-{}", channel, date, host.target)
} else {
format!("{}-{}", channel, host.target)
},
sysroot: sysroot.to_owned(),
};
if !is_custom {
this.sysroot.set_file_name(&this.full);
}
this
}
/// Replace the host, does nothing if ran on a custom toolchain
pub fn replace_host(&mut self, host: &ImagePlatform) -> &mut Self {
if !self.is_custom {
*self = Self::new(&self.channel, &self.date, host, &self.sysroot, false);
self.sysroot.set_file_name(&self.full);
}
self
}
/// Makes a good guess as to what the toolchain is compiled to run on.
pub(crate) fn custom(
name: &str,
sysroot: &Path,
config: &crate::config::Config,
msg_info: &mut MessageInfo,
) -> Result<QualifiedToolchain> {
if let Some(compat) = config.custom_toolchain_compat() {
let mut toolchain: QualifiedToolchain = QualifiedToolchain::parse(
sysroot.to_owned(),
&compat,
config,
msg_info,
)
.wrap_err(
"could not parse CROSS_CUSTOM_TOOLCHAIN_COMPAT as a fully qualified toolchain name",
)?;
toolchain.is_custom = true;
toolchain.full = name.to_owned();
return Ok(toolchain);
}
// a toolchain installed by https://github.com/rust-lang/cargo-bisect-rustc
if name.starts_with("bisector-nightly") {
let (_, toolchain) = name.split_once('-').expect("should include -");
let mut toolchain =
QualifiedToolchain::parse(sysroot.to_owned(), toolchain, config, msg_info)
.wrap_err("could not parse bisector toolchain")?;
toolchain.is_custom = true;
toolchain.full = name.to_owned();
return Ok(toolchain);
} else if let Ok(stdout) = Command::new(sysroot.join("bin/rustc"))
.arg("-Vv")
.run_and_get_stdout(msg_info)
{
let rustc_version::VersionMeta {
build_date,
channel,
host,
..
} = rustc_version::version_meta_for(&stdout)?;
let mut toolchain = QualifiedToolchain::new(
match channel {
rustc_version::Channel::Dev => "dev",
rustc_version::Channel::Nightly => "nightly",
rustc_version::Channel::Beta => "beta",
rustc_version::Channel::Stable => "stable",
},
&build_date,
&ImagePlatform::from_target(host.into())?,
sysroot,
true,
);
toolchain.full = name.to_owned();
return Ok(toolchain);
}
Err(eyre::eyre!(
"cross can not figure out what your custom toolchain is"
))
.suggestion("set `CROSS_CUSTOM_TOOLCHAIN_COMPAT` to a fully qualified toolchain name: i.e `nightly-aarch64-unknown-linux-musl`")
}
pub fn host(&self) -> &ImagePlatform {
&self.host
}
pub fn get_sysroot(&self) -> &Path {
&self.sysroot
}
/// Grab the current default toolchain
pub fn default(config: &crate::config::Config, msg_info: &mut MessageInfo) -> Result<Self> {
let sysroot = sysroot(msg_info)?;
let default_toolchain_name = sysroot
.file_name()
.ok_or_else(|| eyre::eyre!("couldn't get name of active toolchain"))?
.to_str()
.ok_or_else(|| eyre::eyre!("toolchain was not utf-8"))?;
if !config.custom_toolchain() {
QualifiedToolchain::parse(sysroot.clone(), default_toolchain_name, config, msg_info)
} else {
QualifiedToolchain::custom(default_toolchain_name, &sysroot, config, msg_info)
}
}
/// Merge a "picked" toolchain, overriding set fields.
pub fn with_picked(self, picked: Toolchain) -> Result<Self> {
let date = picked.date.or(self.date);
let host = picked
.host
.map_or(Ok(self.host), ImagePlatform::from_target)?;
let channel = picked.channel;
Ok(QualifiedToolchain::new(
&channel,
&date,
&host,
&self.sysroot,
false,
))
}
pub fn set_sysroot(&mut self, convert: impl Fn(&Path) -> PathBuf) {
self.sysroot = convert(&self.sysroot);
}
}
impl std::fmt::Display for QualifiedToolchain {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.full)
}
}
impl QualifiedToolchain {
fn parse(
sysroot: PathBuf,
toolchain: &str,
config: &crate::config::Config,
msg_info: &mut MessageInfo,
) -> Result<Self> {
match toolchain.parse::<Toolchain>() {
Ok(Toolchain {
channel,
date,
host: Some(host),
is_custom,
full,
}) => Ok(QualifiedToolchain {
channel,
date,
host: ImagePlatform::from_target(host)?,
is_custom,
full,
sysroot,
}),
Ok(_) | Err(_) if config.custom_toolchain() => {
QualifiedToolchain::custom(toolchain, &sysroot, config, msg_info)
}
Ok(_) => Err(eyre::eyre!("toolchain is not fully qualified")
.with_note(|| "cross expects the toolchain to be a rustup installed toolchain")
.with_suggestion(|| {
"if you're using a custom toolchain try setting `CROSS_CUSTOM_TOOLCHAIN=1` or install rust via rustup"
})),
Err(e) => Err(e),
}
}
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Toolchain {
pub channel: String,
pub date: Option<String>,
pub host: Option<TargetTriple>,
pub is_custom: bool,
pub full: String,
}
impl Toolchain {
pub fn remove_host(&self) -> Self {
let mut new = Self {
host: None,
..self.clone()
};
if let Some(host) = &self.host {
new.full = new.full.replace(&format!("-{host}"), "");
}
new
}
}
impl std::fmt::Display for Toolchain {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.full)
}
}
impl std::str::FromStr for Toolchain {
type Err = eyre::Report;
fn from_str(s: &str) -> Result<Self, Self::Err> {
fn dig(s: &str) -> bool {
s.chars().all(|c: char| c.is_ascii_digit())
}
if let Some((channel, parts)) = s.split_once('-') {
if parts.starts_with(|c: char| c.is_ascii_digit()) {
// a date, YYYY-MM-DD
let mut split = parts.splitn(4, '-');
let ymd = [split.next(), split.next(), split.next()];
let ymd = match ymd {
[Some(y), Some(m), Some(d)] if dig(y) && dig(m) && dig(d) => {
format!("{y}-{m}-{d}")
}
_ => eyre::bail!("invalid toolchain `{s}`"),
};
Ok(Toolchain {
channel: channel.to_owned(),
date: Some(ymd),
host: split.next().map(|s| s.into()),
is_custom: false,
full: s.to_owned(),
})
} else {
// channel-host
Ok(Toolchain {
channel: channel.to_owned(),
date: None,
host: Some(parts.into()),
is_custom: false,
full: s.to_owned(),
})
}
} else {
Ok(Toolchain {
channel: s.to_owned(),
date: None,
host: None,
is_custom: false,
full: s.to_owned(),
})
}
}
}
#[must_use]
pub fn rustc_command() -> Command {
Command::new(env_program("RUSTC", "rustc"))
}
pub fn target_list(msg_info: &mut MessageInfo) -> Result<TargetList> {
rustc_command()
.args(["--print", "target-list"])
.run_and_get_stdout(msg_info)
.map(|s| TargetList {
triples: s.lines().map(|l| l.to_owned()).collect(),
})
}
pub fn sysroot(msg_info: &mut MessageInfo) -> Result<PathBuf> {
let stdout = rustc_command()
.args(["--print", "sysroot"])
.run_and_get_stdout(msg_info)?
.trim()
.to_owned();
Ok(PathBuf::from(stdout))
}
pub fn version_meta() -> Result<rustc_version::VersionMeta> {
rustc_version::version_meta().wrap_err("couldn't fetch the `rustc` version")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bisect() {
QualifiedToolchain::custom(
"bisector-nightly-2022-04-26-x86_64-unknown-linux-gnu",
"/tmp/cross/sysroot".as_ref(),
&crate::config::Config::new(None),
&mut MessageInfo::create(2, false, None).unwrap(),
)
.unwrap();
}
#[test]
fn hash_from_rustc() {
assert_eq!(
hash_from_version_string("1.61.0 (fe5b13d68 2022-05-18)", 1),
"fe5b13d68"
);
assert_eq!(
hash_from_version_string("rustc 1.61.0 (fe5b13d68 2022-05-18)", 2),
"fe5b13d68"
);
}
}