Skip to content

Commit d3454cb

Browse files
committed
Add tests and warnings for -Zextra-link-arg.
1 parent 13b2624 commit d3454cb

File tree

4 files changed

+108
-15
lines changed

4 files changed

+108
-15
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
289289
paths::create_dir_all(&script_dir)?;
290290
paths::create_dir_all(&script_out_dir)?;
291291

292+
let extra_link_arg = cx.bcx.config.cli_unstable().extra_link_arg;
293+
292294
// Prepare the unit of "dirty work" which will actually run the custom build
293295
// command.
294296
//
@@ -392,8 +394,13 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
392394
paths::set_file_time_no_err(output_file, timestamp);
393395
paths::write(&err_file, &output.stderr)?;
394396
paths::write(&root_output_file, util::path2bytes(&script_out_dir)?)?;
395-
let parsed_output =
396-
BuildOutput::parse(&output.stdout, &pkg_name, &script_out_dir, &script_out_dir)?;
397+
let parsed_output = BuildOutput::parse(
398+
&output.stdout,
399+
&pkg_name,
400+
&script_out_dir,
401+
&script_out_dir,
402+
extra_link_arg,
403+
)?;
397404

398405
if json_messages {
399406
emit_build_output(state, &parsed_output, script_out_dir.as_path(), id);
@@ -417,6 +424,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
417424
&pkg_name,
418425
&prev_script_out_dir,
419426
&script_out_dir,
427+
extra_link_arg,
420428
)?,
421429
};
422430

@@ -466,13 +474,15 @@ impl BuildOutput {
466474
pkg_name: &str,
467475
script_out_dir_when_generated: &Path,
468476
script_out_dir: &Path,
477+
extra_link_arg: bool,
469478
) -> CargoResult<BuildOutput> {
470479
let contents = paths::read_bytes(path)?;
471480
BuildOutput::parse(
472481
&contents,
473482
pkg_name,
474483
script_out_dir_when_generated,
475484
script_out_dir,
485+
extra_link_arg,
476486
)
477487
}
478488

@@ -483,6 +493,7 @@ impl BuildOutput {
483493
pkg_name: &str,
484494
script_out_dir_when_generated: &Path,
485495
script_out_dir: &Path,
496+
extra_link_arg: bool,
486497
) -> CargoResult<BuildOutput> {
487498
let mut library_paths = Vec::new();
488499
let mut library_links = Vec::new();
@@ -536,8 +547,20 @@ impl BuildOutput {
536547
"rustc-link-lib" => library_links.push(value.to_string()),
537548
"rustc-link-search" => library_paths.push(PathBuf::from(value)),
538549
"rustc-cdylib-link-arg" => linker_args.push((Some(LinkType::Cdylib), value)),
539-
"rustc-bin-link-arg" => linker_args.push((Some(LinkType::Bin), value)),
540-
"rustc-link-arg" => linker_args.push((None, value)),
550+
"rustc-bin-link-arg" => {
551+
if extra_link_arg {
552+
linker_args.push((Some(LinkType::Bin), value));
553+
} else {
554+
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
555+
}
556+
}
557+
"rustc-link-arg" => {
558+
if extra_link_arg {
559+
linker_args.push((None, value));
560+
} else {
561+
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
562+
}
563+
}
541564
"rustc-cfg" => cfgs.push(value.to_string()),
542565
"rustc-env" => env.push(BuildOutput::parse_rustc_env(&value, &whence)?),
543566
"warning" => warnings.push(value.to_string()),
@@ -786,12 +809,15 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
786809
.and_then(|bytes| util::bytes2path(&bytes))
787810
.unwrap_or_else(|_| script_out_dir.clone());
788811

812+
let extra_link_arg = cx.bcx.config.cli_unstable().extra_link_arg;
813+
789814
(
790815
BuildOutput::parse_file(
791816
&output_file,
792817
&unit.pkg.to_string(),
793818
&prev_script_out_dir,
794819
&script_out_dir,
820+
extra_link_arg,
795821
)
796822
.ok(),
797823
prev_script_out_dir,

src/cargo/core/compiler/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
215215
// don't pass the `-l` flags.
216216
let pass_l_flag = unit.target.is_lib() || !unit.pkg.targets().iter().any(|t| t.is_lib());
217217
let link_type = (&unit.target).into();
218-
let extra_link_arg = cx.bcx.config.cli_unstable().extra_link_arg;
219218

220219
let dep_info_name = match cx.files().metadata(unit) {
221220
Some(metadata) => format!("{}-{}.d", unit.target.crate_name(), metadata),
@@ -264,7 +263,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
264263
&build_scripts,
265264
pass_l_flag,
266265
link_type,
267-
extra_link_arg,
268266
current_id,
269267
)?;
270268
add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, &root_output)?;
@@ -347,7 +345,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
347345
build_scripts: &BuildScripts,
348346
pass_l_flag: bool,
349347
link_type: Option<LinkType>,
350-
extra_link_arg: bool,
351348
current_id: PackageId,
352349
) -> CargoResult<()> {
353350
for key in build_scripts.to_link.iter() {
@@ -370,14 +367,11 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
370367
}
371368
}
372369
if link_type.is_some() {
373-
output
374-
.linker_args
375-
.iter()
376-
.filter(|x| x.0.is_none() || x.0 == link_type)
377-
.filter(|x| x.0 == Some(LinkType::Cdylib) || extra_link_arg)
378-
.for_each(|x| {
379-
rustc.arg("-C").arg(format!("link-arg={}", x.1));
380-
});
370+
for (lt, arg) in &output.linker_args {
371+
if lt.is_none() || *lt == link_type {
372+
rustc.arg("-C").arg(format!("link-arg={}", arg));
373+
}
374+
}
381375
}
382376
}
383377
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! Tests for -Zextra-link-arg.
2+
3+
use cargo_test_support::{basic_bin_manifest, project};
4+
5+
#[cargo_test]
6+
fn build_script_extra_bin_link_args() {
7+
let p = project()
8+
.file("Cargo.toml", &basic_bin_manifest("foo"))
9+
.file("src/main.rs", "fn main() {}")
10+
.file(
11+
"build.rs",
12+
r#"
13+
fn main() {
14+
println!("cargo:rustc-bin-link-arg=--this-is-a-bogus-flag");
15+
}
16+
"#,
17+
)
18+
.build();
19+
20+
p.cargo("build -Zextra-link-arg -v")
21+
.masquerade_as_nightly_cargo()
22+
.with_status(101)
23+
.with_stderr_contains(
24+
"[RUNNING] `rustc --crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..]",
25+
)
26+
.run();
27+
}
28+
29+
#[cargo_test]
30+
fn build_script_extra_link_args() {
31+
let p = project()
32+
.file("Cargo.toml", &basic_bin_manifest("foo"))
33+
.file("src/main.rs", "fn main() {}")
34+
.file(
35+
"build.rs",
36+
r#"
37+
fn main() {
38+
println!("cargo:rustc-link-arg=--this-is-a-bogus-flag");
39+
}
40+
"#,
41+
)
42+
.build();
43+
44+
p.cargo("build -Zextra-link-arg -v")
45+
.masquerade_as_nightly_cargo()
46+
.with_status(101)
47+
.with_stderr_contains(
48+
"[RUNNING] `rustc --crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..]",
49+
)
50+
.run();
51+
}
52+
53+
#[cargo_test]
54+
fn build_script_extra_link_args_warn_on_stable() {
55+
let p = project()
56+
.file("Cargo.toml", &basic_bin_manifest("foo"))
57+
.file("src/main.rs", "fn main() {}")
58+
.file(
59+
"build.rs",
60+
r#"
61+
fn main() {
62+
println!("cargo:rustc-link-arg=--this-is-a-bogus-flag");
63+
}
64+
"#,
65+
)
66+
.build();
67+
68+
p.cargo("build -vv")
69+
.with_status(0)
70+
.with_stderr_contains("warning: cargo:rustc-link-arg requires -Zextra-link-arg flag")
71+
.run();
72+
}

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod build;
2020
mod build_plan;
2121
mod build_script;
2222
mod build_script_env;
23+
mod build_script_extra_link_arg;
2324
mod cache_messages;
2425
mod cargo_alias_config;
2526
mod cargo_command;

0 commit comments

Comments
 (0)