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