Skip to main content

tidy/
deps.rs

1//! Checks the licenses of third-party dependencies.
2
3use std::collections::{BTreeSet, HashMap, HashSet};
4use std::fmt::{Display, Formatter};
5use std::fs::{self, read_dir};
6use std::path::Path;
7use std::sync::atomic::{AtomicBool, Ordering};
8use std::{io, thread};
9
10use cargo_metadata::semver::Version;
11use cargo_metadata::{Metadata, Package, PackageId};
12
13use crate::diagnostics::{RunningCheck, TidyCtx};
14
15#[derive(Clone, Copy)]
16struct ListLocation {
17    path: &'static str,
18    line: u32,
19}
20
21impl Display for ListLocation {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        write!(f, "{}:{}", self.path, self.line)
24    }
25}
26
27/// Creates a [`ListLocation`] for the current location (with an additional offset to the actual list start);
28macro_rules! location {
29    (+ $offset:literal) => {
30        ListLocation { path: file!(), line: line!() + $offset }
31    };
32}
33
34/// These are licenses that are allowed for all crates, including the runtime,
35/// rustc, tools, etc.
36#[rustfmt::skip]
37const LICENSES: &[&str] = &[
38    // tidy-alphabetical-start
39    "(MIT OR Apache-2.0) AND MIT",
40    "0BSD OR MIT OR Apache-2.0",                           // adler2 license
41    "Apache-2.0 / MIT",
42    "Apache-2.0 OR ISC OR MIT",
43    "Apache-2.0 OR MIT",
44    "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT", // wasi license
45    "Apache-2.0/MIT",
46    "BSD-2-Clause OR Apache-2.0 OR MIT",                   // zerocopy
47    "BSD-2-Clause OR MIT OR Apache-2.0",
48    "BSD-3-Clause/MIT",
49    "CC0-1.0 OR MIT-0 OR Apache-2.0",
50    "ISC",
51    "MIT / Apache-2.0",
52    "MIT AND (MIT OR Apache-2.0)",
53    "MIT AND Apache-2.0 WITH LLVM-exception AND (MIT OR Apache-2.0)", // compiler-builtins
54    "MIT OR Apache-2.0 OR BSD-1-Clause",
55    "MIT OR Apache-2.0 OR LGPL-2.1-or-later",              // r-efi, r-efi-alloc; LGPL is not acceptable, but we use it under MIT OR Apache-2.0
56    "MIT OR Apache-2.0 OR Zlib",                           // tinyvec_macros
57    "MIT OR Apache-2.0",
58    "MIT OR Zlib OR Apache-2.0",                           // miniz_oxide
59    "MIT",
60    "MIT/Apache-2.0",
61    "Unlicense OR MIT",
62    "Unlicense/MIT",
63    "Zlib",                                                // foldhash (FIXME: see PERMITTED_STDLIB_DEPENDENCIES)
64    // tidy-alphabetical-end
65];
66
67/// These are licenses that are allowed for rustc, tools, etc. But not for the runtime!
68#[rustfmt::skip]
69const LICENSES_TOOLS: &[&str] = &[
70    // tidy-alphabetical-start
71    "(Apache-2.0 OR MIT) AND BSD-3-Clause",
72    "(MIT OR Apache-2.0) AND Unicode-3.0",                 // unicode_ident (1.0.14)
73    "(MIT OR Apache-2.0) AND Unicode-DFS-2016",            // unicode_ident (1.0.12)
74    "0BSD",
75    "Apache-2.0 AND ISC",
76    "Apache-2.0 OR BSL-1.0",  // BSL is not acceptable, but we use it under Apache-2.0
77    "Apache-2.0 OR GPL-2.0-only",
78    "Apache-2.0 WITH LLVM-exception",
79    "Apache-2.0",
80    "BSD-2-Clause",
81    "BSD-3-Clause",
82    "CC0-1.0 OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception",
83    "CC0-1.0",
84    "Unicode-3.0",                                         // icu4x
85    "Unicode-DFS-2016",                                    // tinystr
86    "Zlib OR Apache-2.0 OR MIT",                           // tinyvec
87    "Zlib",
88    // tidy-alphabetical-end
89];
90
91type ExceptionList = &'static [(&'static str, &'static str)];
92
93#[derive(Clone, Copy)]
94pub(crate) struct WorkspaceInfo<'a> {
95    /// Path to the directory containing the workspace root Cargo.toml file.
96    pub(crate) path: &'a str,
97    /// The list of license exceptions.
98    pub(crate) exceptions: ExceptionList,
99    /// The list of dependencies that are allowed. If None, any crate with an
100    /// acceptable license is allowed.
101    allowed_deps: Option<PermittedDeps<'a>>,
102    /// Submodules required for the workspace
103    pub(crate) submodules: &'a [&'a str],
104}
105
106#[derive(Clone, Copy)]
107struct PermittedDeps<'a> {
108    /// A list of crates for which dependencies need to be explicitly allowed
109    /// or None to check the entire workspace.
110    roots: Option<&'a [&'a str]>,
111    /// The list of allowed dependencies.
112    deps: &'a [&'a str],
113    /// The source code location of the allowed dependencies list.
114    deps_loc: ListLocation,
115}
116
117impl<'a> PermittedDeps<'a> {
118    const fn new(
119        roots: Option<&'a [&'a str]>,
120        deps: &'a [&'a str],
121        deps_loc: ListLocation,
122    ) -> Self {
123        Self { roots, deps, deps_loc }
124    }
125}
126
127const WORKSPACE_LOCATION: ListLocation = location!(+4);
128
129/// The workspaces to check for licensing and optionally permitted dependencies.
130// FIXME auto detect all cargo workspaces
131pub(crate) const WORKSPACES: &[WorkspaceInfo<'static>] = &[
132    // The root workspace has to be first for check_rustfix to work.
133    WorkspaceInfo {
134        path: ".",
135        exceptions: EXCEPTIONS,
136        allowed_deps: Some(PermittedDeps::new(
137            Some(&["rustc-main"]),
138            PERMITTED_RUSTC_DEPENDENCIES,
139            PERMITTED_RUSTC_DEPS_LOCATION,
140        )),
141        submodules: &[],
142    },
143    WorkspaceInfo {
144        path: "library",
145        exceptions: EXCEPTIONS_STDLIB,
146        allowed_deps: Some(PermittedDeps::new(
147            None,
148            PERMITTED_STDLIB_DEPENDENCIES,
149            PERMITTED_STDLIB_DEPS_LOCATION,
150        )),
151        submodules: &[],
152    },
153    WorkspaceInfo {
154        path: "library/stdarch",
155        exceptions: EXCEPTIONS_STDARCH,
156        allowed_deps: None,
157        submodules: &[],
158    },
159    WorkspaceInfo {
160        path: "compiler/rustc_codegen_cranelift",
161        exceptions: EXCEPTIONS_CRANELIFT,
162        allowed_deps: Some(PermittedDeps::new(
163            None,
164            PERMITTED_CRANELIFT_DEPENDENCIES,
165            PERMITTED_CRANELIFT_DEPS_LOCATION,
166        )),
167        submodules: &[],
168    },
169    WorkspaceInfo {
170        path: "compiler/rustc_codegen_gcc",
171        exceptions: EXCEPTIONS_GCC,
172        allowed_deps: None,
173        submodules: &[],
174    },
175    WorkspaceInfo {
176        path: "src/bootstrap",
177        exceptions: EXCEPTIONS_BOOTSTRAP,
178        allowed_deps: None,
179        submodules: &[],
180    },
181    WorkspaceInfo {
182        path: "src/tools/cargo",
183        exceptions: EXCEPTIONS_CARGO,
184        allowed_deps: None,
185        submodules: &["src/tools/cargo"],
186    },
187    // FIXME uncomment once all deps are vendored
188    //  WorkspaceInfo {
189    //      path: "src/tools/miri/test-cargo-miri",
190    //      crates_and_deps: None
191    //      submodules: &[],
192    //  },
193    // WorkspaceInfo {
194    //      path: "src/tools/miri/test_dependencies",
195    //      crates_and_deps: None,
196    //      submodules: &[],
197    //  }
198    WorkspaceInfo {
199        path: "src/tools/rust-analyzer",
200        exceptions: EXCEPTIONS_RUST_ANALYZER,
201        allowed_deps: None,
202        submodules: &[],
203    },
204    WorkspaceInfo {
205        path: "src/tools/rustbook",
206        exceptions: EXCEPTIONS_RUSTBOOK,
207        allowed_deps: None,
208        submodules: &["src/doc/book", "src/doc/reference"],
209    },
210    WorkspaceInfo {
211        path: "src/tools/rustc-perf",
212        exceptions: EXCEPTIONS_RUSTC_PERF,
213        allowed_deps: None,
214        submodules: &["src/tools/rustc-perf"],
215    },
216    WorkspaceInfo {
217        path: "tests/run-make-cargo/uefi-qemu/uefi_qemu_test",
218        exceptions: EXCEPTIONS_UEFI_QEMU_TEST,
219        allowed_deps: None,
220        submodules: &[],
221    },
222];
223
224/// These are exceptions to Rust's permissive licensing policy, and
225/// should be considered bugs. Exceptions are only allowed in Rust
226/// tooling. It is _crucial_ that no exception crates be dependencies
227/// of the Rust runtime (std/test).
228#[rustfmt::skip]
229const EXCEPTIONS: ExceptionList = &[
230    // tidy-alphabetical-start
231    ("colored", "MPL-2.0"),                                  // rustfmt
232    ("option-ext", "MPL-2.0"),                               // cargo-miri (via `directories`)
233    // tidy-alphabetical-end
234];
235
236/// These are exceptions to Rust's permissive licensing policy, and
237/// should be considered bugs. Exceptions are only allowed in Rust
238/// tooling. It is _crucial_ that no exception crates be dependencies
239/// of the Rust runtime (std/test).
240#[rustfmt::skip]
241const EXCEPTIONS_STDLIB: ExceptionList = &[
242    // tidy-alphabetical-start
243    ("fortanix-sgx-abi", "MPL-2.0"), // libstd but only for `sgx` target. FIXME: this dependency violates the documentation comment above.
244    // tidy-alphabetical-end
245];
246
247const EXCEPTIONS_CARGO: ExceptionList = &[
248    // tidy-alphabetical-start
249    ("bitmaps", "MPL-2.0+"),
250    ("im-rc", "MPL-2.0+"),
251    ("sized-chunks", "MPL-2.0+"),
252    // tidy-alphabetical-end
253];
254
255const EXCEPTIONS_RUST_ANALYZER: ExceptionList = &[
256    // tidy-alphabetical-start
257    ("option-ext", "MPL-2.0"),
258    // tidy-alphabetical-end
259];
260
261const EXCEPTIONS_RUSTC_PERF: ExceptionList = &[
262    // tidy-alphabetical-start
263    ("aws-lc-rs", "ISC AND (Apache-2.0 OR ISC)"),
264    (
265        "aws-lc-sys",
266        "ISC AND (Apache-2.0 OR ISC) AND Apache-2.0 AND MIT AND BSD-3-Clause AND (Apache-2.0 OR ISC OR MIT) AND (Apache-2.0 OR ISC OR MIT-0)",
267    ),
268    ("brotli", "BSD-3-Clause AND MIT"),
269    ("fast-srgb8", "MIT OR Apache-2.0 OR CC0-1.0"),
270    ("inferno", "CDDL-1.0"),
271    ("option-ext", "MPL-2.0"),
272    ("wasite", "Apache-2.0 OR BSL-1.0 OR MIT"),
273    ("webpki-root-certs", "CDLA-Permissive-2.0"),
274    ("whoami", "Apache-2.0 OR BSL-1.0 OR MIT"),
275    // tidy-alphabetical-end
276];
277
278const EXCEPTIONS_RUSTBOOK: ExceptionList = &[
279    // tidy-alphabetical-start
280    ("font-awesome-as-a-crate", "CC-BY-4.0 AND MIT"),
281    ("mdbook-core", "MPL-2.0"),
282    ("mdbook-driver", "MPL-2.0"),
283    ("mdbook-html", "MPL-2.0"),
284    ("mdbook-markdown", "MPL-2.0"),
285    ("mdbook-preprocessor", "MPL-2.0"),
286    ("mdbook-renderer", "MPL-2.0"),
287    ("mdbook-summary", "MPL-2.0"),
288    // tidy-alphabetical-end
289];
290
291const EXCEPTIONS_STDARCH: ExceptionList = &[];
292
293const EXCEPTIONS_CRANELIFT: ExceptionList = &[];
294
295const EXCEPTIONS_GCC: ExceptionList = &[
296    // tidy-alphabetical-start
297    ("gccjit", "GPL-3.0"),
298    ("gccjit_sys", "GPL-3.0"),
299    // tidy-alphabetical-end
300];
301
302const EXCEPTIONS_BOOTSTRAP: ExceptionList = &[];
303
304const EXCEPTIONS_UEFI_QEMU_TEST: ExceptionList = &[];
305
306const PERMITTED_RUSTC_DEPS_LOCATION: ListLocation = location!(+6);
307
308/// Crates rustc is allowed to depend on. Avoid adding to the list if possible.
309///
310/// This list is here to provide a speed-bump to adding a new dependency to
311/// rustc. Please check with the compiler team before adding an entry.
312const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
313    // tidy-alphabetical-start
314    "adler2",
315    "aho-corasick",
316    "allocator-api2", // FIXME: only appears in Cargo.lock due to https://github.com/rust-lang/cargo/issues/10801
317    "annotate-snippets",
318    "anstream",
319    "anstyle",
320    "anstyle-parse",
321    "anstyle-query",
322    "anstyle-wincon",
323    "ar_archive_writer",
324    "arrayref",
325    "arrayvec",
326    "bitflags",
327    "blake3",
328    "block-buffer",
329    "block2",
330    "bstr",
331    "cc",
332    "cfg-if",
333    "cfg_aliases",
334    "colorchoice",
335    "constant_time_eq",
336    "cpufeatures",
337    "crc32fast",
338    "crossbeam-deque",
339    "crossbeam-epoch",
340    "crossbeam-utils",
341    "crypto-common",
342    "ctrlc",
343    "darling",
344    "darling_core",
345    "darling_macro",
346    "datafrog",
347    "derive-where",
348    "derive_setters",
349    "digest",
350    "dispatch2",
351    "displaydoc",
352    "dissimilar",
353    "dyn-clone",
354    "either",
355    "elsa",
356    "ena",
357    "equivalent",
358    "errno",
359    "expect-test",
360    "fastrand",
361    "find-msvc-tools",
362    "flate2",
363    "fluent-bundle",
364    "fluent-langneg",
365    "fluent-syntax",
366    "fnv",
367    "foldhash",
368    "generic-array",
369    "getopts",
370    "getrandom",
371    "gimli",
372    "gsgdt",
373    "hashbrown",
374    "ident_case",
375    "indexmap",
376    "intl-memoizer",
377    "intl_pluralrules",
378    "is_terminal_polyfill",
379    "itertools",
380    "itoa",
381    "jiff",
382    "jiff-static",
383    "jiff-tzdb",
384    "jiff-tzdb-platform",
385    "jobserver",
386    "lazy_static",
387    "leb128fmt",
388    "libc",
389    "libloading",
390    "linux-raw-sys",
391    "lock_api",
392    "log",
393    "matchers",
394    "md-5",
395    "measureme",
396    "memchr",
397    "memmap2",
398    "miniz_oxide",
399    "nix",
400    "nu-ansi-term",
401    "objc2",
402    "objc2-encode",
403    "object",
404    "odht",
405    "once_cell",
406    "once_cell_polyfill",
407    "parking_lot",
408    "parking_lot_core",
409    "pathdiff",
410    "perf-event-open-sys",
411    "pin-project-lite",
412    "polonius-engine",
413    "portable-atomic", // dependency for platforms doesn't support `AtomicU64` in std
414    "portable-atomic-util",
415    "ppv-lite86",
416    "proc-macro-hack",
417    "proc-macro2",
418    "pulldown-cmark",
419    "pulldown-cmark-escape",
420    "punycode",
421    "quote",
422    "r-efi",
423    "rand",
424    "rand_chacha",
425    "rand_core",
426    "rand_xorshift", // dependency for doc-tests in rustc_thread_pool
427    "rand_xoshiro",
428    "redox_syscall",
429    "ref-cast",
430    "ref-cast-impl",
431    "regex",
432    "regex-automata",
433    "regex-syntax",
434    "rustc-demangle",
435    "rustc-hash",
436    "rustc-literal-escaper",
437    "rustc-stable-hash",
438    "rustc_apfloat",
439    "rustix",
440    "ruzstd", // via object in thorin-dwp
441    "ryu",
442    "schemars",
443    "schemars_derive",
444    "scoped-tls",
445    "scopeguard",
446    "self_cell",
447    "semver",
448    "serde",
449    "serde_core",
450    "serde_derive",
451    "serde_derive_internals",
452    "serde_json",
453    "serde_path_to_error",
454    "sha1",
455    "sha2",
456    "sharded-slab",
457    "shlex",
458    "simd-adler32",
459    "smallvec",
460    "stable_deref_trait",
461    "static_assertions",
462    "strsim",
463    "syn",
464    "synstructure",
465    "tempfile",
466    "termize",
467    "thin-vec",
468    "thiserror",
469    "thiserror-impl",
470    "thorin-dwp",
471    "thread_local",
472    "tikv-jemalloc-sys",
473    "tinystr",
474    "tinyvec",
475    "tinyvec_macros",
476    "tracing",
477    "tracing-attributes",
478    "tracing-core",
479    "tracing-log",
480    "tracing-serde",
481    "tracing-subscriber",
482    "tracing-tree",
483    "twox-hash",
484    "type-map",
485    "typenum",
486    "unic-langid",
487    "unic-langid-impl",
488    "unic-langid-macros",
489    "unic-langid-macros-impl",
490    "unicase",
491    "unicode-ident",
492    "unicode-normalization",
493    "unicode-properties",
494    "unicode-script",
495    "unicode-security",
496    "unicode-width",
497    "utf8parse",
498    "valuable",
499    "version_check",
500    "wasi",
501    "wasm-encoder",
502    "wasmparser",
503    "windows",
504    "windows-collections",
505    "windows-core",
506    "windows-future",
507    "windows-implement",
508    "windows-interface",
509    "windows-link",
510    "windows-numerics",
511    "windows-result",
512    "windows-strings",
513    "windows-sys",
514    "windows-threading",
515    "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>
516    "yoke",
517    "yoke-derive",
518    "zerocopy",
519    "zerocopy-derive",
520    "zerofrom",
521    "zerofrom-derive",
522    "zerovec",
523    "zerovec-derive",
524    "zlib-rs",
525    // tidy-alphabetical-end
526];
527
528const PERMITTED_STDLIB_DEPS_LOCATION: ListLocation = location!(+2);
529
530const PERMITTED_STDLIB_DEPENDENCIES: &[&str] = &[
531    // tidy-alphabetical-start
532    "addr2line",
533    "adler2",
534    "cc",
535    "cfg-if",
536    "compiler_builtins",
537    "dlmalloc",
538    "find-msvc-tools", // via cc
539    "foldhash", // FIXME: only appears in Cargo.lock due to https://github.com/rust-lang/cargo/issues/10801
540    "fortanix-sgx-abi",
541    "getopts",
542    "gimli",
543    "hashbrown",
544    "hermit-abi",
545    "libc",
546    "memchr",
547    "miniz_oxide",
548    "moto-rt",
549    "object",
550    "r-efi",
551    "r-efi-alloc",
552    "rand",
553    "rand_core",
554    "rand_xorshift",
555    "rustc-demangle",
556    "rustc-literal-escaper",
557    "shlex",
558    "unwinding",
559    "vex-sdk",
560    "wasip1",
561    "wasip2",
562    "wasip3",
563    "windows-link",
564    "windows-sys@0.61.100", // Enforce the usage of our dummy windows-sys patch. Keep version in sync.
565    "wit-bindgen",
566    // tidy-alphabetical-end
567];
568
569const PERMITTED_CRANELIFT_DEPS_LOCATION: ListLocation = location!(+2);
570
571const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
572    // tidy-alphabetical-start
573    "allocator-api2",
574    "anyhow",
575    "arbitrary",
576    "bitflags",
577    "bumpalo",
578    "cfg-if",
579    "cranelift-assembler-x64",
580    "cranelift-assembler-x64-meta",
581    "cranelift-bforest",
582    "cranelift-bitset",
583    "cranelift-codegen",
584    "cranelift-codegen-meta",
585    "cranelift-codegen-shared",
586    "cranelift-control",
587    "cranelift-entity",
588    "cranelift-frontend",
589    "cranelift-isle",
590    "cranelift-jit",
591    "cranelift-module",
592    "cranelift-native",
593    "cranelift-object",
594    "cranelift-srcgen",
595    "crc32fast",
596    "equivalent",
597    "fnv",
598    "foldhash",
599    "gimli",
600    "hashbrown",
601    "heck",
602    "indexmap",
603    "libc",
604    "libloading",
605    "libm",
606    "log",
607    "mach2",
608    "memchr",
609    "memmap2",
610    "object",
611    "proc-macro2",
612    "quote",
613    "regalloc2",
614    "region",
615    "rustc-hash",
616    "serde",
617    "serde_core",
618    "serde_derive",
619    "smallvec",
620    "stable_deref_trait",
621    "syn",
622    "target-lexicon",
623    "unicode-ident",
624    "wasmtime-internal-core",
625    "wasmtime-internal-jit-icache-coherence",
626    "windows-link",
627    "windows-sys",
628    "windows-targets",
629    "windows_aarch64_gnullvm",
630    "windows_aarch64_msvc",
631    "windows_i686_gnu",
632    "windows_i686_gnullvm",
633    "windows_i686_msvc",
634    "windows_x86_64_gnu",
635    "windows_x86_64_gnullvm",
636    "windows_x86_64_msvc",
637    // tidy-alphabetical-end
638];
639
640/// Dependency checks.
641///
642/// `root` is path to the directory with the root `Cargo.toml` (for the workspace). `cargo` is path
643/// to the cargo executable.
644pub fn check(root: &Path, cargo: &Path, sem: &'_ crate::Semaphore, tidy_ctx: TidyCtx) {
645    let mut check = tidy_ctx.start_check("deps");
646    let bless = tidy_ctx.is_bless_enabled();
647    let is_ci = tidy_ctx.is_running_on_ci();
648
649    let checked_runtime_licenses = AtomicBool::new(false);
650
651    check_proc_macro_dep_list(root, cargo, bless, &mut check);
652
653    thread::scope(|s| {
654        for (i, WorkspaceInfo { path, exceptions, allowed_deps: crates_and_deps, submodules }) in
655            WORKSPACES.iter().enumerate()
656        {
657            let check = &check;
658            let checked_runtime_licenses = &checked_runtime_licenses;
659            // starting deps::check already consumed one semaphore permit, with concurrency == 1
660            // this would deadlock if we tried to get one here for the first thread, so
661            // we only do it for additional threads beyond the first one.
662            let guard = if i > 0 { Some(sem.acquire()) } else { None };
663            s.spawn(move || {
664                if has_missing_submodule(root, submodules, is_ci) {
665                    return;
666                }
667
668                if !root.join(path).join("Cargo.lock").exists() {
669                    check.error(format!("the `{path}` workspace doesn't have a Cargo.lock"));
670                    return;
671                }
672
673                let mut cmd = cargo_metadata::MetadataCommand::new();
674                cmd.cargo_path(cargo)
675                    .manifest_path(root.join(path).join("Cargo.toml"))
676                    .features(cargo_metadata::CargoOpt::AllFeatures)
677                    .other_options(vec!["--locked".to_owned()]);
678                let metadata = t!(cmd.exec());
679
680                // Check for packages which have been moved into a different workspace and not updated
681                let absolute_root =
682                    if *path == "." { root.to_path_buf() } else { t!(std::path::absolute(root.join(path))) };
683                let absolute_root_real = t!(std::path::absolute(&metadata.workspace_root));
684                if absolute_root_real != absolute_root {
685                    check.error(format!("{path} is part of another workspace ({} != {}), remove from `WORKSPACES` ({WORKSPACE_LOCATION})", absolute_root.display(), absolute_root_real.display()));
686                }
687                check_license_exceptions(&metadata, path, exceptions, &check);
688                if let Some(PermittedDeps { roots, deps: permitted_deps, deps_loc }) = crates_and_deps {
689                    let descr = roots.map_or(path, |roots| roots.get(0).unwrap_or(&path));
690                    check_permitted_dependencies(
691                        &metadata,
692                        descr,
693                        permitted_deps,
694                        *roots,
695                        *deps_loc,
696                        &check,
697                    );
698                }
699
700
701                if *path == "library" {
702                    check_runtime_license_exceptions(&metadata, &check);
703                    check_runtime_no_duplicate_dependencies(&metadata, &check);
704                    check_runtime_no_proc_macros(&metadata, &check);
705                    checked_runtime_licenses.store(true, Ordering::Relaxed);
706                }
707
708                let _guard = guard;
709            });
710        }
711    });
712
713    // Sanity check to ensure we don't accidentally remove the workspace containing the runtime
714    // crates.
715    assert!(checked_runtime_licenses.load(Ordering::Relaxed));
716}
717
718/// Ensure the list of proc-macro crate transitive dependencies is up to date
719fn check_proc_macro_dep_list(root: &Path, cargo: &Path, bless: bool, check: &mut RunningCheck) {
720    if std::env::var("RUSTC").is_err() {
721        panic!("tidy must be run under bootstrap (./x test tidy), not as a standalone command");
722    }
723    let mut cmd = cargo_metadata::MetadataCommand::new();
724    cmd.cargo_path(cargo)
725        .manifest_path(root.join("Cargo.toml"))
726        .features(cargo_metadata::CargoOpt::AllFeatures)
727        .other_options(vec!["--locked".to_owned()]);
728    let metadata = t!(cmd.exec());
729    let is_proc_macro_pkg = |pkg: &Package| pkg.targets.iter().any(|target| target.is_proc_macro());
730
731    let mut proc_macro_deps = HashSet::new();
732    for pkg in metadata.packages.iter().filter(|pkg| is_proc_macro_pkg(pkg)) {
733        deps_of(&metadata, &pkg.id, &mut proc_macro_deps);
734    }
735    // Remove the proc-macro crates themselves
736    proc_macro_deps.retain(|pkg| !is_proc_macro_pkg(&metadata[pkg]));
737    // Sort and deduplicate the crate names.
738    // Cargo package names may contain `-`, but will normalize these to `_` before passing to rustc.
739    // As bootstrap parses the `--crate-name` flag, use the name of the actual lib target which has
740    // been normalized.
741    let proc_macro_deps = proc_macro_deps
742        .into_iter()
743        .filter_map(|dep| {
744            metadata[dep].targets.iter().find_map(|target| target.is_lib().then_some(&target.name))
745        })
746        .collect::<BTreeSet<_>>();
747
748    let expected = {
749        use std::fmt::Write;
750
751        const HEADER: &str = "\
752/// Do not update manually - use `./x.py test tidy --bless`
753/// Holds all direct and indirect dependencies of proc-macro crates in tree.
754/// See <https://github.com/rust-lang/rust/issues/134863>
755pub static CRATES: &[&str] = &[
756    // tidy-alphabetical-start
757";
758        const FOOTER: &str = "    // tidy-alphabetical-end
759];
760";
761
762        let mut buf = String::with_capacity(4096);
763        buf.push_str(HEADER);
764        for dep in proc_macro_deps {
765            writeln!(buf, "    {dep:?},").unwrap();
766        }
767        buf.push_str(FOOTER);
768        buf
769    };
770
771    const PROC_MACRO_DEPS_RS: &str = "src/bootstrap/src/utils/proc_macro_deps.rs";
772    let proc_macro_deps_rs_path = &root.join(PROC_MACRO_DEPS_RS);
773    let actual = match fs::read_to_string(proc_macro_deps_rs_path) {
774        Ok(actual) => actual,
775        Err(e) => {
776            if e.kind() == io::ErrorKind::NotFound {
777                check.error(format!(
778                    "`{PROC_MACRO_DEPS_RS}` not found; has it been moved or renamed?"
779                ));
780            } else {
781                check.error(format!("`{PROC_MACRO_DEPS_RS}` could not be read: {e:?}"));
782            }
783            return;
784        }
785    };
786
787    if actual != expected {
788        if bless {
789            fs::write(proc_macro_deps_rs_path, &expected).unwrap();
790        } else {
791            let diff = similar::TextDiff::from_lines(&actual, &expected);
792            let mut unified = diff.unified_diff();
793            unified.header(PROC_MACRO_DEPS_RS, "(expected)");
794
795            check.error(format!("`{PROC_MACRO_DEPS_RS}` is not up-to-date:\n{unified}"));
796            check.message("Run `./x.py test tidy --bless` to regenerate the list");
797        }
798    }
799}
800
801/// Used to skip a check if a submodule is not checked out, and not in a CI environment.
802///
803/// This helps prevent enforcing developers to fetch submodules for tidy.
804pub fn has_missing_submodule(root: &Path, submodules: &[&str], is_ci: bool) -> bool {
805    !is_ci
806        && submodules.iter().any(|submodule| {
807            let path = root.join(submodule);
808            !path.exists()
809            // If the directory is empty, we can consider it as an uninitialized submodule.
810            || read_dir(path).unwrap().next().is_none()
811        })
812}
813
814/// Check that all licenses of runtime dependencies are in the valid list in `LICENSES`.
815///
816/// Unlike for tools we don't allow exceptions to the `LICENSES` list for the runtime with the sole
817/// exception of `fortanix-sgx-abi` which is only used on x86_64-fortanix-unknown-sgx.
818fn check_runtime_license_exceptions(metadata: &Metadata, check: &RunningCheck) {
819    for pkg in &metadata.packages {
820        if pkg.source.is_none() {
821            // No need to check local packages.
822            continue;
823        }
824        let license = match &pkg.license {
825            Some(license) => license,
826            None => {
827                check
828                    .error(format!("dependency `{}` does not define a license expression", pkg.id));
829                continue;
830            }
831        };
832        if !LICENSES.contains(&license.as_str()) {
833            // This is a specific exception because SGX is considered "third party".
834            // See https://github.com/rust-lang/rust/issues/62620 for more.
835            // In general, these should never be added and this exception
836            // should not be taken as precedent for any new target.
837            if *pkg.name == "fortanix-sgx-abi" && pkg.license.as_deref() == Some("MPL-2.0") {
838                continue;
839            }
840
841            check.error(format!("invalid license `{}` in `{}`", license, pkg.id));
842        }
843    }
844}
845
846/// Check that all licenses of tool dependencies are in the valid list in `LICENSES`.
847///
848/// Packages listed in `exceptions` are allowed for tools.
849fn check_license_exceptions(
850    metadata: &Metadata,
851    workspace: &str,
852    exceptions: &[(&str, &str)],
853    check: &RunningCheck,
854) {
855    // Validate the EXCEPTIONS list hasn't changed.
856    for (name, license) in exceptions {
857        // Check that the package actually exists.
858        if !metadata.packages.iter().any(|p| *p.name == *name) {
859            check.error(format!(
860                "could not find exception package `{name}` in workspace `{workspace}`\n\
861                Remove from EXCEPTIONS list if it is no longer used.",
862            ));
863        }
864        // Check that the license hasn't changed.
865        for pkg in metadata.packages.iter().filter(|p| *p.name == *name) {
866            match &pkg.license {
867                None => {
868                    check.error(format!(
869                        "dependency exception `{}` in workspace `{workspace}` does not declare a license expression",
870                        pkg.id
871                    ));
872                }
873                Some(pkg_license) => {
874                    if pkg_license.as_str() != *license {
875                        check.error(format!(r#"dependency exception `{name}` license in workspace `{workspace}` has changed
876    previously `{license}` now `{pkg_license}`
877    update EXCEPTIONS for the new license
878"#));
879                    }
880                }
881            }
882        }
883        if LICENSES.contains(license) || LICENSES_TOOLS.contains(license) {
884            check.error(format!(
885                "dependency exception `{name}` is not necessary. `{license}` is an allowed license"
886            ));
887        }
888    }
889
890    let exception_names: Vec<_> = exceptions.iter().map(|(name, _license)| *name).collect();
891
892    // Check if any package does not have a valid license.
893    for pkg in &metadata.packages {
894        if pkg.source.is_none() {
895            // No need to check local packages.
896            continue;
897        }
898        if exception_names.contains(&pkg.name.as_str()) {
899            continue;
900        }
901        let license = match &pkg.license {
902            Some(license) => license,
903            None => {
904                check.error(format!(
905                    "dependency `{}` in workspace `{workspace}` does not define a license expression",
906                    pkg.id
907                ));
908                continue;
909            }
910        };
911        if !LICENSES.contains(&license.as_str()) && !LICENSES_TOOLS.contains(&license.as_str()) {
912            check.error(format!(
913                "invalid license `{}` for package `{}` in workspace `{workspace}`",
914                license, pkg.id
915            ));
916        }
917    }
918}
919
920fn check_runtime_no_duplicate_dependencies(metadata: &Metadata, check: &RunningCheck) {
921    let mut seen_pkgs = HashSet::new();
922    for pkg in &metadata.packages {
923        if pkg.source.is_none() {
924            continue;
925        }
926
927        if !seen_pkgs.insert(&*pkg.name) {
928            check.error(format!(
929                "duplicate package `{}` is not allowed for the standard library",
930                pkg.name
931            ));
932        }
933    }
934}
935
936fn check_runtime_no_proc_macros(metadata: &Metadata, check: &RunningCheck) {
937    for pkg in &metadata.packages {
938        if pkg.targets.iter().any(|target| target.is_proc_macro()) {
939            check.error(format!(
940                "proc macro `{}` is not allowed as standard library dependency.\n\
941                Using proc macros in the standard library would break cross-compilation \
942                as proc-macros don't get shipped for the host tuple.",
943                pkg.name
944            ));
945        }
946    }
947}
948
949/// Checks the dependency of `restricted_dependency_crates` at the given path. Changes `bad` to
950/// `true` if a check failed.
951///
952/// Specifically, this checks that the dependencies are on the `permitted_dependencies`.
953fn check_permitted_dependencies(
954    metadata: &Metadata,
955    descr: &str,
956    permitted_dependencies: &[&'static str],
957    restricted_dependency_crates: Option<&[&'static str]>,
958    permitted_location: ListLocation,
959    check: &RunningCheck,
960) {
961    let mut has_permitted_dep_error = false;
962    let mut deps = HashSet::new();
963    if let Some(restricted_dependency_crates) = restricted_dependency_crates {
964        for to_check in restricted_dependency_crates {
965            let to_check = pkg_from_name(metadata, to_check);
966            deps_of(metadata, &to_check.id, &mut deps);
967        }
968    } else {
969        for to_check in &metadata.packages {
970            deps_of(metadata, &to_check.id, &mut deps);
971        }
972    }
973
974    // Check that the PERMITTED_DEPENDENCIES does not have unused entries.
975    for permitted in permitted_dependencies {
976        fn compare(pkg: &Package, permitted: &str) -> bool {
977            if let Some((name, version)) = permitted.split_once("@") {
978                let Ok(version) = Version::parse(version) else {
979                    return false;
980                };
981                *pkg.name == name && pkg.version == version
982            } else {
983                *pkg.name == permitted
984            }
985        }
986        if !deps.iter().any(|dep_id| compare(pkg_from_id(metadata, dep_id), permitted)) {
987            check.error(format!(
988                "could not find allowed package `{permitted}`\n\
989                Remove from PERMITTED_DEPENDENCIES list if it is no longer used.",
990            ));
991            has_permitted_dep_error = true;
992        }
993    }
994
995    // Get in a convenient form.
996    let permitted_dependencies: HashMap<_, _> = permitted_dependencies
997        .iter()
998        .map(|s| {
999            if let Some((name, version)) = s.split_once('@') {
1000                (name, Version::parse(version).ok())
1001            } else {
1002                (*s, None)
1003            }
1004        })
1005        .collect();
1006
1007    for dep in deps {
1008        let dep = pkg_from_id(metadata, dep);
1009        // If this path is in-tree, we don't require it to be explicitly permitted.
1010        if dep.source.is_some() {
1011            let is_eq = if let Some(version) = permitted_dependencies.get(dep.name.as_str()) {
1012                if let Some(version) = version { version == &dep.version } else { true }
1013            } else {
1014                false
1015            };
1016            if !is_eq {
1017                check.error(format!("Dependency for {descr} not explicitly permitted: {}", dep.id));
1018                has_permitted_dep_error = true;
1019            }
1020        }
1021    }
1022
1023    if has_permitted_dep_error {
1024        eprintln!("Go to `{}:{}` for the list.", permitted_location.path, permitted_location.line);
1025    }
1026}
1027
1028/// Finds a package with the given name.
1029fn pkg_from_name<'a>(metadata: &'a Metadata, name: &'static str) -> &'a Package {
1030    let mut i = metadata.packages.iter().filter(|p| *p.name == name);
1031    let result =
1032        i.next().unwrap_or_else(|| panic!("could not find package `{name}` in package list"));
1033    assert!(i.next().is_none(), "more than one package found for `{name}`");
1034    result
1035}
1036
1037fn pkg_from_id<'a>(metadata: &'a Metadata, id: &PackageId) -> &'a Package {
1038    metadata.packages.iter().find(|p| &p.id == id).unwrap()
1039}
1040
1041/// Recursively find all dependencies.
1042fn deps_of<'a>(metadata: &'a Metadata, pkg_id: &'a PackageId, result: &mut HashSet<&'a PackageId>) {
1043    if !result.insert(pkg_id) {
1044        return;
1045    }
1046    let node = metadata
1047        .resolve
1048        .as_ref()
1049        .unwrap()
1050        .nodes
1051        .iter()
1052        .find(|n| &n.id == pkg_id)
1053        .unwrap_or_else(|| panic!("could not find `{pkg_id}` in resolve"));
1054    for dep in &node.deps {
1055        deps_of(metadata, &dep.pkg, result);
1056    }
1057}