Skip to content

Commit 4b8a167

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

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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(|| {
@@ -521,7 +523,9 @@ fn rustc(
521523
// now, continue allowing it for cdylib only.
522524
// See https://github.com/rust-lang/cargo/issues/9562
523525
if lt.applies_to(target) && (key.0 == current_id || *lt == LinkArgTarget::Cdylib) {
524-
rustc.arg("-C").arg(format!("link-arg={}", arg));
526+
if !mode.is_any_test() || target.is_test() || target.is_bench() {
527+
rustc.arg("-C").arg(format!("link-arg={}", arg));
528+
}
525529
}
526530
}
527531
}

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 = ["wycats@example.com"]
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)