]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/clean.rs
Do not delete bootstrap.exe on Windows during clean
[rust.git] / src / bootstrap / clean.rs
1 //! Implementation of `make clean` in rustbuild.
2 //!
3 //! Responsible for cleaning out a build directory of all old and stale
4 //! artifacts to prepare for a fresh build. Currently doesn't remove the
5 //! `build/cache` directory (download cache) or the `build/$target/llvm`
6 //! directory unless the `--all` flag is present.
7
8 use std::fs;
9 use std::io::{self, ErrorKind};
10 use std::path::Path;
11
12 use build_helper::t;
13
14 use crate::Build;
15
16 pub fn clean(build: &Build, all: bool) {
17     rm_rf("tmp".as_ref());
18
19     if all {
20         rm_rf(&build.out);
21     } else {
22         rm_rf(&build.out.join("tmp"));
23         rm_rf(&build.out.join("dist"));
24         // Only delete the bootstrap executable on non-Windows systems
25         // Windows does not allow deleting a currently running executable
26         #[cfg(not(windows))]
27         rm_rf(&build.out.join("bootstrap"));
28
29         for host in &build.hosts {
30             let entries = match build.out.join(host.triple).read_dir() {
31                 Ok(iter) => iter,
32                 Err(_) => continue,
33             };
34
35             for entry in entries {
36                 let entry = t!(entry);
37                 if entry.file_name().to_str() == Some("llvm") {
38                     continue;
39                 }
40                 let path = t!(entry.path().canonicalize());
41                 rm_rf(&path);
42             }
43         }
44     }
45 }
46
47 fn rm_rf(path: &Path) {
48     match path.symlink_metadata() {
49         Err(e) => {
50             if e.kind() == ErrorKind::NotFound {
51                 return;
52             }
53             panic!("failed to get metadata for file {}: {}", path.display(), e);
54         }
55         Ok(metadata) => {
56             if metadata.file_type().is_file() || metadata.file_type().is_symlink() {
57                 do_op(path, "remove file", |p| fs::remove_file(p));
58                 return;
59             }
60
61             for file in t!(fs::read_dir(path)) {
62                 rm_rf(&t!(file).path());
63             }
64             do_op(path, "remove dir", |p| fs::remove_dir(p));
65         }
66     };
67 }
68
69 fn do_op<F>(path: &Path, desc: &str, mut f: F)
70 where
71     F: FnMut(&Path) -> io::Result<()>,
72 {
73     match f(path) {
74         Ok(()) => {}
75         // On windows we can't remove a readonly file, and git will often clone files as readonly.
76         // As a result, we have some special logic to remove readonly files on windows.
77         // This is also the reason that we can't use things like fs::remove_dir_all().
78         Err(ref e) if cfg!(windows) && e.kind() == ErrorKind::PermissionDenied => {
79             let mut p = t!(path.symlink_metadata()).permissions();
80             p.set_readonly(false);
81             t!(fs::set_permissions(path, p));
82             f(path).unwrap_or_else(|e| {
83                 panic!("failed to {} {}: {}", desc, path.display(), e);
84             })
85         }
86         Err(e) => {
87             panic!("failed to {} {}: {}", desc, path.display(), e);
88         }
89     }
90 }