]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/clean.rs
Rollup merge of #39622 - alexcrichton:clean-dist, r=brson
[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 as we want that cached between builds.
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) {
25     rm_rf(build, "tmp".as_ref());
26     rm_rf(build, &build.out.join("tmp"));
27     rm_rf(build, &build.out.join("dist"));
28
29     for host in build.config.host.iter() {
30         let entries = match build.out.join(host).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(build, &path);
42         }
43     }
44 }
45
46 fn rm_rf(build: &Build, path: &Path) {
47     if !path.exists() {
48         return
49     }
50     if path.is_file() {
51         return do_op(path, "remove file", |p| fs::remove_file(p));
52     }
53
54     for file in t!(fs::read_dir(path)) {
55         let file = t!(file).path();
56
57         if file.is_dir() {
58             rm_rf(build, &file);
59         } else {
60             // On windows we can't remove a readonly file, and git will
61             // often clone files as readonly. As a result, we have some
62             // special logic to remove readonly files on windows.
63             do_op(&file, "remove file", |p| fs::remove_file(p));
64         }
65     }
66     do_op(path, "remove dir", |p| fs::remove_dir(p));
67 }
68
69 fn do_op<F>(path: &Path, desc: &str, mut f: F)
70     where F: FnMut(&Path) -> io::Result<()>
71 {
72     match f(path) {
73         Ok(()) => {}
74         Err(ref e) if cfg!(windows) &&
75                       e.kind() == ErrorKind::PermissionDenied => {
76             let mut p = t!(path.metadata()).permissions();
77             p.set_readonly(false);
78             t!(fs::set_permissions(path, p));
79             f(path).unwrap_or_else(|e| {
80                 panic!("failed to {} {}: {}", desc, path.display(), e);
81             })
82         }
83         Err(e) => {
84             panic!("failed to {} {}: {}", desc, path.display(), e);
85         }
86     }
87 }