Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 117 additions & 112 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,86 @@ fn clean_specs(

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

// Try to reduce the amount of times we iterate over the same target directory by storing away
// the directories we've iterated over (and cleaned for a given package).
let mut cleaned_packages: HashMap<_, HashSet<_>> = HashMap::default();
for pkg in packages {
let pkg_dir = format!("{}-*", pkg.name());
clean_ctx.progress.on_cleaning_package(&pkg.name())?;

if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
for (_compile_kind, layout) in &layouts_with_host {
let dir = layout.build_dir().build_unit(&pkg.name());
clean_ctx.rm_rf(&dir)?;
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Git is so bad at telling indentation changes…

I use this and it perfectly shows that no code change except nested.

https://app.semanticdiff.com/gh/rust-lang/cargo/pull/16304/commits/151d3b7970a976321008886697d925e7800d2738#src/cargo/ops/cargo_clean.rs?ignore_comments=false

Maybe in the doc we should be more explicit and say that no code change?

// Try to reduce the amount of times we iterate over the same target directory by storing away
// the directories we've iterated over (and cleaned for a given package).
let mut cleaned_packages: HashMap<_, HashSet<_>> = HashMap::default();
for pkg in packages {
let pkg_dir = format!("{}-*", pkg.name());
clean_ctx.progress.on_cleaning_package(&pkg.name())?;

if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
for (_compile_kind, layout) in &layouts_with_host {
let dir = layout.build_dir().build_unit(&pkg.name());
clean_ctx.rm_rf(&dir)?;
}

for target in pkg.targets() {
if target.is_custom_build() {
continue;
}
for &mode in &[
CompileMode::Build,
CompileMode::Test,
CompileMode::Check { test: false },
] {
for (compile_kind, layout) in &layouts {
let triple = target_data.short_name(compile_kind);
let (file_types, _unsupported) = target_data
.info(*compile_kind)
.rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
let artifact_dir = layout
.artifact_dir()
.expect("artifact-dir was not locked during clean");
let uplift_dir = match target.kind() {
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
Some(artifact_dir.examples())
}
// Tests/benchmarks are never uplifted.
TargetKind::Test | TargetKind::Bench => None,
_ => Some(artifact_dir.dest()),
};
// Remove the uplifted copy.
if let Some(uplift_dir) = uplift_dir {
for file_type in file_types {
let uplifted_path =
uplift_dir.join(file_type.uplift_filename(target));
clean_ctx.rm_rf(&uplifted_path)?;
// Dep-info generated by Cargo itself.
let dep_info = uplifted_path.with_extension("d");
clean_ctx.rm_rf(&dep_info)?;
}
}
}
}
}
continue;
}

// Clean fingerprints.
for (_, layout) in &layouts_with_host {
let dir = escape_glob_path(layout.build_dir().legacy_fingerprint())?;
clean_ctx.rm_rf_package_glob_containing_hash(
&pkg.name(),
&Path::new(&dir).join(&pkg_dir),
)?;
}

for target in pkg.targets() {
if target.is_custom_build() {
// Get both the build_script_build and the output directory.
for (_, layout) in &layouts_with_host {
let dir = escape_glob_path(layout.build_dir().build())?;
clean_ctx.rm_rf_package_glob_containing_hash(
&pkg.name(),
&Path::new(&dir).join(&pkg_dir),
)?;
}
continue;
}
let crate_name: Rc<str> = target.crate_name().into();
let path_dot: &str = &format!("{crate_name}.");
let path_dash: &str = &format!("{crate_name}-");
for &mode in &[
CompileMode::Build,
CompileMode::Test,
Expand All @@ -222,17 +285,28 @@ fn clean_specs(
let artifact_dir = layout
.artifact_dir()
.expect("artifact-dir was not locked during clean");
let uplift_dir = match target.kind() {
let (dir, uplift_dir) = match target.kind() {
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
Some(artifact_dir.examples())
(layout.build_dir().examples(), Some(artifact_dir.examples()))
}
// Tests/benchmarks are never uplifted.
TargetKind::Test | TargetKind::Bench => None,
_ => Some(artifact_dir.dest()),
TargetKind::Test | TargetKind::Bench => {
(layout.build_dir().legacy_deps(), None)
}
_ => (layout.build_dir().legacy_deps(), Some(artifact_dir.dest())),
};
// Remove the uplifted copy.
if let Some(uplift_dir) = uplift_dir {
for file_type in file_types {
let mut dir_glob_str = escape_glob_path(dir)?;
let dir_glob = Path::new(&dir_glob_str);
for file_type in file_types {
// Some files include a hash in the filename, some don't.
let hashed_name = file_type.output_filename(target, Some("*"));
let unhashed_name = file_type.output_filename(target, None);

clean_ctx.rm_rf_glob(&dir_glob.join(&hashed_name))?;
clean_ctx.rm_rf(&dir.join(&unhashed_name))?;

// Remove the uplifted copy.
if let Some(uplift_dir) = uplift_dir {
let uplifted_path =
uplift_dir.join(file_type.uplift_filename(target));
clean_ctx.rm_rf(&uplifted_path)?;
Expand All @@ -241,105 +315,36 @@ fn clean_specs(
clean_ctx.rm_rf(&dep_info)?;
}
}
}
}
}
continue;
}
let unhashed_dep_info = dir.join(format!("{}.d", crate_name));
clean_ctx.rm_rf(&unhashed_dep_info)?;

// Clean fingerprints.
for (_, layout) in &layouts_with_host {
let dir = escape_glob_path(layout.build_dir().legacy_fingerprint())?;
clean_ctx
.rm_rf_package_glob_containing_hash(&pkg.name(), &Path::new(&dir).join(&pkg_dir))?;
}

for target in pkg.targets() {
if target.is_custom_build() {
// Get both the build_script_build and the output directory.
for (_, layout) in &layouts_with_host {
let dir = escape_glob_path(layout.build_dir().build())?;
clean_ctx.rm_rf_package_glob_containing_hash(
&pkg.name(),
&Path::new(&dir).join(&pkg_dir),
)?;
}
continue;
}
let crate_name: Rc<str> = target.crate_name().into();
let path_dot: &str = &format!("{crate_name}.");
let path_dash: &str = &format!("{crate_name}-");
for &mode in &[
CompileMode::Build,
CompileMode::Test,
CompileMode::Check { test: false },
] {
for (compile_kind, layout) in &layouts {
let triple = target_data.short_name(compile_kind);
let (file_types, _unsupported) = target_data
.info(*compile_kind)
.rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
let artifact_dir = layout
.artifact_dir()
.expect("artifact-dir was not locked during clean");
let (dir, uplift_dir) = match target.kind() {
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
(layout.build_dir().examples(), Some(artifact_dir.examples()))
}
// Tests/benchmarks are never uplifted.
TargetKind::Test | TargetKind::Bench => {
(layout.build_dir().legacy_deps(), None)
if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
dir_glob_str.push(std::path::MAIN_SEPARATOR);
}
_ => (layout.build_dir().legacy_deps(), Some(artifact_dir.dest())),
};
let mut dir_glob_str = escape_glob_path(dir)?;
let dir_glob = Path::new(&dir_glob_str);
for file_type in file_types {
// Some files include a hash in the filename, some don't.
let hashed_name = file_type.output_filename(target, Some("*"));
let unhashed_name = file_type.output_filename(target, None);

clean_ctx.rm_rf_glob(&dir_glob.join(&hashed_name))?;
clean_ctx.rm_rf(&dir.join(&unhashed_name))?;

// Remove the uplifted copy.
if let Some(uplift_dir) = uplift_dir {
let uplifted_path = uplift_dir.join(file_type.uplift_filename(target));
clean_ctx.rm_rf(&uplifted_path)?;
// Dep-info generated by Cargo itself.
let dep_info = uplifted_path.with_extension("d");
clean_ctx.rm_rf(&dep_info)?;
dir_glob_str.push('*');
let dir_glob_str: Rc<str> = dir_glob_str.into();
if cleaned_packages
.entry(dir_glob_str.clone())
.or_default()
.insert(crate_name.clone())
{
let paths = [
// Remove dep-info file generated by rustc. It is not tracked in
// file_types. It does not have a prefix.
(path_dash, ".d"),
// Remove split-debuginfo files generated by rustc.
(path_dot, ".o"),
(path_dot, ".dwo"),
(path_dot, ".dwp"),
];
clean_ctx.rm_rf_prefix_list(&dir_glob_str, &paths)?;
}
}
let unhashed_dep_info = dir.join(format!("{}.d", crate_name));
clean_ctx.rm_rf(&unhashed_dep_info)?;

if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
dir_glob_str.push(std::path::MAIN_SEPARATOR);
}
dir_glob_str.push('*');
let dir_glob_str: Rc<str> = dir_glob_str.into();
if cleaned_packages
.entry(dir_glob_str.clone())
.or_default()
.insert(crate_name.clone())
{
let paths = [
// Remove dep-info file generated by rustc. It is not tracked in
// file_types. It does not have a prefix.
(path_dash, ".d"),
// Remove split-debuginfo files generated by rustc.
(path_dot, ".o"),
(path_dot, ".dwo"),
(path_dot, ".dwp"),
];
clean_ctx.rm_rf_prefix_list(&dir_glob_str, &paths)?;
// TODO: what to do about build_script_build?
let dir = escape_glob_path(layout.build_dir().incremental())?;
let incremental = Path::new(&dir).join(format!("{}-*", crate_name));
clean_ctx.rm_rf_glob(&incremental)?;
}

// TODO: what to do about build_script_build?
let dir = escape_glob_path(layout.build_dir().incremental())?;
let incremental = Path::new(&dir).join(format!("{}-*", crate_name));
clean_ctx.rm_rf_glob(&incremental)?;
}
}
}
Expand Down