@@ -2,7 +2,7 @@ use super::job::{Freshness, Job, Work};
2
2
use super :: { fingerprint, Context , LinkType , Unit } ;
3
3
use crate :: core:: compiler:: context:: Metadata ;
4
4
use crate :: core:: compiler:: job_queue:: JobState ;
5
- use crate :: core:: { profiles:: ProfileRoot , PackageId } ;
5
+ use crate :: core:: { profiles:: ProfileRoot , PackageId , Target } ;
6
6
use crate :: util:: errors:: CargoResult ;
7
7
use crate :: util:: machine_message:: { self , Message } ;
8
8
use crate :: util:: { internal, profile} ;
@@ -296,6 +296,9 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
296
296
297
297
let extra_link_arg = cx. bcx . config . cli_unstable ( ) . extra_link_arg ;
298
298
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 ( ) ;
299
302
300
303
// Prepare the unit of "dirty work" which will actually run the custom build
301
304
// command.
@@ -405,6 +408,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
405
408
& script_out_dir,
406
409
extra_link_arg,
407
410
nightly_features_allowed,
411
+ & targets,
408
412
) ?;
409
413
410
414
if json_messages {
@@ -432,6 +436,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
432
436
& script_out_dir,
433
437
extra_link_arg,
434
438
nightly_features_allowed,
439
+ & targets_fresh,
435
440
) ?,
436
441
} ;
437
442
@@ -484,6 +489,7 @@ impl BuildOutput {
484
489
script_out_dir : & Path ,
485
490
extra_link_arg : bool ,
486
491
nightly_features_allowed : bool ,
492
+ targets : & [ Target ] ,
487
493
) -> CargoResult < BuildOutput > {
488
494
let contents = paths:: read_bytes ( path) ?;
489
495
BuildOutput :: parse (
@@ -494,6 +500,7 @@ impl BuildOutput {
494
500
script_out_dir,
495
501
extra_link_arg,
496
502
nightly_features_allowed,
503
+ targets,
497
504
)
498
505
}
499
506
@@ -509,6 +516,7 @@ impl BuildOutput {
509
516
script_out_dir : & Path ,
510
517
extra_link_arg : bool ,
511
518
nightly_features_allowed : bool ,
519
+ targets : & [ Target ] ,
512
520
) -> CargoResult < BuildOutput > {
513
521
let mut library_paths = Vec :: new ( ) ;
514
522
let mut library_links = Vec :: new ( ) ;
@@ -562,10 +570,28 @@ impl BuildOutput {
562
570
"rustc-link-lib" => library_links. push ( value. to_string ( ) ) ,
563
571
"rustc-link-search" => library_paths. push ( PathBuf :: from ( value) ) ,
564
572
"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
+ }
565
582
linker_args. push ( ( LinkType :: Cdylib , value) )
566
583
}
567
584
"rustc-link-arg-bins" => {
568
585
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
+ }
569
595
linker_args. push ( ( LinkType :: Bin , value) ) ;
570
596
} else {
571
597
warnings. push ( format ! ( "cargo:{} requires -Zextra-link-arg flag" , key) ) ;
@@ -575,10 +601,21 @@ impl BuildOutput {
575
601
if extra_link_arg {
576
602
let parts = value. splitn ( 2 , "=" ) . collect :: < Vec < _ > > ( ) ;
577
603
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 ( ) ) ) ;
582
619
} else {
583
620
warnings. push ( format ! (
584
621
"cargo:{} has invalid syntax: expected `cargo:{}=BIN=ARG`" ,
@@ -900,6 +937,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
900
937
& script_out_dir,
901
938
extra_link_arg,
902
939
cx. bcx . config . nightly_features_allowed ,
940
+ unit. pkg . targets ( ) ,
903
941
)
904
942
. ok ( ) ,
905
943
prev_script_out_dir,
0 commit comments