Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,10 +938,22 @@ impl<'cfg> RustcTargetData<'cfg> {
}

/// Information about the given target platform, learned by querying rustc.
///
/// # Panics
///
/// Panics, if the target platform described by `kind` can't be found.
/// See [`get_info`](Self::get_info) for a non-panicking alternative.
pub fn info(&self, kind: CompileKind) -> &TargetInfo {
self.get_info(kind).unwrap()
}

/// Information about the given target platform, learned by querying rustc.
///
/// Returns `None` if the target platform described by `kind` can't be found.
pub fn get_info(&self, kind: CompileKind) -> Option<&TargetInfo> {
match kind {
CompileKind::Host => &self.host_info,
CompileKind::Target(s) => &self.target_info[&s],
CompileKind::Host => Some(&self.host_info),
CompileKind::Target(s) => self.target_info.get(&s),
}
}

Expand Down
40 changes: 30 additions & 10 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,7 @@ impl<'cfg> Compilation<'cfg> {
.info(CompileKind::Host)
.sysroot_host_libdir
.clone(),
sysroot_target_libdir: bcx
.all_kinds
.iter()
.map(|&kind| {
(
kind,
bcx.target_data.info(kind).sysroot_target_libdir.clone(),
)
})
.collect(),
sysroot_target_libdir: get_sysroot_target_libdir(bcx)?,
tests: Vec::new(),
binaries: Vec::new(),
cdylibs: Vec::new(),
Expand Down Expand Up @@ -384,6 +375,35 @@ fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
cmd
}

fn get_sysroot_target_libdir(
bcx: &BuildContext<'_, '_>,
) -> CargoResult<HashMap<CompileKind, PathBuf>> {
bcx.all_kinds
.iter()
.map(|&kind| {
let Some(info) = bcx.target_data.get_info(kind) else {
let target = match kind {
CompileKind::Host => "host".to_owned(),
CompileKind::Target(s) => s.short_name().to_owned(),
};

let dependency = bcx
.unit_graph
.iter()
.find_map(|(u, _)| (u.kind == kind).then_some(u.pkg.summary().package_id()))
.unwrap();

anyhow::bail!(
"could not find specification for target `{target}`.\n \
Dependency `{dependency}` requires to build for target `{target}`."
)
};

Ok((kind, info.sysroot_target_libdir.clone()))
})
.collect()
}

fn target_runner(
bcx: &BuildContext<'_, '_>,
kind: CompileKind,
Expand Down
Loading