Skip to content

Commit

Permalink
Auto merge of #11643 - jofas:11260-fix, r=weihanglo
Browse files Browse the repository at this point in the history
Error message for transitive artifact dependencies with targets the package doesn't directly interact with

Address #11260. Produces an error message like described by `@weihanglo` [here](#11260 (comment)):

```
error: could not find specification for target "x86_64-windows-msvc"
  Dependency `bar v0.1.0` requires to build for target "x86_64-windows-msvc".
```

Note that this is not a complete fix for #11260.
  • Loading branch information
bors committed Feb 25, 2023
2 parents ec65242 + 4bdface commit 524231f
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 87 deletions.
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 @@ -984,10 +984,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 @@ -130,16 +130,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 @@ -385,6 +376,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

0 comments on commit 524231f

Please sign in to comment.