This repository has been archived by the owner on Feb 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsysroot.rs
349 lines (305 loc) · 12.3 KB
/
sysroot.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
//! Download and manage sysroots.
use std::env;
use std::fmt;
use std::fs::{self, File};
use std::io::{self, BufRead, Read, BufReader};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::ffi::OsStr;
use chrono::{TimeZone, Utc};
use flate2::bufread::GzDecoder;
use xz2::bufread::XzDecoder;
use reqwest;
use tar::Archive;
use git::Commit;
use errors::{Result, ResultExt};
pub struct Sysroot {
pub sha: String,
pub rustc: PathBuf,
pub rustdoc: PathBuf,
pub cargo: PathBuf,
pub triple: String,
pub preserve: bool,
pub used_fallback_cargo: bool,
pub is_saving_sysroot: bool,
}
impl Sysroot {
// if running with cargo run, we want to avoid things like CARGO_INCREMENTAL
// sneaking into the command's environment, but we do need the PATH to
// find linkers and other things that cargo and rust needs.
pub fn command<P: AsRef<Path>>(&self, path: P) -> Command {
let mut command = Command::new(path.as_ref().as_os_str());
command
.env_clear()
.env("PATH", env::var("PATH").unwrap_or_default())
.env("CARGO", &self.cargo)
.env("CARGO_RELATIVE", &self.cargo.strip_prefix(&env::current_dir().unwrap()).unwrap())
.env("RUSTC", &self.rustc)
.env("RUSTC_RELATIVE", &self.rustc.strip_prefix(&env::current_dir().unwrap()).unwrap())
.env("RUSTDOC", &self.rustdoc)
.env("RUSTDOC_RELATIVE", &self.rustdoc.strip_prefix(&env::current_dir().unwrap()).unwrap());
command
}
pub fn with_local_rustc(commit: &Commit, rustc: &str, triple: &str, preserve: bool, is_saving_sysroot: bool) -> Result<Self> {
let sha: &str = &commit.sha;
let unpack_into = format!("cache");
let mut used_fallback_cargo = false;
let cargo_sha = if commit.date < Utc.ymd(2017, 3, 20).and_hms(0, 0, 0) {
// Versions of rustc older than Mar 20 have bugs in
// their cargo. Use a known-good cargo for older rustcs
// instead.
used_fallback_cargo = true;
// get master commit for known-good cargo
::get_commits(::EPOCH_COMMIT, "master")?.pop().unwrap().sha
} else {
sha.to_string()
};
fs::create_dir_all(&unpack_into)?;
let download = SysrootDownload {
directory: unpack_into.into(),
save_download: preserve,
rust_sha: sha.to_string(),
cargo_sha: cargo_sha,
triple: triple.to_string(),
};
download.get_and_extract("cargo")?;
Ok(Sysroot {
rustc: PathBuf::from(rustc).canonicalize()
.chain_err(|| format!("failed to canonicalize rustc path: {}", rustc))?,
rustdoc: PathBuf::from(rustc).canonicalize()
.chain_err(|| format!("failed to canonicalize rustc path: {}", rustc))?
.parent().unwrap().join("rustdoc"),
cargo: download.directory.join(&download.rust_sha).join("cargo/bin/cargo").canonicalize()
.chain_err(|| format!("failed to canonicalize cargo path for {}", download.cargo_sha))?,
sha: download.rust_sha,
preserve: download.save_download,
triple: download.triple,
used_fallback_cargo,
is_saving_sysroot,
})
}
pub fn install(commit: &Commit, triple: &str, preserve: bool, is_saving_sysroot: bool) -> Result<Self> {
let sha: &str = &commit.sha;
let unpack_into = format!("cache");
let mut used_fallback_cargo = false;
let cargo_sha = if commit.date < Utc.ymd(2017, 3, 20).and_hms(0, 0, 0) {
// Versions of rustc older than Mar 20 have bugs in
// their cargo. Use a known-good cargo for older rustcs
// instead.
used_fallback_cargo = true;
// get master commit for known-good cargo
::get_commits(::EPOCH_COMMIT, "master")?.pop().unwrap().sha
} else {
sha.to_string()
};
fs::create_dir_all(&unpack_into)?;
let download = SysrootDownload {
directory: unpack_into.into(),
save_download: preserve,
rust_sha: sha.to_string(),
cargo_sha: cargo_sha,
triple: triple.to_string(),
};
download.get_and_extract("rustc")?;
download.get_and_extract("rust-std")?;
download.get_and_extract("cargo")?;
download.into_sysroot(used_fallback_cargo, is_saving_sysroot)
}
}
impl Drop for Sysroot {
fn drop(&mut self) {
if !self.is_saving_sysroot {
fs::remove_dir_all(format!("cache/{}", self.sha)).unwrap_or_else(|err| {
info!("failed to remove {:?}, please do so manually: {:?}",
format!("cache/{}", self.sha), err);
});
}
}
}
#[derive(Debug, Clone)]
struct SysrootDownload {
directory: PathBuf,
save_download: bool,
rust_sha: String,
cargo_sha: String,
triple: String,
}
const MODULE_URLS: &[&str] = &[
"https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rustc-builds/@SHA@/@MODULE@-nightly-@[email protected]",
"https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rustc-builds-try/@SHA@/@MODULE@-nightly-@[email protected]",
];
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum ModuleVariant {
Cargo,
Rustc,
Std
}
impl fmt::Display for ModuleVariant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ModuleVariant::Cargo => write!(f, "cargo"),
ModuleVariant::Rustc => write!(f, "rustc"),
ModuleVariant::Std => write!(f, "rust-std"),
}
}
}
#[derive(Debug, Copy, Clone)]
struct Module<'a> {
variant: ModuleVariant,
sysroot: &'a SysrootDownload,
}
impl<'a> Module<'a> {
fn sha(&self) -> &str {
match self.variant {
ModuleVariant::Cargo => &self.sysroot.cargo_sha,
_ => &self.sysroot.rust_sha,
}
}
fn urls(&self) -> Vec<String> {
MODULE_URLS.iter().map(|url| {
url.replace("@MODULE@", &self.variant.to_string())
.replace("@SHA@", self.sha())
.replace("@TRIPLE@", &self.sysroot.triple)
}).collect()
}
fn decompress<'b, R: BufRead + 'b>(&self, reader: R, extension: &str) -> Result<Box<Read + 'b>> {
if extension == "gz" {
Ok(Box::new(GzDecoder::new(reader)?))
} else if extension == "xz" {
Ok(Box::new(XzDecoder::new(reader)))
} else {
bail!("unknown extension {}", extension);
}
}
fn get(&self) -> Result<()> {
let archive_path = |extension| {
self.sysroot.directory.join(format!("{}-{}-{}.tar.{}",
self.sha(), self.sysroot.triple, self.variant, extension))
};
for &extension in &["xz", "gz"] {
let archive_path = archive_path(extension);
let reader = if archive_path.exists() {
BufReader::new(File::open(&archive_path)?)
} else {
continue;
};
match self.decompress(reader, extension)
.and_then(|reader| self.sysroot.extract(self, reader)) {
Ok(()) => return Ok(()),
Err(err) => {
warn!("extracting {} failed: {:?}", archive_path.display(), err);
fs::remove_file(archive_path)?;
continue;
}
}
}
for url in self.urls() {
let extension = if url.ends_with("gz") { "gz" } else { "xz" };
debug!("requesting: {}", url);
let resp = reqwest::get(&url)?;
debug!("{}", resp.status());
let mut reader = if resp.status().is_success() {
BufReader::new(resp)
} else {
continue;
};
let archive_path = archive_path(extension);
let reader: Box<BufRead> = if self.sysroot.save_download && !archive_path.exists() {
let mut file = File::create(&archive_path)?;
io::copy(&mut reader, &mut file)?;
Box::new(BufReader::new(File::open(&archive_path)?))
} else {
Box::new(reader)
};
match self.decompress(reader, extension)
.and_then(|reader| self.sysroot.extract(self, reader)) {
Ok(()) => return Ok(()),
Err(err) => {
warn!("extracting {} failed: {:?}", url, err);
if self.sysroot.save_download {
fs::remove_file(archive_path)?;
}
continue;
}
}
}
bail!("unable to download sha {} triple {} module {}",
self.sha(), self.sysroot.triple, self.variant);
}
}
impl SysrootDownload {
fn into_sysroot(self, used_fallback_cargo: bool, is_saving_sysroot: bool) -> Result<Sysroot> {
Ok(Sysroot {
rustc: self.directory.join(&self.rust_sha).join("rustc/bin/rustc").canonicalize()
.chain_err(|| format!("failed to canonicalize rustc path for {}", self.rust_sha))?,
rustdoc: self.directory.join(&self.rust_sha).join("rustc/bin/rustdoc").canonicalize()
.chain_err(|| format!("failed to canonicalize rustdoc path for {}", self.rust_sha))?,
cargo: self.directory.join(&self.rust_sha).join("cargo/bin/cargo").canonicalize()
.chain_err(|| format!("failed to canonicalize cargo path for {}", self.cargo_sha))?,
sha: self.rust_sha,
preserve: self.save_download,
triple: self.triple,
used_fallback_cargo,
is_saving_sysroot,
})
}
fn get_module(&self, module: &str) -> Result<()> {
Module {
variant: match module {
"cargo" => ModuleVariant::Cargo,
"rustc" => ModuleVariant::Rustc,
"rust-std" => ModuleVariant::Std,
_ => panic!("unknown module variant: {}", module),
},
sysroot: self,
}.get()
}
fn get_and_extract(&self, module: &str) -> Result<()> {
self.get_module(module)
}
fn extract(&self, module: &Module, reader: Box<Read>) -> Result<()> {
let is_std = module.variant == ModuleVariant::Std;
let mut archive = Archive::new(reader);
let std_prefix = format!("rust-std-{}/lib/rustlib", self.triple);
let mut to_link = Vec::new();
let unpack_into = self.directory.join(&self.rust_sha);
for entry in archive.entries()? {
let mut entry = entry?;
let path = entry.path()?.into_owned();
let mut components = path.components();
assert!(components.next().is_some(), "strip container directory");
let path = components.as_path();
let path = if is_std {
if let Ok(path) = path.strip_prefix(&std_prefix) {
if path.extension() == Some(OsStr::new("dylib")) {
to_link.push(path.to_owned());
continue;
} else {
Path::new("rustc/lib/rustlib").join(path)
}
} else {
continue;
}
} else {
path.into()
};
let path = unpack_into.join(path);
fs::create_dir_all(&path.parent().unwrap())
.chain_err(|| format!("could not create intermediate directories for {}",
path.display()))?;
entry.unpack(path)?;
}
let link_dst_prefix = unpack_into.join(format!("rustc/lib/rustlib/{}/lib", self.triple));
let link_src_prefix = format!("{}/lib", self.triple);
for path in to_link {
let src = unpack_into.join("rustc/lib").join(path.strip_prefix(&link_src_prefix)
.chain_err(|| format!("stripping prefix from: {:?}", path))?);
let dst = link_dst_prefix.join(&path);
fs::create_dir_all(&dst.parent().unwrap())
.chain_err(|| format!("could not create intermediate directories for {}", dst.display()))?;
debug!("linking {} to {}", src.display(), dst.display());
fs::hard_link(src, dst)?;
}
Ok(())
}
}