@@ -4,7 +4,6 @@ use crate::core::compiler::context::Metadata;
44use crate :: core:: compiler:: job_queue:: JobState ;
55use crate :: core:: { profiles:: ProfileRoot , PackageId } ;
66use crate :: util:: errors:: { CargoResult , CargoResultExt } ;
7- use crate :: util:: interning:: InternedString ;
87use crate :: util:: machine_message:: { self , Message } ;
98use crate :: util:: { self , internal, paths, profile} ;
109use cargo_platform:: Cfg ;
@@ -268,7 +267,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
268267 }
269268 } )
270269 . collect :: < Vec < _ > > ( ) ;
271- let pkg_name = unit. pkg . name ( ) ;
270+ let library_name = unit. pkg . library ( ) . map ( |t| t . crate_name ( ) ) ;
272271 let pkg_descr = unit. pkg . to_string ( ) ;
273272 let build_script_outputs = Arc :: clone ( & cx. build_script_outputs ) ;
274273 let id = unit. pkg . package_id ( ) ;
@@ -278,7 +277,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
278277 let host_target_root = cx. files ( ) . host_dest ( ) . to_path_buf ( ) ;
279278 let all = (
280279 id,
281- pkg_name ,
280+ library_name . clone ( ) ,
282281 pkg_descr. clone ( ) ,
283282 Arc :: clone ( & build_script_outputs) ,
284283 output_file. clone ( ) ,
@@ -398,7 +397,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
398397 paths:: write ( & root_output_file, util:: path2bytes ( & script_out_dir) ?) ?;
399398 let parsed_output = BuildOutput :: parse (
400399 & output. stdout ,
401- pkg_name ,
400+ library_name ,
402401 & pkg_descr,
403402 & script_out_dir,
404403 & script_out_dir,
@@ -420,12 +419,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
420419 // itself to run when we actually end up just discarding what we calculated
421420 // above.
422421 let fresh = Work :: new ( move |state| {
423- let ( id, pkg_name , pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
422+ let ( id, library_name , pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
424423 let output = match prev_output {
425424 Some ( output) => output,
426425 None => BuildOutput :: parse_file (
427426 & output_file,
428- pkg_name ,
427+ library_name ,
429428 & pkg_descr,
430429 & prev_script_out_dir,
431430 & script_out_dir,
@@ -477,7 +476,7 @@ fn insert_warnings_in_build_outputs(
477476impl BuildOutput {
478477 pub fn parse_file (
479478 path : & Path ,
480- pkg_name : InternedString ,
479+ library_name : Option < String > ,
481480 pkg_descr : & str ,
482481 script_out_dir_when_generated : & Path ,
483482 script_out_dir : & Path ,
@@ -487,7 +486,7 @@ impl BuildOutput {
487486 let contents = paths:: read_bytes ( path) ?;
488487 BuildOutput :: parse (
489488 & contents,
490- pkg_name ,
489+ library_name ,
491490 pkg_descr,
492491 script_out_dir_when_generated,
493492 script_out_dir,
@@ -497,10 +496,12 @@ impl BuildOutput {
497496 }
498497
499498 // Parses the output of a script.
500- // The `pkg_name` is used for error messages.
499+ // The `pkg_descr` is used for error messages.
500+ // The `library_name` is used for determining if RUSTC_BOOTSTRAP should be allowed.
501501 pub fn parse (
502502 input : & [ u8 ] ,
503- pkg_name : InternedString ,
503+ // Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
504+ library_name : Option < String > ,
504505 pkg_descr : & str ,
505506 script_out_dir_when_generated : & Path ,
506507 script_out_dir : & Path ,
@@ -587,7 +588,23 @@ impl BuildOutput {
587588 // to set RUSTC_BOOTSTRAP.
588589 // If this is a nightly build, setting RUSTC_BOOTSTRAP wouldn't affect the
589590 // behavior, so still only give a warning.
590- if nightly_features_allowed {
591+ // NOTE: cargo only allows nightly features on RUSTC_BOOTSTRAP=1, but we
592+ // want setting any value of RUSTC_BOOTSTRAP to downgrade this to a warning
593+ // (so that `RUSTC_BOOTSTRAP=library_name` will work)
594+ let rustc_bootstrap_allows = |name : Option < & str > | {
595+ let name = match name {
596+ // as of 2021, no binaries on crates.io use RUSTC_BOOTSTRAP, so
597+ // fine-grained opt-outs aren't needed. end-users can always use
598+ // RUSTC_BOOTSTRAP=1 from the top-level if it's really a problem.
599+ None => return false ,
600+ Some ( n) => n,
601+ } ;
602+ std:: env:: var ( "RUSTC_BOOTSTRAP" )
603+ . map_or ( false , |var| var. split ( ',' ) . any ( |s| s == name) )
604+ } ;
605+ if nightly_features_allowed
606+ || rustc_bootstrap_allows ( library_name. as_deref ( ) )
607+ {
591608 warnings. push ( format ! ( "Cannot set `RUSTC_BOOTSTRAP={}` from {}.\n \
592609 note: Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.",
593610 val, whence
@@ -600,7 +617,7 @@ impl BuildOutput {
600617 help: If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP={}` before running cargo instead.",
601618 val,
602619 whence,
603- pkg_name ,
620+ library_name . as_deref ( ) . unwrap_or ( "1" ) ,
604621 ) ;
605622 }
606623 } else {
@@ -857,7 +874,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
857874 (
858875 BuildOutput :: parse_file (
859876 & output_file,
860- unit. pkg . name ( ) ,
877+ unit. pkg . library ( ) . map ( |t| t . crate_name ( ) ) ,
861878 & unit. pkg . to_string ( ) ,
862879 & prev_script_out_dir,
863880 & script_out_dir,
0 commit comments