remove stage-specific artifacts when --stage is used

Signed-off-by: ozkanonur <work@onurozkan.dev>
This commit is contained in:
ozkanonur 2023-07-30 23:12:34 +03:00
parent 25268638aa
commit 78326cdb35

View File

@ -26,10 +26,15 @@ fn make_run(run: RunConfig<'_>) {
} }
fn run(self, builder: &Builder<'_>) -> Self::Output { fn run(self, builder: &Builder<'_>) -> Self::Output {
let Subcommand::Clean { all, .. } = builder.config.cmd else { let Subcommand::Clean { all, stage } = builder.config.cmd else {
unreachable!("wrong subcommand?") unreachable!("wrong subcommand?")
}; };
clean_default(builder.build, all)
if all && stage.is_some() {
panic!("--all and --stage can't be used at the same time for `x clean`");
}
clean(builder.build, all, stage)
} }
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
@ -86,35 +91,70 @@ fn run(self, builder: &Builder<'_>) -> Self::Output {
Std, Mode::Std, "sysroot"; Std, Mode::Std, "sysroot";
} }
fn clean_default(build: &Build, all: bool) { fn clean(build: &Build, all: bool, stage: Option<u32>) {
if build.config.dry_run() { if build.config.dry_run() {
return; return;
} }
rm_rf("tmp".as_ref()); rm_rf("tmp".as_ref());
// Clean the entire build directory
if all { if all {
rm_rf(&build.out); rm_rf(&build.out);
} else { return;
rm_rf(&build.out.join("tmp")); }
rm_rf(&build.out.join("dist"));
rm_rf(&build.out.join("bootstrap"));
rm_rf(&build.out.join("rustfmt.stamp"));
for host in &build.hosts { // Clean the target stage artifacts
let entries = match build.out.join(host.triple).read_dir() { if let Some(stage) = stage {
Ok(iter) => iter, clean_specific_stage(build, stage);
Err(_) => continue, return;
}; }
for entry in entries { // Follow the default behaviour
let entry = t!(entry); clean_default(build);
if entry.file_name().to_str() == Some("llvm") { }
continue;
} fn clean_specific_stage(build: &Build, stage: u32) {
let path = t!(entry.path().canonicalize()); for host in &build.hosts {
rm_rf(&path); let entries = match build.out.join(host.triple).read_dir() {
Ok(iter) => iter,
Err(_) => continue,
};
for entry in entries {
let entry = t!(entry);
let stage_prefix = format!("stage{}", stage);
// if current entry is not related with the target stage, continue
if !entry.file_name().to_str().unwrap_or("").contains(&stage_prefix) {
continue;
} }
let path = t!(entry.path().canonicalize());
rm_rf(&path);
}
}
}
fn clean_default(build: &Build) {
rm_rf(&build.out.join("tmp"));
rm_rf(&build.out.join("dist"));
rm_rf(&build.out.join("bootstrap"));
rm_rf(&build.out.join("rustfmt.stamp"));
for host in &build.hosts {
let entries = match build.out.join(host.triple).read_dir() {
Ok(iter) => iter,
Err(_) => continue,
};
for entry in entries {
let entry = t!(entry);
if entry.file_name().to_str() == Some("llvm") {
continue;
}
let path = t!(entry.path().canonicalize());
rm_rf(&path);
} }
} }
} }