1use std::collections::HashSet;
4use std::fs::{File, read_dir};
5use std::io::Write;
6use std::path::Path;
7
8use build_helper::ci::CiEnv;
9use cargo_metadata::{Metadata, Package, PackageId};
10
11#[path = "../../../bootstrap/src/utils/proc_macro_deps.rs"]
12mod proc_macro_deps;
13
14#[rustfmt::skip]
17const LICENSES: &[&str] = &[
18 "(MIT OR Apache-2.0) AND Unicode-3.0", "(MIT OR Apache-2.0) AND Unicode-DFS-2016", "0BSD OR MIT OR Apache-2.0", "0BSD",
23 "Apache-2.0 / MIT",
24 "Apache-2.0 OR ISC OR MIT",
25 "Apache-2.0 OR MIT",
26 "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT", "Apache-2.0",
28 "Apache-2.0/MIT",
29 "BSD-2-Clause OR Apache-2.0 OR MIT", "ISC",
31 "MIT / Apache-2.0",
32 "MIT AND (MIT OR Apache-2.0)",
33 "MIT AND Apache-2.0 WITH LLVM-exception AND (MIT OR Apache-2.0)", "MIT OR Apache-2.0 OR LGPL-2.1-or-later", "MIT OR Apache-2.0 OR Zlib", "MIT OR Apache-2.0",
37 "MIT OR Zlib OR Apache-2.0", "MIT",
39 "MIT/Apache-2.0",
40 "Unicode-3.0", "Unicode-DFS-2016", "Unlicense OR MIT",
43 "Unlicense/MIT",
44 "Zlib OR Apache-2.0 OR MIT", ];
47
48type ExceptionList = &'static [(&'static str, &'static str)];
49
50pub(crate) const WORKSPACES: &[(&str, ExceptionList, Option<(&[&str], &[&str])>, &[&str])] = &[
62 (".", EXCEPTIONS, Some((&["rustc-main"], PERMITTED_RUSTC_DEPENDENCIES)), &[]),
64 ("library", EXCEPTIONS_STDLIB, Some((&["sysroot"], PERMITTED_STDLIB_DEPENDENCIES)), &[]),
65 (
67 "compiler/rustc_codegen_cranelift",
68 EXCEPTIONS_CRANELIFT,
69 Some((&["rustc_codegen_cranelift"], PERMITTED_CRANELIFT_DEPENDENCIES)),
70 &[],
71 ),
72 ("compiler/rustc_codegen_gcc", EXCEPTIONS_GCC, None, &[]),
74 ("src/bootstrap", EXCEPTIONS_BOOTSTRAP, None, &[]),
75 ("src/ci/docker/host-x86_64/test-various/uefi_qemu_test", EXCEPTIONS_UEFI_QEMU_TEST, None, &[]),
76 ("src/etc/test-float-parse", EXCEPTIONS, None, &[]),
77 ("src/tools/cargo", EXCEPTIONS_CARGO, None, &["src/tools/cargo"]),
78 ("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/x", &[], None, &[]),
84 ];
86
87#[rustfmt::skip]
92const EXCEPTIONS: ExceptionList = &[
93 ("ar_archive_writer", "Apache-2.0 WITH LLVM-exception"), ("arrayref", "BSD-2-Clause"), ("blake3", "CC0-1.0 OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception"), ("colored", "MPL-2.0"), ("constant_time_eq", "CC0-1.0 OR MIT-0 OR Apache-2.0"), ("dissimilar", "Apache-2.0"), ("fluent-langneg", "Apache-2.0"), ("foldhash", "Zlib"), ("mdbook", "MPL-2.0"), ("option-ext", "MPL-2.0"), ("rustc_apfloat", "Apache-2.0 WITH LLVM-exception"), ("ryu", "Apache-2.0 OR BSL-1.0"), ("self_cell", "Apache-2.0"), ("wasi-preview1-component-adapter-provider", "Apache-2.0 WITH LLVM-exception"), ];
110
111#[rustfmt::skip]
116const EXCEPTIONS_STDLIB: ExceptionList = &[
117 ("fortanix-sgx-abi", "MPL-2.0"), ];
121
122const EXCEPTIONS_CARGO: ExceptionList = &[
123 ("arrayref", "BSD-2-Clause"),
125 ("bitmaps", "MPL-2.0+"),
126 ("blake3", "CC0-1.0 OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception"),
127 ("bytesize", "Apache-2.0"),
128 ("ciborium", "Apache-2.0"),
129 ("ciborium-io", "Apache-2.0"),
130 ("ciborium-ll", "Apache-2.0"),
131 ("constant_time_eq", "CC0-1.0 OR MIT-0 OR Apache-2.0"),
132 ("dunce", "CC0-1.0 OR MIT-0 OR Apache-2.0"),
133 ("encoding_rs", "(Apache-2.0 OR MIT) AND BSD-3-Clause"),
134 ("fiat-crypto", "MIT OR Apache-2.0 OR BSD-1-Clause"),
135 ("foldhash", "Zlib"),
136 ("im-rc", "MPL-2.0+"),
137 ("normalize-line-endings", "Apache-2.0"),
138 ("openssl", "Apache-2.0"),
139 ("ryu", "Apache-2.0 OR BSL-1.0"), ("sha1_smol", "BSD-3-Clause"),
141 ("similar", "Apache-2.0"),
142 ("sized-chunks", "MPL-2.0+"),
143 ("subtle", "BSD-3-Clause"),
144 ("supports-hyperlinks", "Apache-2.0"),
145 ("unicode-bom", "Apache-2.0"),
146 ];
148
149const EXCEPTIONS_RUST_ANALYZER: ExceptionList = &[
150 ("dissimilar", "Apache-2.0"),
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"), ("scip", "Apache-2.0"),
158 ];
160
161const EXCEPTIONS_RUSTC_PERF: ExceptionList = &[
162 ("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 ("instant", "BSD-3-Clause"),
170 ("ring", NON_STANDARD_LICENSE), ("ryu", "Apache-2.0 OR BSL-1.0"),
172 ("snap", "BSD-3-Clause"),
173 ("subtle", "BSD-3-Clause"),
174 ];
176
177const EXCEPTIONS_RUSTBOOK: ExceptionList = &[
178 ("mdbook", "MPL-2.0"),
180 ("ryu", "Apache-2.0 OR BSL-1.0"),
181 ];
183
184const EXCEPTIONS_CRANELIFT: ExceptionList = &[
185 ("cranelift-bforest", "Apache-2.0 WITH LLVM-exception"),
187 ("cranelift-bitset", "Apache-2.0 WITH LLVM-exception"),
188 ("cranelift-codegen", "Apache-2.0 WITH LLVM-exception"),
189 ("cranelift-codegen-meta", "Apache-2.0 WITH LLVM-exception"),
190 ("cranelift-codegen-shared", "Apache-2.0 WITH LLVM-exception"),
191 ("cranelift-control", "Apache-2.0 WITH LLVM-exception"),
192 ("cranelift-entity", "Apache-2.0 WITH LLVM-exception"),
193 ("cranelift-frontend", "Apache-2.0 WITH LLVM-exception"),
194 ("cranelift-isle", "Apache-2.0 WITH LLVM-exception"),
195 ("cranelift-jit", "Apache-2.0 WITH LLVM-exception"),
196 ("cranelift-module", "Apache-2.0 WITH LLVM-exception"),
197 ("cranelift-native", "Apache-2.0 WITH LLVM-exception"),
198 ("cranelift-object", "Apache-2.0 WITH LLVM-exception"),
199 ("foldhash", "Zlib"),
200 ("mach2", "BSD-2-Clause OR MIT OR Apache-2.0"),
201 ("regalloc2", "Apache-2.0 WITH LLVM-exception"),
202 ("target-lexicon", "Apache-2.0 WITH LLVM-exception"),
203 ("wasmtime-jit-icache-coherence", "Apache-2.0 WITH LLVM-exception"),
204 ];
206
207const EXCEPTIONS_GCC: ExceptionList = &[
208 ("gccjit", "GPL-3.0"),
210 ("gccjit_sys", "GPL-3.0"),
211 ];
213
214const EXCEPTIONS_BOOTSTRAP: ExceptionList = &[
215 ("ryu", "Apache-2.0 OR BSL-1.0"), ];
217
218const EXCEPTIONS_UEFI_QEMU_TEST: ExceptionList = &[
219 ("r-efi", "MIT OR Apache-2.0 OR LGPL-2.1-or-later"), ];
221
222const NON_STANDARD_LICENSE: &str = "NON_STANDARD_LICENSE";
224
225const EXCEPTIONS_NON_STANDARD_LICENSE_DEPS: &[&str] = &[
227 "ring",
234];
235
236const PERMITTED_DEPS_LOCATION: &str = concat!(file!(), ":", line!());
237
238const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
243 "adler2",
245 "ahash",
246 "aho-corasick",
247 "allocator-api2", "annotate-snippets",
249 "anstyle",
250 "ar_archive_writer",
251 "arrayref",
252 "arrayvec",
253 "autocfg",
254 "bitflags",
255 "blake3",
256 "block-buffer",
257 "bstr",
258 "byteorder", "cc",
260 "cfg-if",
261 "cfg_aliases",
262 "constant_time_eq",
263 "cpufeatures",
264 "crc32fast",
265 "crossbeam-channel",
266 "crossbeam-deque",
267 "crossbeam-epoch",
268 "crossbeam-utils",
269 "crypto-common",
270 "ctrlc",
271 "darling",
272 "darling_core",
273 "darling_macro",
274 "datafrog",
275 "deranged",
276 "derive-where",
277 "derive_setters",
278 "digest",
279 "displaydoc",
280 "dissimilar",
281 "either",
282 "elsa",
283 "ena",
284 "equivalent",
285 "errno",
286 "expect-test",
287 "fallible-iterator", "fastrand",
289 "flate2",
290 "fluent-bundle",
291 "fluent-langneg",
292 "fluent-syntax",
293 "fnv",
294 "foldhash",
295 "generic-array",
296 "getopts",
297 "getrandom",
298 "gimli",
299 "gsgdt",
300 "hashbrown",
301 "hermit-abi",
302 "icu_list",
303 "icu_list_data",
304 "icu_locid",
305 "icu_locid_transform",
306 "icu_locid_transform_data",
307 "icu_provider",
308 "icu_provider_adapters",
309 "icu_provider_macros",
310 "ident_case",
311 "indexmap",
312 "intl-memoizer",
313 "intl_pluralrules",
314 "itertools",
315 "itoa",
316 "jobserver",
317 "lazy_static",
318 "leb128",
319 "libc",
320 "libloading",
321 "linux-raw-sys",
322 "litemap",
323 "lock_api",
324 "log",
325 "matchers",
326 "md-5",
327 "measureme",
328 "memchr",
329 "memmap2",
330 "miniz_oxide",
331 "nix",
332 "nu-ansi-term",
333 "num-conv",
334 "num_cpus",
335 "object",
336 "odht",
337 "once_cell",
338 "overload",
339 "parking_lot",
340 "parking_lot_core",
341 "pathdiff",
342 "perf-event-open-sys",
343 "pin-project-lite",
344 "polonius-engine",
345 "portable-atomic", "powerfmt",
347 "ppv-lite86",
348 "proc-macro-hack",
349 "proc-macro2",
350 "psm",
351 "pulldown-cmark",
352 "pulldown-cmark-escape",
353 "punycode",
354 "quote",
355 "rand",
356 "rand_chacha",
357 "rand_core",
358 "rand_xoshiro",
359 "redox_syscall",
360 "regex",
361 "regex-automata",
362 "regex-syntax",
363 "rustc-demangle",
364 "rustc-hash",
365 "rustc-rayon",
366 "rustc-rayon-core",
367 "rustc-stable-hash",
368 "rustc_apfloat",
369 "rustix",
370 "ruzstd", "ryu",
372 "scoped-tls",
373 "scopeguard",
374 "self_cell",
375 "serde",
376 "serde_derive",
377 "serde_json",
378 "sha1",
379 "sha2",
380 "sharded-slab",
381 "shlex",
382 "smallvec",
383 "stable_deref_trait",
384 "stacker",
385 "static_assertions",
386 "strsim",
387 "syn",
388 "synstructure",
389 "tempfile",
390 "termcolor",
391 "termize",
392 "thin-vec",
393 "thiserror",
394 "thiserror-impl",
395 "thorin-dwp",
396 "thread_local",
397 "tikv-jemalloc-sys",
398 "time",
399 "time-core",
400 "time-macros",
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-core",
436 "windows-implement",
437 "windows-interface",
438 "windows-result",
439 "windows-strings",
440 "windows-sys",
441 "windows-targets",
442 "windows_aarch64_gnullvm",
443 "windows_aarch64_msvc",
444 "windows_i686_gnu",
445 "windows_i686_gnullvm",
446 "windows_i686_msvc",
447 "windows_x86_64_gnu",
448 "windows_x86_64_gnullvm",
449 "windows_x86_64_msvc",
450 "writeable",
451 "yoke",
452 "yoke-derive",
453 "zerocopy",
454 "zerocopy-derive",
455 "zerofrom",
456 "zerofrom-derive",
457 "zerovec",
458 "zerovec-derive",
459 ];
461
462const PERMITTED_STDLIB_DEPENDENCIES: &[&str] = &[
463 "addr2line",
465 "adler2",
466 "allocator-api2",
467 "cc",
468 "cfg-if",
469 "compiler_builtins",
470 "dlmalloc",
471 "fortanix-sgx-abi",
472 "getopts",
473 "gimli",
474 "hashbrown",
475 "hermit-abi",
476 "libc",
477 "memchr",
478 "miniz_oxide",
479 "object",
480 "proc-macro2",
481 "quote",
482 "r-efi",
483 "r-efi-alloc",
484 "rand",
485 "rand_core",
486 "rand_xorshift",
487 "rustc-demangle",
488 "shlex",
489 "syn",
490 "unicode-ident",
491 "unicode-width",
492 "unwinding",
493 "wasi",
494 "windows-sys",
495 "windows-targets",
496 "windows_aarch64_gnullvm",
497 "windows_aarch64_msvc",
498 "windows_i686_gnu",
499 "windows_i686_gnullvm",
500 "windows_i686_msvc",
501 "windows_x86_64_gnu",
502 "windows_x86_64_gnullvm",
503 "windows_x86_64_msvc",
504 "zerocopy",
505 "zerocopy-derive",
506 ];
508
509const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
510 "allocator-api2",
512 "anyhow",
513 "arbitrary",
514 "bitflags",
515 "bumpalo",
516 "cfg-if",
517 "cranelift-bforest",
518 "cranelift-bitset",
519 "cranelift-codegen",
520 "cranelift-codegen-meta",
521 "cranelift-codegen-shared",
522 "cranelift-control",
523 "cranelift-entity",
524 "cranelift-frontend",
525 "cranelift-isle",
526 "cranelift-jit",
527 "cranelift-module",
528 "cranelift-native",
529 "cranelift-object",
530 "crc32fast",
531 "equivalent",
532 "fallible-iterator",
533 "foldhash",
534 "gimli",
535 "hashbrown",
536 "indexmap",
537 "libc",
538 "libloading",
539 "log",
540 "mach2",
541 "memchr",
542 "object",
543 "proc-macro2",
544 "quote",
545 "regalloc2",
546 "region",
547 "rustc-hash",
548 "serde",
549 "serde_derive",
550 "smallvec",
551 "stable_deref_trait",
552 "syn",
553 "target-lexicon",
554 "unicode-ident",
555 "wasmtime-jit-icache-coherence",
556 "windows-sys",
557 "windows-targets",
558 "windows_aarch64_gnullvm",
559 "windows_aarch64_msvc",
560 "windows_i686_gnu",
561 "windows_i686_gnullvm",
562 "windows_i686_msvc",
563 "windows_x86_64_gnu",
564 "windows_x86_64_gnullvm",
565 "windows_x86_64_msvc",
566 ];
568
569pub fn check(root: &Path, cargo: &Path, bless: bool, bad: &mut bool) {
574 let mut checked_runtime_licenses = false;
575
576 check_proc_macro_dep_list(root, cargo, bless, bad);
577
578 for &(workspace, exceptions, permitted_deps, submodules) in WORKSPACES {
579 if has_missing_submodule(root, submodules) {
580 continue;
581 }
582
583 if !root.join(workspace).join("Cargo.lock").exists() {
584 tidy_error!(bad, "the `{workspace}` workspace doesn't have a Cargo.lock");
585 continue;
586 }
587
588 let mut cmd = cargo_metadata::MetadataCommand::new();
589 cmd.cargo_path(cargo)
590 .manifest_path(root.join(workspace).join("Cargo.toml"))
591 .features(cargo_metadata::CargoOpt::AllFeatures)
592 .other_options(vec!["--locked".to_owned()]);
593 let metadata = t!(cmd.exec());
594
595 check_license_exceptions(&metadata, exceptions, bad);
596 if let Some((crates, permitted_deps)) = permitted_deps {
597 check_permitted_dependencies(&metadata, workspace, permitted_deps, crates, bad);
598 }
599
600 if workspace == "library" {
601 check_runtime_license_exceptions(&metadata, bad);
602 checked_runtime_licenses = true;
603 }
604 }
605
606 assert!(checked_runtime_licenses);
609}
610
611fn check_proc_macro_dep_list(root: &Path, cargo: &Path, bless: bool, bad: &mut bool) {
613 let mut cmd = cargo_metadata::MetadataCommand::new();
614 cmd.cargo_path(cargo)
615 .manifest_path(root.join("Cargo.toml"))
616 .features(cargo_metadata::CargoOpt::AllFeatures)
617 .other_options(vec!["--locked".to_owned()]);
618 let metadata = t!(cmd.exec());
619 let is_proc_macro_pkg = |pkg: &Package| pkg.targets.iter().any(|target| target.is_proc_macro());
620
621 let mut proc_macro_deps = HashSet::new();
622 for pkg in metadata.packages.iter().filter(|pkg| is_proc_macro_pkg(*pkg)) {
623 deps_of(&metadata, &pkg.id, &mut proc_macro_deps);
624 }
625 proc_macro_deps.retain(|pkg| !is_proc_macro_pkg(&metadata[pkg]));
627
628 let proc_macro_deps: HashSet<_> =
629 proc_macro_deps.into_iter().map(|dep| metadata[dep].name.clone()).collect();
630 let expected = proc_macro_deps::CRATES.iter().map(|s| s.to_string()).collect::<HashSet<_>>();
631
632 let needs_blessing = proc_macro_deps.difference(&expected).next().is_some()
633 || expected.difference(&proc_macro_deps).next().is_some();
634
635 if needs_blessing && bless {
636 let mut proc_macro_deps: Vec<_> = proc_macro_deps.into_iter().collect();
637 proc_macro_deps.sort();
638 let mut file = File::create(root.join("src/bootstrap/src/utils/proc_macro_deps.rs"))
639 .expect("`proc_macro_deps` should exist");
640 writeln!(
641 &mut file,
642 "/// Do not update manually - use `./x.py test tidy --bless`
643/// Holds all direct and indirect dependencies of proc-macro crates in tree.
644/// See <https://github.com/rust-lang/rust/issues/134863>
645pub static CRATES: &[&str] = &[
646 // tidy-alphabetical-start"
647 )
648 .unwrap();
649 for dep in proc_macro_deps {
650 writeln!(&mut file, " {dep:?},").unwrap();
651 }
652 writeln!(
653 &mut file,
654 " // tidy-alphabetical-end
655];"
656 )
657 .unwrap();
658 } else {
659 let old_bad = *bad;
660
661 for missing in proc_macro_deps.difference(&expected) {
662 tidy_error!(
663 bad,
664 "proc-macro crate dependency `{missing}` is not registered in `src/bootstrap/src/utils/proc_macro_deps.rs`",
665 );
666 }
667 for extra in expected.difference(&proc_macro_deps) {
668 tidy_error!(
669 bad,
670 "`{extra}` is not registered in `src/bootstrap/src/utils/proc_macro_deps.rs`, but is not a proc-macro crate dependency",
671 );
672 }
673 if *bad != old_bad {
674 eprintln!("Run `./x.py test tidy --bless` to regenerate the list");
675 }
676 }
677}
678
679pub fn has_missing_submodule(root: &Path, submodules: &[&str]) -> bool {
683 !CiEnv::is_ci()
684 && submodules.iter().any(|submodule| {
685 read_dir(root.join(submodule)).unwrap().next().is_none()
687 })
688}
689
690fn check_runtime_license_exceptions(metadata: &Metadata, bad: &mut bool) {
695 for pkg in &metadata.packages {
696 if pkg.source.is_none() {
697 continue;
699 }
700 let license = match &pkg.license {
701 Some(license) => license,
702 None => {
703 tidy_error!(bad, "dependency `{}` does not define a license expression", pkg.id);
704 continue;
705 }
706 };
707 if !LICENSES.contains(&license.as_str()) {
708 if pkg.name == "fortanix-sgx-abi" && pkg.license.as_deref() == Some("MPL-2.0") {
713 continue;
714 }
715
716 tidy_error!(bad, "invalid license `{}` in `{}`", license, pkg.id);
717 }
718 }
719}
720
721fn check_license_exceptions(metadata: &Metadata, exceptions: &[(&str, &str)], bad: &mut bool) {
725 for (name, license) in exceptions {
727 if !metadata.packages.iter().any(|p| p.name == *name) {
729 tidy_error!(
730 bad,
731 "could not find exception package `{}`\n\
732 Remove from EXCEPTIONS list if it is no longer used.",
733 name
734 );
735 }
736 for pkg in metadata.packages.iter().filter(|p| p.name == *name) {
738 match &pkg.license {
739 None => {
740 if *license == NON_STANDARD_LICENSE
741 && EXCEPTIONS_NON_STANDARD_LICENSE_DEPS.contains(&pkg.name.as_str())
742 {
743 continue;
744 }
745 tidy_error!(
746 bad,
747 "dependency exception `{}` does not declare a license expression",
748 pkg.id
749 );
750 }
751 Some(pkg_license) => {
752 if pkg_license.as_str() != *license {
753 println!("dependency exception `{name}` license has changed");
754 println!(" previously `{license}` now `{pkg_license}`");
755 println!(" update EXCEPTIONS for the new license");
756 *bad = true;
757 }
758 }
759 }
760 }
761 }
762
763 let exception_names: Vec<_> = exceptions.iter().map(|(name, _license)| *name).collect();
764
765 for pkg in &metadata.packages {
767 if pkg.source.is_none() {
768 continue;
770 }
771 if exception_names.contains(&pkg.name.as_str()) {
772 continue;
773 }
774 let license = match &pkg.license {
775 Some(license) => license,
776 None => {
777 tidy_error!(bad, "dependency `{}` does not define a license expression", pkg.id);
778 continue;
779 }
780 };
781 if !LICENSES.contains(&license.as_str()) {
782 tidy_error!(bad, "invalid license `{}` in `{}`", license, pkg.id);
783 }
784 }
785}
786
787fn check_permitted_dependencies(
792 metadata: &Metadata,
793 descr: &str,
794 permitted_dependencies: &[&'static str],
795 restricted_dependency_crates: &[&'static str],
796 bad: &mut bool,
797) {
798 let mut has_permitted_dep_error = false;
799 let mut deps = HashSet::new();
800 for to_check in restricted_dependency_crates {
801 let to_check = pkg_from_name(metadata, to_check);
802 deps_of(metadata, &to_check.id, &mut deps);
803 }
804
805 for permitted in permitted_dependencies {
807 if !deps.iter().any(|dep_id| &pkg_from_id(metadata, dep_id).name == permitted) {
808 tidy_error!(
809 bad,
810 "could not find allowed package `{permitted}`\n\
811 Remove from PERMITTED_DEPENDENCIES list if it is no longer used.",
812 );
813 has_permitted_dep_error = true;
814 }
815 }
816
817 let permitted_dependencies: HashSet<_> = permitted_dependencies.iter().cloned().collect();
819
820 for dep in deps {
821 let dep = pkg_from_id(metadata, dep);
822 if dep.source.is_some() && !permitted_dependencies.contains(dep.name.as_str()) {
824 tidy_error!(bad, "Dependency for {descr} not explicitly permitted: {}", dep.id);
825 has_permitted_dep_error = true;
826 }
827 }
828
829 if has_permitted_dep_error {
830 eprintln!("Go to `{PERMITTED_DEPS_LOCATION}` for the list.");
831 }
832}
833
834fn pkg_from_name<'a>(metadata: &'a Metadata, name: &'static str) -> &'a Package {
836 let mut i = metadata.packages.iter().filter(|p| p.name == name);
837 let result =
838 i.next().unwrap_or_else(|| panic!("could not find package `{name}` in package list"));
839 assert!(i.next().is_none(), "more than one package found for `{name}`");
840 result
841}
842
843fn pkg_from_id<'a>(metadata: &'a Metadata, id: &PackageId) -> &'a Package {
844 metadata.packages.iter().find(|p| &p.id == id).unwrap()
845}
846
847fn deps_of<'a>(metadata: &'a Metadata, pkg_id: &'a PackageId, result: &mut HashSet<&'a PackageId>) {
849 if !result.insert(pkg_id) {
850 return;
851 }
852 let node = metadata
853 .resolve
854 .as_ref()
855 .unwrap()
856 .nodes
857 .iter()
858 .find(|n| &n.id == pkg_id)
859 .unwrap_or_else(|| panic!("could not find `{pkg_id}` in resolve"));
860 for dep in &node.deps {
861 deps_of(metadata, &dep.pkg, result);
862 }
863}