Skip to content

Commit

Permalink
Auto merge of #11434 - weihanglo:issue/10526, r=ehuss
Browse files Browse the repository at this point in the history
artifact deps should works when target field specified coexists with `optional = true`
  • Loading branch information
bors committed Dec 14, 2022
2 parents 06178d7 + eca3e23 commit cc0a320
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,10 @@ impl<'a, 'cfg> State<'a, 'cfg> {
// If this is an optional dependency, and the new feature resolver
// did not enable it, don't include it.
if dep.is_optional() {
let features_for = unit_for.map_to_features_for(dep.artifact());
// This `unit_for` is from parent dep and *SHOULD* contains its own
// artifact dep infomration inside `artifact_target_for_features`.
// So, no need to map any artifact info from an incorrect `dep.artifact()`.
let features_for = unit_for.map_to_features_for(IS_NO_ARTIFACT_DEP);
if !self.is_dep_activated(pkg_id, features_for, dep.name_in_toml()) {
return false;
}
Expand Down
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
105 changes: 105 additions & 0 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2342,3 +2342,108 @@ fn calc_bin_artifact_fingerprint() {
)
.run();
}

#[cargo_test]
fn with_target_and_optional() {
// See rust-lang/cargo#10526
if cross_compile::disabled() {
return;
}
let target = cross_compile::alternate();
let p = project()
.file(
"Cargo.toml",
&r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2021"
[dependencies]
d1 = { path = "d1", artifact = "bin", optional = true, target = "$TARGET" }
"#
.replace("$TARGET", target),
)
.file(
"src/main.rs",
r#"
fn main() {
let _b = include_bytes!(env!("CARGO_BIN_FILE_D1"));
}
"#,
)
.file(
"d1/Cargo.toml",
r#"
[package]
name = "d1"
version = "0.0.1"
edition = "2021"
"#,
)
.file("d1/src/main.rs", "fn main() {}")
.build();

p.cargo("check -Z bindeps -F d1 -v")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr(
"\
[COMPILING] d1 v0.0.1 [..]
[RUNNING] `rustc --crate-name d1 [..]--crate-type bin[..]
[CHECKING] foo v0.0.1 [..]
[RUNNING] `rustc --crate-name foo [..]--cfg[..]d1[..]
[FINISHED] dev [..]
",
)
.run();
}

#[cargo_test]
fn with_assumed_host_target_and_optional_build_dep() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2021"
[build-dependencies]
d1 = { path = "d1", artifact = "bin", optional = true, target = "target" }
"#,
)
.file("src/main.rs", "fn main() {}")
.file(
"build.rs",
r#"
fn main() {
std::env::var("CARGO_BIN_FILE_D1").unwrap();
}
"#,
)
.file(
"d1/Cargo.toml",
r#"
[package]
name = "d1"
version = "0.0.1"
edition = "2021"
"#,
)
.file("d1/src/main.rs", "fn main() {}")
.build();

p.cargo("check -Z bindeps -F d1 -v")
.masquerade_as_nightly_cargo(&["bindeps"])
.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 cc0a320

Please sign in to comment.