| 1 |
Teaches Cargo to source all std dependencies from vendored sources in the rust-src |
| 2 |
component, making -Zbuild-std compatible with vendored builds. |
| 3 |
|
| 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. |
| 7 |
|
| 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. |
| 10 |
|
| 11 |
diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs |
| 12 |
index 3e387bddd..41be56056 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::GlobalContext; |
| 18 |
use std::collections::{HashMap, HashSet}; |
| 19 |
+use std::fs; |
| 20 |
use std::path::PathBuf; |
| 21 |
use std::rc::Rc; |
| 22 |
|
| 23 |
@@ -67,34 +68,54 @@ pub fn resolve_std<'gctx>( |
| 24 |
build_config: &BuildConfig, |
| 25 |
crates: &[String], |
| 26 |
) -> CargoResult<(PackageSet<'gctx>, Resolve, ResolvedFeatures)> { |
| 27 |
+ let gctx = ws.gctx(); |
| 28 |
if build_config.build_plan { |
| 29 |
- ws.gctx() |
| 30 |
+ gctx |
| 31 |
.shell() |
| 32 |
.warn("-Zbuild-std does not currently fully support --build-plan")?; |
| 33 |
} |
| 34 |
|
| 35 |
let src_path = detect_sysroot_src_path(target_data)?; |
| 36 |
- let to_patch = [ |
| 37 |
- "rustc-std-workspace-core", |
| 38 |
- "rustc-std-workspace-alloc", |
| 39 |
- "rustc-std-workspace-std", |
| 40 |
- ]; |
| 41 |
- let patches = to_patch |
| 42 |
- .iter() |
| 43 |
- .map(|&name| { |
| 44 |
- let source_path = SourceId::for_path(&src_path.join("library").join(name))?; |
| 45 |
- let dep = Dependency::parse(name, None, source_path)?; |
| 46 |
+ |
| 47 |
+ // Special std packages should be pulled from `library/` and should be |
| 48 |
+ // prefixed with `rustc-std-workspace-` in certain places. |
| 49 |
+ let libs_prefix = "library/"; |
| 50 |
+ let special_std_prefix = "rustc-std-workspace-"; |
| 51 |
+ let libs_path = src_path.join(libs_prefix); |
| 52 |
+ |
| 53 |
+ // Crates in rust-src to build. libsysroot is in some sense the "root" package |
| 54 |
+ // of std, as nothing else depends on it, so it must be explicitly added. |
| 55 |
+ let mut members = vec![format!("{}sysroot", libs_prefix)]; |
| 56 |
+ |
| 57 |
+ // If rust-src contains a "vendor" directory, then patch in all the crates it contains. |
| 58 |
+ let vendor_path = src_path.join("vendor"); |
| 59 |
+ let vendor_dir = fs::read_dir(vendor_path)?; |
| 60 |
+ let patches = vendor_dir |
| 61 |
+ .into_iter() |
| 62 |
+ .map(|entry| { |
| 63 |
+ let entry = entry?; |
| 64 |
+ let name = entry |
| 65 |
+ .file_name() |
| 66 |
+ .into_string() |
| 67 |
+ .map_err(|_| anyhow::anyhow!("package name wasn't utf8"))?; |
| 68 |
+ |
| 69 |
+ // Remap the rustc-std-workspace crates to the actual rust-src libraries |
| 70 |
+ let path = if let Some(real_name) = name.strip_prefix(special_std_prefix) { |
| 71 |
+ // Record this crate as something to build in the workspace |
| 72 |
+ members.push(format!("{}{}", libs_prefix, real_name)); |
| 73 |
+ libs_path.join(&name) |
| 74 |
+ } else { |
| 75 |
+ entry.path() |
| 76 |
+ }; |
| 77 |
+ let source_path = SourceId::for_path(&path)?; |
| 78 |
+ let package = crate::sources::PathSource::new(&path, source_path, gctx).root_package()?; |
| 79 |
+ let dep = Dependency::parse(package.name(), None, source_path)?; |
| 80 |
Ok(dep) |
| 81 |
}) |
| 82 |
.collect::<CargoResult<Vec<_>>>()?; |
| 83 |
+ |
| 84 |
let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap(); |
| 85 |
let patch = HashMap::from([(crates_io_url, patches)]); |
| 86 |
- let members = vec![ |
| 87 |
- String::from("library/std"), |
| 88 |
- String::from("library/core"), |
| 89 |
- String::from("library/alloc"), |
| 90 |
- String::from("library/sysroot"), |
| 91 |
- ]; |
| 92 |
let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new( |
| 93 |
&src_path, |
| 94 |
&Some(members), |
| 95 |
@@ -115,7 +136,6 @@ pub fn resolve_std<'gctx>( |
| 96 |
None, |
| 97 |
); |
| 98 |
|
| 99 |
- let gctx = ws.gctx(); |
| 100 |
// This is a delicate hack. In order for features to resolve correctly, |
| 101 |
// the resolver needs to run a specific "current" member of the workspace. |
| 102 |
// Thus, in order to set the features for `std`, we need to set `sysroot` |
| 103 |
diff --git a/tests/testsuite/mock-std/library/test/Cargo.toml b/tests/testsuite/mock-std/library/test/Cargo.toml |
| 104 |
index b9f51eda7..fed5f3973 100644 |
| 105 |
--- a/tests/testsuite/mock-std/library/test/Cargo.toml |
| 106 |
+++ b/tests/testsuite/mock-std/library/test/Cargo.toml |
| 107 |
@@ -9,3 +9,4 @@ std = { path = "../std" } |
| 108 |
panic_unwind = { path = "../panic_unwind" } |
| 109 |
compiler_builtins = { path = "../compiler_builtins" } |
| 110 |
registry-dep-using-std = { version = "*", features = ['mockbuild'] } |
| 111 |
+registry-dep-only-used-by-test = { version = "*" } |
| 112 |
diff --git a/tests/testsuite/mock-std/library/test/src/lib.rs b/tests/testsuite/mock-std/library/test/src/lib.rs |
| 113 |
index a112855f5..224b89bb2 100644 |
| 114 |
--- a/tests/testsuite/mock-std/library/test/src/lib.rs |
| 115 |
+++ b/tests/testsuite/mock-std/library/test/src/lib.rs |
| 116 |
@@ -7,4 +7,5 @@ extern crate test; |
| 117 |
pub use test::*; |
| 118 |
|
| 119 |
pub fn custom_api() { |
| 120 |
+ registry_dep_only_used_by_test::wow_testing_is_so_easy(); |
| 121 |
} |
| 122 |
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 |
| 123 |
new file mode 100644 |
| 124 |
index 000000000..31ba65a98 |
| 125 |
--- /dev/null |
| 126 |
+++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml |
| 127 |
@@ -0,0 +1,9 @@ |
| 128 |
+[package] |
| 129 |
+name = "registry-dep-only-used-by-test" |
| 130 |
+version = "1.0.0" |
| 131 |
+authors = ["Alex Crichton <[email protected]>"] |
| 132 |
+edition = "2018" |
| 133 |
+ |
| 134 |
+[dependencies] |
| 135 |
+ |
| 136 |
+[features] |
| 137 |
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 |
| 138 |
new file mode 100644 |
| 139 |
index 000000000..a68d2aeef |
| 140 |
--- /dev/null |
| 141 |
+++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs |
| 142 |
@@ -0,0 +1,2 @@ |
| 143 |
+pub fn wow_testing_is_so_easy() { |
| 144 |
+} |
| 145 |
\ No newline at end of file |
| 146 |
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 |
| 147 |
new file mode 100644 |
| 148 |
index 000000000..f7e4ab232 |
| 149 |
--- /dev/null |
| 150 |
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml |
| 151 |
@@ -0,0 +1,12 @@ |
| 152 |
+[package] |
| 153 |
+name = "registry-dep-using-alloc" |
| 154 |
+version = "1.0.0" |
| 155 |
+authors = ["Alex Crichton <[email protected]>"] |
| 156 |
+edition = "2018" |
| 157 |
+ |
| 158 |
+[dependencies] |
| 159 |
+rustc-std-workspace-alloc = { version = "*", optional = true } |
| 160 |
+rustc-std-workspace-core = { version = "*", optional = true } |
| 161 |
+ |
| 162 |
+[features] |
| 163 |
+mockbuild = ["rustc-std-workspace-alloc", "rustc-std-workspace-core"] |
| 164 |
\ No newline at end of file |
| 165 |
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 |
| 166 |
new file mode 100644 |
| 167 |
index 000000000..b9ab30339 |
| 168 |
--- /dev/null |
| 169 |
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs |
| 170 |
@@ -0,0 +1,9 @@ |
| 171 |
+#[cfg(feature = "mockbuild")] |
| 172 |
+pub fn custom_api() { |
| 173 |
+} |
| 174 |
+ |
| 175 |
+#[cfg(not(feature = "mockbuild"))] |
| 176 |
+pub fn non_sysroot_api() { |
| 177 |
+ core::custom_api(); |
| 178 |
+ alloc::custom_api(); |
| 179 |
+} |
| 180 |
\ No newline at end of file |
| 181 |
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 |
| 182 |
new file mode 100644 |
| 183 |
index 000000000..befb83a63 |
| 184 |
--- /dev/null |
| 185 |
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml |
| 186 |
@@ -0,0 +1,11 @@ |
| 187 |
+[package] |
| 188 |
+name = "registry-dep-using-core" |
| 189 |
+version = "1.0.0" |
| 190 |
+authors = ["Alex Crichton <[email protected]>"] |
| 191 |
+edition = "2018" |
| 192 |
+ |
| 193 |
+[dependencies] |
| 194 |
+rustc-std-workspace-core = { version = "*", optional = true } |
| 195 |
+ |
| 196 |
+[features] |
| 197 |
+mockbuild = ["rustc-std-workspace-core"] |
| 198 |
\ No newline at end of file |
| 199 |
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 |
| 200 |
new file mode 100644 |
| 201 |
index 000000000..f9dbac0f4 |
| 202 |
--- /dev/null |
| 203 |
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs |
| 204 |
@@ -0,0 +1,8 @@ |
| 205 |
+#[cfg(feature = "mockbuild")] |
| 206 |
+pub fn custom_api() { |
| 207 |
+} |
| 208 |
+ |
| 209 |
+#[cfg(not(feature = "mockbuild"))] |
| 210 |
+pub fn non_sysroot_api() { |
| 211 |
+ core::custom_api(); |
| 212 |
+} |
| 213 |
\ No newline at end of file |
| 214 |
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 |
| 215 |
new file mode 100644 |
| 216 |
index 000000000..71ef0a42f |
| 217 |
--- /dev/null |
| 218 |
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml |
| 219 |
@@ -0,0 +1,11 @@ |
| 220 |
+[package] |
| 221 |
+name = "registry-dep-using-std" |
| 222 |
+version = "1.0.0" |
| 223 |
+authors = ["Alex Crichton <[email protected]>"] |
| 224 |
+edition = "2018" |
| 225 |
+ |
| 226 |
+[dependencies] |
| 227 |
+rustc-std-workspace-std = { version = "*", optional = true } |
| 228 |
+ |
| 229 |
+[features] |
| 230 |
+mockbuild = ["rustc-std-workspace-std"] |
| 231 |
\ No newline at end of file |
| 232 |
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 |
| 233 |
new file mode 100644 |
| 234 |
index 000000000..f3af39178 |
| 235 |
--- /dev/null |
| 236 |
+++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs |
| 237 |
@@ -0,0 +1,8 @@ |
| 238 |
+#[cfg(feature = "mockbuild")] |
| 239 |
+pub fn custom_api() { |
| 240 |
+} |
| 241 |
+ |
| 242 |
+#[cfg(not(feature = "mockbuild"))] |
| 243 |
+pub fn non_sysroot_api() { |
| 244 |
+ std::custom_api(); |
| 245 |
+} |
| 246 |
\ No newline at end of file |
| 247 |
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 |
| 248 |
new file mode 100644 |
| 249 |
index 000000000..4465a08a8 |
| 250 |
--- /dev/null |
| 251 |
+++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml |
| 252 |
@@ -0,0 +1 @@ |
| 253 |
+this file shouldn't be read |
| 254 |
\ No newline at end of file |
| 255 |
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 |
| 256 |
new file mode 100644 |
| 257 |
index 000000000..4465a08a8 |
| 258 |
--- /dev/null |
| 259 |
+++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml |
| 260 |
@@ -0,0 +1 @@ |
| 261 |
+this file shouldn't be read |
| 262 |
\ No newline at end of file |
| 263 |
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 |
| 264 |
new file mode 100644 |
| 265 |
index 000000000..4465a08a8 |
| 266 |
--- /dev/null |
| 267 |
+++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml |
| 268 |
@@ -0,0 +1 @@ |
| 269 |
+this file shouldn't be read |
| 270 |
\ No newline at end of file |
| 271 |
diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs |
| 272 |
index d3be303ea..486a9b4e0 100644 |
| 273 |
--- a/tests/testsuite/standard_lib.rs |
| 274 |
+++ b/tests/testsuite/standard_lib.rs |
| 275 |
@@ -15,71 +15,18 @@ struct Setup { |
| 276 |
} |
| 277 |
|
| 278 |
fn setup() -> Setup { |
| 279 |
- // Our mock sysroot requires a few packages from crates.io, so make sure |
| 280 |
- // they're "published" to crates.io. Also edit their code a bit to make sure |
| 281 |
- // that they have access to our custom crates with custom apis. |
| 282 |
+ // Register a version of one of the std dependencies that doesn't compile. |
| 283 |
+ // This ensures that the mock-std's vendor is actually being used. |
| 284 |
Package::new("registry-dep-using-core", "1.0.0") |
| 285 |
.file( |
| 286 |
"src/lib.rs", |
| 287 |
" |
| 288 |
- #![no_std] |
| 289 |
- |
| 290 |
- #[cfg(feature = \"mockbuild\")] |
| 291 |
- pub fn custom_api() { |
| 292 |
- } |
| 293 |
- |
| 294 |
- #[cfg(not(feature = \"mockbuild\"))] |
| 295 |
- pub fn non_sysroot_api() { |
| 296 |
- core::custom_api(); |
| 297 |
- } |
| 298 |
+ don't compile me bro!! |
| 299 |
", |
| 300 |
) |
| 301 |
.add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true)) |
| 302 |
.feature("mockbuild", &["rustc-std-workspace-core"]) |
| 303 |
.publish(); |
| 304 |
- Package::new("registry-dep-using-alloc", "1.0.0") |
| 305 |
- .file( |
| 306 |
- "src/lib.rs", |
| 307 |
- " |
| 308 |
- #![no_std] |
| 309 |
- |
| 310 |
- extern crate alloc; |
| 311 |
- |
| 312 |
- #[cfg(feature = \"mockbuild\")] |
| 313 |
- pub fn custom_api() { |
| 314 |
- } |
| 315 |
- |
| 316 |
- #[cfg(not(feature = \"mockbuild\"))] |
| 317 |
- pub fn non_sysroot_api() { |
| 318 |
- core::custom_api(); |
| 319 |
- alloc::custom_api(); |
| 320 |
- } |
| 321 |
- ", |
| 322 |
- ) |
| 323 |
- .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true)) |
| 324 |
- .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true)) |
| 325 |
- .feature( |
| 326 |
- "mockbuild", |
| 327 |
- &["rustc-std-workspace-core", "rustc-std-workspace-alloc"], |
| 328 |
- ) |
| 329 |
- .publish(); |
| 330 |
- Package::new("registry-dep-using-std", "1.0.0") |
| 331 |
- .file( |
| 332 |
- "src/lib.rs", |
| 333 |
- " |
| 334 |
- #[cfg(feature = \"mockbuild\")] |
| 335 |
- pub fn custom_api() { |
| 336 |
- } |
| 337 |
- |
| 338 |
- #[cfg(not(feature = \"mockbuild\"))] |
| 339 |
- pub fn non_sysroot_api() { |
| 340 |
- std::custom_api(); |
| 341 |
- } |
| 342 |
- ", |
| 343 |
- ) |
| 344 |
- .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true)) |
| 345 |
- .feature("mockbuild", &["rustc-std-workspace-std"]) |
| 346 |
- .publish(); |
| 347 |
|
| 348 |
let p = ProjectBuilder::new(paths::root().join("rustc-wrapper")) |
| 349 |
.file( |
| 350 |
@@ -335,6 +282,81 @@ fn depend_same_as_std() { |
| 351 |
fn test() { |
| 352 |
let setup = setup(); |
| 353 |
|
| 354 |
+ // Our mock sysroot requires a few packages from crates.io, so make sure |
| 355 |
+ // they're "published" to crates.io. Also edit their code a bit to make sure |
| 356 |
+ // that they have access to our custom crates with custom apis. |
| 357 |
+ Package::new("registry-dep-using-core", "1.0.0") |
| 358 |
+ .file( |
| 359 |
+ "src/lib.rs", |
| 360 |
+ " |
| 361 |
+ #![no_std] |
| 362 |
+ |
| 363 |
+ #[cfg(feature = \"mockbuild\")] |
| 364 |
+ pub fn custom_api() { |
| 365 |
+ } |
| 366 |
+ |
| 367 |
+ #[cfg(not(feature = \"mockbuild\"))] |
| 368 |
+ pub fn non_sysroot_api() { |
| 369 |
+ core::custom_api(); |
| 370 |
+ } |
| 371 |
+ ", |
| 372 |
+ ) |
| 373 |
+ .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true)) |
| 374 |
+ .feature("mockbuild", &["rustc-std-workspace-core"]) |
| 375 |
+ .publish(); |
| 376 |
+ Package::new("registry-dep-using-alloc", "1.0.0") |
| 377 |
+ .file( |
| 378 |
+ "src/lib.rs", |
| 379 |
+ " |
| 380 |
+ #![no_std] |
| 381 |
+ |
| 382 |
+ extern crate alloc; |
| 383 |
+ |
| 384 |
+ #[cfg(feature = \"mockbuild\")] |
| 385 |
+ pub fn custom_api() { |
| 386 |
+ } |
| 387 |
+ |
| 388 |
+ #[cfg(not(feature = \"mockbuild\"))] |
| 389 |
+ pub fn non_sysroot_api() { |
| 390 |
+ core::custom_api(); |
| 391 |
+ alloc::custom_api(); |
| 392 |
+ } |
| 393 |
+ ", |
| 394 |
+ ) |
| 395 |
+ .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true)) |
| 396 |
+ .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true)) |
| 397 |
+ .feature( |
| 398 |
+ "mockbuild", |
| 399 |
+ &["rustc-std-workspace-core", "rustc-std-workspace-alloc"], |
| 400 |
+ ) |
| 401 |
+ .publish(); |
| 402 |
+ Package::new("registry-dep-using-std", "1.0.0") |
| 403 |
+ .file( |
| 404 |
+ "src/lib.rs", |
| 405 |
+ " |
| 406 |
+ #[cfg(feature = \"mockbuild\")] |
| 407 |
+ pub fn custom_api() { |
| 408 |
+ } |
| 409 |
+ |
| 410 |
+ #[cfg(not(feature = \"mockbuild\"))] |
| 411 |
+ pub fn non_sysroot_api() { |
| 412 |
+ std::custom_api(); |
| 413 |
+ } |
| 414 |
+ ", |
| 415 |
+ ) |
| 416 |
+ .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true)) |
| 417 |
+ .feature("mockbuild", &["rustc-std-workspace-std"]) |
| 418 |
+ .publish(); |
| 419 |
+ Package::new("registry-dep-only-used-by-test", "1.0.0") |
| 420 |
+ .file( |
| 421 |
+ "src/lib.rs", |
| 422 |
+ " |
| 423 |
+ pub fn wow_testing_is_so_easy() { |
| 424 |
+ } |
| 425 |
+ ", |
| 426 |
+ ) |
| 427 |
+ .publish(); |
| 428 |
+ |
| 429 |
let p = project() |
| 430 |
.file( |
| 431 |
"src/lib.rs", |