Skip to content

Commit 9d98645

Browse files
committed
build-std: remove sysroot probe
1 parent 9c29861 commit 9d98645

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/cargo/core/compiler/build_context/target_info.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct TargetInfo {
2929
crate_types: RefCell<HashMap<String, Option<(String, String)>>>,
3030
/// `cfg` information extracted from `rustc --print=cfg`.
3131
cfg: Vec<Cfg>,
32+
/// Path to the sysroot.
33+
pub sysroot: PathBuf,
3234
/// Path to the "lib" or "bin" directory that rustc uses for its dynamic
3335
/// libraries.
3436
pub sysroot_host_libdir: PathBuf,
@@ -134,13 +136,13 @@ impl TargetInfo {
134136
output_err_info(&process, &output, &error)
135137
),
136138
};
137-
let mut sysroot_host_libdir = PathBuf::from(line);
138-
if cfg!(windows) {
139-
sysroot_host_libdir.push("bin");
139+
let sysroot = PathBuf::from(line);
140+
let sysroot_host_libdir = if cfg!(windows) {
141+
sysroot.join("bin")
140142
} else {
141-
sysroot_host_libdir.push("lib");
142-
}
143-
let mut sysroot_target_libdir = PathBuf::from(line);
143+
sysroot.join("lib")
144+
};
145+
let mut sysroot_target_libdir = sysroot.clone();
144146
sysroot_target_libdir.push("lib");
145147
sysroot_target_libdir.push("rustlib");
146148
sysroot_target_libdir.push(match &kind {
@@ -162,6 +164,7 @@ impl TargetInfo {
162164
Ok(TargetInfo {
163165
crate_type_process,
164166
crate_types: RefCell::new(map),
167+
sysroot,
165168
sysroot_host_libdir,
166169
sysroot_target_libdir,
167170
// recalculate `rustflags` from above now that we have `cfg`

src/cargo/core/compiler/standard_lib.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn resolve_std<'cfg>(
3636
requested_target: CompileKind,
3737
crates: &[String],
3838
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
39-
let src_path = detect_sysroot_src_path(ws)?;
39+
let src_path = detect_sysroot_src_path(target_data)?;
4040
let to_patch = [
4141
"rustc-std-workspace-core",
4242
"rustc-std-workspace-alloc",
@@ -163,21 +163,19 @@ pub fn generate_std_roots<'a>(
163163
.collect::<CargoResult<Vec<_>>>()
164164
}
165165

166-
fn detect_sysroot_src_path(ws: &Workspace<'_>) -> CargoResult<PathBuf> {
166+
fn detect_sysroot_src_path(target_data: &RustcTargetData) -> CargoResult<PathBuf> {
167167
if let Some(s) = env::var_os("__CARGO_TESTS_ONLY_SRC_ROOT") {
168168
return Ok(s.into());
169169
}
170170

171171
// NOTE: This is temporary until we figure out how to acquire the source.
172-
// If we decide to keep the sysroot probe, then BuildConfig will need to
173-
// be restructured so that the TargetInfo is created earlier and passed
174-
// in, so we don't have this extra call to rustc.
175-
let rustc = ws.config().load_global_rustc(Some(ws))?;
176-
let output = rustc.process().arg("--print=sysroot").exec_with_output()?;
177-
let s = String::from_utf8(output.stdout)
178-
.map_err(|e| anyhow::format_err!("rustc didn't return utf8 output: {:?}", e))?;
179-
let sysroot = PathBuf::from(s.trim());
180-
let src_path = sysroot.join("lib").join("rustlib").join("src").join("rust");
172+
let src_path = target_data
173+
.info(CompileKind::Host)
174+
.sysroot
175+
.join("lib")
176+
.join("rustlib")
177+
.join("src")
178+
.join("rust");
181179
let lock = src_path.join("Cargo.lock");
182180
if !lock.exists() {
183181
anyhow::bail!(

0 commit comments

Comments
 (0)