1use 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#[rustfmt::skip]
18const LICENSES: &[&str] = &[
19 "(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",
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", "Apache-2.0",
29 "Apache-2.0/MIT",
30 "BSD-2-Clause OR Apache-2.0 OR MIT", "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)", "MIT OR Apache-2.0 OR LGPL-2.1-or-later", "MIT OR Apache-2.0 OR Zlib", "MIT OR Apache-2.0",
38 "MIT OR Zlib OR Apache-2.0", "MIT",
40 "MIT/Apache-2.0",
41 "Unicode-3.0", "Unicode-DFS-2016", "Unlicense OR MIT",
44 "Unlicense/MIT",
45 "Zlib OR Apache-2.0 OR MIT", ];
48
49type ExceptionList = &'static [(&'static str, &'static str)];
50
51pub(crate) const WORKSPACES: &[(&str, ExceptionList, Option<(&[&str], &[&str])>, &[&str])] = &[
63 (".", EXCEPTIONS, Some((&["rustc-main"], PERMITTED_RUSTC_DEPENDENCIES)), &[]),
65 ("library", EXCEPTIONS_STDLIB, Some((&["sysroot"], PERMITTED_STDLIB_DEPENDENCIES)), &[]),
66 (
68 "compiler/rustc_codegen_cranelift",
69 EXCEPTIONS_CRANELIFT,
70 Some((&["rustc_codegen_cranelift"], PERMITTED_CRANELIFT_DEPENDENCIES)),
71 &[],
72 ),
73 ("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/etc/test-float-parse", EXCEPTIONS, None, &[]),
78 ("src/tools/cargo", EXCEPTIONS_CARGO, None, &["src/tools/cargo"]),
79 ("src/tools/rust-analyzer", EXCEPTIONS_RUST_ANALYZER, None, &[]),
82 ("src/tools/rustbook", EXCEPTIONS_RUSTBOOK, None, &["src/doc/book", "src/doc/reference"]),
83 ("src/tools/rustc-perf", EXCEPTIONS_RUSTC_PERF, None, &["src/tools/rustc-perf"]),
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"), ("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"), ];
109
110#[rustfmt::skip]
115const EXCEPTIONS_STDLIB: ExceptionList = &[
116 ("fortanix-sgx-abi", "MPL-2.0"), ];
120
121const EXCEPTIONS_CARGO: ExceptionList = &[
122 ("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 ("normalize-line-endings", "Apache-2.0"),
136 ("openssl", "Apache-2.0"),
137 ("ryu", "Apache-2.0 OR BSL-1.0"), ("sha1_smol", "BSD-3-Clause"),
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 ];
146
147const EXCEPTIONS_RUST_ANALYZER: ExceptionList = &[
148 ("dissimilar", "Apache-2.0"),
150 ("notify", "CC0-1.0"),
151 ("option-ext", "MPL-2.0"),
152 ("pulldown-cmark-to-cmark", "Apache-2.0"),
153 ("rustc_apfloat", "Apache-2.0 WITH LLVM-exception"),
154 ("ryu", "Apache-2.0 OR BSL-1.0"), ("scip", "Apache-2.0"),
156 ];
158
159const EXCEPTIONS_RUSTC_PERF: ExceptionList = &[
160 ("alloc-no-stdlib", "BSD-3-Clause"),
162 ("alloc-stdlib", "BSD-3-Clause"),
163 ("brotli", "BSD-3-Clause/MIT"),
164 ("brotli-decompressor", "BSD-3-Clause/MIT"),
165 ("encoding_rs", "(Apache-2.0 OR MIT) AND BSD-3-Clause"),
166 ("inferno", "CDDL-1.0"),
167 ("instant", "BSD-3-Clause"),
168 ("ring", NON_STANDARD_LICENSE), ("ryu", "Apache-2.0 OR BSL-1.0"),
170 ("snap", "BSD-3-Clause"),
171 ("subtle", "BSD-3-Clause"),
172 ];
174
175const EXCEPTIONS_RUSTBOOK: ExceptionList = &[
176 ("mdbook", "MPL-2.0"),
178 ("ryu", "Apache-2.0 OR BSL-1.0"),
179 ];
181
182const EXCEPTIONS_CRANELIFT: ExceptionList = &[
183 ("cranelift-bforest", "Apache-2.0 WITH LLVM-exception"),
185 ("cranelift-bitset", "Apache-2.0 WITH LLVM-exception"),
186 ("cranelift-codegen", "Apache-2.0 WITH LLVM-exception"),
187 ("cranelift-codegen-meta", "Apache-2.0 WITH LLVM-exception"),
188 ("cranelift-codegen-shared", "Apache-2.0 WITH LLVM-exception"),
189 ("cranelift-control", "Apache-2.0 WITH LLVM-exception"),
190 ("cranelift-entity", "Apache-2.0 WITH LLVM-exception"),
191 ("cranelift-frontend", "Apache-2.0 WITH LLVM-exception"),
192 ("cranelift-isle", "Apache-2.0 WITH LLVM-exception"),
193 ("cranelift-jit", "Apache-2.0 WITH LLVM-exception"),
194 ("cranelift-module", "Apache-2.0 WITH LLVM-exception"),
195 ("cranelift-native", "Apache-2.0 WITH LLVM-exception"),
196 ("cranelift-object", "Apache-2.0 WITH LLVM-exception"),
197 ("foldhash", "Zlib"),
198 ("mach2", "BSD-2-Clause OR MIT OR Apache-2.0"),
199 ("regalloc2", "Apache-2.0 WITH LLVM-exception"),
200 ("target-lexicon", "Apache-2.0 WITH LLVM-exception"),
201 ("wasmtime-jit-icache-coherence", "Apache-2.0 WITH LLVM-exception"),
202 ];
204
205const EXCEPTIONS_GCC: ExceptionList = &[
206 ("gccjit", "GPL-3.0"),
208 ("gccjit_sys", "GPL-3.0"),
209 ];
211
212const EXCEPTIONS_BOOTSTRAP: ExceptionList = &[
213 ("ryu", "Apache-2.0 OR BSL-1.0"), ];
215
216const EXCEPTIONS_UEFI_QEMU_TEST: ExceptionList = &[
217 ("r-efi", "MIT OR Apache-2.0 OR LGPL-2.1-or-later"), ];
219
220const NON_STANDARD_LICENSE: &str = "NON_STANDARD_LICENSE";
222
223const EXCEPTIONS_NON_STANDARD_LICENSE_DEPS: &[&str] = &[
225 "ring",
232];
233
234const PERMITTED_DEPS_LOCATION: &str = concat!(file!(), ":", line!());
235
236const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
241 "adler2",
243 "aho-corasick",
244 "allocator-api2", "annotate-snippets",
246 "anstyle",
247 "ar_archive_writer",
248 "arrayref",
249 "arrayvec",
250 "autocfg",
251 "bitflags",
252 "blake3",
253 "block-buffer",
254 "bstr",
255 "byteorder", "cc",
257 "cfg-if",
258 "cfg_aliases",
259 "constant_time_eq",
260 "cpufeatures",
261 "crc32fast",
262 "crossbeam-channel",
263 "crossbeam-deque",
264 "crossbeam-epoch",
265 "crossbeam-utils",
266 "crypto-common",
267 "ctrlc",
268 "darling",
269 "darling_core",
270 "darling_macro",
271 "datafrog",
272 "deranged",
273 "derive-where",
274 "derive_setters",
275 "digest",
276 "displaydoc",
277 "dissimilar",
278 "either",
279 "elsa",
280 "ena",
281 "equivalent",
282 "errno",
283 "expect-test",
284 "fallible-iterator", "fastrand",
286 "flate2",
287 "fluent-bundle",
288 "fluent-langneg",
289 "fluent-syntax",
290 "fnv",
291 "foldhash",
292 "generic-array",
293 "getopts",
294 "getrandom",
295 "gimli",
296 "gsgdt",
297 "hashbrown",
298 "hermit-abi",
299 "icu_list",
300 "icu_list_data",
301 "icu_locid",
302 "icu_locid_transform",
303 "icu_locid_transform_data",
304 "icu_provider",
305 "icu_provider_adapters",
306 "icu_provider_macros",
307 "ident_case",
308 "indexmap",
309 "intl-memoizer",
310 "intl_pluralrules",
311 "itertools",
312 "itoa",
313 "jobserver",
314 "lazy_static",
315 "leb128",
316 "libc",
317 "libloading",
318 "linux-raw-sys",
319 "litemap",
320 "lock_api",
321 "log",
322 "matchers",
323 "md-5",
324 "measureme",
325 "memchr",
326 "memmap2",
327 "miniz_oxide",
328 "nix",
329 "nu-ansi-term",
330 "num-conv",
331 "num_cpus",
332 "object",
333 "odht",
334 "once_cell",
335 "overload",
336 "parking_lot",
337 "parking_lot_core",
338 "pathdiff",
339 "perf-event-open-sys",
340 "pin-project-lite",
341 "polonius-engine",
342 "portable-atomic", "powerfmt",
344 "ppv-lite86",
345 "proc-macro-hack",
346 "proc-macro2",
347 "psm",
348 "pulldown-cmark",
349 "pulldown-cmark-escape",
350 "punycode",
351 "quote",
352 "rand",
353 "rand_chacha",
354 "rand_core",
355 "rand_xoshiro",
356 "redox_syscall",
357 "regex",
358 "regex-automata",
359 "regex-syntax",
360 "rustc-demangle",
361 "rustc-hash",
362 "rustc-rayon",
363 "rustc-rayon-core",
364 "rustc-stable-hash",
365 "rustc_apfloat",
366 "rustix",
367 "ruzstd", "ryu",
369 "scoped-tls",
370 "scopeguard",
371 "self_cell",
372 "serde",
373 "serde_derive",
374 "serde_json",
375 "sha1",
376 "sha2",
377 "sharded-slab",
378 "shlex",
379 "smallvec",
380 "stable_deref_trait",
381 "stacker",
382 "static_assertions",
383 "strsim",
384 "syn",
385 "synstructure",
386 "tempfile",
387 "termcolor",
388 "termize",
389 "thin-vec",
390 "thiserror",
391 "thiserror-impl",
392 "thorin-dwp",
393 "thread_local",
394 "tikv-jemalloc-sys",
395 "time",
396 "time-core",
397 "time-macros",
398 "tinystr",
399 "tinyvec",
400 "tinyvec_macros",
401 "tracing",
402 "tracing-attributes",
403 "tracing-core",
404 "tracing-log",
405 "tracing-subscriber",
406 "tracing-tree",
407 "twox-hash",
408 "type-map",
409 "typenum",
410 "unic-langid",
411 "unic-langid-impl",
412 "unic-langid-macros",
413 "unic-langid-macros-impl",
414 "unicase",
415 "unicode-ident",
416 "unicode-normalization",
417 "unicode-properties",
418 "unicode-script",
419 "unicode-security",
420 "unicode-width",
421 "unicode-xid",
422 "valuable",
423 "version_check",
424 "wasi",
425 "wasm-encoder",
426 "wasmparser",
427 "winapi",
428 "winapi-i686-pc-windows-gnu",
429 "winapi-util",
430 "winapi-x86_64-pc-windows-gnu",
431 "windows",
432 "windows-core",
433 "windows-implement",
434 "windows-interface",
435 "windows-result",
436 "windows-strings",
437 "windows-sys",
438 "windows-targets",
439 "windows_aarch64_gnullvm",
440 "windows_aarch64_msvc",
441 "windows_i686_gnu",
442 "windows_i686_gnullvm",
443 "windows_i686_msvc",
444 "windows_x86_64_gnu",
445 "windows_x86_64_gnullvm",
446 "windows_x86_64_msvc",
447 "wit-bindgen-rt@0.33.0", "writeable",
449 "yoke",
450 "yoke-derive",
451 "zerocopy",
452 "zerocopy-derive",
453 "zerofrom",
454 "zerofrom-derive",
455 "zerovec",
456 "zerovec-derive",
457 ];
459
460const PERMITTED_STDLIB_DEPENDENCIES: &[&str] = &[
461 "addr2line",
463 "adler2",
464 "allocator-api2",
465 "cc",
466 "cfg-if",
467 "compiler_builtins",
468 "dlmalloc",
469 "fortanix-sgx-abi",
470 "getopts",
471 "gimli",
472 "hashbrown",
473 "hermit-abi",
474 "libc",
475 "memchr",
476 "miniz_oxide",
477 "object",
478 "proc-macro2",
479 "quote",
480 "r-efi",
481 "r-efi-alloc",
482 "rand",
483 "rand_core",
484 "rand_xorshift",
485 "rustc-demangle",
486 "shlex",
487 "syn",
488 "unicode-ident",
489 "unicode-width",
490 "unwinding",
491 "wasi",
492 "windows-sys",
493 "windows-targets",
494 "windows_aarch64_gnullvm",
495 "windows_aarch64_msvc",
496 "windows_i686_gnu",
497 "windows_i686_gnullvm",
498 "windows_i686_msvc",
499 "windows_x86_64_gnu",
500 "windows_x86_64_gnullvm",
501 "windows_x86_64_msvc",
502 "zerocopy",
503 "zerocopy-derive",
504 ];
506
507const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
508 "allocator-api2",
510 "anyhow",
511 "arbitrary",
512 "bitflags",
513 "bumpalo",
514 "cfg-if",
515 "cranelift-bforest",
516 "cranelift-bitset",
517 "cranelift-codegen",
518 "cranelift-codegen-meta",
519 "cranelift-codegen-shared",
520 "cranelift-control",
521 "cranelift-entity",
522 "cranelift-frontend",
523 "cranelift-isle",
524 "cranelift-jit",
525 "cranelift-module",
526 "cranelift-native",
527 "cranelift-object",
528 "crc32fast",
529 "equivalent",
530 "fallible-iterator",
531 "foldhash",
532 "gimli",
533 "hashbrown",
534 "indexmap",
535 "libc",
536 "libloading",
537 "log",
538 "mach2",
539 "memchr",
540 "object",
541 "proc-macro2",
542 "quote",
543 "regalloc2",
544 "region",
545 "rustc-hash",
546 "serde",
547 "serde_derive",
548 "smallvec",
549 "stable_deref_trait",
550 "syn",
551 "target-lexicon",
552 "unicode-ident",
553 "wasmtime-jit-icache-coherence",
554 "windows-sys",
555 "windows-targets",
556 "windows_aarch64_gnullvm",
557 "windows_aarch64_msvc",
558 "windows_i686_gnu",
559 "windows_i686_gnullvm",
560 "windows_i686_msvc",
561 "windows_x86_64_gnu",
562 "windows_x86_64_gnullvm",
563 "windows_x86_64_msvc",
564 ];
566
567pub fn check(root: &Path, cargo: &Path, bless: bool, bad: &mut bool) {
572 let mut checked_runtime_licenses = false;
573
574 check_proc_macro_dep_list(root, cargo, bless, bad);
575
576 for &(workspace, exceptions, permitted_deps, submodules) in WORKSPACES {
577 if has_missing_submodule(root, submodules) {
578 continue;
579 }
580
581 if !root.join(workspace).join("Cargo.lock").exists() {
582 tidy_error!(bad, "the `{workspace}` workspace doesn't have a Cargo.lock");
583 continue;
584 }
585
586 let mut cmd = cargo_metadata::MetadataCommand::new();
587 cmd.cargo_path(cargo)
588 .manifest_path(root.join(workspace).join("Cargo.toml"))
589 .features(cargo_metadata::CargoOpt::AllFeatures)
590 .other_options(vec!["--locked".to_owned()]);
591 let metadata = t!(cmd.exec());
592
593 check_license_exceptions(&metadata, exceptions, bad);
594 if let Some((crates, permitted_deps)) = permitted_deps {
595 check_permitted_dependencies(&metadata, workspace, permitted_deps, crates, bad);
596 }
597
598 if workspace == "library" {
599 check_runtime_license_exceptions(&metadata, bad);
600 checked_runtime_licenses = true;
601 }
602 }
603
604 assert!(checked_runtime_licenses);
607}
608
609fn check_proc_macro_dep_list(root: &Path, cargo: &Path, bless: bool, bad: &mut bool) {
611 let mut cmd = cargo_metadata::MetadataCommand::new();
612 cmd.cargo_path(cargo)
613 .manifest_path(root.join("Cargo.toml"))
614 .features(cargo_metadata::CargoOpt::AllFeatures)
615 .other_options(vec!["--locked".to_owned()]);
616 let metadata = t!(cmd.exec());
617 let is_proc_macro_pkg = |pkg: &Package| pkg.targets.iter().any(|target| target.is_proc_macro());
618
619 let mut proc_macro_deps = HashSet::new();
620 for pkg in metadata.packages.iter().filter(|pkg| is_proc_macro_pkg(*pkg)) {
621 deps_of(&metadata, &pkg.id, &mut proc_macro_deps);
622 }
623 proc_macro_deps.retain(|pkg| !is_proc_macro_pkg(&metadata[pkg]));
625
626 let proc_macro_deps: HashSet<_> =
627 proc_macro_deps.into_iter().map(|dep| metadata[dep].name.clone()).collect();
628 let expected = proc_macro_deps::CRATES.iter().map(|s| s.to_string()).collect::<HashSet<_>>();
629
630 let needs_blessing = proc_macro_deps.difference(&expected).next().is_some()
631 || expected.difference(&proc_macro_deps).next().is_some();
632
633 if needs_blessing && bless {
634 let mut proc_macro_deps: Vec<_> = proc_macro_deps.into_iter().collect();
635 proc_macro_deps.sort();
636 let mut file = File::create(root.join("src/bootstrap/src/utils/proc_macro_deps.rs"))
637 .expect("`proc_macro_deps` should exist");
638 writeln!(
639 &mut file,
640 "/// Do not update manually - use `./x.py test tidy --bless`
641/// Holds all direct and indirect dependencies of proc-macro crates in tree.
642/// See <https://github.com/rust-lang/rust/issues/134863>
643pub static CRATES: &[&str] = &[
644 // tidy-alphabetical-start"
645 )
646 .unwrap();
647 for dep in proc_macro_deps {
648 writeln!(&mut file, " {dep:?},").unwrap();
649 }
650 writeln!(
651 &mut file,
652 " // tidy-alphabetical-end
653];"
654 )
655 .unwrap();
656 } else {
657 let old_bad = *bad;
658
659 for missing in proc_macro_deps.difference(&expected) {
660 tidy_error!(
661 bad,
662 "proc-macro crate dependency `{missing}` is not registered in `src/bootstrap/src/utils/proc_macro_deps.rs`",
663 );
664 }
665 for extra in expected.difference(&proc_macro_deps) {
666 tidy_error!(
667 bad,
668 "`{extra}` is registered in `src/bootstrap/src/utils/proc_macro_deps.rs`, but is not a proc-macro crate dependency",
669 );
670 }
671 if *bad != old_bad {
672 eprintln!("Run `./x.py test tidy --bless` to regenerate the list");
673 }
674 }
675}
676
677pub fn has_missing_submodule(root: &Path, submodules: &[&str]) -> bool {
681 !CiEnv::is_ci()
682 && submodules.iter().any(|submodule| {
683 read_dir(root.join(submodule)).unwrap().next().is_none()
685 })
686}
687
688fn check_runtime_license_exceptions(metadata: &Metadata, bad: &mut bool) {
693 for pkg in &metadata.packages {
694 if pkg.source.is_none() {
695 continue;
697 }
698 let license = match &pkg.license {
699 Some(license) => license,
700 None => {
701 tidy_error!(bad, "dependency `{}` does not define a license expression", pkg.id);
702 continue;
703 }
704 };
705 if !LICENSES.contains(&license.as_str()) {
706 if pkg.name == "fortanix-sgx-abi" && pkg.license.as_deref() == Some("MPL-2.0") {
711 continue;
712 }
713
714 tidy_error!(bad, "invalid license `{}` in `{}`", license, pkg.id);
715 }
716 }
717}
718
719fn check_license_exceptions(metadata: &Metadata, exceptions: &[(&str, &str)], bad: &mut bool) {
723 for (name, license) in exceptions {
725 if !metadata.packages.iter().any(|p| p.name == *name) {
727 tidy_error!(
728 bad,
729 "could not find exception package `{}`\n\
730 Remove from EXCEPTIONS list if it is no longer used.",
731 name
732 );
733 }
734 for pkg in metadata.packages.iter().filter(|p| p.name == *name) {
736 match &pkg.license {
737 None => {
738 if *license == NON_STANDARD_LICENSE
739 && EXCEPTIONS_NON_STANDARD_LICENSE_DEPS.contains(&pkg.name.as_str())
740 {
741 continue;
742 }
743 tidy_error!(
744 bad,
745 "dependency exception `{}` does not declare a license expression",
746 pkg.id
747 );
748 }
749 Some(pkg_license) => {
750 if pkg_license.as_str() != *license {
751 println!("dependency exception `{name}` license has changed");
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 for pkg in &metadata.packages {
765 if pkg.source.is_none() {
766 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!(bad, "dependency `{}` does not define a license expression", pkg.id);
776 continue;
777 }
778 };
779 if !LICENSES.contains(&license.as_str()) {
780 tidy_error!(bad, "invalid license `{}` in `{}`", license, pkg.id);
781 }
782 }
783}
784
785fn check_permitted_dependencies(
790 metadata: &Metadata,
791 descr: &str,
792 permitted_dependencies: &[&'static str],
793 restricted_dependency_crates: &[&'static str],
794 bad: &mut bool,
795) {
796 let mut has_permitted_dep_error = false;
797 let mut deps = HashSet::new();
798 for to_check in restricted_dependency_crates {
799 let to_check = pkg_from_name(metadata, to_check);
800 deps_of(metadata, &to_check.id, &mut deps);
801 }
802
803 for permitted in permitted_dependencies {
805 fn compare(pkg: &Package, permitted: &str) -> bool {
806 if let Some((name, version)) = permitted.split_once("@") {
807 let Ok(version) = Version::parse(version) else {
808 return false;
809 };
810 pkg.name == name && pkg.version == version
811 } else {
812 pkg.name == permitted
813 }
814 }
815 if !deps.iter().any(|dep_id| compare(pkg_from_id(metadata, dep_id), permitted)) {
816 tidy_error!(
817 bad,
818 "could not find allowed package `{permitted}`\n\
819 Remove from PERMITTED_DEPENDENCIES list if it is no longer used.",
820 );
821 has_permitted_dep_error = true;
822 }
823 }
824
825 let permitted_dependencies: HashMap<_, _> = permitted_dependencies
827 .iter()
828 .map(|s| {
829 if let Some((name, version)) = s.split_once('@') {
830 (name, Version::parse(version).ok())
831 } else {
832 (*s, None)
833 }
834 })
835 .collect();
836
837 for dep in deps {
838 let dep = pkg_from_id(metadata, dep);
839 if dep.source.is_some() {
841 let is_eq = if let Some(version) = permitted_dependencies.get(dep.name.as_str()) {
842 if let Some(version) = version { version == &dep.version } else { true }
843 } else {
844 false
845 };
846 if !is_eq {
847 tidy_error!(bad, "Dependency for {descr} not explicitly permitted: {}", dep.id);
848 has_permitted_dep_error = true;
849 }
850 }
851 }
852
853 if has_permitted_dep_error {
854 eprintln!("Go to `{PERMITTED_DEPS_LOCATION}` for the list.");
855 }
856}
857
858fn pkg_from_name<'a>(metadata: &'a Metadata, name: &'static str) -> &'a Package {
860 let mut i = metadata.packages.iter().filter(|p| p.name == name);
861 let result =
862 i.next().unwrap_or_else(|| panic!("could not find package `{name}` in package list"));
863 assert!(i.next().is_none(), "more than one package found for `{name}`");
864 result
865}
866
867fn pkg_from_id<'a>(metadata: &'a Metadata, id: &PackageId) -> &'a Package {
868 metadata.packages.iter().find(|p| &p.id == id).unwrap()
869}
870
871fn deps_of<'a>(metadata: &'a Metadata, pkg_id: &'a PackageId, result: &mut HashSet<&'a PackageId>) {
873 if !result.insert(pkg_id) {
874 return;
875 }
876 let node = metadata
877 .resolve
878 .as_ref()
879 .unwrap()
880 .nodes
881 .iter()
882 .find(|n| &n.id == pkg_id)
883 .unwrap_or_else(|| panic!("could not find `{pkg_id}` in resolve"));
884 for dep in &node.deps {
885 deps_of(metadata, &dep.pkg, result);
886 }
887}