summaryrefslogtreecommitdiffstats
path: root/build/build-rust/cargo-vendor-std.patch
blob: 1a79846d61821c23c6b2451462d7f32264f9fb78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
Teaches Cargo to source all std dependencies from vendored sources in the rust-src
component, making -Zbuild-std compatible with vendored builds.

This was originally landed in https://github.com/rust-lang/cargo/pull/8834
but was backed out for causing breakage in other situations. It works fine
for Firefox's usecase, though.

Most of these changes just add/edit tests for the functionality. Only the
change to src/cargo/core/compiler/standard_lib.rs is important.

diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs
index c456c58d5..333389bc8 100644
--- a/src/cargo/core/compiler/standard_lib.rs
+++ b/src/cargo/core/compiler/standard_lib.rs
@@ -11,6 +11,7 @@ use crate::ops::{self, Packages};
 use crate::util::errors::CargoResult;
 use crate::Config;
 use std::collections::{HashMap, HashSet};
+use std::fs;
 use std::path::PathBuf;
 
 use super::BuildConfig;
@@ -73,27 +74,45 @@ pub fn resolve_std<'cfg>(
     }
 
     let src_path = detect_sysroot_src_path(target_data)?;
-    let to_patch = [
-        "rustc-std-workspace-core",
-        "rustc-std-workspace-alloc",
-        "rustc-std-workspace-std",
-    ];
-    let patches = to_patch
-        .iter()
-        .map(|&name| {
-            let source_path = SourceId::for_path(&src_path.join("library").join(name))?;
-            let dep = Dependency::parse(name, None, source_path)?;
+
+    // Special std packages should be pulled from `library/` and should be
+    // prefixed with `rustc-std-workspace-` in certain places.
+    let libs_prefix = "library/";
+    let special_std_prefix = "rustc-std-workspace-";
+    let libs_path = src_path.join(libs_prefix);
+
+    // Crates in rust-src to build. libsysroot is in some sense the "root" package
+    // of std, as nothing else depends on it, so it must be explicitly added.
+    let mut members = vec![format!("{}sysroot", libs_prefix)];
+
+    // If rust-src contains a "vendor" directory, then patch in all the crates it contains.
+    let vendor_path = src_path.join("vendor");
+    let vendor_dir = fs::read_dir(vendor_path)?;
+    let patches = vendor_dir
+        .into_iter()
+        .map(|entry| {
+            let entry = entry?;
+            let name = entry
+                .file_name()
+                .into_string()
+                .map_err(|_| anyhow::anyhow!("package name wasn't utf8"))?;
+
+            // Remap the rustc-std-workspace crates to the actual rust-src libraries
+            let path = if let Some(real_name) = name.strip_prefix(special_std_prefix) {
+                // Record this crate as something to build in the workspace
+                members.push(format!("{}{}", libs_prefix, real_name));
+                libs_path.join(&name)
+            } else {
+                entry.path()
+            };
+            let source_path = SourceId::for_path(&path)?;
+            let dep = Dependency::parse(&name, None, source_path)?;
             Ok(dep)
         })
         .collect::<CargoResult<Vec<_>>>()?;
+
     let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap();
     let patch = HashMap::from([(crates_io_url, patches)]);
-    let members = vec![
-        String::from("library/std"),
-        String::from("library/core"),
-        String::from("library/alloc"),
-        String::from("library/sysroot"),
-    ];
     let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new(
         &src_path,
         &Some(members),
diff --git a/tests/testsuite/mock-std/library/test/Cargo.toml b/tests/testsuite/mock-std/library/test/Cargo.toml
index b9f51eda7..fed5f3973 100644
--- a/tests/testsuite/mock-std/library/test/Cargo.toml
+++ b/tests/testsuite/mock-std/library/test/Cargo.toml
@@ -9,3 +9,4 @@ std = { path = "../std" }
 panic_unwind = { path = "../panic_unwind" }
 compiler_builtins = { path = "../compiler_builtins" }
 registry-dep-using-std = { version = "*", features = ['mockbuild'] }
+registry-dep-only-used-by-test = { version = "*" }
diff --git a/tests/testsuite/mock-std/library/test/src/lib.rs b/tests/testsuite/mock-std/library/test/src/lib.rs
index a112855f5..224b89bb2 100644
--- a/tests/testsuite/mock-std/library/test/src/lib.rs
+++ b/tests/testsuite/mock-std/library/test/src/lib.rs
@@ -7,4 +7,5 @@ extern crate test;
 pub use test::*;
 
 pub fn custom_api() {
+    registry_dep_only_used_by_test::wow_testing_is_so_easy();
 }
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
new file mode 100644
index 000000000..31ba65a98
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "registry-dep-only-used-by-test"
+version = "1.0.0"
+authors = ["Alex Crichton <alex@alexcrichton.com>"]
+edition = "2018"
+
+[dependencies]
+
+[features]
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
new file mode 100644
index 000000000..a68d2aeef
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs
@@ -0,0 +1,2 @@
+pub fn wow_testing_is_so_easy() {
+}
\ No newline at end of file
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
new file mode 100644
index 000000000..f7e4ab232
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "registry-dep-using-alloc"
+version = "1.0.0"
+authors = ["Alex Crichton <alex@alexcrichton.com>"]
+edition = "2018"
+
+[dependencies]
+rustc-std-workspace-alloc = { version = "*", optional = true }
+rustc-std-workspace-core = { version = "*", optional = true }
+
+[features]
+mockbuild = ["rustc-std-workspace-alloc", "rustc-std-workspace-core"]
\ No newline at end of file
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
new file mode 100644
index 000000000..b9ab30339
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs
@@ -0,0 +1,9 @@
+#[cfg(feature = "mockbuild")]
+pub fn custom_api() {
+}
+
+#[cfg(not(feature = "mockbuild"))]
+pub fn non_sysroot_api() {
+    core::custom_api();
+    alloc::custom_api();
+}
\ No newline at end of file
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
new file mode 100644
index 000000000..befb83a63
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "registry-dep-using-core"
+version = "1.0.0"
+authors = ["Alex Crichton <alex@alexcrichton.com>"]
+edition = "2018"
+
+[dependencies]
+rustc-std-workspace-core = { version = "*", optional = true }
+
+[features]
+mockbuild = ["rustc-std-workspace-core"]
\ No newline at end of file
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
new file mode 100644
index 000000000..f9dbac0f4
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs
@@ -0,0 +1,8 @@
+#[cfg(feature = "mockbuild")]
+pub fn custom_api() {
+}
+
+#[cfg(not(feature = "mockbuild"))]
+pub fn non_sysroot_api() {
+    core::custom_api();
+}
\ No newline at end of file
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
new file mode 100644
index 000000000..71ef0a42f
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "registry-dep-using-std"
+version = "1.0.0"
+authors = ["Alex Crichton <alex@alexcrichton.com>"]
+edition = "2018"
+
+[dependencies]
+rustc-std-workspace-std = { version = "*", optional = true }
+
+[features]
+mockbuild = ["rustc-std-workspace-std"]
\ No newline at end of file
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
new file mode 100644
index 000000000..f3af39178
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs
@@ -0,0 +1,8 @@
+#[cfg(feature = "mockbuild")]
+pub fn custom_api() {
+}
+
+#[cfg(not(feature = "mockbuild"))]
+pub fn non_sysroot_api() {
+    std::custom_api();
+}
\ No newline at end of file
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
new file mode 100644
index 000000000..4465a08a8
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml
@@ -0,0 +1 @@
+this file shouldn't be read 
\ No newline at end of file
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
new file mode 100644
index 000000000..4465a08a8
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml
@@ -0,0 +1 @@
+this file shouldn't be read 
\ No newline at end of file
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
new file mode 100644
index 000000000..4465a08a8
--- /dev/null
+++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml
@@ -0,0 +1 @@
+this file shouldn't be read 
\ No newline at end of file
diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs
index d3be303ea..486a9b4e0 100644
--- a/tests/testsuite/standard_lib.rs
+++ b/tests/testsuite/standard_lib.rs
@@ -15,71 +15,18 @@ struct Setup {
 }
 
 fn setup() -> Setup {
-    // Our mock sysroot requires a few packages from crates.io, so make sure
-    // they're "published" to crates.io. Also edit their code a bit to make sure
-    // that they have access to our custom crates with custom apis.
+    // Register a version of one of the std dependencies that doesn't compile.
+    // This ensures that the mock-std's vendor is actually being used.
     Package::new("registry-dep-using-core", "1.0.0")
         .file(
             "src/lib.rs",
             "
-                #![no_std]
-
-                #[cfg(feature = \"mockbuild\")]
-                pub fn custom_api() {
-                }
-
-                #[cfg(not(feature = \"mockbuild\"))]
-                pub fn non_sysroot_api() {
-                    core::custom_api();
-                }
+               don't compile me bro!!
             ",
         )
         .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
         .feature("mockbuild", &["rustc-std-workspace-core"])
         .publish();
-    Package::new("registry-dep-using-alloc", "1.0.0")
-        .file(
-            "src/lib.rs",
-            "
-                #![no_std]
-
-                extern crate alloc;
-
-                #[cfg(feature = \"mockbuild\")]
-                pub fn custom_api() {
-                }
-
-                #[cfg(not(feature = \"mockbuild\"))]
-                pub fn non_sysroot_api() {
-                    core::custom_api();
-                    alloc::custom_api();
-                }
-            ",
-        )
-        .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
-        .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true))
-        .feature(
-            "mockbuild",
-            &["rustc-std-workspace-core", "rustc-std-workspace-alloc"],
-        )
-        .publish();
-    Package::new("registry-dep-using-std", "1.0.0")
-        .file(
-            "src/lib.rs",
-            "
-                #[cfg(feature = \"mockbuild\")]
-                pub fn custom_api() {
-                }
-
-                #[cfg(not(feature = \"mockbuild\"))]
-                pub fn non_sysroot_api() {
-                    std::custom_api();
-                }
-            ",
-        )
-        .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true))
-        .feature("mockbuild", &["rustc-std-workspace-std"])
-        .publish();
 
     let p = ProjectBuilder::new(paths::root().join("rustc-wrapper"))
         .file(
@@ -335,6 +282,81 @@ fn depend_same_as_std() {
 fn test() {
     let setup = setup();
 
+    // Our mock sysroot requires a few packages from crates.io, so make sure
+    // they're "published" to crates.io. Also edit their code a bit to make sure
+    // that they have access to our custom crates with custom apis.
+    Package::new("registry-dep-using-core", "1.0.0")
+        .file(
+            "src/lib.rs",
+            "
+                #![no_std]
+
+                #[cfg(feature = \"mockbuild\")]
+                pub fn custom_api() {
+                }
+
+                #[cfg(not(feature = \"mockbuild\"))]
+                pub fn non_sysroot_api() {
+                    core::custom_api();
+                }
+            ",
+        )
+        .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
+        .feature("mockbuild", &["rustc-std-workspace-core"])
+        .publish();
+    Package::new("registry-dep-using-alloc", "1.0.0")
+        .file(
+            "src/lib.rs",
+            "
+                #![no_std]
+
+                extern crate alloc;
+
+                #[cfg(feature = \"mockbuild\")]
+                pub fn custom_api() {
+                }
+
+                #[cfg(not(feature = \"mockbuild\"))]
+                pub fn non_sysroot_api() {
+                    core::custom_api();
+                    alloc::custom_api();
+                }
+            ",
+        )
+        .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
+        .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true))
+        .feature(
+            "mockbuild",
+            &["rustc-std-workspace-core", "rustc-std-workspace-alloc"],
+        )
+        .publish();
+    Package::new("registry-dep-using-std", "1.0.0")
+        .file(
+            "src/lib.rs",
+            "
+                #[cfg(feature = \"mockbuild\")]
+                pub fn custom_api() {
+                }
+
+                #[cfg(not(feature = \"mockbuild\"))]
+                pub fn non_sysroot_api() {
+                    std::custom_api();
+                }
+            ",
+        )
+        .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true))
+        .feature("mockbuild", &["rustc-std-workspace-std"])
+        .publish();
+    Package::new("registry-dep-only-used-by-test", "1.0.0")
+        .file(
+            "src/lib.rs",
+            "
+                pub fn wow_testing_is_so_easy() {
+                }
+            ",
+        )
+        .publish();
+
     let p = project()
         .file(
             "src/lib.rs",