Skip to content

Commit

Permalink
Make cargo:rustc-link-arg-bin without the = an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed May 29, 2021
1 parent f676b49 commit 836e537
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,29 +599,32 @@ impl BuildOutput {
}
"rustc-link-arg-bin" => {
if extra_link_arg {
let parts = value.splitn(2, "=").collect::<Vec<_>>();
if parts.len() == 2 {
let bin_name = parts[0].to_string();
if !targets
.iter()
.any(|target| target.is_bin() && target.name() == bin_name)
{
bail!(
"invalid instruction `cargo:{}` from {}\n\
The package {} does not have a bin target with the name `{}`.",
key,
whence,
pkg_descr,
bin_name
);
}
linker_args.push((LinkType::SingleBin(bin_name), parts[1].to_string()));
} else {
warnings.push(format!(
"cargo:{} has invalid syntax: expected `cargo:{}=BIN=ARG`",
key, key
));
let mut parts = value.splitn(2, '=');
let bin_name = parts.next().unwrap().to_string();
let arg = parts.next().ok_or_else(|| {
anyhow::format_err!(
"invalid instruction `cargo:{}={}` from {}\n\
The instruction should have the form cargo:{}=BIN=ARG",
key,
value,
whence,
key
)
})?;
if !targets
.iter()
.any(|target| target.is_bin() && target.name() == bin_name)
{
bail!(
"invalid instruction `cargo:{}` from {}\n\
The package {} does not have a bin target with the name `{}`.",
key,
whence,
pkg_descr,
bin_name
);
}
linker_args.push((LinkType::SingleBin(bin_name), arg.to_string()));
} else {
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
}
Expand Down
17 changes: 17 additions & 0 deletions tests/testsuite/build_script_extra_link_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target.
[COMPILING] foo [..]
error: invalid instruction `cargo:rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)`
The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `abc`.
",
)
.run();

p.change_file(
"build.rs",
r#"fn main() { println!("cargo:rustc-link-arg-bin=abc"); }"#,
);

p.cargo("check -Zextra-link-arg")
.masquerade_as_nightly_cargo()
.with_status(101)
.with_stderr(
"\
[COMPILING] foo [..]
error: invalid instruction `cargo:rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)`
The instruction should have the form cargo:rustc-link-arg-bin=BIN=ARG
",
)
.run();
Expand Down

0 comments on commit 836e537

Please sign in to comment.