Skip to content

Commit

Permalink
fix(bindeps): assumed host target and optional = true coexist
Browse files Browse the repository at this point in the history
`{ …, artifact = "bin", target = "target" }` should also be considered
to being built for `FeaturesFor::ArtifactDep` instead of `NormalOrDev`.

[This line][1] requests for `ArtifactDep` but [here][2] Cargo ignore
assumed host target of artifact dep.

Change it to explicit set host target triple when

- `{ …, target = "target" }`, and
- `--target` is not presented, meaning it would be build on host platform.

[1]: https://github.com/rust-lang/cargo/blob/1382b44e431f58199afdffc2b757962b0e163602/src/cargo/core/compiler/unit_dependencies.rs#L887
[2]: https://github.com/rust-lang/cargo/blob/1382b44e431f58199afdffc2b757962b0e163602/src/cargo/core/resolver/features.rs#L857
  • Loading branch information
weihanglo committed Nov 28, 2022
1 parent 67e14be commit ce9d5ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,14 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
ArtifactTarget::BuildDependencyAssumeTarget => self
.requested_targets
.iter()
.filter_map(|kind| match kind {
CompileKind::Host => None,
CompileKind::Target(target) => {
Some(FeaturesFor::ArtifactDep(*target))
.map(|kind| match kind {
CompileKind::Host => {
let host_triple = self.target_data.rustc.host;
CompileTarget::new(&host_triple).unwrap()
}
CompileKind::Target(target) => *target,
})
.map(FeaturesFor::ArtifactDep)
.collect(),
}),
)
Expand Down
15 changes: 11 additions & 4 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2399,7 +2399,6 @@ fn with_target_and_optional() {

#[cargo_test]
fn with_assumed_host_target_and_optional_build_dep() {
// This exercises an incorrect behaviour got to be fixed by the following commit.
let p = project()
.file(
"Cargo.toml",
Expand Down Expand Up @@ -2435,8 +2434,16 @@ fn with_assumed_host_target_and_optional_build_dep() {

p.cargo("check -Z bindeps -F d1 -v")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_status(101)
.with_stderr_contains("[ERROR] failed to run custom build command for `foo v0.0.1 ([..])`")
.with_stderr_contains("[..]thread '[..]' panicked at 'called `Result::unwrap()`[..]")
.with_stderr_unordered(
"\
[COMPILING] foo v0.0.1 ([CWD])
[COMPILING] d1 v0.0.1 ([CWD]/d1)
[RUNNING] `rustc --crate-name build_script_build [..]--crate-type bin[..]
[RUNNING] `rustc --crate-name d1 [..]--crate-type bin[..]
[RUNNING] `[CWD]/target/debug/build/foo-[..]/build-script-build`
[RUNNING] `rustc --crate-name foo [..]--cfg[..]d1[..]
[FINISHED] dev [..]
",
)
.run();
}

0 comments on commit ce9d5ff

Please sign in to comment.