@@ -269,7 +269,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
269269 }
270270 } )
271271 . collect :: < Vec < _ > > ( ) ;
272- let crate_name = unit. target . crate_name ( ) ;
272+ let library_name = unit
273+ . pkg
274+ . targets ( )
275+ . iter ( )
276+ . find ( |t| t. is_lib ( ) )
277+ . map ( |t| t. crate_name ( ) ) ;
273278 let pkg_descr = unit. pkg . to_string ( ) ;
274279 let build_script_outputs = Arc :: clone ( & cx. build_script_outputs ) ;
275280 let id = unit. pkg . package_id ( ) ;
@@ -279,7 +284,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
279284 let host_target_root = cx. files ( ) . host_dest ( ) . to_path_buf ( ) ;
280285 let all = (
281286 id,
282- crate_name . clone ( ) ,
287+ library_name . clone ( ) ,
283288 pkg_descr. clone ( ) ,
284289 Arc :: clone ( & build_script_outputs) ,
285290 output_file. clone ( ) ,
@@ -399,7 +404,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
399404 paths:: write ( & root_output_file, paths:: path2bytes ( & script_out_dir) ?) ?;
400405 let parsed_output = BuildOutput :: parse (
401406 & output. stdout ,
402- crate_name ,
407+ library_name ,
403408 & pkg_descr,
404409 & script_out_dir,
405410 & script_out_dir,
@@ -421,12 +426,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
421426 // itself to run when we actually end up just discarding what we calculated
422427 // above.
423428 let fresh = Work :: new ( move |state| {
424- let ( id, crate_name , pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
429+ let ( id, library_name , pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
425430 let output = match prev_output {
426431 Some ( output) => output,
427432 None => BuildOutput :: parse_file (
428433 & output_file,
429- crate_name ,
434+ library_name ,
430435 & pkg_descr,
431436 & prev_script_out_dir,
432437 & script_out_dir,
@@ -478,7 +483,7 @@ fn insert_warnings_in_build_outputs(
478483impl BuildOutput {
479484 pub fn parse_file (
480485 path : & Path ,
481- crate_name : String ,
486+ library_name : Option < String > ,
482487 pkg_descr : & str ,
483488 script_out_dir_when_generated : & Path ,
484489 script_out_dir : & Path ,
@@ -488,7 +493,7 @@ impl BuildOutput {
488493 let contents = paths:: read_bytes ( path) ?;
489494 BuildOutput :: parse (
490495 & contents,
491- crate_name ,
496+ library_name ,
492497 pkg_descr,
493498 script_out_dir_when_generated,
494499 script_out_dir,
@@ -499,11 +504,11 @@ impl BuildOutput {
499504
500505 // Parses the output of a script.
501506 // The `pkg_descr` is used for error messages.
502- // The `crate_name ` is used for determining if RUSTC_BOOTSTRAP should be allowed.
507+ // The `library_name ` is used for determining if RUSTC_BOOTSTRAP should be allowed.
503508 pub fn parse (
504509 input : & [ u8 ] ,
505510 // Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
506- crate_name : String ,
511+ library_name : Option < String > ,
507512 pkg_descr : & str ,
508513 script_out_dir_when_generated : & Path ,
509514 script_out_dir : & Path ,
@@ -592,12 +597,21 @@ impl BuildOutput {
592597 // behavior, so still only give a warning.
593598 // NOTE: cargo only allows nightly features on RUSTC_BOOTSTRAP=1, but we
594599 // want setting any value of RUSTC_BOOTSTRAP to downgrade this to a warning
595- // (so that `RUSTC_BOOTSTRAP=crate_name` will work)
596- let rustc_bootstrap_allows = |name : & str | {
600+ // (so that `RUSTC_BOOTSTRAP=library_name` will work)
601+ let rustc_bootstrap_allows = |name : Option < & str > | {
602+ let name = match name {
603+ // as of 2021, no binaries on crates.io use RUSTC_BOOTSTRAP, so
604+ // fine-grained opt-outs aren't needed. end-users can always use
605+ // RUSTC_BOOTSTRAP=1 from the top-level if it's really a problem.
606+ None => return false ,
607+ Some ( n) => n,
608+ } ;
597609 std:: env:: var ( "RUSTC_BOOTSTRAP" )
598610 . map_or ( false , |var| var. split ( ',' ) . any ( |s| s == name) )
599611 } ;
600- if nightly_features_allowed || rustc_bootstrap_allows ( & * crate_name) {
612+ if nightly_features_allowed
613+ || rustc_bootstrap_allows ( library_name. as_deref ( ) )
614+ {
601615 warnings. push ( format ! ( "Cannot set `RUSTC_BOOTSTRAP={}` from {}.\n \
602616 note: Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.",
603617 val, whence
@@ -610,7 +624,7 @@ impl BuildOutput {
610624 help: If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP={}` before running cargo instead.",
611625 val,
612626 whence,
613- crate_name ,
627+ library_name . as_deref ( ) . unwrap_or ( "1" ) ,
614628 ) ;
615629 }
616630 } else {
@@ -867,7 +881,11 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
867881 (
868882 BuildOutput :: parse_file (
869883 & output_file,
870- unit. target . crate_name ( ) ,
884+ unit. pkg
885+ . targets ( )
886+ . iter ( )
887+ . find ( |t| t. is_lib ( ) )
888+ . map ( |t| t. crate_name ( ) ) ,
871889 & unit. pkg . to_string ( ) ,
872890 & prev_script_out_dir,
873891 & script_out_dir,
0 commit comments