Skip to content

Commit

Permalink
Merge pull request rust-lang#396 from GuillaumeGomez/rustify-clean-all
Browse files Browse the repository at this point in the history
Rustify `clean_all.sh`
  • Loading branch information
antoyo committed Dec 20, 2023
2 parents e0c4d65 + e26e074 commit 7dad07a
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
./y.sh build --features master
# TODO: remove --features master when it is back to the default.
cargo test --features master
./clean_all.sh
./y.sh clean all
- name: Prepare dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gcc12.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
./y.sh prepare --only-libcore --libgccjit12-patches
./y.sh build --no-default-features --sysroot-panic-abort
cargo test --no-default-features
./clean_all.sh
./y.sh clean all
- name: Prepare dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/m68k.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
./y.sh build --target-triple m68k-unknown-linux-gnu --features master
# TODO: remove --features master when it is back to the default.
CG_GCC_TEST_TARGET=m68k-unknown-linux-gnu cargo test --features master
./clean_all.sh
./y.sh clean all
- name: Prepare dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
EMBED_LTO_BITCODE=1 ./y.sh build --release --release-sysroot --features master
# TODO: remove --features master when it is back to the default.
cargo test --features master
./clean_all.sh
./y.sh clean all
- name: Prepare dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/stdarch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
- name: Clean
if: ${{ !matrix.cargo_runner }}
run: |
./clean_all.sh
./y.sh clean all
- name: Prepare dependencies
run: |
Expand Down
34 changes: 0 additions & 34 deletions build_sysroot/build_sysroot.sh

This file was deleted.

73 changes: 73 additions & 0 deletions build_system/src/clean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::utils::remove_file;

use std::fs::remove_dir_all;

#[derive(Default)]
struct CleanArg {
all: bool,
}

impl CleanArg {
fn new() -> Result<Option<Self>, String> {
let mut args = CleanArg::default();

// We skip the binary and the "clean" option.
for arg in std::env::args().skip(2) {
match arg.as_str() {
"all" => args.all = true,
"--help" => {
Self::usage();
return Ok(None);
}
a => return Err(format!("Unknown argument `{}`", a)),
}
}
Ok(Some(args))
}

fn usage() {
println!(
r#"
`clean` command help:
all : Clean all data
--help : Show this help
"#
)
}
}

fn clean_all() -> Result<(), String> {
let dirs_to_remove = [
"target",
"build_sysroot/sysroot",
"build_sysroot/sysroot_src",
"build_sysroot/target",
"regex",
"simple-raytracer",
];
for dir in dirs_to_remove {
let _ = remove_dir_all(dir);
}

let files_to_remove = ["build_sysroot/Cargo.lock", "perf.data", "perf.data.old"];

for file in files_to_remove {
let _ = remove_file(file);
}

println!("Successfully ran `clean all`");
Ok(())
}

pub fn run() -> Result<(), String> {
let args = match CleanArg::new()? {
Some(a) => a,
None => return Ok(()),
};

if args.all {
clean_all()?;
}
Ok(())
}
5 changes: 5 additions & 0 deletions build_system/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env;
use std::process;

mod build;
mod clean;
mod config;
mod prepare;
mod rustc_info;
Expand All @@ -22,6 +23,7 @@ fn usage() {
"\
Available commands for build_system:
clean : Run clean command
prepare : Run prepare command
build : Run build command
test : Run test command
Expand All @@ -30,6 +32,7 @@ Available commands for build_system:
}

pub enum Command {
Clean,
Prepare,
Build,
Test,
Expand All @@ -41,6 +44,7 @@ fn main() {
}

let command = match env::args().nth(1).as_deref() {
Some("clean") => Command::Clean,
Some("prepare") => Command::Prepare,
Some("build") => Command::Build,
Some("test") => Command::Test,
Expand All @@ -57,6 +61,7 @@ fn main() {
};

if let Err(e) = match command {
Command::Clean => clean::run(),
Command::Prepare => prepare::run(),
Command::Build => build::run(),
Command::Test => test::run(),
Expand Down
2 changes: 1 addition & 1 deletion build_system/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ pub fn split_args(args: &str) -> Result<Vec<String>, String> {
Ok(out)
}

pub fn remove_file<P: AsRef<Path>>(file_path: &P) -> Result<(), String> {
pub fn remove_file<P: AsRef<Path> + ?Sized>(file_path: &P) -> Result<(), String> {
std::fs::remove_file(file_path).map_err(|error| {
format!(
"Failed to remove `{}`: {:?}",
Expand Down
6 changes: 0 additions & 6 deletions clean_all.sh

This file was deleted.

2 changes: 1 addition & 1 deletion rustup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ case $1 in
rustup toolchain uninstall $nightly
done

./clean_all.sh
./y.sh clean all
./y.sh prepare
;;
"commit")
Expand Down

0 comments on commit 7dad07a

Please sign in to comment.