@@ -420,3 +420,56 @@ impl Step for CoverageDump {
420420 cmd. run ( builder) ;
421421 }
422422}
423+
424+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
425+ pub struct Rustfmt ;
426+
427+ impl Step for Rustfmt {
428+ type Output = ( ) ;
429+ const ONLY_HOSTS : bool = true ;
430+
431+ fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
432+ run. path ( "src/tools/rustfmt" )
433+ }
434+
435+ fn make_run ( run : RunConfig < ' _ > ) {
436+ run. builder . ensure ( Rustfmt ) ;
437+ }
438+
439+ fn run ( self , builder : & Builder < ' _ > ) {
440+ let host = builder. build . build ;
441+
442+ // `x run` uses stage 0 by default but rustfmt does not work well with stage 0.
443+ // Change the stage to 1 if it's not set explicitly.
444+ let stage = if builder. config . is_explicit_stage ( ) || builder. top_stage >= 1 {
445+ builder. top_stage
446+ } else {
447+ 1
448+ } ;
449+
450+ if stage == 0 {
451+ eprintln ! ( "rustfmt cannot be run at stage 0" ) ;
452+ eprintln ! ( "HELP: Use `x fmt` to use stage 0 rustfmt." ) ;
453+ std:: process:: exit ( 1 ) ;
454+ }
455+
456+ let compiler = builder. compiler ( stage, host) ;
457+ let rustfmt_build = builder. ensure ( tool:: Rustfmt { compiler, target : host } ) ;
458+
459+ let mut rustfmt = tool:: prepare_tool_cargo (
460+ builder,
461+ rustfmt_build. build_compiler ,
462+ Mode :: ToolRustc ,
463+ host,
464+ Kind :: Run ,
465+ "src/tools/rustfmt" ,
466+ SourceType :: InTree ,
467+ & [ ] ,
468+ ) ;
469+
470+ rustfmt. args ( [ "--bin" , "rustfmt" , "--" ] ) ;
471+ rustfmt. args ( builder. config . args ( ) ) ;
472+
473+ rustfmt. into_cmd ( ) . run ( builder) ;
474+ }
475+ }
0 commit comments