rust_tidy/
main.rs

1//! Tidy checks source code in this repository.
2//!
3//! This program runs all of the various tidy checks for style, cleanliness,
4//! etc. This is run by default on `./x.py test` and as part of the auto
5//! builders. The tidy checks can be executed with `./x.py test tidy`.
6
7use std::collections::VecDeque;
8use std::num::NonZeroUsize;
9use std::path::PathBuf;
10use std::str::FromStr;
11use std::sync::atomic::{AtomicBool, Ordering};
12use std::thread::{self, ScopedJoinHandle, scope};
13use std::{env, process};
14
15use tidy::*;
16
17fn main() {
18    // Running Cargo will read the libstd Cargo.toml
19    // which uses the unstable `public-dependency` feature.
20    //
21    // `setenv` might not be thread safe, so run it before using multiple threads.
22    env::set_var("RUSTC_BOOTSTRAP", "1");
23
24    let root_path: PathBuf = env::args_os().nth(1).expect("need path to root of repo").into();
25    let cargo: PathBuf = env::args_os().nth(2).expect("need path to cargo").into();
26    let output_directory: PathBuf =
27        env::args_os().nth(3).expect("need path to output directory").into();
28    let concurrency: NonZeroUsize =
29        FromStr::from_str(&env::args().nth(4).expect("need concurrency"))
30            .expect("concurrency must be a number");
31
32    let root_manifest = root_path.join("Cargo.toml");
33    let src_path = root_path.join("src");
34    let tests_path = root_path.join("tests");
35    let library_path = root_path.join("library");
36    let compiler_path = root_path.join("compiler");
37    let librustdoc_path = src_path.join("librustdoc");
38    let crashes_path = tests_path.join("crashes");
39
40    let args: Vec<String> = env::args().skip(1).collect();
41    let (cfg_args, pos_args) = match args.iter().position(|arg| arg == "--") {
42        Some(pos) => (&args[..pos], &args[pos + 1..]),
43        None => (&args[..], [].as_slice()),
44    };
45    let verbose = cfg_args.iter().any(|s| *s == "--verbose");
46    let bless = cfg_args.iter().any(|s| *s == "--bless");
47    let extra_checks =
48        cfg_args.iter().find(|s| s.starts_with("--extra-checks=")).map(String::as_str);
49
50    let bad = std::sync::Arc::new(AtomicBool::new(false));
51
52    let drain_handles = |handles: &mut VecDeque<ScopedJoinHandle<'_, ()>>| {
53        // poll all threads for completion before awaiting the oldest one
54        for i in (0..handles.len()).rev() {
55            if handles[i].is_finished() {
56                handles.swap_remove_back(i).unwrap().join().unwrap();
57            }
58        }
59
60        while handles.len() >= concurrency.get() {
61            handles.pop_front().unwrap().join().unwrap();
62        }
63    };
64
65    scope(|s| {
66        let mut handles: VecDeque<ScopedJoinHandle<'_, ()>> =
67            VecDeque::with_capacity(concurrency.get());
68
69        macro_rules! check {
70            ($p:ident) => {
71                check!(@ $p, name=format!("{}", stringify!($p)));
72            };
73            ($p:ident, $path:expr $(, $args:expr)* ) => {
74                let shortened = $path.strip_prefix(&root_path).unwrap();
75                let name = if shortened == std::path::Path::new("") {
76                    format!("{} (.)", stringify!($p))
77                } else {
78                    format!("{} ({})", stringify!($p), shortened.display())
79                };
80                check!(@ $p, name=name, $path $(,$args)*);
81            };
82            (@ $p:ident, name=$name:expr $(, $args:expr)* ) => {
83                drain_handles(&mut handles);
84
85                let handle = thread::Builder::new().name($name).spawn_scoped(s, || {
86                    let mut flag = false;
87                    $p::check($($args, )* &mut flag);
88                    if (flag) {
89                        bad.store(true, Ordering::Relaxed);
90                    }
91                }).unwrap();
92                handles.push_back(handle);
93            }
94        }
95
96        check!(target_specific_tests, &tests_path);
97
98        // Checks that are done on the cargo workspace.
99        check!(deps, &root_path, &cargo, bless);
100        check!(extdeps, &root_path);
101
102        // Checks over tests.
103        check!(tests_placement, &root_path);
104        check!(tests_revision_unpaired_stdout_stderr, &tests_path);
105        check!(debug_artifacts, &tests_path);
106        check!(ui_tests, &root_path, bless);
107        check!(mir_opt_tests, &tests_path, bless);
108        check!(rustdoc_gui_tests, &tests_path);
109        check!(rustdoc_css_themes, &librustdoc_path);
110        check!(rustdoc_templates, &librustdoc_path);
111        check!(known_bug, &crashes_path);
112        check!(unknown_revision, &tests_path);
113
114        // Checks that only make sense for the compiler.
115        check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], verbose);
116        check!(fluent_alphabetical, &compiler_path, bless);
117        check!(fluent_period, &compiler_path);
118        check!(target_policy, &root_path);
119
120        // Checks that only make sense for the std libs.
121        check!(pal, &library_path);
122
123        // Checks that need to be done for both the compiler and std libraries.
124        check!(unit_tests, &src_path);
125        check!(unit_tests, &compiler_path);
126        check!(unit_tests, &library_path);
127
128        if bins::check_filesystem_support(&[&root_path], &output_directory) {
129            check!(bins, &root_path);
130        }
131
132        check!(style, &src_path);
133        check!(style, &tests_path);
134        check!(style, &compiler_path);
135        check!(style, &library_path);
136
137        check!(edition, &src_path);
138        check!(edition, &compiler_path);
139        check!(edition, &library_path);
140
141        check!(alphabetical, &root_manifest);
142        check!(alphabetical, &src_path);
143        check!(alphabetical, &tests_path);
144        check!(alphabetical, &compiler_path);
145        check!(alphabetical, &library_path);
146
147        check!(x_version, &root_path, &cargo);
148
149        check!(triagebot, &root_path);
150
151        let collected = {
152            drain_handles(&mut handles);
153
154            let mut flag = false;
155            let r = features::check(
156                &src_path,
157                &tests_path,
158                &compiler_path,
159                &library_path,
160                &mut flag,
161                verbose,
162            );
163            if flag {
164                bad.store(true, Ordering::Relaxed);
165            }
166            r
167        };
168        check!(unstable_book, &src_path, collected);
169
170        check!(ext_tool_checks, &root_path, &output_directory, bless, extra_checks, pos_args);
171    });
172
173    if bad.load(Ordering::Relaxed) {
174        eprintln!("some tidy checks failed");
175        process::exit(1);
176    }
177}