Skip to content

Commit eadb4fc

Browse files
committed
Check if rust-src contains a vendor dir, and patch it in
This is the cargo side of rust-lang/wg-cargo-std-aware#23
1 parent d5556ae commit eadb4fc

File tree

15 files changed

+188
-72
lines changed

15 files changed

+188
-72
lines changed

src/cargo/core/compiler/standard_lib.rs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::ops::{self, Packages};
1010
use crate::util::errors::CargoResult;
1111
use std::collections::{HashMap, HashSet};
1212
use std::env;
13+
use std::fs;
1314
use std::path::PathBuf;
1415

1516
/// Parse the `-Zbuild-std` flag.
@@ -38,28 +39,46 @@ pub fn resolve_std<'cfg>(
3839
crates: &[String],
3940
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
4041
let src_path = detect_sysroot_src_path(target_data)?;
41-
let to_patch = [
42-
"rustc-std-workspace-core",
43-
"rustc-std-workspace-alloc",
44-
"rustc-std-workspace-std",
45-
];
46-
let patches = to_patch
47-
.iter()
48-
.map(|&name| {
49-
let source_path = SourceId::for_path(&src_path.join("library").join(name))?;
50-
let dep = Dependency::parse_no_deprecated(name, None, source_path)?;
42+
43+
// Special std packages should be pulled from `library/` and should be
44+
// prefixed with `rustc-std-workspace-` in certain places.
45+
let libs_prefix = "library/";
46+
let special_std_prefix = "rustc-std-workspace-";
47+
let libs_path = src_path.join(libs_prefix);
48+
49+
// Crates in rust-src to build. libtest is in some sense the "root" package
50+
// of std, as nothing else depends on it, so it must be explicitly added.
51+
let mut members = vec![format!("{}test", libs_prefix)];
52+
53+
// If rust-src contains a "vendor" directory, then patch in all the crates it contains.
54+
let vendor_path = src_path.join("vendor");
55+
let vendor_dir = fs::read_dir(vendor_path)?;
56+
let patches = vendor_dir
57+
.into_iter()
58+
.map(|entry| {
59+
let entry = entry?;
60+
let name = entry
61+
.file_name()
62+
.into_string()
63+
.map_err(|_| anyhow::anyhow!("package name wasn't utf8"))?;
64+
65+
// Remap the rustc-std-workspace crates to the actual rust-src libraries
66+
let path = if let Some(real_name) = name.strip_prefix(special_std_prefix) {
67+
// Record this crate as something to build in the workspace
68+
members.push(format!("{}{}", libs_prefix, real_name));
69+
libs_path.join(&name)
70+
} else {
71+
entry.path()
72+
};
73+
let source_path = SourceId::for_path(&path)?;
74+
let dep = Dependency::parse_no_deprecated(&name, None, source_path)?;
5175
Ok(dep)
5276
})
5377
.collect::<CargoResult<Vec<_>>>()?;
78+
5479
let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap();
5580
let mut patch = HashMap::new();
5681
patch.insert(crates_io_url, patches);
57-
let members = vec![
58-
String::from("library/std"),
59-
String::from("library/core"),
60-
String::from("library/alloc"),
61-
String::from("library/test"),
62-
];
6382
let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new(
6483
&src_path,
6584
&Some(members),

tests/testsuite/mock-std/library/test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ std = { path = "../std" }
1010
panic_unwind = { path = "../panic_unwind" }
1111
compiler_builtins = { path = "../compiler_builtins" }
1212
registry-dep-using-std = { version = "*", features = ['mockbuild'] }
13+
registry-dep-only-used-by-test = { version = "*" }
1314

1415
[features]
1516
panic-unwind = []

tests/testsuite/mock-std/library/test/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ extern crate test;
77
pub use test::*;
88

99
pub fn custom_api() {
10+
registry_dep_only_used_by_test::wow_testing_is_so_easy();
1011
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "registry-dep-only-used-by-test"
3+
version = "1.0.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
9+
[features]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub fn wow_testing_is_so_easy() {
2+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "registry-dep-using-alloc"
3+
version = "1.0.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
rustc-std-workspace-alloc = { version = "*", optional = true }
9+
rustc-std-workspace-core = { version = "*", optional = true }
10+
11+
[features]
12+
mockbuild = ["rustc-std-workspace-alloc", "rustc-std-workspace-core"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[cfg(feature = "mockbuild")]
2+
pub fn custom_api() {
3+
}
4+
5+
#[cfg(not(feature = "mockbuild"))]
6+
pub fn non_sysroot_api() {
7+
core::custom_api();
8+
alloc::custom_api();
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "registry-dep-using-core"
3+
version = "1.0.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
rustc-std-workspace-core = { version = "*", optional = true }
9+
10+
[features]
11+
mockbuild = ["rustc-std-workspace-core"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[cfg(feature = "mockbuild")]
2+
pub fn custom_api() {
3+
}
4+
5+
#[cfg(not(feature = "mockbuild"))]
6+
pub fn non_sysroot_api() {
7+
core::custom_api();
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "registry-dep-using-std"
3+
version = "1.0.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
rustc-std-workspace-std = { version = "*", optional = true }
9+
10+
[features]
11+
mockbuild = ["rustc-std-workspace-std"]

0 commit comments

Comments
 (0)