]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/clean.rs
Rollup merge of #94806 - jyn514:cargo-run-tidy, r=Mark-Simulacrum
[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 crate::util::t;
13 use crate::Build;
14
15 pub fn clean(build: &Build, all: bool) {
16     rm_rf("tmp".as_ref());
17
18     if all {
19         rm_rf(&build.out);
20     } else {
21         rm_rf(&build.out.join("tmp"));
22         rm_rf(&build.out.join("dist"));
23         rm_rf(&build.out.join("bootstrap"));
24
25         for host in &build.hosts {
26             let entries = match build.out.join(host.triple).read_dir() {
27                 Ok(iter) => iter,
28                 Err(_) => continue,
29             };
30
31             for entry in entries {
32                 let entry = t!(entry);
33                 if entry.file_name().to_str() == Some("llvm") {
34                     continue;
35                 }
36                 let path = t!(entry.path().canonicalize());
37                 rm_rf(&path);
38             }
39         }
40     }
41 }
42
43 fn rm_rf(path: &Path) {
44     match path.symlink_metadata() {
45         Err(e) => {
46             if e.kind() == ErrorKind::NotFound {
47                 return;
48             }
49             panic!("failed to get metadata for file {}: {}", path.display(), e);
50         }
51         Ok(metadata) => {
52             if metadata.file_type().is_file() || metadata.file_type().is_symlink() {
53                 do_op(path, "remove file", |p| {
54                     fs::remove_file(p).or_else(|e| {
55                         // Work around the fact that we cannot
56                         // delete an executable while it runs on Windows.
57                         #[cfg(windows)]
58                         if e.kind() == std::io::ErrorKind::PermissionDenied
59                             && p.file_name().and_then(std::ffi::OsStr::to_str)
60                                 == Some("bootstrap.exe")
61                         {
62                             eprintln!("warning: failed to delete '{}'.", p.display());
63                             return Ok(());
64                         }
65                         Err(e)
66                     })
67                 });
68                 return;
69             }
70
71             for file in t!(fs::read_dir(path)) {
72                 rm_rf(&t!(file).path());
73             }
74             do_op(path, "remove dir", |p| {
75                 fs::remove_dir(p).or_else(|e| {
76                     // Check for dir not empty on Windows
77                     // FIXME: Once `ErrorKind::DirectoryNotEmpty` is stabilized,
78                     // match on `e.kind()` instead.
79                     #[cfg(windows)]
80                     if e.raw_os_error() == Some(145) {
81                         return Ok(());
82                     }
83
84                     Err(e)
85                 })
86             });
87         }
88     };
89 }
90
91 fn do_op<F>(path: &Path, desc: &str, mut f: F)
92 where
93     F: FnMut(&Path) -> io::Result<()>,
94 {
95     match f(path) {
96         Ok(()) => {}
97         // On windows we can't remove a readonly file, and git will often clone files as readonly.
98         // As a result, we have some special logic to remove readonly files on windows.
99         // This is also the reason that we can't use things like fs::remove_dir_all().
100         Err(ref e) if cfg!(windows) && e.kind() == ErrorKind::PermissionDenied => {
101             let m = t!(path.symlink_metadata());
102             let mut p = m.permissions();
103             p.set_readonly(false);
104             t!(fs::set_permissions(path, p));
105             f(path).unwrap_or_else(|e| {
106                 // Delete symlinked directories on Windows
107                 #[cfg(windows)]
108                 if m.file_type().is_symlink() && path.is_dir() && fs::remove_dir(path).is_ok() {
109                     return;
110                 }
111                 panic!("failed to {} {}: {}", desc, path.display(), e);
112             });
113         }
114         Err(e) => {
115             panic!("failed to {} {}: {}", desc, path.display(), e);
116         }
117     }
118 }