Skip to content

Commit

Permalink
Use build/tmp instead of adding a dependency on tempfile.
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Apr 25, 2022
1 parent 12b132d commit 7885ade
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 33 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ dependencies = [
"serde",
"serde_json",
"tar",
"tempfile",
"toml",
"winapi",
"xz2",
Expand Down
1 change: 0 additions & 1 deletion src/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ libc = "0.2"
serde = { version = "1.0.8", features = ["derive"] }
serde_json = "1.0.2"
tar = "0.4"
tempfile = "3"
toml = "0.5"
ignore = "0.4.10"
opener = "0.5"
Expand Down
23 changes: 8 additions & 15 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,21 +1391,14 @@ impl Build {
paths
}

pub fn rename(&self, src: &Path, dst: &Path) {
if self.config.dry_run {
return;
}
self.verbose_than(1, &format!("Move {:?} to {:?}", src, dst));
if src == dst {
return;
}
if let Err(e) = fs::rename(src, dst) {
if e.raw_os_error() == Some(libc::EXDEV) {
self.copy(src, dst);
return;
}
panic!("failed to rename `{}` to `{}`: {}", src.display(), dst.display(), e);
}
/// Create a temporary directory in `out` and return its path.
///
/// NOTE: this temporary directory is shared between all steps;
/// if you need an empty directory, create a new subdirectory inside it.
fn tempdir(&self) -> PathBuf {
let tmp = self.out.join("tmp");
t!(fs::create_dir_all(&tmp));
tmp
}

/// Copies a file from `src` to `dst`
Expand Down
16 changes: 7 additions & 9 deletions src/bootstrap/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,14 @@ fn fix_bin_or_dylib(builder: &Builder<'_>, fname: &Path) {

fn download_component(builder: &Builder<'_>, base: &str, url: &str, dest_path: &Path) {
// Use a temporary file in case we crash while downloading, to avoid a corrupt download in cache/.
let tempfile = t!(tempfile::NamedTempFile::new());
let temppath = tempfile.path().to_owned();
drop(tempfile);
let tempfile_str = temppath.to_str().expect("tempdir must be valid unicode");
let tempfile = builder.tempdir().join(dest_path.file_name().unwrap());
// FIXME: support `do_verify` (only really needed for nightly rustfmt)
download_with_retries(builder, tempfile_str, &format!("{}/{}", base, url));
builder.rename(&temppath, dest_path);
// FIXME: support non-utf8 paths?
download_with_retries(builder, tempfile.to_str().unwrap(), &format!("{}/{}", base, url));
t!(std::fs::rename(&tempfile, dest_path));
}

fn download_with_retries(builder: &Builder<'_>, tempdir: &str, url: &str) {
fn download_with_retries(builder: &Builder<'_>, tempfile: &str, url: &str) {
println!("downloading {}", url);

// FIXME: check if curl is installed instead of skipping straight to powershell
Expand All @@ -318,7 +316,7 @@ fn download_with_retries(builder: &Builder<'_>, tempdir: &str, url: &str) {
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",
&format!(
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
url, tempdir
url, tempfile
),
])) {
return;
Expand All @@ -338,7 +336,7 @@ fn download_with_retries(builder: &Builder<'_>, tempdir: &str, url: &str) {
"3",
"-Sf",
"-o",
tempdir,
tempfile,
url,
]));
}
Expand Down
11 changes: 4 additions & 7 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,9 +1577,7 @@ note: if you're sure you want to do this, please open an issue as to why. In the
cmd.env("RUSTC_PROFILER_SUPPORT", "1");
}

let tmp = builder.out.join("tmp");
std::fs::create_dir_all(&tmp).unwrap();
cmd.env("RUST_TEST_TMPDIR", tmp);
cmd.env("RUST_TEST_TMPDIR", builder.tempdir());

cmd.arg("--adb-path").arg("adb");
cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
Expand Down Expand Up @@ -2259,14 +2257,13 @@ impl Step for RemoteCopyLibs {
builder.ensure(compile::Std { compiler, target });

builder.info(&format!("REMOTE copy libs to emulator ({})", target));
t!(fs::create_dir_all(builder.out.join("tmp")));

let server = builder.ensure(tool::RemoteTestServer { compiler, target });

// Spawn the emulator and wait for it to come online
let tool = builder.tool_exe(Tool::RemoteTestClient);
let mut cmd = Command::new(&tool);
cmd.arg("spawn-emulator").arg(target.triple).arg(&server).arg(builder.out.join("tmp"));
cmd.arg("spawn-emulator").arg(target.triple).arg(&server).arg(builder.tempdir());
if let Some(rootfs) = builder.qemu_rootfs(target) {
cmd.arg(rootfs);
}
Expand Down Expand Up @@ -2300,7 +2297,7 @@ impl Step for Distcheck {
/// Runs "distcheck", a 'make check' from a tarball
fn run(self, builder: &Builder<'_>) {
builder.info("Distcheck");
let dir = builder.out.join("tmp").join("distcheck");
let dir = builder.tempdir().join("distcheck");
let _ = fs::remove_dir_all(&dir);
t!(fs::create_dir_all(&dir));

Expand All @@ -2326,7 +2323,7 @@ impl Step for Distcheck {

// Now make sure that rust-src has all of libstd's dependencies
builder.info("Distcheck rust-src");
let dir = builder.out.join("tmp").join("distcheck-src");
let dir = builder.tempdir().join("distcheck-src");
let _ = fs::remove_dir_all(&dir);
t!(fs::create_dir_all(&dir));

Expand Down

0 comments on commit 7885ade

Please sign in to comment.