]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/clean.rs
Separate codepaths for fat and thin LTO in write.rs
[rust.git] / src / bootstrap / clean.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Implementation of `make clean` in rustbuild.
12 //!
13 //! Responsible for cleaning out a build directory of all old and stale
14 //! artifacts to prepare for a fresh build. Currently doesn't remove the
15 //! `build/cache` directory (download cache) or the `build/$target/llvm`
16 //! directory unless the --all flag is present.
17
18 use std::fs;
19 use std::io::{self, ErrorKind};
20 use std::path::Path;
21
22 use Build;
23
24 pub fn clean(build: &Build, all: bool) {
25     rm_rf("tmp".as_ref());
26
27     if all {
28         rm_rf(&build.out);
29     } else {
30         rm_rf(&build.out.join("tmp"));
31         rm_rf(&build.out.join("dist"));
32
33         for host in &build.hosts {
34             let entries = match build.out.join(host).read_dir() {
35                 Ok(iter) => iter,
36                 Err(_) => continue,
37             };
38
39             for entry in entries {
40                 let entry = t!(entry);
41                 if entry.file_name().to_str() == Some("llvm") {
42                     continue
43                 }
44                 let path = t!(entry.path().canonicalize());
45                 rm_rf(&path);
46             }
47         }
48     }
49 }
50
51 fn rm_rf(path: &Path) {
52     match path.symlink_metadata() {
53         Err(e) => {
54             if e.kind() == ErrorKind::NotFound {
55                 return;
56             }
57             panic!("failed to get metadata for file {}: {}", path.display(), e);
58         },
59         Ok(metadata) => {
60             if metadata.file_type().is_file() || metadata.file_type().is_symlink() {
61                 do_op(path, "remove file", |p| fs::remove_file(p));
62                 return;
63             }
64
65             for file in t!(fs::read_dir(path)) {
66                 rm_rf(&t!(file).path());
67             }
68             do_op(path, "remove dir", |p| fs::remove_dir(p));
69         },
70     };
71 }
72
73 fn do_op<F>(path: &Path, desc: &str, mut f: F)
74     where F: FnMut(&Path) -> io::Result<()>
75 {
76     match f(path) {
77         Ok(()) => {}
78         // On windows we can't remove a readonly file, and git will often clone files as readonly.
79         // As a result, we have some special logic to remove readonly files on windows.
80         // This is also the reason that we can't use things like fs::remove_dir_all().
81         Err(ref e) if cfg!(windows) &&
82                       e.kind() == ErrorKind::PermissionDenied => {
83             let mut p = t!(path.symlink_metadata()).permissions();
84             p.set_readonly(false);
85             t!(fs::set_permissions(path, p));
86             f(path).unwrap_or_else(|e| {
87                 panic!("failed to {} {}: {}", desc, path.display(), e);
88             })
89         }
90         Err(e) => {
91             panic!("failed to {} {}: {}", desc, path.display(), e);
92         }
93     }
94 }