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