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