@@ -392,3 +392,55 @@ impl Step for CyclicStep {
392392 builder. ensure ( CyclicStep { n : self . n . saturating_sub ( 1 ) } )
393393 }
394394}
395+
396+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
397+ pub struct Rustfmt ;
398+
399+ impl Step for Rustfmt {
400+ type Output = ( ) ;
401+ const ONLY_HOSTS : bool = true ;
402+
403+ fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
404+ run. path ( "src/tools/rustfmt" )
405+ }
406+
407+ fn make_run ( run : RunConfig < ' _ > ) {
408+ run. builder . ensure ( Rustfmt ) ;
409+ }
410+
411+ fn run ( self , builder : & Builder < ' _ > ) {
412+ let host = builder. build . build ;
413+
414+ // `x run` uses stage 0 by default but rustfmt does not work well with stage 0.
415+ // Change the stage to 1 if it's not set explicitly.
416+ let stage = if builder. config . is_explicit_stage ( ) || builder. top_stage >= 1 {
417+ builder. top_stage
418+ } else {
419+ 1
420+ } ;
421+
422+ if stage == 0 {
423+ eprintln ! ( "rustfmt cannot be run at stage 0" ) ;
424+ std:: process:: exit ( 1 ) ;
425+ }
426+
427+ let compiler = builder. compiler ( stage, host) ;
428+ let rustfmt_build = builder. ensure ( tool:: Rustfmt { compiler, target : host } ) ;
429+
430+ let mut rustfmt = tool:: prepare_tool_cargo (
431+ builder,
432+ rustfmt_build. build_compiler ,
433+ Mode :: ToolRustc ,
434+ host,
435+ Kind :: Run ,
436+ "src/tools/rustfmt" ,
437+ SourceType :: InTree ,
438+ & [ ] ,
439+ ) ;
440+
441+ rustfmt. args ( [ "--bin" , "rustfmt" , "--" ] ) ;
442+ rustfmt. args ( builder. config . args ( ) ) ;
443+
444+ rustfmt. into_cmd ( ) . run ( builder) ;
445+ }
446+ }
0 commit comments