Skip to content

Commit 9dbf5b0

Browse files
committed
do not pass cdylib link args to test
1 parent 662213b commit 9dbf5b0

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

src/cargo/core/compiler/build_runner/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
262262
}
263263

264264
for (lt, arg) in &output.linker_args {
265-
if lt.applies_to(&unit.target) {
265+
if lt.applies_to(&unit.target, unit.mode) {
266266
args.push("-C".into());
267267
args.push(format!("link-arg={}", arg).into());
268268
}

src/cargo/core/compiler/custom_build.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::core::compiler::build_runner::UnitHash;
3737
use crate::core::compiler::fingerprint::DirtyReason;
3838
use crate::core::compiler::job_queue::JobState;
3939
use crate::core::{profiles::ProfileRoot, PackageId, Target};
40+
use crate::util::command_prelude::CompileMode;
4041
use crate::util::errors::CargoResult;
4142
use crate::util::internal;
4243
use crate::util::machine_message::{self, Message};
@@ -196,10 +197,11 @@ pub enum LinkArgTarget {
196197

197198
impl LinkArgTarget {
198199
/// Checks if this link type applies to a given [`Target`].
199-
pub fn applies_to(&self, target: &Target) -> bool {
200+
pub fn applies_to(&self, target: &Target, mode: CompileMode) -> bool {
201+
let is_test = mode.is_any_test();
200202
match self {
201203
LinkArgTarget::All => true,
202-
LinkArgTarget::Cdylib => target.is_cdylib(),
204+
LinkArgTarget::Cdylib => !is_test && target.is_cdylib(),
203205
LinkArgTarget::Bin => target.is_bin(),
204206
LinkArgTarget::SingleBin(name) => target.is_bin() && target.name() == name,
205207
LinkArgTarget::Test => target.is_test(),

src/cargo/core/compiler/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ fn rustc(
360360
pass_l_flag,
361361
&target,
362362
current_id,
363+
mode,
363364
)?;
364365
add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, &root_output)?;
365366
}
@@ -494,6 +495,7 @@ fn rustc(
494495
pass_l_flag: bool,
495496
target: &Target,
496497
current_id: PackageId,
498+
mode: CompileMode,
497499
) -> CargoResult<()> {
498500
for key in build_scripts.to_link.iter() {
499501
let output = build_script_outputs.get(key.1).ok_or_else(|| {
@@ -520,7 +522,9 @@ fn rustc(
520522
// clause should have been kept in the `if` block above. For
521523
// now, continue allowing it for cdylib only.
522524
// See https://github.com/rust-lang/cargo/issues/9562
523-
if lt.applies_to(target) && (key.0 == current_id || *lt == LinkArgTarget::Cdylib) {
525+
if lt.applies_to(target, mode)
526+
&& (key.0 == current_id || *lt == LinkArgTarget::Cdylib)
527+
{
524528
rustc.arg("-C").arg(format!("link-arg={}", arg));
525529
}
526530
}

tests/testsuite/build_script_extra_link_arg.rs

+37
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,40 @@ fn cdylib_both_forms() {
443443
"#]])
444444
.run();
445445
}
446+
447+
// https://github.com/rust-lang/cargo/issues/12663
448+
#[cargo_test]
449+
fn cdylib_extra_link_args_should_not_apply_to_unit_tests() {
450+
let p = project()
451+
.file(
452+
"Cargo.toml",
453+
r#"
454+
[package]
455+
name = "foo"
456+
version = "0.5.0"
457+
edition = "2015"
458+
459+
[lib]
460+
crate-type = ["lib", "cdylib"]
461+
"#,
462+
)
463+
.file(
464+
"src/lib.rs",
465+
r#"
466+
#[test]
467+
fn noop() {}
468+
"#,
469+
)
470+
.file(
471+
"build.rs",
472+
r#"
473+
fn main() {
474+
// This would fail if cargo passed `-lhack` to building the test because `hack` doesn't exist.
475+
println!("cargo::rustc-link-arg-cdylib=-lhack");
476+
}
477+
"#,
478+
)
479+
.build();
480+
481+
p.cargo("test --lib").run();
482+
}

0 commit comments

Comments
 (0)