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