Skip to content

Commit 5ccb263

Browse files
committed
Download rust-src component.
1 parent 3c4bc04 commit 5ccb263

File tree

1 file changed

+40
-71
lines changed

1 file changed

+40
-71
lines changed

collector/src/bin/rustc-perf-collector/sysroot.rs

+40-71
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use anyhow::{anyhow, Context};
22
use chrono::{DateTime, Utc};
33
use collector::Sha;
4-
use std::ffi::OsStr;
54
use std::fmt;
65
use std::fs::{self, File};
76
use std::io::{BufReader, Read};
8-
use std::path::{Path, PathBuf};
7+
use std::path::PathBuf;
98
use tar::Archive;
109
use xz2::bufread::XzDecoder;
1110

@@ -42,10 +41,11 @@ impl Sysroot {
4241
download.get_and_extract(ModuleVariant::Rustc)?;
4342
download.get_and_extract(ModuleVariant::Std)?;
4443
download.get_and_extract(ModuleVariant::Cargo)?;
44+
download.get_and_extract(ModuleVariant::RustSrc)?;
4545

4646
download.into_sysroot()
47+
}
4748
}
48-
}
4949

5050
impl Drop for Sysroot {
5151
fn drop(&mut self) {
@@ -66,14 +66,15 @@ struct SysrootDownload {
6666
triple: String,
6767
}
6868

69-
const MODULE_URL: &str =
70-
"https://rust-lang-ci2.s3.amazonaws.com/rustc-builds/@SHA@/@MODULE@-nightly-@[email protected]";
69+
const BASE_URL: &str = "https://rust-lang-ci2.s3.amazonaws.com/rustc-builds";
7170

71+
// FIXME(eddyb) rename to just `Component`.
7272
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
7373
enum ModuleVariant {
7474
Cargo,
7575
Rustc,
7676
Std,
77+
RustSrc,
7778
}
7879

7980
impl fmt::Display for ModuleVariant {
@@ -82,47 +83,41 @@ impl fmt::Display for ModuleVariant {
8283
ModuleVariant::Cargo => write!(f, "cargo"),
8384
ModuleVariant::Rustc => write!(f, "rustc"),
8485
ModuleVariant::Std => write!(f, "rust-std"),
86+
ModuleVariant::RustSrc => write!(f, "rust-src"),
8587
}
8688
}
8789
}
8890

8991
impl ModuleVariant {
9092
fn url(&self, sysroot: &SysrootDownload, triple: &str) -> String {
91-
MODULE_URL
92-
.replace("@MODULE@", &self.to_string())
93-
.replace("@SHA@", &sysroot.rust_sha)
94-
.replace("@TRIPLE@", triple)
93+
let suffix = if *self == ModuleVariant::RustSrc {
94+
String::new()
95+
} else {
96+
format!("-{}", triple)
97+
};
98+
format!(
99+
"{base}/{sha}/{module}-nightly{suffix}.tar.xz",
100+
base = BASE_URL,
101+
module = self,
102+
sha = sysroot.rust_sha,
103+
suffix = suffix,
104+
)
95105
}
96106
}
97107

98108
impl SysrootDownload {
99109
fn into_sysroot(self) -> anyhow::Result<Sysroot> {
110+
let sysroot_dir = self.directory.join(&self.rust_sha);
111+
let sysroot_dir = sysroot_dir.canonicalize().with_context(|| {
112+
format!(
113+
"failed to canonicalize sysroot path for {}: {:?}",
114+
self.rust_sha, sysroot_dir
115+
)
116+
})?;
100117
Ok(Sysroot {
101-
rustc: self
102-
.directory
103-
.join(&self.rust_sha)
104-
.join("rustc/bin/rustc")
105-
.canonicalize()
106-
.with_context(|| {
107-
format!("failed to canonicalize rustc path for {}", self.rust_sha)
108-
})?,
109-
rustdoc: self
110-
.directory
111-
.join(&self.rust_sha)
112-
.join("rustc/bin/rustdoc")
113-
.canonicalize()
114-
.with_context(|| {
115-
format!("failed to canonicalize rustdoc path for {}", self.rust_sha)
116-
})?,
117-
cargo: {
118-
let path = self.directory.join(&self.rust_sha).join("cargo/bin/cargo");
119-
path.canonicalize().with_context(|| {
120-
format!(
121-
"failed to canonicalize cargo path for {}: {:?}",
122-
self.rust_sha, path
123-
)
124-
})?
125-
},
118+
rustc: sysroot_dir.join("bin/rustc"),
119+
rustdoc: sysroot_dir.join("bin/rustdoc"),
120+
cargo: sysroot_dir.join("bin/cargo"),
126121
sha: self.rust_sha,
127122
triple: self.triple,
128123
})
@@ -161,19 +156,21 @@ impl SysrootDownload {
161156
}
162157

163158
return Err(anyhow!(
164-
"unable to download sha {} triple {} module {}",
159+
"unable to download sha {} triple {} module {} from {}",
165160
self.rust_sha,
166161
self.triple,
167-
variant
162+
variant,
163+
url
168164
));
169165
}
170166

171167
fn extract<T: Read>(&self, variant: ModuleVariant, reader: T) -> anyhow::Result<()> {
172-
let is_std = variant == ModuleVariant::Std;
173168
let mut archive = Archive::new(reader);
174-
let std_prefix = format!("rust-std-{}/lib/rustlib", self.triple);
175-
176-
let mut to_link = Vec::new();
169+
let prefix = if variant == ModuleVariant::Std {
170+
format!("rust-std-{}", self.triple)
171+
} else {
172+
variant.to_string()
173+
};
177174

178175
let unpack_into = self.directory.join(&self.rust_sha);
179176

@@ -184,21 +181,11 @@ impl SysrootDownload {
184181
assert!(components.next().is_some(), "strip container directory");
185182
let path = components.as_path();
186183

187-
let path = if is_std {
188-
if let Ok(path) = path.strip_prefix(&std_prefix) {
189-
if path.extension() == Some(OsStr::new("dylib")) {
190-
to_link.push(path.to_owned());
191-
continue;
192-
} else {
193-
Path::new("rustc/lib/rustlib").join(path)
194-
}
195-
} else {
196-
continue;
197-
}
184+
let path = if let Ok(path) = path.strip_prefix(&prefix) {
185+
unpack_into.join(path)
198186
} else {
199-
path.into()
187+
continue;
200188
};
201-
let path = unpack_into.join(path);
202189
fs::create_dir_all(&path.parent().unwrap()).with_context(|| {
203190
format!(
204191
"could not create intermediate directories for {}",
@@ -208,24 +195,6 @@ impl SysrootDownload {
208195
entry.unpack(path)?;
209196
}
210197

211-
let link_dst_prefix = unpack_into.join(format!("rustc/lib/rustlib/{}/lib", self.triple));
212-
let link_src_prefix = format!("{}/lib", self.triple);
213-
for path in to_link {
214-
let src = unpack_into.join("rustc/lib").join(
215-
path.strip_prefix(&link_src_prefix)
216-
.with_context(|| format!("stripping prefix from: {:?}", path))?,
217-
);
218-
let dst = link_dst_prefix.join(&path);
219-
fs::create_dir_all(&dst.parent().unwrap()).with_context(|| {
220-
format!(
221-
"could not create intermediate directories for {}",
222-
dst.display()
223-
)
224-
})?;
225-
log::trace!("linking {} to {}", src.display(), dst.display());
226-
fs::hard_link(src, dst)?;
227-
}
228-
229198
Ok(())
230199
}
231200
}

0 commit comments

Comments
 (0)