diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index de660380431..bad9ef77dc4 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -1177,12 +1177,18 @@ impl UnitFor { } else { self.panic_setting }; + let artifact_target_for_features = + if dep_target.proc_macro() || parent.target.is_custom_build() { + None + } else { + self.artifact_target_for_features + }; UnitFor { host: self.host || dep_for_host, host_features, panic_setting, root_compile_kind, - artifact_target_for_features: self.artifact_target_for_features, + artifact_target_for_features, } } diff --git a/src/cargo/core/resolver/features.rs b/src/cargo/core/resolver/features.rs index e7d4da8d1f4..5355bd871d5 100644 --- a/src/cargo/core/resolver/features.rs +++ b/src/cargo/core/resolver/features.rs @@ -906,11 +906,11 @@ impl<'a, 'gctx> FeatureResolver<'a, 'gctx> { // All this may result in a dependency being built multiple times // for various targets which are either specified in the manifest // or on the cargo command-line. - let lib_fk = if fk == FeaturesFor::default() { - (self.track_for_host - && (dep.is_build() || self.has_proc_macro_lib(dep_id))) - .then(|| FeaturesFor::HostDep) - .unwrap_or_default() + let lib_fk = if fk != FeaturesFor::HostDep + && self.track_for_host + && (dep.is_build() || self.has_proc_macro_lib(dep_id)) + { + FeaturesFor::HostDep } else { fk }; diff --git a/tests/testsuite/artifact_dep.rs b/tests/testsuite/artifact_dep.rs index 1850f6184ed..fb520f5fa3d 100644 --- a/tests/testsuite/artifact_dep.rs +++ b/tests/testsuite/artifact_dep.rs @@ -3394,3 +3394,179 @@ staticlib present: true "#]], ); } + +#[cargo_test] +fn artifact_dep_target_does_not_propagate_to_deps_of_build_script() { + if cross_compile_disabled() { + return; + } + let bindeps_target = cross_compile::alternate(); + let native_target = cross_compile::native(); + + let p = project() + .file( + "Cargo.toml", + &r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + resolver = "2" + + [dependencies.artifact] + path = "artifact" + artifact = "bin" + target = "$TARGET" + "# + .replace("$TARGET", bindeps_target), + ) + .file( + "src/main.rs", + r#" + fn main() { + let _b = include_bytes!(env!("CARGO_BIN_FILE_ARTIFACT")); + } + "#, + ) + .file( + "artifact/Cargo.toml", + r#" + [package] + name = "artifact" + version = "0.0.1" + edition = "2015" + + [build-dependencies] + builder = { path = "../builder" } + "#, + ) + .file("artifact/src/main.rs", "fn main() { }") + .file( + "artifact/build.rs", + r#" + extern crate builder; + fn main() { + let _ = builder::add(1, 2); + } + "#, + ) + .file( + "builder/Cargo.toml", + &r#" + [package] + name = "builder" + version = "0.0.1" + edition = "2015" + + [target.'$TARGET'.dependencies] + arch = { path = "../arch" } + "# + .replace("$TARGET", native_target), + ) + .file( + "builder/src/lib.rs", + r#" + extern crate arch; + pub fn add(a: i32, b: i32) -> i32 { arch::add(a, b) } + "#, + ) + .file( + "arch/Cargo.toml", + r#" + [package] + name = "arch" + version = "0.0.1" + edition = "2015" + "#, + ) + .file( + "arch/src/lib.rs", + r#"pub fn add(a: i32, b: i32) -> i32 { a + b }"#, + ) + .build(); + p.cargo("test -Z bindeps") + .masquerade_as_nightly_cargo(&["bindeps"]) + .run(); +} + +#[cargo_test] +fn artifact_dep_target_does_not_propagate_to_proc_macro() { + if cross_compile_disabled() { + return; + } + let bindeps_target = cross_compile::alternate(); + let native_target = cross_compile::native(); + + let p = project() + .file( + "Cargo.toml", + &r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + resolver = "2" + + [dependencies.artifact] + path = "artifact" + artifact = "bin" + target = "$TARGET" + "# + .replace("$TARGET", bindeps_target), + ) + .file( + "src/main.rs", + r#" + fn main() { + let _b = include_bytes!(env!("CARGO_BIN_FILE_ARTIFACT")); + } + "#, + ) + .file( + "artifact/Cargo.toml", + r#" + [package] + name = "artifact" + version = "0.0.1" + edition = "2015" + + [dependencies] + macro = { path = "../macro" } + "#, + ) + .file("artifact/src/main.rs", "fn main() { }") + .file( + "macro/Cargo.toml", + &r#" + [package] + name = "macro" + version = "0.0.1" + edition = "2015" + + [lib] + proc-macro = true + + [target.'$TARGET'.dependencies] + arch = { path = "../arch" } + "# + .replace("$TARGET", native_target), + ) + .file("macro/src/lib.rs", "") + .file( + "arch/Cargo.toml", + r#" + [package] + name = "arch" + version = "0.0.1" + edition = "2015" + "#, + ) + .file( + "arch/src/lib.rs", + "pub fn add(a: i32, b: i32) -> i32 { a + b }", + ) + .build(); + p.cargo("test -Z bindeps") + .masquerade_as_nightly_cargo(&["bindeps"]) + .run(); +}