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 tools_path = src_path.join("tools");
39    let crashes_path = tests_path.join("crashes");
40
41    let args: Vec<String> = env::args().skip(1).collect();
42    let (cfg_args, pos_args) = match args.iter().position(|arg| arg == "--") {
43        Some(pos) => (&args[..pos], &args[pos + 1..]),
44        None => (&args[..], [].as_slice()),
45    };
46    let verbose = cfg_args.iter().any(|s| *s == "--verbose");
47    let bless = cfg_args.iter().any(|s| *s == "--bless");
48    let extra_checks =
49        cfg_args.iter().find(|s| s.starts_with("--extra-checks=")).map(String::as_str);
50
51    let bad = std::sync::Arc::new(AtomicBool::new(false));
52
53    let drain_handles = |handles: &mut VecDeque<ScopedJoinHandle<'_, ()>>| {
54        // poll all threads for completion before awaiting the oldest one
55        for i in (0..handles.len()).rev() {
56            if handles[i].is_finished() {
57                handles.swap_remove_back(i).unwrap().join().unwrap();
58            }
59        }
60
61        while handles.len() >= concurrency.get() {
62            handles.pop_front().unwrap().join().unwrap();
63        }
64    };
65
66    scope(|s| {
67        let mut handles: VecDeque<ScopedJoinHandle<'_, ()>> =
68            VecDeque::with_capacity(concurrency.get());
69
70        macro_rules! check {
71            ($p:ident) => {
72                check!(@ $p, name=format!("{}", stringify!($p)));
73            };
74            ($p:ident, $path:expr $(, $args:expr)* ) => {
75                let shortened = $path.strip_prefix(&root_path).unwrap();
76                let name = if shortened == std::path::Path::new("") {
77                    format!("{} (.)", stringify!($p))
78                } else {
79                    format!("{} ({})", stringify!($p), shortened.display())
80                };
81                check!(@ $p, name=name, $path $(,$args)*);
82            };
83            (@ $p:ident, name=$name:expr $(, $args:expr)* ) => {
84                drain_handles(&mut handles);
85
86                let handle = thread::Builder::new().name($name).spawn_scoped(s, || {
87                    let mut flag = false;
88                    $p::check($($args, )* &mut flag);
89                    if (flag) {
90                        bad.store(true, Ordering::Relaxed);
91                    }
92                }).unwrap();
93                handles.push_back(handle);
94            }
95        }
96
97        check!(target_specific_tests, &tests_path);
98
99        // Checks that are done on the cargo workspace.
100        check!(deps, &root_path, &cargo, bless);
101        check!(extdeps, &root_path);
102
103        // Checks over tests.
104        check!(tests_placement, &root_path);
105        check!(tests_revision_unpaired_stdout_stderr, &tests_path);
106        check!(debug_artifacts, &tests_path);
107        check!(ui_tests, &root_path, bless);
108        check!(mir_opt_tests, &tests_path, bless);
109        check!(rustdoc_gui_tests, &tests_path);
110        check!(rustdoc_css_themes, &librustdoc_path);
111        check!(rustdoc_templates, &librustdoc_path);
112        check!(rustdoc_js, &librustdoc_path, &tools_path, &src_path);
113        check!(known_bug, &crashes_path);
114        check!(unknown_revision, &tests_path);
115
116        // Checks that only make sense for the compiler.
117        check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], verbose);
118        check!(fluent_alphabetical, &compiler_path, bless);
119        check!(fluent_period, &compiler_path);
120        check!(target_policy, &root_path);
121        check!(gcc_submodule, &root_path, &compiler_path);
122
123        // Checks that only make sense for the std libs.
124        check!(pal, &library_path);
125
126        // Checks that need to be done for both the compiler and std libraries.
127        check!(unit_tests, &src_path);
128        check!(unit_tests, &compiler_path);
129        check!(unit_tests, &library_path);
130
131        if bins::check_filesystem_support(&[&root_path], &output_directory) {
132            check!(bins, &root_path);
133        }
134
135        check!(style, &src_path);
136        check!(style, &tests_path);
137        check!(style, &compiler_path);
138        check!(style, &library_path);
139
140        check!(edition, &src_path);
141        check!(edition, &compiler_path);
142        check!(edition, &library_path);
143
144        check!(alphabetical, &root_manifest);
145        check!(alphabetical, &src_path);
146        check!(alphabetical, &tests_path);
147        check!(alphabetical, &compiler_path);
148        check!(alphabetical, &library_path);
149
150        check!(x_version, &root_path, &cargo);
151
152        check!(triagebot, &root_path);
153
154        let collected = {
155            drain_handles(&mut handles);
156
157            let mut flag = false;
158            let r = features::check(
159                &src_path,
160                &tests_path,
161                &compiler_path,
162                &library_path,
163                &mut flag,
164                verbose,
165            );
166            if flag {
167                bad.store(true, Ordering::Relaxed);
168            }
169            r
170        };
171        check!(unstable_book, &src_path, collected);
172
173        check!(ext_tool_checks, &root_path, &output_directory, bless, extra_checks, pos_args);
174    });
175
176    if bad.load(Ordering::Relaxed) {
177        eprintln!("some tidy checks failed");
178        process::exit(1);
179    }
180}