Skip to content

Commit 2285c8c

Browse files
committed
Address some rustc inconsistency issues
We noticed when building rustc multiple time in a roll, some files will not be consistent across the build despite the fact that they are built from same source under the same environment. This patch addresses the inconsistency issue we found on libunwind.a by sorting the order of the files passed to the linker. This patch also adds a passthrough of "SOURCE_DATE_EPOCH" env arg from bootstrap to cargo to partially address the inconsistency on the cargo binary.
1 parent 1c6de21 commit 2285c8c

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,11 +2228,20 @@ impl Step for Assemble {
22282228
let rustc_libdir = builder.rustc_libdir(target_compiler);
22292229
t!(fs::create_dir_all(&rustc_libdir));
22302230
let src_libdir = builder.sysroot_target_libdir(build_compiler, host);
2231-
for f in builder.read_dir(&src_libdir) {
2232-
let filename = f.file_name().into_string().unwrap();
2231+
let mut files = builder.read_dir(&src_libdir)
2232+
.map(|entry| {
2233+
entry
2234+
.path()
2235+
.canonicalize()
2236+
.unwrap()
2237+
})
2238+
.collect::<Vec<_>>();
2239+
files.sort();
2240+
for f in files {
2241+
let filename = f.file_name().unwrap().to_os_string().into_string().unwrap();
22332242

22342243
let is_proc_macro = proc_macros.contains(&filename);
2235-
let is_dylib_or_debug = is_dylib(&f.path()) || is_debug_info(&filename);
2244+
let is_dylib_or_debug = is_dylib(&f) || is_debug_info(&filename);
22362245

22372246
// If we link statically to stdlib, do not copy the libstd dynamic library file
22382247
// FIXME: Also do this for Windows once incremental post-optimization stage0 tests
@@ -2248,7 +2257,7 @@ impl Step for Assemble {
22482257
};
22492258

22502259
if is_dylib_or_debug && can_be_rustc_dynamic_dep && !is_proc_macro {
2251-
builder.copy_link(&f.path(), &rustc_libdir.join(&filename), FileType::Regular);
2260+
builder.copy_link(&f, &rustc_libdir.join(&filename), FileType::Regular);
22522261
}
22532262
}
22542263

@@ -2376,15 +2385,19 @@ pub fn run_cargo(
23762385
let mut deps = Vec::new();
23772386
let mut toplevel = Vec::new();
23782387
let ok = stream_cargo(builder, cargo, tail_args, &mut |msg| {
2379-
let (filenames, crate_types) = match msg {
2388+
let (filenames_vec, crate_types) = match msg {
23802389
CargoMessage::CompilerArtifact {
23812390
filenames,
23822391
target: CargoTarget { crate_types },
23832392
..
2384-
} => (filenames, crate_types),
2393+
} => {
2394+
let mut f: Vec<String> = filenames.into_iter().map(|s| s.into_owned()).collect();
2395+
f.sort(); // Sort the filenames
2396+
(f, crate_types)
2397+
},
23852398
_ => return,
23862399
};
2387-
for filename in filenames {
2400+
for filename in filenames_vec {
23882401
// Skip files like executables
23892402
let mut keep = false;
23902403
if filename.ends_with(".lib")

src/bootstrap/src/core/build_steps/llvm.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,8 +1528,18 @@ impl Step for Libunwind {
15281528

15291529
// FIXME: https://github.com/alexcrichton/cc-rs/issues/545#issuecomment-679242845
15301530
let mut count = 0;
1531-
for entry in fs::read_dir(&out_dir).unwrap() {
1532-
let file = entry.unwrap().path().canonicalize().unwrap();
1531+
let mut files = fs::read_dir(&out_dir)
1532+
.unwrap()
1533+
.map(|entry| {
1534+
entry
1535+
.unwrap()
1536+
.path()
1537+
.canonicalize()
1538+
.unwrap()
1539+
})
1540+
.collect::<Vec<_>>();
1541+
files.sort();
1542+
for file in files {
15331543
if file.is_file() && file.extension() == Some(OsStr::new("o")) {
15341544
// Object file name without the hash prefix is "Unwind-EHABI", "Unwind-seh" or "libunwind".
15351545
let base_name = unhashed_basename(&file);

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,16 @@ impl Builder<'_> {
12691269
if self.link_std_into_rustc_driver(target) { "1" } else { "0" },
12701270
);
12711271

1272+
// Crates like openssl will record current timestamp at build time.
1273+
// The current timestamp can be override by SOURCE_DATE_EPOCH
1274+
// environment variable.
1275+
// Pass through SOURCE_DATE_EPOCH environment variable to allow
1276+
// some crates to be reproducible.
1277+
let source_date_epoch_env = "SOURCE_DATE_EPOCH";
1278+
if let Ok(var) = std::env::var(&source_date_epoch_env) {
1279+
cargo.env(&source_date_epoch_env, &var);
1280+
}
1281+
12721282
// When building incrementally we default to a lower ThinLTO import limit
12731283
// (unless explicitly specified otherwise). This will produce a somewhat
12741284
// slower code but give way better compile times.

0 commit comments

Comments
 (0)