mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-25 11:14:46 +00:00
do not pass cdylib link args to test (#15317)
<!-- Thanks for submitting a pull request 🎉! Here are some tips for you: * If this is your first contribution, read "Cargo Contribution Guide" first: https://doc.crates.io/contrib/ * Run `cargo fmt --all` to format your code changes. * Small commits and pull requests are always preferable and easy to review. * If your idea is large and needs feedback from the community, read how: https://doc.crates.io/contrib/process/#working-on-large-features * Cargo takes care of compatibility. Read our design principles: https://doc.crates.io/contrib/design.html * When changing help text of cargo commands, follow the steps to generate docs: https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages * If your PR is not finished, set it as "draft" PR or add "WIP" in its title. * It's ok to use the CI resources to test your PR, but please don't abuse them. ### What does this PR try to resolve? Explain the motivation behind this change. A clear overview along with an in-depth explanation are helpful. You can use `Fixes #<issue number>` to associate this PR to an existing issue. ### How should we test and review this PR? Demonstrate how you test this change and guide reviewers through your PR. With a smooth review process, a pull request usually gets reviewed quicker. If you don't know how to write and run your tests, please read the guide: https://doc.crates.io/contrib/tests ### Additional information Other information you want to mention in this PR, such as prior arts, future extensions, an unresolved problem, or a TODO list. --> fixes #12663
This commit is contained in:
commit
6a70640c91
@ -262,7 +262,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
|
||||
}
|
||||
|
||||
for (lt, arg) in &output.linker_args {
|
||||
if lt.applies_to(&unit.target) {
|
||||
if lt.applies_to(&unit.target, unit.mode) {
|
||||
args.push("-C".into());
|
||||
args.push(format!("link-arg={}", arg).into());
|
||||
}
|
||||
|
@ -37,6 +37,7 @@ use crate::core::compiler::build_runner::UnitHash;
|
||||
use crate::core::compiler::fingerprint::DirtyReason;
|
||||
use crate::core::compiler::job_queue::JobState;
|
||||
use crate::core::{profiles::ProfileRoot, PackageId, Target};
|
||||
use crate::util::command_prelude::CompileMode;
|
||||
use crate::util::errors::CargoResult;
|
||||
use crate::util::internal;
|
||||
use crate::util::machine_message::{self, Message};
|
||||
@ -196,10 +197,11 @@ pub enum LinkArgTarget {
|
||||
|
||||
impl LinkArgTarget {
|
||||
/// Checks if this link type applies to a given [`Target`].
|
||||
pub fn applies_to(&self, target: &Target) -> bool {
|
||||
pub fn applies_to(&self, target: &Target, mode: CompileMode) -> bool {
|
||||
let is_test = mode.is_any_test();
|
||||
match self {
|
||||
LinkArgTarget::All => true,
|
||||
LinkArgTarget::Cdylib => target.is_cdylib(),
|
||||
LinkArgTarget::Cdylib => !is_test && target.is_cdylib(),
|
||||
LinkArgTarget::Bin => target.is_bin(),
|
||||
LinkArgTarget::SingleBin(name) => target.is_bin() && target.name() == name,
|
||||
LinkArgTarget::Test => target.is_test(),
|
||||
|
@ -360,6 +360,7 @@ fn rustc(
|
||||
pass_l_flag,
|
||||
&target,
|
||||
current_id,
|
||||
mode,
|
||||
)?;
|
||||
add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, &root_output)?;
|
||||
}
|
||||
@ -494,6 +495,7 @@ fn rustc(
|
||||
pass_l_flag: bool,
|
||||
target: &Target,
|
||||
current_id: PackageId,
|
||||
mode: CompileMode,
|
||||
) -> CargoResult<()> {
|
||||
for key in build_scripts.to_link.iter() {
|
||||
let output = build_script_outputs.get(key.1).ok_or_else(|| {
|
||||
@ -520,7 +522,9 @@ fn rustc(
|
||||
// clause should have been kept in the `if` block above. For
|
||||
// now, continue allowing it for cdylib only.
|
||||
// See https://github.com/rust-lang/cargo/issues/9562
|
||||
if lt.applies_to(target) && (key.0 == current_id || *lt == LinkArgTarget::Cdylib) {
|
||||
if lt.applies_to(target, mode)
|
||||
&& (key.0 == current_id || *lt == LinkArgTarget::Cdylib)
|
||||
{
|
||||
rustc.arg("-C").arg(format!("link-arg={}", arg));
|
||||
}
|
||||
}
|
||||
|
@ -443,3 +443,40 @@ fn cdylib_both_forms() {
|
||||
"#]])
|
||||
.run();
|
||||
}
|
||||
|
||||
// https://github.com/rust-lang/cargo/issues/12663
|
||||
#[cargo_test]
|
||||
fn cdylib_extra_link_args_should_not_apply_to_unit_tests() {
|
||||
let p = project()
|
||||
.file(
|
||||
"Cargo.toml",
|
||||
r#"
|
||||
[package]
|
||||
name = "foo"
|
||||
version = "0.5.0"
|
||||
edition = "2015"
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib", "cdylib"]
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#[test]
|
||||
fn noop() {}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"build.rs",
|
||||
r#"
|
||||
fn main() {
|
||||
// This would fail if cargo passed `-lhack` to building the test because `hack` doesn't exist.
|
||||
println!("cargo::rustc-link-arg-cdylib=-lhack");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
p.cargo("test --lib").run();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user