@@ -26,10 +26,15 @@ impl Step for CleanAll {
26
26
}
27
27
28
28
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 {
30
30
unreachable ! ( "wrong subcommand?" )
31
31
} ;
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)
33
38
}
34
39
35
40
fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
@@ -86,35 +91,70 @@ clean_crate_tree! {
86
91
Std , Mode :: Std , "sysroot" ;
87
92
}
88
93
89
- fn clean_default ( build : & Build , all : bool ) {
94
+ fn clean ( build : & Build , all : bool , stage : Option < u32 > ) {
90
95
if build. config . dry_run ( ) {
91
96
return ;
92
97
}
93
98
94
99
rm_rf ( "tmp" . as_ref ( ) ) ;
95
100
101
+ // Clean the entire build directory
96
102
if all {
97
103
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 ;
117
155
}
156
+ let path = t ! ( entry. path( ) . canonicalize( ) ) ;
157
+ rm_rf ( & path) ;
118
158
}
119
159
}
120
160
}
0 commit comments