From 3e8962cc14b0af47f0b07beeab2a560447f969f4 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 22 Oct 2024 09:52:33 -0700 Subject: [PATCH] Clean up dep-info files from OUT_DIR --- build.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 1d3ae17..bced1e0 100644 --- a/build.rs +++ b/build.rs @@ -3,6 +3,8 @@ use std::env; use std::ffi::OsString; +use std::fs; +use std::io::ErrorKind; use std::iter; use std::path::Path; use std::process::{self, Command, Stdio}; @@ -146,8 +148,16 @@ fn compile_probe(rustc_bootstrap: bool) -> bool { let rustc = cargo_env_var("RUSTC"); let out_dir = cargo_env_var("OUT_DIR"); + let out_subdir = Path::new(&out_dir).join("probe"); let probefile = Path::new("build").join("probe.rs"); + if let Err(err) = fs::create_dir(&out_subdir) { + if err.kind() != ErrorKind::AlreadyExists { + eprintln!("Failed to create {}: {}", out_subdir.display(), err); + process::exit(1); + } + } + let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty()); let rustc_workspace_wrapper = env::var_os("RUSTC_WORKSPACE_WRAPPER").filter(|wrapper| !wrapper.is_empty()); @@ -169,7 +179,7 @@ fn compile_probe(rustc_bootstrap: bool) -> bool { .arg("--cap-lints=allow") .arg("--emit=dep-info,metadata") .arg("--out-dir") - .arg(out_dir) + .arg(&out_subdir) .arg(probefile); if let Some(target) = env::var_os("TARGET") { @@ -185,10 +195,22 @@ fn compile_probe(rustc_bootstrap: bool) -> bool { } } - match cmd.status() { + let success = match cmd.status() { Ok(status) => status.success(), Err(_) => false, + }; + + // Clean up to avoid leaving nondeterministic absolute paths in the dep-info + // file in OUT_DIR, which causes nonreproducible builds in build systems + // that treat the entire OUT_DIR as an artifact. + if let Err(err) = fs::remove_dir_all(&out_subdir) { + if err.kind() != ErrorKind::NotFound { + eprintln!("Failed to clean up {}: {}", out_subdir.display(), err); + process::exit(1); + } } + + success } fn rustc_minor_version() -> Option {