@@ -25,9 +25,7 @@ struct Rustflags(String, TargetSelection);
2525
2626impl Rustflags {
2727 fn new ( target : TargetSelection ) -> Rustflags {
28- let mut ret = Rustflags ( String :: new ( ) , target) ;
29- ret. propagate_cargo_env ( "RUSTFLAGS" ) ;
30- ret
28+ Rustflags ( String :: new ( ) , target)
3129 }
3230
3331 /// By default, cargo will pick up on various variables in the environment. However, bootstrap
@@ -60,6 +58,16 @@ impl Rustflags {
6058 self . 0 . push_str ( arg) ;
6159 self
6260 }
61+
62+ fn propagate_rustflag_envs ( & mut self , build_compiler_stage : u32 ) {
63+ self . propagate_cargo_env ( "RUSTFLAGS" ) ;
64+ if build_compiler_stage != 0 {
65+ self . env ( "RUSTFLAGS_NOT_BOOTSTRAP" ) ;
66+ } else {
67+ self . env ( "RUSTFLAGS_BOOTSTRAP" ) ;
68+ self . arg ( "--cfg=bootstrap" ) ;
69+ }
70+ }
6371}
6472
6573/// Flags that are passed to the `rustc` shim binary. These flags will only be applied when
@@ -96,6 +104,7 @@ pub struct Cargo {
96104 hostflags : HostFlags ,
97105 allow_features : String ,
98106 release_build : bool ,
107+ build_compiler_stage : u32 ,
99108}
100109
101110impl Cargo {
@@ -386,6 +395,32 @@ impl Cargo {
386395
387396 self
388397 }
398+
399+ /// Propagate all the `*FLAGS*` env vars to cargo.
400+ /// This method should only be called once and after all other flags have been added.
401+ /// We want to propagate the envs at the end to make sure they override any previously set flags.
402+ fn propagate_flag_envs ( & mut self ) {
403+ self . rustflags . propagate_rustflag_envs ( self . build_compiler_stage ) ;
404+ self . rustdocflags . propagate_rustflag_envs ( self . build_compiler_stage ) ;
405+
406+ self . rustdocflags . propagate_cargo_env ( "RUSTDOCFLAGS" ) ;
407+
408+ if self . build_compiler_stage == 0 {
409+ self . rustdocflags . env ( "RUSTDOCFLAGS_BOOTSTRAP" ) ;
410+ if let Ok ( s) = env:: var ( "CARGOFLAGS_BOOTSTRAP" ) {
411+ self . args ( s. split_whitespace ( ) ) ;
412+ }
413+ } else {
414+ self . rustdocflags . env ( "RUSTDOCFLAGS_NOT_BOOTSTRAP" ) ;
415+ if let Ok ( s) = env:: var ( "CARGOFLAGS_NOT_BOOTSTRAP" ) {
416+ self . args ( s. split_whitespace ( ) ) ;
417+ }
418+ }
419+
420+ if let Ok ( s) = env:: var ( "CARGOFLAGS" ) {
421+ self . args ( s. split_whitespace ( ) ) ;
422+ }
423+ }
389424}
390425
391426impl From < Cargo > for BootstrapCommand {
@@ -394,6 +429,9 @@ impl From<Cargo> for BootstrapCommand {
394429 cargo. args . insert ( 0 , "--release" . into ( ) ) ;
395430 }
396431
432+ // don't forget to actually propagate flags from env vars
433+ cargo. propagate_flag_envs ( ) ;
434+
397435 cargo. command . args ( cargo. args ) ;
398436
399437 let rustflags = & cargo. rustflags . 0 ;
@@ -601,18 +639,6 @@ impl Builder<'_> {
601639 }
602640
603641 let mut rustflags = Rustflags :: new ( target) ;
604- if build_compiler_stage != 0 {
605- if let Ok ( s) = env:: var ( "CARGOFLAGS_NOT_BOOTSTRAP" ) {
606- cargo. args ( s. split_whitespace ( ) ) ;
607- }
608- rustflags. env ( "RUSTFLAGS_NOT_BOOTSTRAP" ) ;
609- } else {
610- if let Ok ( s) = env:: var ( "CARGOFLAGS_BOOTSTRAP" ) {
611- cargo. args ( s. split_whitespace ( ) ) ;
612- }
613- rustflags. env ( "RUSTFLAGS_BOOTSTRAP" ) ;
614- rustflags. arg ( "--cfg=bootstrap" ) ;
615- }
616642
617643 if cmd_kind == Kind :: Clippy {
618644 // clippy overwrites sysroot if we pass it to cargo.
@@ -711,16 +737,6 @@ impl Builder<'_> {
711737 // but this breaks CI. At the very least, stage0 `rustdoc` needs `--cfg bootstrap`. See
712738 // #71458.
713739 let mut rustdocflags = rustflags. clone ( ) ;
714- rustdocflags. propagate_cargo_env ( "RUSTDOCFLAGS" ) ;
715- if build_compiler_stage == 0 {
716- rustdocflags. env ( "RUSTDOCFLAGS_BOOTSTRAP" ) ;
717- } else {
718- rustdocflags. env ( "RUSTDOCFLAGS_NOT_BOOTSTRAP" ) ;
719- }
720-
721- if let Ok ( s) = env:: var ( "CARGOFLAGS" ) {
722- cargo. args ( s. split_whitespace ( ) ) ;
723- }
724740
725741 match mode {
726742 Mode :: Std | Mode :: ToolBootstrap | Mode :: ToolStd | Mode :: ToolTarget => { }
@@ -1374,6 +1390,7 @@ impl Builder<'_> {
13741390 hostflags,
13751391 allow_features,
13761392 release_build,
1393+ build_compiler_stage,
13771394 }
13781395 }
13791396}
0 commit comments