Skip to content

Commit 151d3b7

Browse files
committed
refactor(clean): Add extra nesting
1 parent 23f5b2b commit 151d3b7

File tree

1 file changed

+117
-112
lines changed

1 file changed

+117
-112
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 117 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -192,23 +192,86 @@ fn clean_specs(
192192

193193
clean_ctx.progress = Box::new(CleaningPackagesBar::new(clean_ctx.gctx, packages.len()));
194194

195-
// Try to reduce the amount of times we iterate over the same target directory by storing away
196-
// the directories we've iterated over (and cleaned for a given package).
197-
let mut cleaned_packages: HashMap<_, HashSet<_>> = HashMap::default();
198-
for pkg in packages {
199-
let pkg_dir = format!("{}-*", pkg.name());
200-
clean_ctx.progress.on_cleaning_package(&pkg.name())?;
201-
202-
if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
203-
for (_compile_kind, layout) in &layouts_with_host {
204-
let dir = layout.build_dir().build_unit(&pkg.name());
205-
clean_ctx.rm_rf(&dir)?;
195+
{
196+
// Try to reduce the amount of times we iterate over the same target directory by storing away
197+
// the directories we've iterated over (and cleaned for a given package).
198+
let mut cleaned_packages: HashMap<_, HashSet<_>> = HashMap::default();
199+
for pkg in packages {
200+
let pkg_dir = format!("{}-*", pkg.name());
201+
clean_ctx.progress.on_cleaning_package(&pkg.name())?;
202+
203+
if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
204+
for (_compile_kind, layout) in &layouts_with_host {
205+
let dir = layout.build_dir().build_unit(&pkg.name());
206+
clean_ctx.rm_rf(&dir)?;
207+
}
208+
209+
for target in pkg.targets() {
210+
if target.is_custom_build() {
211+
continue;
212+
}
213+
for &mode in &[
214+
CompileMode::Build,
215+
CompileMode::Test,
216+
CompileMode::Check { test: false },
217+
] {
218+
for (compile_kind, layout) in &layouts {
219+
let triple = target_data.short_name(compile_kind);
220+
let (file_types, _unsupported) = target_data
221+
.info(*compile_kind)
222+
.rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
223+
let artifact_dir = layout
224+
.artifact_dir()
225+
.expect("artifact-dir was not locked during clean");
226+
let uplift_dir = match target.kind() {
227+
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
228+
Some(artifact_dir.examples())
229+
}
230+
// Tests/benchmarks are never uplifted.
231+
TargetKind::Test | TargetKind::Bench => None,
232+
_ => Some(artifact_dir.dest()),
233+
};
234+
// Remove the uplifted copy.
235+
if let Some(uplift_dir) = uplift_dir {
236+
for file_type in file_types {
237+
let uplifted_path =
238+
uplift_dir.join(file_type.uplift_filename(target));
239+
clean_ctx.rm_rf(&uplifted_path)?;
240+
// Dep-info generated by Cargo itself.
241+
let dep_info = uplifted_path.with_extension("d");
242+
clean_ctx.rm_rf(&dep_info)?;
243+
}
244+
}
245+
}
246+
}
247+
}
248+
continue;
249+
}
250+
251+
// Clean fingerprints.
252+
for (_, layout) in &layouts_with_host {
253+
let dir = escape_glob_path(layout.build_dir().legacy_fingerprint())?;
254+
clean_ctx.rm_rf_package_glob_containing_hash(
255+
&pkg.name(),
256+
&Path::new(&dir).join(&pkg_dir),
257+
)?;
206258
}
207259

208260
for target in pkg.targets() {
209261
if target.is_custom_build() {
262+
// Get both the build_script_build and the output directory.
263+
for (_, layout) in &layouts_with_host {
264+
let dir = escape_glob_path(layout.build_dir().build())?;
265+
clean_ctx.rm_rf_package_glob_containing_hash(
266+
&pkg.name(),
267+
&Path::new(&dir).join(&pkg_dir),
268+
)?;
269+
}
210270
continue;
211271
}
272+
let crate_name: Rc<str> = target.crate_name().into();
273+
let path_dot: &str = &format!("{crate_name}.");
274+
let path_dash: &str = &format!("{crate_name}-");
212275
for &mode in &[
213276
CompileMode::Build,
214277
CompileMode::Test,
@@ -222,17 +285,28 @@ fn clean_specs(
222285
let artifact_dir = layout
223286
.artifact_dir()
224287
.expect("artifact-dir was not locked during clean");
225-
let uplift_dir = match target.kind() {
288+
let (dir, uplift_dir) = match target.kind() {
226289
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
227-
Some(artifact_dir.examples())
290+
(layout.build_dir().examples(), Some(artifact_dir.examples()))
228291
}
229292
// Tests/benchmarks are never uplifted.
230-
TargetKind::Test | TargetKind::Bench => None,
231-
_ => Some(artifact_dir.dest()),
293+
TargetKind::Test | TargetKind::Bench => {
294+
(layout.build_dir().legacy_deps(), None)
295+
}
296+
_ => (layout.build_dir().legacy_deps(), Some(artifact_dir.dest())),
232297
};
233-
// Remove the uplifted copy.
234-
if let Some(uplift_dir) = uplift_dir {
235-
for file_type in file_types {
298+
let mut dir_glob_str = escape_glob_path(dir)?;
299+
let dir_glob = Path::new(&dir_glob_str);
300+
for file_type in file_types {
301+
// Some files include a hash in the filename, some don't.
302+
let hashed_name = file_type.output_filename(target, Some("*"));
303+
let unhashed_name = file_type.output_filename(target, None);
304+
305+
clean_ctx.rm_rf_glob(&dir_glob.join(&hashed_name))?;
306+
clean_ctx.rm_rf(&dir.join(&unhashed_name))?;
307+
308+
// Remove the uplifted copy.
309+
if let Some(uplift_dir) = uplift_dir {
236310
let uplifted_path =
237311
uplift_dir.join(file_type.uplift_filename(target));
238312
clean_ctx.rm_rf(&uplifted_path)?;
@@ -241,105 +315,36 @@ fn clean_specs(
241315
clean_ctx.rm_rf(&dep_info)?;
242316
}
243317
}
244-
}
245-
}
246-
}
247-
continue;
248-
}
318+
let unhashed_dep_info = dir.join(format!("{}.d", crate_name));
319+
clean_ctx.rm_rf(&unhashed_dep_info)?;
249320

250-
// Clean fingerprints.
251-
for (_, layout) in &layouts_with_host {
252-
let dir = escape_glob_path(layout.build_dir().legacy_fingerprint())?;
253-
clean_ctx
254-
.rm_rf_package_glob_containing_hash(&pkg.name(), &Path::new(&dir).join(&pkg_dir))?;
255-
}
256-
257-
for target in pkg.targets() {
258-
if target.is_custom_build() {
259-
// Get both the build_script_build and the output directory.
260-
for (_, layout) in &layouts_with_host {
261-
let dir = escape_glob_path(layout.build_dir().build())?;
262-
clean_ctx.rm_rf_package_glob_containing_hash(
263-
&pkg.name(),
264-
&Path::new(&dir).join(&pkg_dir),
265-
)?;
266-
}
267-
continue;
268-
}
269-
let crate_name: Rc<str> = target.crate_name().into();
270-
let path_dot: &str = &format!("{crate_name}.");
271-
let path_dash: &str = &format!("{crate_name}-");
272-
for &mode in &[
273-
CompileMode::Build,
274-
CompileMode::Test,
275-
CompileMode::Check { test: false },
276-
] {
277-
for (compile_kind, layout) in &layouts {
278-
let triple = target_data.short_name(compile_kind);
279-
let (file_types, _unsupported) = target_data
280-
.info(*compile_kind)
281-
.rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
282-
let artifact_dir = layout
283-
.artifact_dir()
284-
.expect("artifact-dir was not locked during clean");
285-
let (dir, uplift_dir) = match target.kind() {
286-
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
287-
(layout.build_dir().examples(), Some(artifact_dir.examples()))
288-
}
289-
// Tests/benchmarks are never uplifted.
290-
TargetKind::Test | TargetKind::Bench => {
291-
(layout.build_dir().legacy_deps(), None)
321+
if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
322+
dir_glob_str.push(std::path::MAIN_SEPARATOR);
292323
}
293-
_ => (layout.build_dir().legacy_deps(), Some(artifact_dir.dest())),
294-
};
295-
let mut dir_glob_str = escape_glob_path(dir)?;
296-
let dir_glob = Path::new(&dir_glob_str);
297-
for file_type in file_types {
298-
// Some files include a hash in the filename, some don't.
299-
let hashed_name = file_type.output_filename(target, Some("*"));
300-
let unhashed_name = file_type.output_filename(target, None);
301-
302-
clean_ctx.rm_rf_glob(&dir_glob.join(&hashed_name))?;
303-
clean_ctx.rm_rf(&dir.join(&unhashed_name))?;
304-
305-
// Remove the uplifted copy.
306-
if let Some(uplift_dir) = uplift_dir {
307-
let uplifted_path = uplift_dir.join(file_type.uplift_filename(target));
308-
clean_ctx.rm_rf(&uplifted_path)?;
309-
// Dep-info generated by Cargo itself.
310-
let dep_info = uplifted_path.with_extension("d");
311-
clean_ctx.rm_rf(&dep_info)?;
324+
dir_glob_str.push('*');
325+
let dir_glob_str: Rc<str> = dir_glob_str.into();
326+
if cleaned_packages
327+
.entry(dir_glob_str.clone())
328+
.or_default()
329+
.insert(crate_name.clone())
330+
{
331+
let paths = [
332+
// Remove dep-info file generated by rustc. It is not tracked in
333+
// file_types. It does not have a prefix.
334+
(path_dash, ".d"),
335+
// Remove split-debuginfo files generated by rustc.
336+
(path_dot, ".o"),
337+
(path_dot, ".dwo"),
338+
(path_dot, ".dwp"),
339+
];
340+
clean_ctx.rm_rf_prefix_list(&dir_glob_str, &paths)?;
312341
}
313-
}
314-
let unhashed_dep_info = dir.join(format!("{}.d", crate_name));
315-
clean_ctx.rm_rf(&unhashed_dep_info)?;
316342

317-
if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
318-
dir_glob_str.push(std::path::MAIN_SEPARATOR);
319-
}
320-
dir_glob_str.push('*');
321-
let dir_glob_str: Rc<str> = dir_glob_str.into();
322-
if cleaned_packages
323-
.entry(dir_glob_str.clone())
324-
.or_default()
325-
.insert(crate_name.clone())
326-
{
327-
let paths = [
328-
// Remove dep-info file generated by rustc. It is not tracked in
329-
// file_types. It does not have a prefix.
330-
(path_dash, ".d"),
331-
// Remove split-debuginfo files generated by rustc.
332-
(path_dot, ".o"),
333-
(path_dot, ".dwo"),
334-
(path_dot, ".dwp"),
335-
];
336-
clean_ctx.rm_rf_prefix_list(&dir_glob_str, &paths)?;
343+
// TODO: what to do about build_script_build?
344+
let dir = escape_glob_path(layout.build_dir().incremental())?;
345+
let incremental = Path::new(&dir).join(format!("{}-*", crate_name));
346+
clean_ctx.rm_rf_glob(&incremental)?;
337347
}
338-
339-
// TODO: what to do about build_script_build?
340-
let dir = escape_glob_path(layout.build_dir().incremental())?;
341-
let incremental = Path::new(&dir).join(format!("{}-*", crate_name));
342-
clean_ctx.rm_rf_glob(&incremental)?;
343348
}
344349
}
345350
}

0 commit comments

Comments
 (0)