]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/main.rs
cabaee5d0600551ac1df0488456353b3ebc669b5
[rust.git] / src / tools / tidy / src / main.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 //! Tidy checks for source code in this repository
12 //!
13 //! This program runs all of the various tidy checks for style, cleanliness,
14 //! etc. This is run by default on `make check` and as part of the auto
15 //! builders.
16
17 use std::fs;
18 use std::path::{PathBuf, Path};
19 use std::env;
20
21 macro_rules! t {
22     ($e:expr, $p:expr) => (match $e {
23         Ok(e) => e,
24         Err(e) => panic!("{} failed on {} with {}", stringify!($e), ($p).display(), e),
25     });
26
27     ($e:expr) => (match $e {
28         Ok(e) => e,
29         Err(e) => panic!("{} failed with {}", stringify!($e), e),
30     })
31 }
32
33 mod bins;
34 mod style;
35 mod errors;
36 mod features;
37 mod cargo;
38 mod cargo_lock;
39 mod pal;
40
41 fn main() {
42     let path = env::args_os().skip(1).next().expect("need an argument");
43     let path = PathBuf::from(path);
44
45     let mut bad = false;
46     bins::check(&path, &mut bad);
47     style::check(&path, &mut bad);
48     errors::check(&path, &mut bad);
49     cargo::check(&path, &mut bad);
50     features::check(&path, &mut bad);
51     cargo_lock::check(&path, &mut bad);
52     pal::check(&path, &mut bad);
53
54     if bad {
55         panic!("some tidy checks failed");
56     }
57 }
58
59 fn filter_dirs(path: &Path) -> bool {
60     let skip = [
61         "src/jemalloc",
62         "src/llvm",
63         "src/libbacktrace",
64         "src/compiler-rt",
65         "src/rt/hoedown",
66         "src/rustllvm",
67         "src/rust-installer",
68         "src/liblibc",
69     ];
70     skip.iter().any(|p| path.ends_with(p))
71 }
72
73
74 fn walk(path: &Path, skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
75     for entry in t!(fs::read_dir(path), path) {
76         let entry = t!(entry);
77         let kind = t!(entry.file_type());
78         let path = entry.path();
79         if kind.is_dir() {
80             if !skip(&path) {
81                 walk(&path, skip, f);
82             }
83         } else {
84             f(&path);
85         }
86     }
87 }