Skip to content

Commit f676b49

Browse files
committed
Add some errors if rustc-link-arg-* specifies a non-existent target.
1 parent 795aab1 commit f676b49

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::job::{Freshness, Job, Work};
22
use super::{fingerprint, Context, LinkType, Unit};
33
use crate::core::compiler::context::Metadata;
44
use crate::core::compiler::job_queue::JobState;
5-
use crate::core::{profiles::ProfileRoot, PackageId};
5+
use crate::core::{profiles::ProfileRoot, PackageId, Target};
66
use crate::util::errors::CargoResult;
77
use crate::util::machine_message::{self, Message};
88
use crate::util::{internal, profile};
@@ -296,6 +296,9 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
296296

297297
let extra_link_arg = cx.bcx.config.cli_unstable().extra_link_arg;
298298
let nightly_features_allowed = cx.bcx.config.nightly_features_allowed;
299+
let targets: Vec<Target> = unit.pkg.targets().iter().cloned().collect();
300+
// Need a separate copy for the fresh closure.
301+
let targets_fresh = targets.clone();
299302

300303
// Prepare the unit of "dirty work" which will actually run the custom build
301304
// command.
@@ -405,6 +408,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
405408
&script_out_dir,
406409
extra_link_arg,
407410
nightly_features_allowed,
411+
&targets,
408412
)?;
409413

410414
if json_messages {
@@ -432,6 +436,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
432436
&script_out_dir,
433437
extra_link_arg,
434438
nightly_features_allowed,
439+
&targets_fresh,
435440
)?,
436441
};
437442

@@ -484,6 +489,7 @@ impl BuildOutput {
484489
script_out_dir: &Path,
485490
extra_link_arg: bool,
486491
nightly_features_allowed: bool,
492+
targets: &[Target],
487493
) -> CargoResult<BuildOutput> {
488494
let contents = paths::read_bytes(path)?;
489495
BuildOutput::parse(
@@ -494,6 +500,7 @@ impl BuildOutput {
494500
script_out_dir,
495501
extra_link_arg,
496502
nightly_features_allowed,
503+
targets,
497504
)
498505
}
499506

@@ -509,6 +516,7 @@ impl BuildOutput {
509516
script_out_dir: &Path,
510517
extra_link_arg: bool,
511518
nightly_features_allowed: bool,
519+
targets: &[Target],
512520
) -> CargoResult<BuildOutput> {
513521
let mut library_paths = Vec::new();
514522
let mut library_links = Vec::new();
@@ -562,10 +570,28 @@ impl BuildOutput {
562570
"rustc-link-lib" => library_links.push(value.to_string()),
563571
"rustc-link-search" => library_paths.push(PathBuf::from(value)),
564572
"rustc-link-arg-cdylib" | "rustc-cdylib-link-arg" => {
573+
if !targets.iter().any(|target| target.is_cdylib()) {
574+
bail!(
575+
"invalid instruction `cargo:{}` from {}\n\
576+
The package {} does not have a cdylib target.",
577+
key,
578+
whence,
579+
pkg_descr
580+
);
581+
}
565582
linker_args.push((LinkType::Cdylib, value))
566583
}
567584
"rustc-link-arg-bins" => {
568585
if extra_link_arg {
586+
if !targets.iter().any(|target| target.is_bin()) {
587+
bail!(
588+
"invalid instruction `cargo:{}` from {}\n\
589+
The package {} does not have a bin target.",
590+
key,
591+
whence,
592+
pkg_descr
593+
);
594+
}
569595
linker_args.push((LinkType::Bin, value));
570596
} else {
571597
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
@@ -575,10 +601,21 @@ impl BuildOutput {
575601
if extra_link_arg {
576602
let parts = value.splitn(2, "=").collect::<Vec<_>>();
577603
if parts.len() == 2 {
578-
linker_args.push((
579-
LinkType::SingleBin(parts[0].to_string()),
580-
parts[1].to_string(),
581-
));
604+
let bin_name = parts[0].to_string();
605+
if !targets
606+
.iter()
607+
.any(|target| target.is_bin() && target.name() == bin_name)
608+
{
609+
bail!(
610+
"invalid instruction `cargo:{}` from {}\n\
611+
The package {} does not have a bin target with the name `{}`.",
612+
key,
613+
whence,
614+
pkg_descr,
615+
bin_name
616+
);
617+
}
618+
linker_args.push((LinkType::SingleBin(bin_name), parts[1].to_string()));
582619
} else {
583620
warnings.push(format!(
584621
"cargo:{} has invalid syntax: expected `cargo:{}=BIN=ARG`",
@@ -900,6 +937,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
900937
&script_out_dir,
901938
extra_link_arg,
902939
cx.bcx.config.nightly_features_allowed,
940+
unit.pkg.targets(),
903941
)
904942
.ok(),
905943
prev_script_out_dir,

tests/testsuite/build_script_extra_link_arg.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,56 @@ fn build_script_extra_link_arg_warn_without_flag() {
113113
.with_stderr_contains("warning: cargo:rustc-link-arg requires -Zextra-link-arg flag")
114114
.run();
115115
}
116+
117+
#[cargo_test]
118+
fn link_arg_missing_target() {
119+
// Errors when a given target doesn't exist.
120+
let p = project()
121+
.file("src/lib.rs", "")
122+
.file(
123+
"build.rs",
124+
r#"fn main() { println!("cargo:rustc-link-arg-cdylib=--bogus"); }"#,
125+
)
126+
.build();
127+
128+
p.cargo("check")
129+
.with_status(101)
130+
.with_stderr("\
131+
[COMPILING] foo [..]
132+
error: invalid instruction `cargo:rustc-link-arg-cdylib` from build script of `foo v0.0.1 ([ROOT]/foo)`
133+
The package foo v0.0.1 ([ROOT]/foo) does not have a cdylib target.
134+
")
135+
.run();
136+
137+
p.change_file(
138+
"build.rs",
139+
r#"fn main() { println!("cargo:rustc-link-arg-bins=--bogus"); }"#,
140+
);
141+
142+
p.cargo("check -Zextra-link-arg")
143+
.masquerade_as_nightly_cargo()
144+
.with_status(101)
145+
.with_stderr("\
146+
[COMPILING] foo [..]
147+
error: invalid instruction `cargo:rustc-link-arg-bins` from build script of `foo v0.0.1 ([ROOT]/foo)`
148+
The package foo v0.0.1 ([ROOT]/foo) does not have a bin target.
149+
")
150+
.run();
151+
152+
p.change_file(
153+
"build.rs",
154+
r#"fn main() { println!("cargo:rustc-link-arg-bin=abc=--bogus"); }"#,
155+
);
156+
157+
p.cargo("check -Zextra-link-arg")
158+
.masquerade_as_nightly_cargo()
159+
.with_status(101)
160+
.with_stderr(
161+
"\
162+
[COMPILING] foo [..]
163+
error: invalid instruction `cargo:rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)`
164+
The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `abc`.
165+
",
166+
)
167+
.run();
168+
}

0 commit comments

Comments
 (0)