@@ -193,6 +193,8 @@ fn clean_specs(
193
193
continue ;
194
194
}
195
195
let crate_name = target. crate_name ( ) ;
196
+ let path_dot = format ! ( "{crate_name}." ) ;
197
+ let path_dash = format ! ( "{crate_name}-" ) ;
196
198
for & mode in & [
197
199
CompileMode :: Build ,
198
200
CompileMode :: Test ,
@@ -212,8 +214,8 @@ fn clean_specs(
212
214
TargetKind :: Test | TargetKind :: Bench => ( layout. deps ( ) , None ) ,
213
215
_ => ( layout. deps ( ) , Some ( layout. dest ( ) ) ) ,
214
216
} ;
215
- let dir_glob = escape_glob_path ( dir) ?;
216
- let dir_glob = Path :: new ( & dir_glob ) ;
217
+ let mut dir_glob_str = escape_glob_path ( dir) ?;
218
+ let dir_glob = Path :: new ( & dir_glob_str ) ;
217
219
for file_type in file_types {
218
220
// Some files include a hash in the filename, some don't.
219
221
let hashed_name = file_type. output_filename ( target, Some ( "*" ) ) ;
@@ -233,17 +235,21 @@ fn clean_specs(
233
235
}
234
236
// Remove dep-info file generated by rustc. It is not tracked in
235
237
// file_types. It does not have a prefix.
236
- let hashed_dep_info = dir_glob. join ( format ! ( "{}-*.d" , crate_name) ) ;
237
- clean_ctx. rm_rf_glob ( & hashed_dep_info) ?;
238
238
let unhashed_dep_info = dir. join ( format ! ( "{}.d" , crate_name) ) ;
239
239
clean_ctx. rm_rf ( & unhashed_dep_info) ?;
240
240
// Remove split-debuginfo files generated by rustc.
241
- let split_debuginfo_obj = dir_glob. join ( format ! ( "{}.*.o" , crate_name) ) ;
242
- clean_ctx. rm_rf_glob ( & split_debuginfo_obj) ?;
243
- let split_debuginfo_dwo = dir_glob. join ( format ! ( "{}.*.dwo" , crate_name) ) ;
244
- clean_ctx. rm_rf_glob ( & split_debuginfo_dwo) ?;
245
- let split_debuginfo_dwp = dir_glob. join ( format ! ( "{}.*.dwp" , crate_name) ) ;
246
- clean_ctx. rm_rf_glob ( & split_debuginfo_dwp) ?;
241
+
242
+ let paths = [
243
+ ( path_dash. as_str ( ) , ".d" ) ,
244
+ ( path_dot. as_str ( ) , ".o" ) ,
245
+ ( path_dot. as_str ( ) , ".dwo" ) ,
246
+ ( path_dot. as_str ( ) , ".dwp" ) ,
247
+ ] ;
248
+ if !dir_glob_str. ends_with ( std:: path:: MAIN_SEPARATOR ) {
249
+ dir_glob_str. push ( std:: path:: MAIN_SEPARATOR ) ;
250
+ }
251
+ dir_glob_str. push ( '*' ) ;
252
+ clean_ctx. rm_rf_prefix_list ( & dir_glob_str. as_str ( ) , & paths) ?;
247
253
248
254
// TODO: what to do about build_script_build?
249
255
let dir = escape_glob_path ( layout. incremental ( ) ) ?;
@@ -323,6 +329,26 @@ impl<'gctx> CleanContext<'gctx> {
323
329
Ok ( ( ) )
324
330
}
325
331
332
+ fn rm_rf_prefix_list (
333
+ & mut self ,
334
+ pattern : & str ,
335
+ path_matchers : & [ ( & str , & str ) ] ,
336
+ ) -> CargoResult < ( ) > {
337
+ // TODO: Display utf8 warning to user? Or switch to globset?
338
+
339
+ for path in glob:: glob ( pattern) ? {
340
+ let path = path?;
341
+ let filename = path. file_name ( ) . and_then ( |name| name. to_str ( ) ) . unwrap ( ) ;
342
+ if path_matchers
343
+ . iter ( )
344
+ . any ( |( prefix, suffix) | filename. starts_with ( prefix) && filename. ends_with ( suffix) )
345
+ {
346
+ self . rm_rf ( & path) ?;
347
+ }
348
+ }
349
+ Ok ( ( ) )
350
+ }
351
+
326
352
pub fn rm_rf ( & mut self , path : & Path ) -> CargoResult < ( ) > {
327
353
let meta = match fs:: symlink_metadata ( path) {
328
354
Ok ( meta) => meta,
0 commit comments