about summary refs log tree commit diff
diff options
context:
space:
mode:
authorantoyo <antoyo@users.noreply.github.com>2023-12-20 10:09:16 -0500
committerGitHub <noreply@github.com>2023-12-20 10:09:16 -0500
commit7dad07a67ca32e8fc518a61c9168e24ccfd9d09f (patch)
tree8ca4a5dffdcb814c7f373f5fae039121ab25aff4
parente0c4d65e079e470ee18446cef9feb5b753ba8349 (diff)
parente26e074261c841508cf4114645291c92ab552be2 (diff)
downloadrust-7dad07a67ca32e8fc518a61c9168e24ccfd9d09f.tar.gz
rust-7dad07a67ca32e8fc518a61c9168e24ccfd9d09f.zip
Merge pull request #396 from GuillaumeGomez/rustify-clean-all
Rustify `clean_all.sh`
-rw-r--r--.github/workflows/ci.yml2
-rw-r--r--.github/workflows/gcc12.yml2
-rw-r--r--.github/workflows/m68k.yml2
-rw-r--r--.github/workflows/release.yml2
-rw-r--r--.github/workflows/stdarch.yml2
-rwxr-xr-xbuild_sysroot/build_sysroot.sh34
-rw-r--r--build_system/src/clean.rs73
-rw-r--r--build_system/src/main.rs5
-rw-r--r--build_system/src/utils.rs2
-rwxr-xr-xclean_all.sh6
-rwxr-xr-xrustup.sh2
11 files changed, 85 insertions, 47 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index b04ea1550ba..d14f30338b0 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -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: |
diff --git a/.github/workflows/gcc12.yml b/.github/workflows/gcc12.yml
index a27ef913c21..eef26f01789 100644
--- a/.github/workflows/gcc12.yml
+++ b/.github/workflows/gcc12.yml
@@ -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: |
diff --git a/.github/workflows/m68k.yml b/.github/workflows/m68k.yml
index ac141e06247..a7489b10744 100644
--- a/.github/workflows/m68k.yml
+++ b/.github/workflows/m68k.yml
@@ -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: |
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 9798bc338f3..6dc950f88a2 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -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: |
diff --git a/.github/workflows/stdarch.yml b/.github/workflows/stdarch.yml
index d290f1d0562..dc670c5701c 100644
--- a/.github/workflows/stdarch.yml
+++ b/.github/workflows/stdarch.yml
@@ -100,7 +100,7 @@ jobs:
     - name: Clean
       if: ${{ !matrix.cargo_runner }}
       run: |
-        ./clean_all.sh
+        ./y.sh clean all
 
     - name: Prepare dependencies
       run: |
diff --git a/build_sysroot/build_sysroot.sh b/build_sysroot/build_sysroot.sh
deleted file mode 100755
index ebc7dc375b1..00000000000
--- a/build_sysroot/build_sysroot.sh
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/usr/bin/env bash
-
-# Requires the CHANNEL env var to be set to `debug` or `release.`
-
-set -e
-cd $(dirname "$0")
-
-pushd ../
-source ./config.sh
-popd
-
-# Cleanup for previous run
-#     v Clean target dir except for build scripts and incremental cache
-rm -r target/*/{debug,release}/{build,deps,examples,libsysroot*,native} 2>/dev/null || true
-rm Cargo.lock test_target/Cargo.lock 2>/dev/null || true
-rm -r sysroot/ 2>/dev/null || true
-
-# Build libs
-export RUSTFLAGS="$RUSTFLAGS -Z force-unstable-if-unmarked"
-if [[ "$1" == "--release" ]]; then
-    sysroot_channel='release'
-    RUSTFLAGS="$RUSTFLAGS -Zmir-opt-level=3" cargo build --target $TARGET_TRIPLE --release
-else
-    sysroot_channel='debug'
-    cargo build --target $TARGET_TRIPLE
-fi
-
-# Copy files to sysroot
-mkdir -p sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
-cp -r target/$TARGET_TRIPLE/$sysroot_channel/deps/* sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
-# Copy the source files to the sysroot (Rust for Linux needs this).
-source_dir=sysroot/lib/rustlib/src/rust
-mkdir -p $source_dir
-cp -r sysroot_src/library/ $source_dir
diff --git a/build_system/src/clean.rs b/build_system/src/clean.rs
new file mode 100644
index 00000000000..56cc19d255f
--- /dev/null
+++ b/build_system/src/clean.rs
@@ -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(())
+}
diff --git a/build_system/src/main.rs b/build_system/src/main.rs
index e0091ff6977..1ed44b22a95 100644
--- a/build_system/src/main.rs
+++ b/build_system/src/main.rs
@@ -2,6 +2,7 @@ use std::env;
 use std::process;
 
 mod build;
+mod clean;
 mod config;
 mod prepare;
 mod rustc_info;
@@ -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
@@ -30,6 +32,7 @@ Available commands for build_system:
 }
 
 pub enum Command {
+    Clean,
     Prepare,
     Build,
     Test,
@@ -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,
@@ -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(),
diff --git a/build_system/src/utils.rs b/build_system/src/utils.rs
index 9c3a86ad68a..276f4f2d989 100644
--- a/build_system/src/utils.rs
+++ b/build_system/src/utils.rs
@@ -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 `{}`: {:?}",
diff --git a/clean_all.sh b/clean_all.sh
deleted file mode 100755
index 782bd3e5058..00000000000
--- a/clean_all.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/usr/bin/env bash
-set -e
-set -v
-
-rm -rf target/ build_sysroot/{sysroot/,sysroot_src/,target/,Cargo.lock} perf.data{,.old}
-rm -rf regex/ simple-raytracer/
diff --git a/rustup.sh b/rustup.sh
index a4f938e4b5b..3cdc07ca520 100755
--- a/rustup.sh
+++ b/rustup.sh
@@ -15,7 +15,7 @@ case $1 in
             rustup toolchain uninstall $nightly
         done
 
-        ./clean_all.sh
+        ./y.sh clean all
         ./y.sh prepare
         ;;
     "commit")