@@ -26,10 +26,15 @@ impl Step for CleanAll {
2626 }
2727
2828 fn run ( self , builder : & Builder < ' _ > ) -> Self :: Output {
29- let Subcommand :: Clean { all, .. } = builder. config . cmd else {
29+ let Subcommand :: Clean { all, stage } = builder. config . cmd else {
3030 unreachable ! ( "wrong subcommand?" )
3131 } ;
32- clean_default ( builder. build , all)
32+
33+ if all && stage. is_some ( ) {
34+ panic ! ( "--all and --stage can't be used at the same time for `x clean`" ) ;
35+ }
36+
37+ clean ( builder. build , all, stage)
3338 }
3439
3540 fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
@@ -86,35 +91,70 @@ clean_crate_tree! {
8691 Std , Mode :: Std , "sysroot" ;
8792}
8893
89- fn clean_default ( build : & Build , all : bool ) {
94+ fn clean ( build : & Build , all : bool , stage : Option < u32 > ) {
9095 if build. config . dry_run ( ) {
9196 return ;
9297 }
9398
9499 rm_rf ( "tmp" . as_ref ( ) ) ;
95100
101+ // Clean the entire build directory
96102 if all {
97103 rm_rf ( & build. out ) ;
98- } else {
99- rm_rf ( & build. out . join ( "tmp" ) ) ;
100- rm_rf ( & build. out . join ( "dist" ) ) ;
101- rm_rf ( & build. out . join ( "bootstrap" ) ) ;
102- rm_rf ( & build. out . join ( "rustfmt.stamp" ) ) ;
103-
104- for host in & build. hosts {
105- let entries = match build. out . join ( host. triple ) . read_dir ( ) {
106- Ok ( iter) => iter,
107- Err ( _) => continue ,
108- } ;
109-
110- for entry in entries {
111- let entry = t ! ( entry) ;
112- if entry. file_name ( ) . to_str ( ) == Some ( "llvm" ) {
113- continue ;
114- }
115- let path = t ! ( entry. path( ) . canonicalize( ) ) ;
116- rm_rf ( & path) ;
104+ return ;
105+ }
106+
107+ // Clean the target stage artifacts
108+ if let Some ( stage) = stage {
109+ clean_specific_stage ( build, stage) ;
110+ return ;
111+ }
112+
113+ // Follow the default behaviour
114+ clean_default ( build) ;
115+ }
116+
117+ fn clean_specific_stage ( build : & Build , stage : u32 ) {
118+ for host in & build. hosts {
119+ let entries = match build. out . join ( host. triple ) . read_dir ( ) {
120+ Ok ( iter) => iter,
121+ Err ( _) => continue ,
122+ } ;
123+
124+ for entry in entries {
125+ let entry = t ! ( entry) ;
126+ let stage_prefix = format ! ( "stage{}" , stage) ;
127+
128+ // if current entry is not related with the target stage, continue
129+ if !entry. file_name ( ) . to_str ( ) . unwrap_or ( "" ) . contains ( & stage_prefix) {
130+ continue ;
131+ }
132+
133+ let path = t ! ( entry. path( ) . canonicalize( ) ) ;
134+ rm_rf ( & path) ;
135+ }
136+ }
137+ }
138+
139+ fn clean_default ( build : & Build ) {
140+ rm_rf ( & build. out . join ( "tmp" ) ) ;
141+ rm_rf ( & build. out . join ( "dist" ) ) ;
142+ rm_rf ( & build. out . join ( "bootstrap" ) ) ;
143+ rm_rf ( & build. out . join ( "rustfmt.stamp" ) ) ;
144+
145+ for host in & build. hosts {
146+ let entries = match build. out . join ( host. triple ) . read_dir ( ) {
147+ Ok ( iter) => iter,
148+ Err ( _) => continue ,
149+ } ;
150+
151+ for entry in entries {
152+ let entry = t ! ( entry) ;
153+ if entry. file_name ( ) . to_str ( ) == Some ( "llvm" ) {
154+ continue ;
117155 }
156+ let path = t ! ( entry. path( ) . canonicalize( ) ) ;
157+ rm_rf ( & path) ;
118158 }
119159 }
120160}
0 commit comments