]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/deps.rs
Auto merge of #62452 - Centril:rollup-5jww3h7, r=Centril
[rust.git] / src / tools / tidy / src / deps.rs
1 //! Checks the licenses of third-party dependencies by inspecting vendors.
2
3 use std::collections::{BTreeSet, HashSet, HashMap};
4 use std::fs;
5 use std::path::Path;
6 use std::process::Command;
7
8 use serde::Deserialize;
9 use serde_json;
10
11 const LICENSES: &[&str] = &[
12     "MIT/Apache-2.0",
13     "MIT / Apache-2.0",
14     "Apache-2.0/MIT",
15     "Apache-2.0 / MIT",
16     "MIT OR Apache-2.0",
17     "Apache-2.0 OR MIT",
18     "MIT",
19     "Unlicense/MIT",
20     "Unlicense OR MIT",
21 ];
22
23 /// These are exceptions to Rust's permissive licensing policy, and
24 /// should be considered bugs. Exceptions are only allowed in Rust
25 /// tooling. It is _crucial_ that no exception crates be dependencies
26 /// of the Rust runtime (std/test).
27 const EXCEPTIONS: &[&str] = &[
28     "mdbook",             // MPL2, mdbook
29     "openssl",            // BSD+advertising clause, cargo, mdbook
30     "pest",               // MPL2, mdbook via handlebars
31     "arrayref",           // BSD-2-Clause, mdbook via handlebars via pest
32     "thread-id",          // Apache-2.0, mdbook
33     "toml-query",         // MPL-2.0, mdbook
34     "is-match",           // MPL-2.0, mdbook
35     "cssparser",          // MPL-2.0, rustdoc
36     "smallvec",           // MPL-2.0, rustdoc
37     "rdrand",             // ISC, mdbook, rustfmt
38     "fuchsia-cprng",      // BSD-3-Clause, mdbook, rustfmt
39     "fuchsia-zircon-sys", // BSD-3-Clause, rustdoc, rustc, cargo
40     "fuchsia-zircon",     // BSD-3-Clause, rustdoc, rustc, cargo (jobserver & tempdir)
41     "cssparser-macros",   // MPL-2.0, rustdoc
42     "selectors",          // MPL-2.0, rustdoc
43     "clippy_lints",       // MPL-2.0, rls
44     "colored",            // MPL-2.0, rustfmt
45     "ordslice",           // Apache-2.0, rls
46     "cloudabi",           // BSD-2-Clause, (rls -> crossbeam-channel 0.2 -> rand 0.5)
47     "ryu",                // Apache-2.0, rls/cargo/... (because of serde)
48     "bytesize",           // Apache-2.0, cargo
49     "im-rc",              // MPL-2.0+, cargo
50     "adler32",            // BSD-3-Clause AND Zlib, cargo dep that isn't used
51     "fortanix-sgx-abi",   // MPL-2.0+, libstd but only for `sgx` target
52     "constant_time_eq",   // CC0-1.0, rustfmt
53     "utf8parse",          // Apache-2.0 OR MIT, cargo via strip-ansi-escapes
54     "vte",                // Apache-2.0 OR MIT, cargo via strip-ansi-escapes
55     "sized-chunks",       // MPL-2.0+, cargo via im-rc
56 ];
57
58 /// Which crates to check against the whitelist?
59 const WHITELIST_CRATES: &[CrateVersion<'_>] = &[
60     CrateVersion("rustc", "0.0.0"),
61     CrateVersion("rustc_codegen_llvm", "0.0.0"),
62 ];
63
64 /// Whitelist of crates rustc is allowed to depend on. Avoid adding to the list if possible.
65 const WHITELIST: &[Crate<'_>] = &[
66     Crate("adler32"),
67     Crate("aho-corasick"),
68     Crate("annotate-snippets"),
69     Crate("ansi_term"),
70     Crate("arrayvec"),
71     Crate("atty"),
72     Crate("autocfg"),
73     Crate("backtrace"),
74     Crate("backtrace-sys"),
75     Crate("bitflags"),
76     Crate("build_const"),
77     Crate("byteorder"),
78     Crate("cc"),
79     Crate("cfg-if"),
80     Crate("chalk-engine"),
81     Crate("chalk-macros"),
82     Crate("cloudabi"),
83     Crate("cmake"),
84     Crate("compiler_builtins"),
85     Crate("crc"),
86     Crate("crc32fast"),
87     Crate("crossbeam-deque"),
88     Crate("crossbeam-epoch"),
89     Crate("crossbeam-utils"),
90     Crate("datafrog"),
91     Crate("either"),
92     Crate("ena"),
93     Crate("env_logger"),
94     Crate("filetime"),
95     Crate("flate2"),
96     Crate("fuchsia-zircon"),
97     Crate("fuchsia-zircon-sys"),
98     Crate("getopts"),
99     Crate("humantime"),
100     Crate("indexmap"),
101     Crate("itertools"),
102     Crate("jobserver"),
103     Crate("kernel32-sys"),
104     Crate("lazy_static"),
105     Crate("libc"),
106     Crate("libz-sys"),
107     Crate("lock_api"),
108     Crate("log"),
109     Crate("log_settings"),
110     Crate("measureme"),
111     Crate("memchr"),
112     Crate("memmap"),
113     Crate("memoffset"),
114     Crate("miniz-sys"),
115     Crate("miniz_oxide"),
116     Crate("miniz_oxide_c_api"),
117     Crate("nodrop"),
118     Crate("num_cpus"),
119     Crate("owning_ref"),
120     Crate("parking_lot"),
121     Crate("parking_lot_core"),
122     Crate("pkg-config"),
123     Crate("polonius-engine"),
124     Crate("proc-macro2"),
125     Crate("quick-error"),
126     Crate("quote"),
127     Crate("rand"),
128     Crate("rand_chacha"),
129     Crate("rand_core"),
130     Crate("rand_hc"),
131     Crate("rand_isaac"),
132     Crate("rand_pcg"),
133     Crate("rand_xorshift"),
134     Crate("redox_syscall"),
135     Crate("redox_termios"),
136     Crate("regex"),
137     Crate("regex-syntax"),
138     Crate("remove_dir_all"),
139     Crate("rustc-demangle"),
140     Crate("rustc-hash"),
141     Crate("rustc-rayon"),
142     Crate("rustc-rayon-core"),
143     Crate("rustc_version"),
144     Crate("scoped-tls"),
145     Crate("scopeguard"),
146     Crate("semver"),
147     Crate("semver-parser"),
148     Crate("serde"),
149     Crate("serde_derive"),
150     Crate("smallvec"),
151     Crate("stable_deref_trait"),
152     Crate("syn"),
153     Crate("synstructure"),
154     Crate("tempfile"),
155     Crate("termcolor"),
156     Crate("terminon"),
157     Crate("termion"),
158     Crate("thread_local"),
159     Crate("ucd-util"),
160     Crate("unicode-width"),
161     Crate("unicode-xid"),
162     Crate("unreachable"),
163     Crate("utf8-ranges"),
164     Crate("vcpkg"),
165     Crate("version_check"),
166     Crate("void"),
167     Crate("winapi"),
168     Crate("winapi-build"),
169     Crate("winapi-i686-pc-windows-gnu"),
170     Crate("winapi-util"),
171     Crate("winapi-x86_64-pc-windows-gnu"),
172     Crate("wincolor"),
173 ];
174
175 // Some types for Serde to deserialize the output of `cargo metadata` to.
176
177 #[derive(Deserialize)]
178 struct Output {
179     resolve: Resolve,
180 }
181
182 #[derive(Deserialize)]
183 struct Resolve {
184     nodes: Vec<ResolveNode>,
185 }
186
187 #[derive(Deserialize)]
188 struct ResolveNode {
189     id: String,
190     dependencies: Vec<String>,
191 }
192
193 /// A unique identifier for a crate.
194 #[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Hash)]
195 struct Crate<'a>(&'a str); // (name)
196
197 #[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Hash)]
198 struct CrateVersion<'a>(&'a str, &'a str); // (name, version)
199
200 impl Crate<'_> {
201     pub fn id_str(&self) -> String {
202         format!("{} ", self.0)
203     }
204 }
205
206 impl<'a> CrateVersion<'a> {
207     /// Returns the struct and whether or not the dependency is in-tree.
208     pub fn from_str(s: &'a str) -> (Self, bool) {
209         let mut parts = s.split(' ');
210         let name = parts.next().unwrap();
211         let version = parts.next().unwrap();
212         let path = parts.next().unwrap();
213
214         let is_path_dep = path.starts_with("(path+");
215
216         (CrateVersion(name, version), is_path_dep)
217     }
218
219     pub fn id_str(&self) -> String {
220         format!("{} {}", self.0, self.1)
221     }
222 }
223
224 impl<'a> From<CrateVersion<'a>> for Crate<'a> {
225     fn from(cv: CrateVersion<'a>) -> Crate<'a> {
226         Crate(cv.0)
227     }
228 }
229
230 /// Checks the dependency at the given path. Changes `bad` to `true` if a check failed.
231 ///
232 /// Specifically, this checks that the license is correct.
233 pub fn check(path: &Path, bad: &mut bool) {
234     // Check licences.
235     let path = path.join("../vendor");
236     assert!(path.exists(), "vendor directory missing");
237     let mut saw_dir = false;
238     for dir in t!(path.read_dir()) {
239         saw_dir = true;
240         let dir = t!(dir);
241
242         // Skip our exceptions.
243         let is_exception = EXCEPTIONS.iter().any(|exception| {
244             dir.path()
245                 .to_str()
246                 .unwrap()
247                 .contains(&format!("vendor/{}", exception))
248         });
249         if is_exception {
250             continue;
251         }
252
253         let toml = dir.path().join("Cargo.toml");
254         *bad = !check_license(&toml) || *bad;
255     }
256     assert!(saw_dir, "no vendored source");
257 }
258
259 /// Checks the dependency of `WHITELIST_CRATES` at the given path. Changes `bad` to `true` if a
260 /// check failed.
261 ///
262 /// Specifically, this checks that the dependencies are on the `WHITELIST`.
263 pub fn check_whitelist(path: &Path, cargo: &Path, bad: &mut bool) {
264     // Get dependencies from Cargo metadata.
265     let resolve = get_deps(path, cargo);
266
267     // Get the whitelist in a convenient form.
268     let whitelist: HashSet<_> = WHITELIST.iter().cloned().collect();
269
270     // Check dependencies.
271     let mut visited = BTreeSet::new();
272     let mut unapproved = BTreeSet::new();
273     for &krate in WHITELIST_CRATES.iter() {
274         let mut bad = check_crate_whitelist(&whitelist, &resolve, &mut visited, krate, false);
275         unapproved.append(&mut bad);
276     }
277
278     if !unapproved.is_empty() {
279         println!("Dependencies not on the whitelist:");
280         for dep in unapproved {
281             println!("* {}", dep.id_str());
282         }
283         *bad = true;
284     }
285
286     check_crate_duplicate(&resolve, bad);
287 }
288
289 fn check_license(path: &Path) -> bool {
290     if !path.exists() {
291         panic!("{} does not exist", path.display());
292     }
293     let contents = t!(fs::read_to_string(&path));
294
295     let mut found_license = false;
296     for line in contents.lines() {
297         if !line.starts_with("license") {
298             continue;
299         }
300         let license = extract_license(line);
301         if !LICENSES.contains(&&*license) {
302             println!("invalid license {} in {}", license, path.display());
303             return false;
304         }
305         found_license = true;
306         break;
307     }
308     if !found_license {
309         println!("no license in {}", path.display());
310         return false;
311     }
312
313     true
314 }
315
316 fn extract_license(line: &str) -> String {
317     let first_quote = line.find('"');
318     let last_quote = line.rfind('"');
319     if let (Some(f), Some(l)) = (first_quote, last_quote) {
320         let license = &line[f + 1..l];
321         license.into()
322     } else {
323         "bad-license-parse".into()
324     }
325 }
326
327 /// Gets the dependencies of the crate at the given path using `cargo metadata`.
328 fn get_deps(path: &Path, cargo: &Path) -> Resolve {
329     // Run `cargo metadata` to get the set of dependencies.
330     let output = Command::new(cargo)
331         .arg("metadata")
332         .arg("--format-version")
333         .arg("1")
334         .arg("--manifest-path")
335         .arg(path.join("../Cargo.toml"))
336         .output()
337         .expect("Unable to run `cargo metadata`")
338         .stdout;
339     let output = String::from_utf8_lossy(&output);
340     let output: Output = serde_json::from_str(&output).unwrap();
341
342     output.resolve
343 }
344
345 /// Checks the dependencies of the given crate from the given cargo metadata to see if they are on
346 /// the whitelist. Returns a list of illegal dependencies.
347 fn check_crate_whitelist<'a>(
348     whitelist: &'a HashSet<Crate<'_>>,
349     resolve: &'a Resolve,
350     visited: &mut BTreeSet<CrateVersion<'a>>,
351     krate: CrateVersion<'a>,
352     must_be_on_whitelist: bool,
353 ) -> BTreeSet<Crate<'a>> {
354     // This will contain bad deps.
355     let mut unapproved = BTreeSet::new();
356
357     // Check if we have already visited this crate.
358     if visited.contains(&krate) {
359         return unapproved;
360     }
361
362     visited.insert(krate);
363
364     // If this path is in-tree, we don't require it to be on the whitelist.
365     if must_be_on_whitelist {
366         // If this dependency is not on `WHITELIST`, add to bad set.
367         if !whitelist.contains(&krate.into()) {
368             unapproved.insert(krate.into());
369         }
370     }
371
372     // Do a DFS in the crate graph (it's a DAG, so we know we have no cycles!).
373     let to_check = resolve
374         .nodes
375         .iter()
376         .find(|n| n.id.starts_with(&krate.id_str()))
377         .expect("crate does not exist");
378
379     for dep in to_check.dependencies.iter() {
380         let (krate, is_path_dep) = CrateVersion::from_str(dep);
381
382         let mut bad = check_crate_whitelist(whitelist, resolve, visited, krate, !is_path_dep);
383         unapproved.append(&mut bad);
384     }
385
386     unapproved
387 }
388
389 fn check_crate_duplicate(resolve: &Resolve, bad: &mut bool) {
390     const FORBIDDEN_TO_HAVE_DUPLICATES: &[&str] = &[
391         // These two crates take quite a long time to build, so don't allow two versions of them
392         // to accidentally sneak into our dependency graph, in order to ensure we keep our CI times
393         // under control.
394
395         "cargo",
396         "rustc-ap-syntax",
397     ];
398     let mut name_to_id: HashMap<_, Vec<_>> = HashMap::new();
399     for node in resolve.nodes.iter() {
400         name_to_id.entry(node.id.split_whitespace().next().unwrap())
401             .or_default()
402             .push(&node.id);
403     }
404
405     for name in FORBIDDEN_TO_HAVE_DUPLICATES {
406         if name_to_id[name].len() <= 1 {
407             continue
408         }
409         println!("crate `{}` is duplicated in `Cargo.lock`", name);
410         for id in name_to_id[name].iter() {
411             println!("  * {}", id);
412         }
413         *bad = true;
414     }
415 }