tidy/
deps.rs

1//! Checks the licenses of third-party dependencies.
2
3use std::collections::{HashMap, HashSet};
4use std::fs::{File, read_dir};
5use std::io::Write;
6use std::path::Path;
7
8use build_helper::ci::CiEnv;
9use cargo_metadata::semver::Version;
10use cargo_metadata::{Metadata, Package, PackageId};
11
12#[path = "../../../bootstrap/src/utils/proc_macro_deps.rs"]
13mod proc_macro_deps;
14
15/// These are licenses that are allowed for all crates, including the runtime,
16/// rustc, tools, etc.
17#[rustfmt::skip]
18const LICENSES: &[&str] = &[
19    // tidy-alphabetical-start
20    "(MIT OR Apache-2.0) AND Unicode-3.0",                 // unicode_ident (1.0.14)
21    "(MIT OR Apache-2.0) AND Unicode-DFS-2016",            // unicode_ident (1.0.12)
22    "0BSD OR MIT OR Apache-2.0",                           // adler2 license
23    "0BSD",
24    "Apache-2.0 / MIT",
25    "Apache-2.0 OR ISC OR MIT",
26    "Apache-2.0 OR MIT",
27    "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT", // wasi license
28    "Apache-2.0",
29    "Apache-2.0/MIT",
30    "BSD-2-Clause OR Apache-2.0 OR MIT",                   // zerocopy
31    "ISC",
32    "MIT / Apache-2.0",
33    "MIT AND (MIT OR Apache-2.0)",
34    "MIT AND Apache-2.0 WITH LLVM-exception AND (MIT OR Apache-2.0)", // compiler-builtins
35    "MIT OR Apache-2.0 OR LGPL-2.1-or-later",              // r-efi, r-efi-alloc
36    "MIT OR Apache-2.0 OR Zlib",                           // tinyvec_macros
37    "MIT OR Apache-2.0",
38    "MIT OR Zlib OR Apache-2.0",                           // miniz_oxide
39    "MIT",
40    "MIT/Apache-2.0",
41    "Unicode-3.0",                                         // icu4x
42    "Unicode-DFS-2016",                                    // tinystr
43    "Unlicense OR MIT",
44    "Unlicense/MIT",
45    "Zlib OR Apache-2.0 OR MIT",                           // tinyvec
46    // tidy-alphabetical-end
47];
48
49type ExceptionList = &'static [(&'static str, &'static str)];
50
51/// The workspaces to check for licensing and optionally permitted dependencies.
52///
53/// Each entry consists of a tuple with the following elements:
54///
55/// * The path to the workspace root Cargo.toml file.
56/// * The list of license exceptions.
57/// * Optionally a tuple of:
58///     * A list of crates for which dependencies need to be explicitly allowed.
59///     * The list of allowed dependencies.
60/// * Submodules required for the workspace.
61// FIXME auto detect all cargo workspaces
62pub(crate) const WORKSPACES: &[(&str, ExceptionList, Option<(&[&str], &[&str])>, &[&str])] = &[
63    // The root workspace has to be first for check_rustfix to work.
64    (".", EXCEPTIONS, Some((&["rustc-main"], PERMITTED_RUSTC_DEPENDENCIES)), &[]),
65    ("library", EXCEPTIONS_STDLIB, Some((&["sysroot"], PERMITTED_STDLIB_DEPENDENCIES)), &[]),
66    // Outside of the alphabetical section because rustfmt formats it using multiple lines.
67    (
68        "compiler/rustc_codegen_cranelift",
69        EXCEPTIONS_CRANELIFT,
70        Some((&["rustc_codegen_cranelift"], PERMITTED_CRANELIFT_DEPENDENCIES)),
71        &[],
72    ),
73    // tidy-alphabetical-start
74    ("compiler/rustc_codegen_gcc", EXCEPTIONS_GCC, None, &[]),
75    ("src/bootstrap", EXCEPTIONS_BOOTSTRAP, None, &[]),
76    ("src/tools/cargo", EXCEPTIONS_CARGO, None, &["src/tools/cargo"]),
77    //("src/tools/miri/test-cargo-miri", &[], None), // FIXME uncomment once all deps are vendored
78    //("src/tools/miri/test_dependencies", &[], None), // FIXME uncomment once all deps are vendored
79    ("src/tools/rust-analyzer", EXCEPTIONS_RUST_ANALYZER, None, &[]),
80    ("src/tools/rustbook", EXCEPTIONS_RUSTBOOK, None, &["src/doc/book", "src/doc/reference"]),
81    ("src/tools/rustc-perf", EXCEPTIONS_RUSTC_PERF, None, &["src/tools/rustc-perf"]),
82    ("src/tools/test-float-parse", EXCEPTIONS, None, &[]),
83    ("tests/run-make-cargo/uefi-qemu/uefi_qemu_test", EXCEPTIONS_UEFI_QEMU_TEST, None, &[]),
84    // tidy-alphabetical-end
85];
86
87/// These are exceptions to Rust's permissive licensing policy, and
88/// should be considered bugs. Exceptions are only allowed in Rust
89/// tooling. It is _crucial_ that no exception crates be dependencies
90/// of the Rust runtime (std/test).
91#[rustfmt::skip]
92const EXCEPTIONS: ExceptionList = &[
93    // tidy-alphabetical-start
94    ("ar_archive_writer", "Apache-2.0 WITH LLVM-exception"), // rustc
95    ("arrayref", "BSD-2-Clause"),                            // rustc
96    ("blake3", "CC0-1.0 OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception"),  // rustc
97    ("colored", "MPL-2.0"),                                  // rustfmt
98    ("constant_time_eq", "CC0-1.0 OR MIT-0 OR Apache-2.0"),  // rustc
99    ("dissimilar", "Apache-2.0"),                            // rustdoc, rustc_lexer (few tests) via expect-test, (dev deps)
100    ("fluent-langneg", "Apache-2.0"),                        // rustc (fluent translations)
101    ("foldhash", "Zlib"),                                    // rustc
102    ("option-ext", "MPL-2.0"),                               // cargo-miri (via `directories`)
103    ("rustc_apfloat", "Apache-2.0 WITH LLVM-exception"),     // rustc (license is the same as LLVM uses)
104    ("ryu", "Apache-2.0 OR BSL-1.0"), // BSL is not acceptble, but we use it under Apache-2.0                       // cargo/... (because of serde)
105    ("self_cell", "Apache-2.0"),                             // rustc (fluent translations)
106    ("wasi-preview1-component-adapter-provider", "Apache-2.0 WITH LLVM-exception"), // rustc
107    // tidy-alphabetical-end
108];
109
110/// These are exceptions to Rust's permissive licensing policy, and
111/// should be considered bugs. Exceptions are only allowed in Rust
112/// tooling. It is _crucial_ that no exception crates be dependencies
113/// of the Rust runtime (std/test).
114#[rustfmt::skip]
115const EXCEPTIONS_STDLIB: ExceptionList = &[
116    // tidy-alphabetical-start
117    ("fortanix-sgx-abi", "MPL-2.0"), // libstd but only for `sgx` target. FIXME: this dependency violates the documentation comment above.
118    // tidy-alphabetical-end
119];
120
121const EXCEPTIONS_CARGO: ExceptionList = &[
122    // tidy-alphabetical-start
123    ("arrayref", "BSD-2-Clause"),
124    ("bitmaps", "MPL-2.0+"),
125    ("blake3", "CC0-1.0 OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception"),
126    ("ciborium", "Apache-2.0"),
127    ("ciborium-io", "Apache-2.0"),
128    ("ciborium-ll", "Apache-2.0"),
129    ("constant_time_eq", "CC0-1.0 OR MIT-0 OR Apache-2.0"),
130    ("dunce", "CC0-1.0 OR MIT-0 OR Apache-2.0"),
131    ("encoding_rs", "(Apache-2.0 OR MIT) AND BSD-3-Clause"),
132    ("fiat-crypto", "MIT OR Apache-2.0 OR BSD-1-Clause"),
133    ("foldhash", "Zlib"),
134    ("im-rc", "MPL-2.0+"),
135    ("libz-rs-sys", "Zlib"),
136    ("normalize-line-endings", "Apache-2.0"),
137    ("openssl", "Apache-2.0"),
138    ("ring", "Apache-2.0 AND ISC"),
139    ("ryu", "Apache-2.0 OR BSL-1.0"), // BSL is not acceptble, but we use it under Apache-2.0
140    ("similar", "Apache-2.0"),
141    ("sized-chunks", "MPL-2.0+"),
142    ("subtle", "BSD-3-Clause"),
143    ("supports-hyperlinks", "Apache-2.0"),
144    ("unicode-bom", "Apache-2.0"),
145    ("zlib-rs", "Zlib"),
146    // tidy-alphabetical-end
147];
148
149const EXCEPTIONS_RUST_ANALYZER: ExceptionList = &[
150    // tidy-alphabetical-start
151    ("dissimilar", "Apache-2.0"),
152    ("foldhash", "Zlib"),
153    ("notify", "CC0-1.0"),
154    ("option-ext", "MPL-2.0"),
155    ("pulldown-cmark-to-cmark", "Apache-2.0"),
156    ("rustc_apfloat", "Apache-2.0 WITH LLVM-exception"),
157    ("ryu", "Apache-2.0 OR BSL-1.0"), // BSL is not acceptble, but we use it under Apache-2.0
158    ("scip", "Apache-2.0"),
159    // tidy-alphabetical-end
160];
161
162const EXCEPTIONS_RUSTC_PERF: ExceptionList = &[
163    // tidy-alphabetical-start
164    ("alloc-no-stdlib", "BSD-3-Clause"),
165    ("alloc-stdlib", "BSD-3-Clause"),
166    ("brotli", "BSD-3-Clause/MIT"),
167    ("brotli-decompressor", "BSD-3-Clause/MIT"),
168    ("encoding_rs", "(Apache-2.0 OR MIT) AND BSD-3-Clause"),
169    ("inferno", "CDDL-1.0"),
170    ("option-ext", "MPL-2.0"),
171    ("ryu", "Apache-2.0 OR BSL-1.0"),
172    ("snap", "BSD-3-Clause"),
173    ("subtle", "BSD-3-Clause"),
174    // tidy-alphabetical-end
175];
176
177const EXCEPTIONS_RUSTBOOK: ExceptionList = &[
178    // tidy-alphabetical-start
179    ("cssparser", "MPL-2.0"),
180    ("cssparser-macros", "MPL-2.0"),
181    ("dtoa-short", "MPL-2.0"),
182    ("mdbook", "MPL-2.0"),
183    ("ryu", "Apache-2.0 OR BSL-1.0"),
184    // tidy-alphabetical-end
185];
186
187const EXCEPTIONS_CRANELIFT: ExceptionList = &[
188    // tidy-alphabetical-start
189    ("cranelift-assembler-x64", "Apache-2.0 WITH LLVM-exception"),
190    ("cranelift-assembler-x64-meta", "Apache-2.0 WITH LLVM-exception"),
191    ("cranelift-bforest", "Apache-2.0 WITH LLVM-exception"),
192    ("cranelift-bitset", "Apache-2.0 WITH LLVM-exception"),
193    ("cranelift-codegen", "Apache-2.0 WITH LLVM-exception"),
194    ("cranelift-codegen-meta", "Apache-2.0 WITH LLVM-exception"),
195    ("cranelift-codegen-shared", "Apache-2.0 WITH LLVM-exception"),
196    ("cranelift-control", "Apache-2.0 WITH LLVM-exception"),
197    ("cranelift-entity", "Apache-2.0 WITH LLVM-exception"),
198    ("cranelift-frontend", "Apache-2.0 WITH LLVM-exception"),
199    ("cranelift-isle", "Apache-2.0 WITH LLVM-exception"),
200    ("cranelift-jit", "Apache-2.0 WITH LLVM-exception"),
201    ("cranelift-module", "Apache-2.0 WITH LLVM-exception"),
202    ("cranelift-native", "Apache-2.0 WITH LLVM-exception"),
203    ("cranelift-object", "Apache-2.0 WITH LLVM-exception"),
204    ("cranelift-srcgen", "Apache-2.0 WITH LLVM-exception"),
205    ("foldhash", "Zlib"),
206    ("mach2", "BSD-2-Clause OR MIT OR Apache-2.0"),
207    ("regalloc2", "Apache-2.0 WITH LLVM-exception"),
208    ("target-lexicon", "Apache-2.0 WITH LLVM-exception"),
209    ("wasmtime-jit-icache-coherence", "Apache-2.0 WITH LLVM-exception"),
210    ("wasmtime-math", "Apache-2.0 WITH LLVM-exception"),
211    // tidy-alphabetical-end
212];
213
214const EXCEPTIONS_GCC: ExceptionList = &[
215    // tidy-alphabetical-start
216    ("gccjit", "GPL-3.0"),
217    ("gccjit_sys", "GPL-3.0"),
218    // tidy-alphabetical-end
219];
220
221const EXCEPTIONS_BOOTSTRAP: ExceptionList = &[
222    ("ryu", "Apache-2.0 OR BSL-1.0"), // through serde. BSL is not acceptble, but we use it under Apache-2.0
223];
224
225const EXCEPTIONS_UEFI_QEMU_TEST: ExceptionList = &[
226    ("r-efi", "MIT OR Apache-2.0 OR LGPL-2.1-or-later"), // LGPL is not acceptable, but we use it under MIT OR Apache-2.0
227];
228
229const PERMITTED_DEPS_LOCATION: &str = concat!(file!(), ":", line!());
230
231/// Crates rustc is allowed to depend on. Avoid adding to the list if possible.
232///
233/// This list is here to provide a speed-bump to adding a new dependency to
234/// rustc. Please check with the compiler team before adding an entry.
235const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
236    // tidy-alphabetical-start
237    "adler2",
238    "aho-corasick",
239    "allocator-api2", // FIXME: only appears in Cargo.lock due to https://github.com/rust-lang/cargo/issues/10801
240    "annotate-snippets",
241    "anstyle",
242    "ar_archive_writer",
243    "arrayref",
244    "arrayvec",
245    "autocfg",
246    "bitflags",
247    "blake3",
248    "block-buffer",
249    "bstr",
250    "cc",
251    "cfg-if",
252    "cfg_aliases",
253    "constant_time_eq",
254    "cpufeatures",
255    "crc32fast",
256    "crossbeam-deque",
257    "crossbeam-epoch",
258    "crossbeam-utils",
259    "crypto-common",
260    "ctrlc",
261    "darling",
262    "darling_core",
263    "darling_macro",
264    "datafrog",
265    "derive-where",
266    "derive_setters",
267    "digest",
268    "displaydoc",
269    "dissimilar",
270    "either",
271    "elsa",
272    "ena",
273    "equivalent",
274    "errno",
275    "expect-test",
276    "fallible-iterator", // dependency of `thorin`
277    "fastrand",
278    "flate2",
279    "fluent-bundle",
280    "fluent-langneg",
281    "fluent-syntax",
282    "fnv",
283    "foldhash",
284    "generic-array",
285    "getopts",
286    "getrandom",
287    "gimli",
288    "gsgdt",
289    "hashbrown",
290    "icu_collections",
291    "icu_list",
292    "icu_locale",
293    "icu_locale_core",
294    "icu_locale_data",
295    "icu_provider",
296    "ident_case",
297    "indexmap",
298    "intl-memoizer",
299    "intl_pluralrules",
300    "itertools",
301    "itoa",
302    "jiff",
303    "jiff-static",
304    "jobserver",
305    "lazy_static",
306    "leb128",
307    "libc",
308    "libloading",
309    "linux-raw-sys",
310    "litemap",
311    "lock_api",
312    "log",
313    "matchers",
314    "md-5",
315    "measureme",
316    "memchr",
317    "memmap2",
318    "miniz_oxide",
319    "nix",
320    "nu-ansi-term",
321    "object",
322    "odht",
323    "once_cell",
324    "overload",
325    "parking_lot",
326    "parking_lot_core",
327    "pathdiff",
328    "perf-event-open-sys",
329    "pin-project-lite",
330    "polonius-engine",
331    "portable-atomic", // dependency for platforms doesn't support `AtomicU64` in std
332    "portable-atomic-util",
333    "potential_utf",
334    "ppv-lite86",
335    "proc-macro-hack",
336    "proc-macro2",
337    "psm",
338    "pulldown-cmark",
339    "pulldown-cmark-escape",
340    "punycode",
341    "quote",
342    "r-efi",
343    "rand",
344    "rand_chacha",
345    "rand_core",
346    "rand_xorshift", // dependency for doc-tests in rustc_thread_pool
347    "rand_xoshiro",
348    "redox_syscall",
349    "regex",
350    "regex-automata",
351    "regex-syntax",
352    "rustc-demangle",
353    "rustc-hash",
354    "rustc-literal-escaper",
355    "rustc-stable-hash",
356    "rustc_apfloat",
357    "rustix",
358    "ruzstd", // via object in thorin-dwp
359    "ryu",
360    "scoped-tls",
361    "scopeguard",
362    "self_cell",
363    "serde",
364    "serde_derive",
365    "serde_json",
366    "serde_path_to_error",
367    "sha1",
368    "sha2",
369    "sharded-slab",
370    "shlex",
371    "smallvec",
372    "stable_deref_trait",
373    "stacker",
374    "static_assertions",
375    "strsim",
376    "syn",
377    "synstructure",
378    "tempfile",
379    "termcolor",
380    "termize",
381    "thin-vec",
382    "thiserror",
383    "thiserror-impl",
384    "thorin-dwp",
385    "thread_local",
386    "tikv-jemalloc-sys",
387    "tinystr",
388    "tinyvec",
389    "tinyvec_macros",
390    "tracing",
391    "tracing-attributes",
392    "tracing-core",
393    "tracing-log",
394    "tracing-subscriber",
395    "tracing-tree",
396    "twox-hash",
397    "type-map",
398    "typenum",
399    "unic-langid",
400    "unic-langid-impl",
401    "unic-langid-macros",
402    "unic-langid-macros-impl",
403    "unicase",
404    "unicode-ident",
405    "unicode-normalization",
406    "unicode-properties",
407    "unicode-script",
408    "unicode-security",
409    "unicode-width",
410    "unicode-xid",
411    "valuable",
412    "version_check",
413    "wasi",
414    "wasm-encoder",
415    "wasmparser",
416    "winapi",
417    "winapi-i686-pc-windows-gnu",
418    "winapi-util",
419    "winapi-x86_64-pc-windows-gnu",
420    "windows",
421    "windows-collections",
422    "windows-core",
423    "windows-future",
424    "windows-implement",
425    "windows-interface",
426    "windows-link",
427    "windows-numerics",
428    "windows-result",
429    "windows-strings",
430    "windows-sys",
431    "windows-targets",
432    "windows-threading",
433    "windows_aarch64_gnullvm",
434    "windows_aarch64_msvc",
435    "windows_i686_gnu",
436    "windows_i686_gnullvm",
437    "windows_i686_msvc",
438    "windows_x86_64_gnu",
439    "windows_x86_64_gnullvm",
440    "windows_x86_64_msvc",
441    "wit-bindgen-rt@0.39.0", // pinned to a specific version due to using a binary blob: <https://github.com/rust-lang/rust/pull/136395#issuecomment-2692769062>
442    "writeable",
443    "yoke",
444    "yoke-derive",
445    "zerocopy",
446    "zerocopy-derive",
447    "zerofrom",
448    "zerofrom-derive",
449    "zerotrie",
450    "zerovec",
451    "zerovec-derive",
452    // tidy-alphabetical-end
453];
454
455const PERMITTED_STDLIB_DEPENDENCIES: &[&str] = &[
456    // tidy-alphabetical-start
457    "addr2line",
458    "adler2",
459    "cc",
460    "cfg-if",
461    "compiler_builtins",
462    "dlmalloc",
463    "fortanix-sgx-abi",
464    "getopts",
465    "gimli",
466    "hashbrown",
467    "hermit-abi",
468    "libc",
469    "memchr",
470    "miniz_oxide",
471    "object",
472    "r-efi",
473    "r-efi-alloc",
474    "rand",
475    "rand_core",
476    "rand_xorshift",
477    "rustc-demangle",
478    "rustc-literal-escaper",
479    "shlex",
480    "unwinding",
481    "wasi",
482    "windows-sys",
483    "windows-targets",
484    "windows_aarch64_gnullvm",
485    "windows_aarch64_msvc",
486    "windows_i686_gnu",
487    "windows_i686_gnullvm",
488    "windows_i686_msvc",
489    "windows_x86_64_gnu",
490    "windows_x86_64_gnullvm",
491    "windows_x86_64_msvc",
492    "wit-bindgen",
493    // tidy-alphabetical-end
494];
495
496const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
497    // tidy-alphabetical-start
498    "allocator-api2",
499    "anyhow",
500    "arbitrary",
501    "bitflags",
502    "bumpalo",
503    "cfg-if",
504    "cranelift-assembler-x64",
505    "cranelift-assembler-x64-meta",
506    "cranelift-bforest",
507    "cranelift-bitset",
508    "cranelift-codegen",
509    "cranelift-codegen-meta",
510    "cranelift-codegen-shared",
511    "cranelift-control",
512    "cranelift-entity",
513    "cranelift-frontend",
514    "cranelift-isle",
515    "cranelift-jit",
516    "cranelift-module",
517    "cranelift-native",
518    "cranelift-object",
519    "cranelift-srcgen",
520    "crc32fast",
521    "equivalent",
522    "fallible-iterator",
523    "foldhash",
524    "gimli",
525    "hashbrown",
526    "indexmap",
527    "libc",
528    "libloading",
529    "libm",
530    "log",
531    "mach2",
532    "memchr",
533    "object",
534    "proc-macro2",
535    "quote",
536    "regalloc2",
537    "region",
538    "rustc-hash",
539    "serde",
540    "serde_derive",
541    "smallvec",
542    "stable_deref_trait",
543    "syn",
544    "target-lexicon",
545    "unicode-ident",
546    "wasmtime-jit-icache-coherence",
547    "wasmtime-math",
548    "windows-sys",
549    "windows-targets",
550    "windows_aarch64_gnullvm",
551    "windows_aarch64_msvc",
552    "windows_i686_gnu",
553    "windows_i686_gnullvm",
554    "windows_i686_msvc",
555    "windows_x86_64_gnu",
556    "windows_x86_64_gnullvm",
557    "windows_x86_64_msvc",
558    // tidy-alphabetical-end
559];
560
561/// Dependency checks.
562///
563/// `root` is path to the directory with the root `Cargo.toml` (for the workspace). `cargo` is path
564/// to the cargo executable.
565pub fn check(root: &Path, cargo: &Path, bless: bool, bad: &mut bool) {
566    let mut checked_runtime_licenses = false;
567
568    check_proc_macro_dep_list(root, cargo, bless, bad);
569
570    for &(workspace, exceptions, permitted_deps, submodules) in WORKSPACES {
571        if has_missing_submodule(root, submodules) {
572            continue;
573        }
574
575        if !root.join(workspace).join("Cargo.lock").exists() {
576            tidy_error!(bad, "the `{workspace}` workspace doesn't have a Cargo.lock");
577            continue;
578        }
579
580        let mut cmd = cargo_metadata::MetadataCommand::new();
581        cmd.cargo_path(cargo)
582            .manifest_path(root.join(workspace).join("Cargo.toml"))
583            .features(cargo_metadata::CargoOpt::AllFeatures)
584            .other_options(vec!["--locked".to_owned()]);
585        let metadata = t!(cmd.exec());
586
587        check_license_exceptions(&metadata, workspace, exceptions, bad);
588        if let Some((crates, permitted_deps)) = permitted_deps {
589            check_permitted_dependencies(&metadata, workspace, permitted_deps, crates, bad);
590        }
591
592        if workspace == "library" {
593            check_runtime_license_exceptions(&metadata, bad);
594            check_runtime_no_duplicate_dependencies(&metadata, bad);
595            check_runtime_no_proc_macros(&metadata, bad);
596            checked_runtime_licenses = true;
597        }
598    }
599
600    // Sanity check to ensure we don't accidentally remove the workspace containing the runtime
601    // crates.
602    assert!(checked_runtime_licenses);
603}
604
605/// Ensure the list of proc-macro crate transitive dependencies is up to date
606fn check_proc_macro_dep_list(root: &Path, cargo: &Path, bless: bool, bad: &mut bool) {
607    let mut cmd = cargo_metadata::MetadataCommand::new();
608    cmd.cargo_path(cargo)
609        .manifest_path(root.join("Cargo.toml"))
610        .features(cargo_metadata::CargoOpt::AllFeatures)
611        .other_options(vec!["--locked".to_owned()]);
612    let metadata = t!(cmd.exec());
613    let is_proc_macro_pkg = |pkg: &Package| pkg.targets.iter().any(|target| target.is_proc_macro());
614
615    let mut proc_macro_deps = HashSet::new();
616    for pkg in metadata.packages.iter().filter(|pkg| is_proc_macro_pkg(pkg)) {
617        deps_of(&metadata, &pkg.id, &mut proc_macro_deps);
618    }
619    // Remove the proc-macro crates themselves
620    proc_macro_deps.retain(|pkg| !is_proc_macro_pkg(&metadata[pkg]));
621
622    let proc_macro_deps: HashSet<_> =
623        proc_macro_deps.into_iter().map(|dep| metadata[dep].name.as_ref()).collect();
624    let expected = proc_macro_deps::CRATES.iter().copied().collect::<HashSet<_>>();
625
626    let needs_blessing = proc_macro_deps.difference(&expected).next().is_some()
627        || expected.difference(&proc_macro_deps).next().is_some();
628
629    if needs_blessing && bless {
630        let mut proc_macro_deps: Vec<_> = proc_macro_deps.into_iter().collect();
631        proc_macro_deps.sort();
632        let mut file = File::create(root.join("src/bootstrap/src/utils/proc_macro_deps.rs"))
633            .expect("`proc_macro_deps` should exist");
634        writeln!(
635            &mut file,
636            "/// Do not update manually - use `./x.py test tidy --bless`
637/// Holds all direct and indirect dependencies of proc-macro crates in tree.
638/// See <https://github.com/rust-lang/rust/issues/134863>
639pub static CRATES: &[&str] = &[
640    // tidy-alphabetical-start"
641        )
642        .unwrap();
643        for dep in proc_macro_deps {
644            writeln!(&mut file, "    {dep:?},").unwrap();
645        }
646        writeln!(
647            &mut file,
648            "    // tidy-alphabetical-end
649];"
650        )
651        .unwrap();
652    } else {
653        let old_bad = *bad;
654
655        for missing in proc_macro_deps.difference(&expected) {
656            tidy_error!(
657                bad,
658                "proc-macro crate dependency `{missing}` is not registered in `src/bootstrap/src/utils/proc_macro_deps.rs`",
659            );
660        }
661        for extra in expected.difference(&proc_macro_deps) {
662            tidy_error!(
663                bad,
664                "`{extra}` is registered in `src/bootstrap/src/utils/proc_macro_deps.rs`, but is not a proc-macro crate dependency",
665            );
666        }
667        if *bad != old_bad {
668            eprintln!("Run `./x.py test tidy --bless` to regenerate the list");
669        }
670    }
671}
672
673/// Used to skip a check if a submodule is not checked out, and not in a CI environment.
674///
675/// This helps prevent enforcing developers to fetch submodules for tidy.
676pub fn has_missing_submodule(root: &Path, submodules: &[&str]) -> bool {
677    !CiEnv::is_ci()
678        && submodules.iter().any(|submodule| {
679            let path = root.join(submodule);
680            !path.exists()
681            // If the directory is empty, we can consider it as an uninitialized submodule.
682            || read_dir(path).unwrap().next().is_none()
683        })
684}
685
686/// Check that all licenses of runtime dependencies are in the valid list in `LICENSES`.
687///
688/// Unlike for tools we don't allow exceptions to the `LICENSES` list for the runtime with the sole
689/// exception of `fortanix-sgx-abi` which is only used on x86_64-fortanix-unknown-sgx.
690fn check_runtime_license_exceptions(metadata: &Metadata, bad: &mut bool) {
691    for pkg in &metadata.packages {
692        if pkg.source.is_none() {
693            // No need to check local packages.
694            continue;
695        }
696        let license = match &pkg.license {
697            Some(license) => license,
698            None => {
699                tidy_error!(bad, "dependency `{}` does not define a license expression", pkg.id);
700                continue;
701            }
702        };
703        if !LICENSES.contains(&license.as_str()) {
704            // This is a specific exception because SGX is considered "third party".
705            // See https://github.com/rust-lang/rust/issues/62620 for more.
706            // In general, these should never be added and this exception
707            // should not be taken as precedent for any new target.
708            if *pkg.name == "fortanix-sgx-abi" && pkg.license.as_deref() == Some("MPL-2.0") {
709                continue;
710            }
711
712            tidy_error!(bad, "invalid license `{}` in `{}`", license, pkg.id);
713        }
714    }
715}
716
717/// Check that all licenses of tool dependencies are in the valid list in `LICENSES`.
718///
719/// Packages listed in `exceptions` are allowed for tools.
720fn check_license_exceptions(
721    metadata: &Metadata,
722    workspace: &str,
723    exceptions: &[(&str, &str)],
724    bad: &mut bool,
725) {
726    // Validate the EXCEPTIONS list hasn't changed.
727    for (name, license) in exceptions {
728        // Check that the package actually exists.
729        if !metadata.packages.iter().any(|p| *p.name == *name) {
730            tidy_error!(
731                bad,
732                "could not find exception package `{}` in workspace `{workspace}`\n\
733                Remove from EXCEPTIONS list if it is no longer used.",
734                name
735            );
736        }
737        // Check that the license hasn't changed.
738        for pkg in metadata.packages.iter().filter(|p| *p.name == *name) {
739            match &pkg.license {
740                None => {
741                    tidy_error!(
742                        bad,
743                        "dependency exception `{}` in workspace `{workspace}` does not declare a license expression",
744                        pkg.id
745                    );
746                }
747                Some(pkg_license) => {
748                    if pkg_license.as_str() != *license {
749                        println!(
750                            "dependency exception `{name}` license in workspace `{workspace}` has changed"
751                        );
752                        println!("    previously `{license}` now `{pkg_license}`");
753                        println!("    update EXCEPTIONS for the new license");
754                        *bad = true;
755                    }
756                }
757            }
758        }
759    }
760
761    let exception_names: Vec<_> = exceptions.iter().map(|(name, _license)| *name).collect();
762
763    // Check if any package does not have a valid license.
764    for pkg in &metadata.packages {
765        if pkg.source.is_none() {
766            // No need to check local packages.
767            continue;
768        }
769        if exception_names.contains(&pkg.name.as_str()) {
770            continue;
771        }
772        let license = match &pkg.license {
773            Some(license) => license,
774            None => {
775                tidy_error!(
776                    bad,
777                    "dependency `{}` in workspace `{workspace}` does not define a license expression",
778                    pkg.id
779                );
780                continue;
781            }
782        };
783        if !LICENSES.contains(&license.as_str()) {
784            tidy_error!(
785                bad,
786                "invalid license `{}` for package `{}` in workspace `{workspace}`",
787                license,
788                pkg.id
789            );
790        }
791    }
792}
793
794fn check_runtime_no_duplicate_dependencies(metadata: &Metadata, bad: &mut bool) {
795    let mut seen_pkgs = HashSet::new();
796    for pkg in &metadata.packages {
797        if pkg.source.is_none() {
798            continue;
799        }
800
801        // Skip the `wasi` crate here which the standard library explicitly
802        // depends on two version of (one for the `wasm32-wasip1` target and
803        // another for the `wasm32-wasip2` target).
804        if pkg.name.to_string() != "wasi" && !seen_pkgs.insert(&*pkg.name) {
805            tidy_error!(
806                bad,
807                "duplicate package `{}` is not allowed for the standard library",
808                pkg.name
809            );
810        }
811    }
812}
813
814fn check_runtime_no_proc_macros(metadata: &Metadata, bad: &mut bool) {
815    for pkg in &metadata.packages {
816        if pkg.targets.iter().any(|target| target.is_proc_macro()) {
817            tidy_error!(
818                bad,
819                "proc macro `{}` is not allowed as standard library dependency.\n\
820                Using proc macros in the standard library would break cross-compilation \
821                as proc-macros don't get shipped for the host tuple.",
822                pkg.name
823            );
824        }
825    }
826}
827
828/// Checks the dependency of `restricted_dependency_crates` at the given path. Changes `bad` to
829/// `true` if a check failed.
830///
831/// Specifically, this checks that the dependencies are on the `permitted_dependencies`.
832fn check_permitted_dependencies(
833    metadata: &Metadata,
834    descr: &str,
835    permitted_dependencies: &[&'static str],
836    restricted_dependency_crates: &[&'static str],
837    bad: &mut bool,
838) {
839    let mut has_permitted_dep_error = false;
840    let mut deps = HashSet::new();
841    for to_check in restricted_dependency_crates {
842        let to_check = pkg_from_name(metadata, to_check);
843        deps_of(metadata, &to_check.id, &mut deps);
844    }
845
846    // Check that the PERMITTED_DEPENDENCIES does not have unused entries.
847    for permitted in permitted_dependencies {
848        fn compare(pkg: &Package, permitted: &str) -> bool {
849            if let Some((name, version)) = permitted.split_once("@") {
850                let Ok(version) = Version::parse(version) else {
851                    return false;
852                };
853                *pkg.name == name && pkg.version == version
854            } else {
855                *pkg.name == permitted
856            }
857        }
858        if !deps.iter().any(|dep_id| compare(pkg_from_id(metadata, dep_id), permitted)) {
859            tidy_error!(
860                bad,
861                "could not find allowed package `{permitted}`\n\
862                Remove from PERMITTED_DEPENDENCIES list if it is no longer used.",
863            );
864            has_permitted_dep_error = true;
865        }
866    }
867
868    // Get in a convenient form.
869    let permitted_dependencies: HashMap<_, _> = permitted_dependencies
870        .iter()
871        .map(|s| {
872            if let Some((name, version)) = s.split_once('@') {
873                (name, Version::parse(version).ok())
874            } else {
875                (*s, None)
876            }
877        })
878        .collect();
879
880    for dep in deps {
881        let dep = pkg_from_id(metadata, dep);
882        // If this path is in-tree, we don't require it to be explicitly permitted.
883        if dep.source.is_some() {
884            let is_eq = if let Some(version) = permitted_dependencies.get(dep.name.as_str()) {
885                if let Some(version) = version { version == &dep.version } else { true }
886            } else {
887                false
888            };
889            if !is_eq {
890                tidy_error!(bad, "Dependency for {descr} not explicitly permitted: {}", dep.id);
891                has_permitted_dep_error = true;
892            }
893        }
894    }
895
896    if has_permitted_dep_error {
897        eprintln!("Go to `{PERMITTED_DEPS_LOCATION}` for the list.");
898    }
899}
900
901/// Finds a package with the given name.
902fn pkg_from_name<'a>(metadata: &'a Metadata, name: &'static str) -> &'a Package {
903    let mut i = metadata.packages.iter().filter(|p| *p.name == name);
904    let result =
905        i.next().unwrap_or_else(|| panic!("could not find package `{name}` in package list"));
906    assert!(i.next().is_none(), "more than one package found for `{name}`");
907    result
908}
909
910fn pkg_from_id<'a>(metadata: &'a Metadata, id: &PackageId) -> &'a Package {
911    metadata.packages.iter().find(|p| &p.id == id).unwrap()
912}
913
914/// Recursively find all dependencies.
915fn deps_of<'a>(metadata: &'a Metadata, pkg_id: &'a PackageId, result: &mut HashSet<&'a PackageId>) {
916    if !result.insert(pkg_id) {
917        return;
918    }
919    let node = metadata
920        .resolve
921        .as_ref()
922        .unwrap()
923        .nodes
924        .iter()
925        .find(|n| &n.id == pkg_id)
926        .unwrap_or_else(|| panic!("could not find `{pkg_id}` in resolve"));
927    for dep in &node.deps {
928        deps_of(metadata, &dep.pkg, result);
929    }
930}