Skip to content

Commit bfd5985

Browse files
authored
refactor(clean): Better divide old / new layout (#16304)
### What does this PR try to resolve? A follow up to #16300. This doesn't split out any functions - The stuff before hand is quite significant - For #16264, we made add more before and some after - We could pull out only the branches of the `if` into functions but then we'd just move them back when this is done and seems like extra churn ### How to test and review this PR?
2 parents 3a4485d + 4414a60 commit bfd5985

File tree

1 file changed

+104
-97
lines changed

1 file changed

+104
-97
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 104 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,17 @@ 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 {
195+
if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
196+
for pkg in packages {
197+
clean_ctx.progress.on_cleaning_package(&pkg.name())?;
198+
199+
// Remove intermediate artifacts
203200
for (_compile_kind, layout) in &layouts_with_host {
204201
let dir = layout.build_dir().build_unit(&pkg.name());
205202
clean_ctx.rm_rf(&dir)?;
206203
}
207204

205+
// Remove the uplifted copy.
208206
for target in pkg.targets() {
209207
if target.is_custom_build() {
210208
continue;
@@ -230,7 +228,6 @@ fn clean_specs(
230228
TargetKind::Test | TargetKind::Bench => None,
231229
_ => Some(artifact_dir.dest()),
232230
};
233-
// Remove the uplifted copy.
234231
if let Some(uplift_dir) = uplift_dir {
235232
for file_type in file_types {
236233
let uplifted_path =
@@ -244,102 +241,112 @@ fn clean_specs(
244241
}
245242
}
246243
}
247-
continue;
248-
}
249-
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))?;
255244
}
245+
} else {
246+
// Try to reduce the amount of times we iterate over the same target directory by storing away
247+
// the directories we've iterated over (and cleaned for a given package).
248+
let mut cleaned_packages: HashMap<_, HashSet<_>> = HashMap::default();
249+
for pkg in packages {
250+
let pkg_dir = format!("{}-*", pkg.name());
251+
clean_ctx.progress.on_cleaning_package(&pkg.name())?;
252+
253+
// Clean fingerprints.
254+
for (_, layout) in &layouts_with_host {
255+
let dir = escape_glob_path(layout.build_dir().legacy_fingerprint())?;
256+
clean_ctx.rm_rf_package_glob_containing_hash(
257+
&pkg.name(),
258+
&Path::new(&dir).join(&pkg_dir),
259+
)?;
260+
}
256261

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

317-
if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
318-
dir_glob_str.push(std::path::MAIN_SEPARATOR);
345+
// TODO: what to do about build_script_build?
346+
let dir = escape_glob_path(layout.build_dir().incremental())?;
347+
let incremental = Path::new(&dir).join(format!("{}-*", crate_name));
348+
clean_ctx.rm_rf_glob(&incremental)?;
319349
}
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)?;
337-
}
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)?;
343350
}
344351
}
345352
}

0 commit comments

Comments
 (0)