Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Build only the specified artifact library when multiple types are available #13842

Merged
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
13 changes: 8 additions & 5 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::core::compiler::unit_graph::{UnitDep, UnitGraph};
use crate::core::compiler::{
CompileKind, CompileMode, CrateType, RustcTargetData, Unit, UnitInterner,
};
use crate::core::dependency::{Artifact, ArtifactTarget, DepKind};
use crate::core::dependency::{Artifact, ArtifactKind, ArtifactTarget, DepKind};
use crate::core::profiles::{Profile, Profiles, UnitFor};
use crate::core::resolver::features::{FeaturesFor, ResolvedFeatures};
use crate::core::resolver::Resolve;
Expand Down Expand Up @@ -555,17 +555,20 @@ fn artifact_targets_to_unit_deps(
let ret =
match_artifacts_kind_with_targets(dep, artifact_pkg.targets(), parent.pkg.name().as_str())?
.into_iter()
.map(|(_artifact_kind, target)| target)
.flat_map(|target| {
.flat_map(|(artifact_kind, target)| {
// We split target libraries into individual units, even though rustc is able
// to produce multiple kinds in an single invocation for the sole reason that
// to produce multiple kinds in a single invocation for the sole reason that
// each artifact kind has its own output directory, something we can't easily
// teach rustc for now.
match target.kind() {
TargetKind::Lib(kinds) => Box::new(
kinds
.iter()
.filter(|tk| matches!(tk, CrateType::Cdylib | CrateType::Staticlib))
.filter(move |tk| match (tk, artifact_kind) {
(CrateType::Cdylib, ArtifactKind::Cdylib) => true,
(CrateType::Staticlib, ArtifactKind::Staticlib) => true,
_ => false,
})
.map(|target_kind| {
new_unit_dep(
state,
Expand Down
74 changes: 74 additions & 0 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3189,3 +3189,77 @@ fn check_transitive_artifact_dependency_with_different_target() {
.with_status(101)
.run();
}

#[cargo_test]
fn build_only_specified_artifact_library() {
// Create a project with:
// - A crate `bar` with both `staticlib` and `cdylib` as crate-types.
// - A crate `foo` which depends on either the `staticlib` or `cdylib` artifact of bar,
// whose build-script simply checks which library artifacts are present.
let create_project = |artifact_lib| {
project()
.file(
"bar/Cargo.toml",
r#"
[package]
name = "bar"
version = "1.0.0"

[lib]
crate-type = ["staticlib", "cdylib"]
"#,
)
.file("bar/src/lib.rs", "")
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "1.0.0"

[build-dependencies]
bar = {{ path = "bar", artifact = "{artifact_lib}" }}
"#),
)
.file("src/lib.rs", "")
.file(
"build.rs",
r#"
fn main() {
println!("cdylib present: {}", std::env::var_os("CARGO_CDYLIB_FILE_BAR").is_some());
println!("staticlib present: {}", std::env::var_os("CARGO_STATICLIB_FILE_BAR").is_some());
}
"#,
)
.build()
};

let cdylib = create_project("cdylib");
cdylib
.cargo("build -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.run();
match_exact(
"cdylib present: true\nstaticlib present: false",
skogseth marked this conversation as resolved.
Show resolved Hide resolved
&build_script_output_string(&cdylib, "foo"),
"build script output",
"",
None,
)
.unwrap();

let staticlib = create_project("staticlib");
staticlib
.cargo("build -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.run();
match_exact(
weihanglo marked this conversation as resolved.
Show resolved Hide resolved
"cdylib present: false\nstaticlib present: true",
skogseth marked this conversation as resolved.
Show resolved Hide resolved
&build_script_output_string(&staticlib, "foo"),
"build script output",
"",
None,
)
.unwrap();
}