Bug 1885602 - Part 4: Implement navigating to the settings from the menu header for...
[gecko.git] / build / build-rust / cargo-vendor-std.patch
blob1a79846d61821c23c6b2451462d7f32264f9fb78
1 Teaches Cargo to source all std dependencies from vendored sources in the rust-src
2 component, making -Zbuild-std compatible with vendored builds.
4 This was originally landed in https://github.com/rust-lang/cargo/pull/8834
5 but was backed out for causing breakage in other situations. It works fine
6 for Firefox's usecase, though.
8 Most of these changes just add/edit tests for the functionality. Only the
9 change to src/cargo/core/compiler/standard_lib.rs is important.
11 diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs
12 index c456c58d5..333389bc8 100644
13 --- a/src/cargo/core/compiler/standard_lib.rs
14 +++ b/src/cargo/core/compiler/standard_lib.rs
15 @@ -11,6 +11,7 @@ use crate::ops::{self, Packages};
16 use crate::util::errors::CargoResult;
17 use crate::Config;
18 use std::collections::{HashMap, HashSet};
19 +use std::fs;
20 use std::path::PathBuf;
22 use super::BuildConfig;
23 @@ -73,27 +74,45 @@ pub fn resolve_std<'cfg>(
26 let src_path = detect_sysroot_src_path(target_data)?;
27 - let to_patch = [
28 - "rustc-std-workspace-core",
29 - "rustc-std-workspace-alloc",
30 - "rustc-std-workspace-std",
31 - ];
32 - let patches = to_patch
33 - .iter()
34 - .map(|&name| {
35 - let source_path = SourceId::for_path(&src_path.join("library").join(name))?;
36 - let dep = Dependency::parse(name, None, source_path)?;
38 + // Special std packages should be pulled from `library/` and should be
39 + // prefixed with `rustc-std-workspace-` in certain places.
40 + let libs_prefix = "library/";
41 + let special_std_prefix = "rustc-std-workspace-";
42 + let libs_path = src_path.join(libs_prefix);
44 + // Crates in rust-src to build. libsysroot is in some sense the "root" package
45 + // of std, as nothing else depends on it, so it must be explicitly added.
46 + let mut members = vec![format!("{}sysroot", libs_prefix)];
48 + // If rust-src contains a "vendor" directory, then patch in all the crates it contains.
49 + let vendor_path = src_path.join("vendor");
50 + let vendor_dir = fs::read_dir(vendor_path)?;
51 + let patches = vendor_dir
52 + .into_iter()
53 + .map(|entry| {
54 + let entry = entry?;
55 + let name = entry
56 + .file_name()
57 + .into_string()
58 + .map_err(|_| anyhow::anyhow!("package name wasn't utf8"))?;
60 + // Remap the rustc-std-workspace crates to the actual rust-src libraries
61 + let path = if let Some(real_name) = name.strip_prefix(special_std_prefix) {
62 + // Record this crate as something to build in the workspace
63 + members.push(format!("{}{}", libs_prefix, real_name));
64 + libs_path.join(&name)
65 + } else {
66 + entry.path()
67 + };
68 + let source_path = SourceId::for_path(&path)?;
69 + let dep = Dependency::parse(&name, None, source_path)?;
70 Ok(dep)
72 .collect::<CargoResult<Vec<_>>>()?;
74 let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap();
75 let patch = HashMap::from([(crates_io_url, patches)]);
76 - let members = vec![
77 - String::from("library/std"),
78 - String::from("library/core"),
79 - String::from("library/alloc"),
80 - String::from("library/sysroot"),
81 - ];
82 let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new(
83 &src_path,
84 &Some(members),
85 diff --git a/tests/testsuite/mock-std/library/test/Cargo.toml b/tests/testsuite/mock-std/library/test/Cargo.toml
86 index b9f51eda7..fed5f3973 100644
87 --- a/tests/testsuite/mock-std/library/test/Cargo.toml
88 +++ b/tests/testsuite/mock-std/library/test/Cargo.toml
89 @@ -9,3 +9,4 @@ std = { path = "../std" }
90 panic_unwind = { path = "../panic_unwind" }
91 compiler_builtins = { path = "../compiler_builtins" }
92 registry-dep-using-std = { version = "*", features = ['mockbuild'] }
93 +registry-dep-only-used-by-test = { version = "*" }
94 diff --git a/tests/testsuite/mock-std/library/test/src/lib.rs b/tests/testsuite/mock-std/library/test/src/lib.rs
95 index a112855f5..224b89bb2 100644
96 --- a/tests/testsuite/mock-std/library/test/src/lib.rs
97 +++ b/tests/testsuite/mock-std/library/test/src/lib.rs
98 @@ -7,4 +7,5 @@ extern crate test;
99 pub use test::*;
101 pub fn custom_api() {
102 + registry_dep_only_used_by_test::wow_testing_is_so_easy();
104 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml
105 new file mode 100644
106 index 000000000..31ba65a98
107 --- /dev/null
108 +++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml
109 @@ -0,0 +1,9 @@
110 +[package]
111 +name = "registry-dep-only-used-by-test"
112 +version = "1.0.0"
113 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
114 +edition = "2018"
116 +[dependencies]
118 +[features]
119 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs
120 new file mode 100644
121 index 000000000..a68d2aeef
122 --- /dev/null
123 +++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs
124 @@ -0,0 +1,2 @@
125 +pub fn wow_testing_is_so_easy() {
127 \ No newline at end of file
128 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml
129 new file mode 100644
130 index 000000000..f7e4ab232
131 --- /dev/null
132 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml
133 @@ -0,0 +1,12 @@
134 +[package]
135 +name = "registry-dep-using-alloc"
136 +version = "1.0.0"
137 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
138 +edition = "2018"
140 +[dependencies]
141 +rustc-std-workspace-alloc = { version = "*", optional = true }
142 +rustc-std-workspace-core = { version = "*", optional = true }
144 +[features]
145 +mockbuild = ["rustc-std-workspace-alloc", "rustc-std-workspace-core"]
146 \ No newline at end of file
147 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs
148 new file mode 100644
149 index 000000000..b9ab30339
150 --- /dev/null
151 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs
152 @@ -0,0 +1,9 @@
153 +#[cfg(feature = "mockbuild")]
154 +pub fn custom_api() {
157 +#[cfg(not(feature = "mockbuild"))]
158 +pub fn non_sysroot_api() {
159 + core::custom_api();
160 + alloc::custom_api();
162 \ No newline at end of file
163 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml b/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml
164 new file mode 100644
165 index 000000000..befb83a63
166 --- /dev/null
167 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml
168 @@ -0,0 +1,11 @@
169 +[package]
170 +name = "registry-dep-using-core"
171 +version = "1.0.0"
172 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
173 +edition = "2018"
175 +[dependencies]
176 +rustc-std-workspace-core = { version = "*", optional = true }
178 +[features]
179 +mockbuild = ["rustc-std-workspace-core"]
180 \ No newline at end of file
181 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs b/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs
182 new file mode 100644
183 index 000000000..f9dbac0f4
184 --- /dev/null
185 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs
186 @@ -0,0 +1,8 @@
187 +#[cfg(feature = "mockbuild")]
188 +pub fn custom_api() {
191 +#[cfg(not(feature = "mockbuild"))]
192 +pub fn non_sysroot_api() {
193 + core::custom_api();
195 \ No newline at end of file
196 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml b/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml
197 new file mode 100644
198 index 000000000..71ef0a42f
199 --- /dev/null
200 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml
201 @@ -0,0 +1,11 @@
202 +[package]
203 +name = "registry-dep-using-std"
204 +version = "1.0.0"
205 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
206 +edition = "2018"
208 +[dependencies]
209 +rustc-std-workspace-std = { version = "*", optional = true }
211 +[features]
212 +mockbuild = ["rustc-std-workspace-std"]
213 \ No newline at end of file
214 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs b/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs
215 new file mode 100644
216 index 000000000..f3af39178
217 --- /dev/null
218 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs
219 @@ -0,0 +1,8 @@
220 +#[cfg(feature = "mockbuild")]
221 +pub fn custom_api() {
224 +#[cfg(not(feature = "mockbuild"))]
225 +pub fn non_sysroot_api() {
226 + std::custom_api();
228 \ No newline at end of file
229 diff --git a/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml b/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml
230 new file mode 100644
231 index 000000000..4465a08a8
232 --- /dev/null
233 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml
234 @@ -0,0 +1 @@
235 +this file shouldn't be read
236 \ No newline at end of file
237 diff --git a/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml b/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml
238 new file mode 100644
239 index 000000000..4465a08a8
240 --- /dev/null
241 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml
242 @@ -0,0 +1 @@
243 +this file shouldn't be read
244 \ No newline at end of file
245 diff --git a/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml b/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml
246 new file mode 100644
247 index 000000000..4465a08a8
248 --- /dev/null
249 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml
250 @@ -0,0 +1 @@
251 +this file shouldn't be read
252 \ No newline at end of file
253 diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs
254 index d3be303ea..486a9b4e0 100644
255 --- a/tests/testsuite/standard_lib.rs
256 +++ b/tests/testsuite/standard_lib.rs
257 @@ -15,71 +15,18 @@ struct Setup {
260 fn setup() -> Setup {
261 - // Our mock sysroot requires a few packages from crates.io, so make sure
262 - // they're "published" to crates.io. Also edit their code a bit to make sure
263 - // that they have access to our custom crates with custom apis.
264 + // Register a version of one of the std dependencies that doesn't compile.
265 + // This ensures that the mock-std's vendor is actually being used.
266 Package::new("registry-dep-using-core", "1.0.0")
267 .file(
268 "src/lib.rs",
270 - #![no_std]
272 - #[cfg(feature = \"mockbuild\")]
273 - pub fn custom_api() {
276 - #[cfg(not(feature = \"mockbuild\"))]
277 - pub fn non_sysroot_api() {
278 - core::custom_api();
280 + don't compile me bro!!
283 .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
284 .feature("mockbuild", &["rustc-std-workspace-core"])
285 .publish();
286 - Package::new("registry-dep-using-alloc", "1.0.0")
287 - .file(
288 - "src/lib.rs",
290 - #![no_std]
292 - extern crate alloc;
294 - #[cfg(feature = \"mockbuild\")]
295 - pub fn custom_api() {
298 - #[cfg(not(feature = \"mockbuild\"))]
299 - pub fn non_sysroot_api() {
300 - core::custom_api();
301 - alloc::custom_api();
303 - ",
305 - .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
306 - .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true))
307 - .feature(
308 - "mockbuild",
309 - &["rustc-std-workspace-core", "rustc-std-workspace-alloc"],
311 - .publish();
312 - Package::new("registry-dep-using-std", "1.0.0")
313 - .file(
314 - "src/lib.rs",
316 - #[cfg(feature = \"mockbuild\")]
317 - pub fn custom_api() {
320 - #[cfg(not(feature = \"mockbuild\"))]
321 - pub fn non_sysroot_api() {
322 - std::custom_api();
324 - ",
326 - .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true))
327 - .feature("mockbuild", &["rustc-std-workspace-std"])
328 - .publish();
330 let p = ProjectBuilder::new(paths::root().join("rustc-wrapper"))
331 .file(
332 @@ -335,6 +282,81 @@ fn depend_same_as_std() {
333 fn test() {
334 let setup = setup();
336 + // Our mock sysroot requires a few packages from crates.io, so make sure
337 + // they're "published" to crates.io. Also edit their code a bit to make sure
338 + // that they have access to our custom crates with custom apis.
339 + Package::new("registry-dep-using-core", "1.0.0")
340 + .file(
341 + "src/lib.rs",
343 + #![no_std]
345 + #[cfg(feature = \"mockbuild\")]
346 + pub fn custom_api() {
349 + #[cfg(not(feature = \"mockbuild\"))]
350 + pub fn non_sysroot_api() {
351 + core::custom_api();
353 + ",
355 + .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
356 + .feature("mockbuild", &["rustc-std-workspace-core"])
357 + .publish();
358 + Package::new("registry-dep-using-alloc", "1.0.0")
359 + .file(
360 + "src/lib.rs",
362 + #![no_std]
364 + extern crate alloc;
366 + #[cfg(feature = \"mockbuild\")]
367 + pub fn custom_api() {
370 + #[cfg(not(feature = \"mockbuild\"))]
371 + pub fn non_sysroot_api() {
372 + core::custom_api();
373 + alloc::custom_api();
375 + ",
377 + .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
378 + .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true))
379 + .feature(
380 + "mockbuild",
381 + &["rustc-std-workspace-core", "rustc-std-workspace-alloc"],
383 + .publish();
384 + Package::new("registry-dep-using-std", "1.0.0")
385 + .file(
386 + "src/lib.rs",
388 + #[cfg(feature = \"mockbuild\")]
389 + pub fn custom_api() {
392 + #[cfg(not(feature = \"mockbuild\"))]
393 + pub fn non_sysroot_api() {
394 + std::custom_api();
396 + ",
398 + .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true))
399 + .feature("mockbuild", &["rustc-std-workspace-std"])
400 + .publish();
401 + Package::new("registry-dep-only-used-by-test", "1.0.0")
402 + .file(
403 + "src/lib.rs",
405 + pub fn wow_testing_is_so_easy() {
407 + ",
409 + .publish();
411 let p = project()
412 .file(
413 "src/lib.rs",