11use std:: env;
22use std:: ffi:: { OsStr , OsString } ;
33use std:: path:: { Path , PathBuf } ;
4+ use std:: process:: Command ;
45
56use super :: { Builder , Kind } ;
67use crate :: core:: build_steps:: tool:: SourceType ;
@@ -94,6 +95,7 @@ pub struct Cargo {
9495 rustdocflags : Rustflags ,
9596 hostflags : HostFlags ,
9697 allow_features : String ,
98+ release_build : bool ,
9799}
98100
99101impl Cargo {
@@ -121,6 +123,10 @@ impl Cargo {
121123 cargo
122124 }
123125
126+ pub fn release_build ( & mut self , release_build : bool ) {
127+ self . release_build = release_build;
128+ }
129+
124130 pub fn compiler ( & self ) -> Compiler {
125131 self . compiler
126132 }
@@ -335,6 +341,31 @@ impl Cargo {
335341
336342impl From < Cargo > for BootstrapCommand {
337343 fn from ( mut cargo : Cargo ) -> BootstrapCommand {
344+ if cargo. release_build {
345+ let mut args: Vec < String > = cargo
346+ . command
347+ . as_command_mut ( )
348+ . get_args ( )
349+ . map ( |arg| arg. to_str ( ) . unwrap ( ) . to_owned ( ) )
350+ . collect ( ) ;
351+
352+ args. insert ( 1 , "--release" . into ( ) ) ;
353+
354+ let mut new_command = Command :: new ( cargo. command . as_command_mut ( ) . get_program ( ) ) ;
355+ new_command. args ( args) ;
356+
357+ if let Some ( p) = cargo. command . as_command_mut ( ) . get_current_dir ( ) {
358+ new_command. current_dir ( p) ;
359+ }
360+
361+ for ( key, value) in cargo. command . get_envs ( ) {
362+ let Some ( value) = value else { continue } ;
363+ new_command. env ( key, value) ;
364+ }
365+
366+ cargo. command . replace_inner_command ( new_command) ;
367+ }
368+
338369 let rustflags = & cargo. rustflags . 0 ;
339370 if !rustflags. is_empty ( ) {
340371 cargo. command . env ( "RUSTFLAGS" , rustflags) ;
@@ -353,6 +384,7 @@ impl From<Cargo> for BootstrapCommand {
353384 if !cargo. allow_features . is_empty ( ) {
354385 cargo. command . env ( "RUSTC_ALLOW_FEATURES" , cargo. allow_features ) ;
355386 }
387+
356388 cargo. command
357389 }
358390}
@@ -422,13 +454,6 @@ impl Builder<'_> {
422454 assert_eq ! ( target, compiler. host) ;
423455 }
424456
425- if self . config . rust_optimize . is_release ( ) &&
426- // cargo bench/install do not accept `--release` and miri doesn't want it
427- !matches ! ( cmd_kind, Kind :: Bench | Kind :: Install | Kind :: Miri | Kind :: MiriSetup | Kind :: MiriTest )
428- {
429- cargo. arg ( "--release" ) ;
430- }
431-
432457 // Remove make-related flags to ensure Cargo can correctly set things up
433458 cargo. env_remove ( "MAKEFLAGS" ) ;
434459 cargo. env_remove ( "MFLAGS" ) ;
@@ -1214,6 +1239,10 @@ impl Builder<'_> {
12141239 rustflags. arg ( "-Zmir_strip_debuginfo=locals-in-tiny-functions" ) ;
12151240 }
12161241
1242+ let release_build = self . config . rust_optimize . is_release ( ) &&
1243+ // cargo bench/install do not accept `--release` and miri doesn't want it
1244+ !matches ! ( cmd_kind, Kind :: Bench | Kind :: Install | Kind :: Miri | Kind :: MiriSetup | Kind :: MiriTest ) ;
1245+
12171246 Cargo {
12181247 command : cargo,
12191248 compiler,
@@ -1222,6 +1251,7 @@ impl Builder<'_> {
12221251 rustdocflags,
12231252 hostflags,
12241253 allow_features,
1254+ release_build,
12251255 }
12261256 }
12271257}
0 commit comments