Debian Codegraph @

build/build-rust/rust-vendor-std.patch

raw
1 Teaches Rust's build system to vendor `std`'s dependencies into the
2 `rust-src` component.
3
4 This was originally landed in <https://github.com/rust-lang/rust/pull/78790>,
5 but was backed out for causing some breakage for distro maintainers who
6 need to build Rust itself in a vendored/offline context. It doesn't actually
7 fetch anything interesting from Crates.io, just the magic fake `std`/`core` crates
8 that exist to make the build work right. Those crates *are* vendored but
9 their contents are ignored in favour of the actual stdlib.
10
11 For Firefox's purposes, these patches still work fine, and are necessary
12 to make `-Zbuild-std` work in a vendored environment.
13
14 diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs
15 index 58f86aa996d..ef8c1584011 100644
16 --- a/src/bootstrap/src/core/build_steps/dist.rs
17 +++ b/src/bootstrap/src/core/build_steps/dist.rs
18 @@ -941,6 +941,35 @@ fn run(self, builder: &Builder<'_>) -> GeneratedTarball {
19 builder.copy_link(&builder.src.join(file), &dst_src.join(file));
20 }
21
22 + // libsysroot includes std and everything else, so vendoring it
23 + // creates exactly what's needed for `cargo -Zbuild-std` or any
24 + // other analysis of the stdlib's source. Cargo also needs help
25 + // finding the lock, so we copy it to libsysroot temporarily.
26 + //
27 + // Note that this requires std to only have one version of each
28 + // crate. e.g. two versions of getopts won't be patchable.
29 + let dst_libsysroot = dst_src.join("library/sysroot");
30 + let dst_vendor = dst_src.join("vendor");
31 + let root_lock = dst_src.join("Cargo.lock");
32 + let temp_lock = dst_libsysroot.join("Cargo.lock");
33 +
34 + // `cargo vendor` will delete everything from the lockfile that
35 + // isn't used by libsysroot, so we need to not use any links!
36 + builder.really_copy(&root_lock, &temp_lock);
37 +
38 + let mut cmd = command(&builder.initial_cargo);
39 + cmd.arg("vendor").arg(dst_vendor).current_dir(&dst_libsysroot);
40 + cmd.env("RUSTC_BOOTSTRAP", "1");
41 + builder.info("Dist src");
42 + let _time = timeit(builder);
43 + builder.run(
44 + &mut cmd,
45 + crate::utils::exec::OutputMode::Print,
46 + crate::utils::exec::OutputMode::Print,
47 + );
48 +
49 + builder.remove(&temp_lock);
50 +
51 tarball.generate()
52 }
53 }
54 diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs
55 index 453fb39327d..af579af9eca 100644
56 --- a/src/bootstrap/src/lib.rs
57 +++ b/src/bootstrap/src/lib.rs
58 @@ -1732,6 +1732,30 @@ fn read_stamp_file(&self, stamp: &Path) -> Vec<(PathBuf, DependencyType)> {
59 paths
60 }
61
62 + /// Copies a file from `src` to `dst` and doesn't use links, so
63 + /// that the copy can be modified without affecting the original.
64 + pub fn really_copy(&self, src: &Path, dst: &Path) {
65 + if self.config.dry_run() {
66 + return;
67 + }
68 + self.verbose_than(1, || println!("Copy {:?} to {:?}", src, dst));
69 + if src == dst {
70 + return;
71 + }
72 + let _ = fs::remove_file(dst);
73 + let metadata = t!(src.symlink_metadata());
74 + if let Err(e) = fs::copy(src, dst) {
75 + panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
76 + }
77 + t!(fs::set_permissions(dst, metadata.permissions()));
78 + let file_times = fs::FileTimes::new()
79 + .set_accessed(t!(metadata.accessed()))
80 + .set_modified(t!(metadata.modified()));
81 +
82 + let dst_file = t!(fs::File::open(dst));
83 + t!(dst_file.set_times(file_times));
84 + }
85 +
86 /// Links a file from `src` to `dst`.
87 /// Attempts to use hard links if possible, falling back to copying.
88 /// You can neither rely on this being a copy nor it being a link,