@@ -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 {
@@ -394,6 +403,28 @@ impl From<Cargo> for BootstrapCommand {
394403 cargo. args . insert ( 0 , "--release" . into ( ) ) ;
395404 }
396405
406+ // Propagate the envs here at the very end to make sure they override any previously set flags.
407+ cargo. rustflags . propagate_rustflag_envs ( cargo. build_compiler_stage ) ;
408+ cargo. rustdocflags . propagate_rustflag_envs ( cargo. build_compiler_stage ) ;
409+
410+ cargo. rustdocflags . propagate_cargo_env ( "RUSTDOCFLAGS" ) ;
411+
412+ if cargo. build_compiler_stage == 0 {
413+ cargo. rustdocflags . env ( "RUSTDOCFLAGS_BOOTSTRAP" ) ;
414+ if let Ok ( s) = env:: var ( "CARGOFLAGS_BOOTSTRAP" ) {
415+ cargo. args ( s. split_whitespace ( ) ) ;
416+ }
417+ } else {
418+ cargo. rustdocflags . env ( "RUSTDOCFLAGS_NOT_BOOTSTRAP" ) ;
419+ if let Ok ( s) = env:: var ( "CARGOFLAGS_NOT_BOOTSTRAP" ) {
420+ cargo. args ( s. split_whitespace ( ) ) ;
421+ }
422+ }
423+
424+ if let Ok ( s) = env:: var ( "CARGOFLAGS" ) {
425+ cargo. args ( s. split_whitespace ( ) ) ;
426+ }
427+
397428 cargo. command . args ( cargo. args ) ;
398429
399430 let rustflags = & cargo. rustflags . 0 ;
@@ -601,18 +632,6 @@ impl Builder<'_> {
601632 }
602633
603634 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- }
616635
617636 if cmd_kind == Kind :: Clippy {
618637 // clippy overwrites sysroot if we pass it to cargo.
@@ -711,16 +730,6 @@ impl Builder<'_> {
711730 // but this breaks CI. At the very least, stage0 `rustdoc` needs `--cfg bootstrap`. See
712731 // #71458.
713732 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- }
724733
725734 match mode {
726735 Mode :: Std | Mode :: ToolBootstrap | Mode :: ToolStd | Mode :: ToolTarget => { }
@@ -1374,6 +1383,7 @@ impl Builder<'_> {
13741383 hostflags,
13751384 allow_features,
13761385 release_build,
1386+ build_compiler_stage,
13771387 }
13781388 }
13791389}
0 commit comments