Skip to content

Commit 1609e02

Browse files
committed
do not pass cdylib link args to test
1 parent 437ff04 commit 1609e02

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 5 deletions
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,15 +197,16 @@ 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(),
203-
LinkArgTarget::Bin => target.is_bin(),
204-
LinkArgTarget::SingleBin(name) => target.is_bin() && target.name() == name,
204+
LinkArgTarget::Cdylib => !is_test && target.is_cdylib(),
205+
LinkArgTarget::Bin => !is_test && target.is_bin(),
206+
LinkArgTarget::SingleBin(name) => !is_test && target.is_bin() && target.name() == name,
205207
LinkArgTarget::Test => target.is_test(),
206208
LinkArgTarget::Bench => target.is_bench(),
207-
LinkArgTarget::Example => target.is_exe_example(),
209+
LinkArgTarget::Example => !is_test && target.is_exe_example(),
208210
}
209211
}
210212
}

src/cargo/core/compiler/mod.rs

Lines changed: 5 additions & 1 deletion
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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,39 @@ fn build_script_extra_link_arg_examples() {
403403
)
404404
.run();
405405
}
406+
407+
#[cargo_test]
408+
fn issue_12663() {
409+
let p = project()
410+
.file(
411+
"Cargo.toml",
412+
r#"
413+
[package]
414+
name = "foo"
415+
version = "0.5.0"
416+
authors = ["[email protected]"]
417+
edition = "2015"
418+
419+
[lib]
420+
crate-type = ["lib", "cdylib"]
421+
"#,
422+
)
423+
.file(
424+
"src/lib.rs",
425+
r#"
426+
#[test]
427+
fn noop() {}
428+
"#,
429+
)
430+
.file(
431+
"build.rs",
432+
r#"
433+
fn main() {
434+
println!("cargo::rustc-cdylib-link-arg=-lhack");
435+
}
436+
"#,
437+
)
438+
.build();
439+
440+
p.cargo("test --lib").run();
441+
}

0 commit comments

Comments
 (0)