Skip to content

Commit 3309b37

Browse files
authored
Merge pull request #325 from dtolnay/outdir
Clean up dep-info files from OUT_DIR
2 parents a72ea77 + f563b1d commit 3309b37

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

build.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::env;
22
use std::ffi::OsString;
3+
use std::fs;
4+
use std::io::ErrorKind;
35
use std::iter;
46
use std::path::Path;
57
use std::process::{self, Command, Stdio};
@@ -68,8 +70,16 @@ fn compile_probe(rustc_bootstrap: bool) -> bool {
6870

6971
let rustc = cargo_env_var("RUSTC");
7072
let out_dir = cargo_env_var("OUT_DIR");
73+
let out_subdir = Path::new(&out_dir).join("probe");
7174
let probefile = Path::new("build").join("probe.rs");
7275

76+
if let Err(err) = fs::create_dir(&out_subdir) {
77+
if err.kind() != ErrorKind::AlreadyExists {
78+
eprintln!("Failed to create {}: {}", out_subdir.display(), err);
79+
process::exit(1);
80+
}
81+
}
82+
7383
let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty());
7484
let rustc_workspace_wrapper =
7585
env::var_os("RUSTC_WORKSPACE_WRAPPER").filter(|wrapper| !wrapper.is_empty());
@@ -91,7 +101,7 @@ fn compile_probe(rustc_bootstrap: bool) -> bool {
91101
.arg("--cap-lints=allow")
92102
.arg("--emit=dep-info,metadata")
93103
.arg("--out-dir")
94-
.arg(out_dir)
104+
.arg(&out_subdir)
95105
.arg(probefile);
96106

97107
if let Some(target) = env::var_os("TARGET") {
@@ -107,10 +117,22 @@ fn compile_probe(rustc_bootstrap: bool) -> bool {
107117
}
108118
}
109119

110-
match cmd.status() {
120+
let success = match cmd.status() {
111121
Ok(status) => status.success(),
112122
Err(_) => false,
123+
};
124+
125+
// Clean up to avoid leaving nondeterministic absolute paths in the dep-info
126+
// file in OUT_DIR, which causes nonreproducible builds in build systems
127+
// that treat the entire OUT_DIR as an artifact.
128+
if let Err(err) = fs::remove_dir_all(&out_subdir) {
129+
if err.kind() != ErrorKind::NotFound {
130+
eprintln!("Failed to clean up {}: {}", out_subdir.display(), err);
131+
process::exit(1);
132+
}
113133
}
134+
135+
success
114136
}
115137

116138
fn cargo_env_var(key: &str) -> OsString {

0 commit comments

Comments
 (0)