1use std::collections::{HashMap, HashSet};
4use std::fmt::{Display, Formatter};
5use std::fs::{File, read_dir};
6use std::io::Write;
7use std::path::Path;
8
9use build_helper::ci::CiEnv;
10use cargo_metadata::semver::Version;
11use cargo_metadata::{Metadata, Package, PackageId};
12
13use crate::diagnostics::{RunningCheck, TidyCtx};
14
15#[path = "../../../bootstrap/src/utils/proc_macro_deps.rs"]
16mod proc_macro_deps;
17
18#[derive(Clone, Copy)]
19struct ListLocation {
20 path: &'static str,
21 line: u32,
22}
23
24impl Display for ListLocation {
25 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26 write!(f, "{}:{}", self.path, self.line)
27 }
28}
29
30macro_rules! location {
32 (+ $offset:literal) => {
33 ListLocation { path: file!(), line: line!() + $offset }
34 };
35}
36
37#[rustfmt::skip]
40const LICENSES: &[&str] = &[
41 "0BSD OR MIT OR Apache-2.0", "Apache-2.0 / MIT",
44 "Apache-2.0 OR ISC OR MIT",
45 "Apache-2.0 OR MIT",
46 "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT", "Apache-2.0/MIT",
48 "BSD-2-Clause OR Apache-2.0 OR MIT", "BSD-2-Clause OR MIT OR Apache-2.0",
50 "BSD-3-Clause/MIT",
51 "CC0-1.0 OR MIT-0 OR Apache-2.0",
52 "ISC",
53 "MIT / Apache-2.0",
54 "MIT AND (MIT OR Apache-2.0)",
55 "MIT AND Apache-2.0 WITH LLVM-exception AND (MIT OR Apache-2.0)", "MIT OR Apache-2.0 OR BSD-1-Clause",
57 "MIT OR Apache-2.0 OR LGPL-2.1-or-later", "MIT OR Apache-2.0 OR Zlib", "MIT OR Apache-2.0",
60 "MIT OR Zlib OR Apache-2.0", "MIT",
62 "MIT/Apache-2.0",
63 "Unlicense OR MIT",
64 "Unlicense/MIT",
65 "Zlib", ];
68
69#[rustfmt::skip]
71const LICENSES_TOOLS: &[&str] = &[
72 "(Apache-2.0 OR MIT) AND BSD-3-Clause",
74 "(MIT OR Apache-2.0) AND Unicode-3.0", "(MIT OR Apache-2.0) AND Unicode-DFS-2016", "0BSD",
77 "Apache-2.0 AND ISC",
78 "Apache-2.0 OR BSL-1.0", "Apache-2.0 OR GPL-2.0-only",
80 "Apache-2.0 WITH LLVM-exception",
81 "Apache-2.0",
82 "BSD-2-Clause",
83 "BSD-3-Clause",
84 "CC0-1.0 OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception",
85 "CC0-1.0",
86 "Unicode-3.0", "Unicode-DFS-2016", "Zlib OR Apache-2.0 OR MIT", "Zlib",
90 ];
92
93type ExceptionList = &'static [(&'static str, &'static str)];
94
95#[derive(Clone, Copy)]
96pub(crate) struct WorkspaceInfo<'a> {
97 pub(crate) path: &'a str,
99 pub(crate) exceptions: ExceptionList,
101 crates_and_deps: Option<(&'a [&'a str], &'a [&'a str], ListLocation)>,
106 pub(crate) submodules: &'a [&'a str],
108}
109
110const WORKSPACE_LOCATION: ListLocation = location!(+4);
111
112pub(crate) const WORKSPACES: &[WorkspaceInfo<'static>] = &[
115 WorkspaceInfo {
117 path: ".",
118 exceptions: EXCEPTIONS,
119 crates_and_deps: Some((
120 &["rustc-main"],
121 PERMITTED_RUSTC_DEPENDENCIES,
122 PERMITTED_RUSTC_DEPS_LOCATION,
123 )),
124 submodules: &[],
125 },
126 WorkspaceInfo {
127 path: "library",
128 exceptions: EXCEPTIONS_STDLIB,
129 crates_and_deps: Some((
130 &["sysroot"],
131 PERMITTED_STDLIB_DEPENDENCIES,
132 PERMITTED_STDLIB_DEPS_LOCATION,
133 )),
134 submodules: &[],
135 },
136 WorkspaceInfo {
137 path: "compiler/rustc_codegen_cranelift",
138 exceptions: EXCEPTIONS_CRANELIFT,
139 crates_and_deps: Some((
140 &["rustc_codegen_cranelift"],
141 PERMITTED_CRANELIFT_DEPENDENCIES,
142 PERMITTED_CRANELIFT_DEPS_LOCATION,
143 )),
144 submodules: &[],
145 },
146 WorkspaceInfo {
147 path: "compiler/rustc_codegen_gcc",
148 exceptions: EXCEPTIONS_GCC,
149 crates_and_deps: None,
150 submodules: &[],
151 },
152 WorkspaceInfo {
153 path: "src/bootstrap",
154 exceptions: EXCEPTIONS_BOOTSTRAP,
155 crates_and_deps: None,
156 submodules: &[],
157 },
158 WorkspaceInfo {
159 path: "src/tools/cargo",
160 exceptions: EXCEPTIONS_CARGO,
161 crates_and_deps: None,
162 submodules: &["src/tools/cargo"],
163 },
164 WorkspaceInfo {
176 path: "src/tools/rust-analyzer",
177 exceptions: EXCEPTIONS_RUST_ANALYZER,
178 crates_and_deps: None,
179 submodules: &[],
180 },
181 WorkspaceInfo {
182 path: "src/tools/rustbook",
183 exceptions: EXCEPTIONS_RUSTBOOK,
184 crates_and_deps: None,
185 submodules: &["src/doc/book", "src/doc/reference"],
186 },
187 WorkspaceInfo {
188 path: "src/tools/rustc-perf",
189 exceptions: EXCEPTIONS_RUSTC_PERF,
190 crates_and_deps: None,
191 submodules: &["src/tools/rustc-perf"],
192 },
193 WorkspaceInfo {
194 path: "tests/run-make-cargo/uefi-qemu/uefi_qemu_test",
195 exceptions: EXCEPTIONS_UEFI_QEMU_TEST,
196 crates_and_deps: None,
197 submodules: &[],
198 },
199];
200
201#[rustfmt::skip]
206const EXCEPTIONS: ExceptionList = &[
207 ("colored", "MPL-2.0"), ("option-ext", "MPL-2.0"), ];
212
213#[rustfmt::skip]
218const EXCEPTIONS_STDLIB: ExceptionList = &[
219 ("fortanix-sgx-abi", "MPL-2.0"), ];
223
224const EXCEPTIONS_CARGO: ExceptionList = &[
225 ("bitmaps", "MPL-2.0+"),
227 ("im-rc", "MPL-2.0+"),
228 ("sized-chunks", "MPL-2.0+"),
229 ];
231
232const EXCEPTIONS_RUST_ANALYZER: ExceptionList = &[
233 ("option-ext", "MPL-2.0"),
235 ];
237
238const EXCEPTIONS_RUSTC_PERF: ExceptionList = &[
239 ("inferno", "CDDL-1.0"),
241 ("option-ext", "MPL-2.0"),
242 ];
244
245const EXCEPTIONS_RUSTBOOK: ExceptionList = &[
246 ("font-awesome-as-a-crate", "CC-BY-4.0 AND MIT"),
248 ("mdbook-core", "MPL-2.0"),
249 ("mdbook-driver", "MPL-2.0"),
250 ("mdbook-html", "MPL-2.0"),
251 ("mdbook-markdown", "MPL-2.0"),
252 ("mdbook-preprocessor", "MPL-2.0"),
253 ("mdbook-renderer", "MPL-2.0"),
254 ("mdbook-summary", "MPL-2.0"),
255 ];
257
258const EXCEPTIONS_CRANELIFT: ExceptionList = &[];
259
260const EXCEPTIONS_GCC: ExceptionList = &[
261 ("gccjit", "GPL-3.0"),
263 ("gccjit_sys", "GPL-3.0"),
264 ];
266
267const EXCEPTIONS_BOOTSTRAP: ExceptionList = &[];
268
269const EXCEPTIONS_UEFI_QEMU_TEST: ExceptionList = &[];
270
271const PERMITTED_RUSTC_DEPS_LOCATION: ListLocation = location!(+6);
272
273const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
278 "adler2",
280 "aho-corasick",
281 "allocator-api2", "annotate-snippets",
283 "anstream",
284 "anstyle",
285 "anstyle-parse",
286 "anstyle-query",
287 "anstyle-wincon",
288 "ar_archive_writer",
289 "arrayref",
290 "arrayvec",
291 "bitflags",
292 "blake3",
293 "block-buffer",
294 "block2",
295 "bstr",
296 "cc",
297 "cfg-if",
298 "cfg_aliases",
299 "colorchoice",
300 "constant_time_eq",
301 "cpufeatures",
302 "crc32fast",
303 "crossbeam-deque",
304 "crossbeam-epoch",
305 "crossbeam-utils",
306 "crypto-common",
307 "ctrlc",
308 "darling",
309 "darling_core",
310 "darling_macro",
311 "datafrog",
312 "derive-where",
313 "derive_setters",
314 "digest",
315 "dispatch2",
316 "displaydoc",
317 "dissimilar",
318 "dyn-clone",
319 "either",
320 "elsa",
321 "ena",
322 "equivalent",
323 "errno",
324 "expect-test",
325 "fallible-iterator", "fastrand",
327 "find-msvc-tools",
328 "flate2",
329 "fluent-bundle",
330 "fluent-langneg",
331 "fluent-syntax",
332 "fnv",
333 "foldhash",
334 "generic-array",
335 "getopts",
336 "getrandom",
337 "gimli",
338 "gsgdt",
339 "hashbrown",
340 "icu_collections",
341 "icu_list",
342 "icu_locale",
343 "icu_locale_core",
344 "icu_locale_data",
345 "icu_provider",
346 "ident_case",
347 "indexmap",
348 "intl-memoizer",
349 "intl_pluralrules",
350 "is_terminal_polyfill",
351 "itertools",
352 "itoa",
353 "jiff",
354 "jiff-static",
355 "jobserver",
356 "lazy_static",
357 "leb128",
358 "libc",
359 "libloading",
360 "linux-raw-sys",
361 "litemap",
362 "lock_api",
363 "log",
364 "matchers",
365 "md-5",
366 "measureme",
367 "memchr",
368 "memmap2",
369 "miniz_oxide",
370 "nix",
371 "nu-ansi-term",
372 "objc2",
373 "objc2-encode",
374 "object",
375 "odht",
376 "once_cell",
377 "once_cell_polyfill",
378 "parking_lot",
379 "parking_lot_core",
380 "pathdiff",
381 "perf-event-open-sys",
382 "pin-project-lite",
383 "polonius-engine",
384 "portable-atomic", "portable-atomic-util",
386 "potential_utf",
387 "ppv-lite86",
388 "proc-macro-hack",
389 "proc-macro2",
390 "psm",
391 "pulldown-cmark",
392 "pulldown-cmark-escape",
393 "punycode",
394 "quote",
395 "r-efi",
396 "rand",
397 "rand_chacha",
398 "rand_core",
399 "rand_xorshift", "rand_xoshiro",
401 "redox_syscall",
402 "ref-cast",
403 "ref-cast-impl",
404 "regex",
405 "regex-automata",
406 "regex-syntax",
407 "rustc-demangle",
408 "rustc-hash",
409 "rustc-literal-escaper",
410 "rustc-stable-hash",
411 "rustc_apfloat",
412 "rustix",
413 "ruzstd", "ryu",
415 "schemars",
416 "schemars_derive",
417 "scoped-tls",
418 "scopeguard",
419 "self_cell",
420 "serde",
421 "serde_core",
422 "serde_derive",
423 "serde_derive_internals",
424 "serde_json",
425 "serde_path_to_error",
426 "sha1",
427 "sha2",
428 "sharded-slab",
429 "shlex",
430 "simd-adler32",
431 "smallvec",
432 "stable_deref_trait",
433 "stacker",
434 "static_assertions",
435 "strsim",
436 "syn",
437 "synstructure",
438 "tempfile",
439 "termize",
440 "thin-vec",
441 "thiserror",
442 "thiserror-impl",
443 "thorin-dwp",
444 "thread_local",
445 "tikv-jemalloc-sys",
446 "tinystr",
447 "tinyvec",
448 "tinyvec_macros",
449 "tracing",
450 "tracing-attributes",
451 "tracing-core",
452 "tracing-log",
453 "tracing-serde",
454 "tracing-subscriber",
455 "tracing-tree",
456 "twox-hash",
457 "type-map",
458 "typenum",
459 "unic-langid",
460 "unic-langid-impl",
461 "unic-langid-macros",
462 "unic-langid-macros-impl",
463 "unicase",
464 "unicode-ident",
465 "unicode-normalization",
466 "unicode-properties",
467 "unicode-script",
468 "unicode-security",
469 "unicode-width",
470 "utf8parse",
471 "valuable",
472 "version_check",
473 "wasi",
474 "wasm-encoder",
475 "wasmparser",
476 "windows",
477 "windows-collections",
478 "windows-core",
479 "windows-future",
480 "windows-implement",
481 "windows-interface",
482 "windows-link",
483 "windows-numerics",
484 "windows-result",
485 "windows-strings",
486 "windows-sys",
487 "windows-targets",
488 "windows-threading",
489 "windows_aarch64_gnullvm",
490 "windows_aarch64_msvc",
491 "windows_i686_gnu",
492 "windows_i686_gnullvm",
493 "windows_i686_msvc",
494 "windows_x86_64_gnu",
495 "windows_x86_64_gnullvm",
496 "windows_x86_64_msvc",
497 "wit-bindgen-rt@0.39.0", "writeable",
499 "yoke",
500 "yoke-derive",
501 "zerocopy",
502 "zerocopy-derive",
503 "zerofrom",
504 "zerofrom-derive",
505 "zerotrie",
506 "zerovec",
507 "zerovec-derive",
508 ];
510
511const PERMITTED_STDLIB_DEPS_LOCATION: ListLocation = location!(+2);
512
513const PERMITTED_STDLIB_DEPENDENCIES: &[&str] = &[
514 "addr2line",
516 "adler2",
517 "cc",
518 "cfg-if",
519 "compiler_builtins",
520 "dlmalloc",
521 "foldhash", "fortanix-sgx-abi",
523 "getopts",
524 "gimli",
525 "hashbrown",
526 "hermit-abi",
527 "libc",
528 "memchr",
529 "miniz_oxide",
530 "moto-rt",
531 "object",
532 "r-efi",
533 "r-efi-alloc",
534 "rand",
535 "rand_core",
536 "rand_xorshift",
537 "rustc-demangle",
538 "rustc-literal-escaper",
539 "shlex",
540 "unwinding",
541 "vex-sdk",
542 "wasi",
543 "windows-link",
544 "windows-sys",
545 "windows-targets",
546 "windows_aarch64_gnullvm",
547 "windows_aarch64_msvc",
548 "windows_i686_gnu",
549 "windows_i686_gnullvm",
550 "windows_i686_msvc",
551 "windows_x86_64_gnu",
552 "windows_x86_64_gnullvm",
553 "windows_x86_64_msvc",
554 "wit-bindgen",
555 ];
557
558const PERMITTED_CRANELIFT_DEPS_LOCATION: ListLocation = location!(+2);
559
560const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
561 "allocator-api2",
563 "anyhow",
564 "arbitrary",
565 "bitflags",
566 "bumpalo",
567 "cfg-if",
568 "cranelift-assembler-x64",
569 "cranelift-assembler-x64-meta",
570 "cranelift-bforest",
571 "cranelift-bitset",
572 "cranelift-codegen",
573 "cranelift-codegen-meta",
574 "cranelift-codegen-shared",
575 "cranelift-control",
576 "cranelift-entity",
577 "cranelift-frontend",
578 "cranelift-isle",
579 "cranelift-jit",
580 "cranelift-module",
581 "cranelift-native",
582 "cranelift-object",
583 "cranelift-srcgen",
584 "crc32fast",
585 "equivalent",
586 "fallible-iterator",
587 "foldhash",
588 "gimli",
589 "hashbrown",
590 "heck",
591 "indexmap",
592 "libc",
593 "libloading",
594 "libm",
595 "log",
596 "mach2",
597 "memchr",
598 "object",
599 "proc-macro2",
600 "quote",
601 "regalloc2",
602 "region",
603 "rustc-hash",
604 "serde",
605 "serde_core",
606 "serde_derive",
607 "smallvec",
608 "stable_deref_trait",
609 "syn",
610 "target-lexicon",
611 "unicode-ident",
612 "wasmtime-internal-jit-icache-coherence",
613 "wasmtime-internal-math",
614 "windows-link",
615 "windows-sys",
616 "windows-targets",
617 "windows_aarch64_gnullvm",
618 "windows_aarch64_msvc",
619 "windows_i686_gnu",
620 "windows_i686_gnullvm",
621 "windows_i686_msvc",
622 "windows_x86_64_gnu",
623 "windows_x86_64_gnullvm",
624 "windows_x86_64_msvc",
625 ];
627
628pub fn check(root: &Path, cargo: &Path, tidy_ctx: TidyCtx) {
633 let mut check = tidy_ctx.start_check("deps");
634 let bless = tidy_ctx.is_bless_enabled();
635
636 let mut checked_runtime_licenses = false;
637
638 check_proc_macro_dep_list(root, cargo, bless, &mut check);
639
640 for &WorkspaceInfo { path, exceptions, crates_and_deps, submodules } in WORKSPACES {
641 if has_missing_submodule(root, submodules) {
642 continue;
643 }
644
645 if !root.join(path).join("Cargo.lock").exists() {
646 check.error(format!("the `{path}` workspace doesn't have a Cargo.lock"));
647 continue;
648 }
649
650 let mut cmd = cargo_metadata::MetadataCommand::new();
651 cmd.cargo_path(cargo)
652 .manifest_path(root.join(path).join("Cargo.toml"))
653 .features(cargo_metadata::CargoOpt::AllFeatures)
654 .other_options(vec!["--locked".to_owned()]);
655 let metadata = t!(cmd.exec());
656
657 let absolute_root =
659 if path == "." { root.to_path_buf() } else { t!(std::path::absolute(root.join(path))) };
660 let absolute_root_real = t!(std::path::absolute(&metadata.workspace_root));
661 if absolute_root_real != absolute_root {
662 check.error(format!("{path} is part of another workspace ({} != {}), remove from `WORKSPACES` ({WORKSPACE_LOCATION})", absolute_root.display(), absolute_root_real.display()));
663 }
664 check_license_exceptions(&metadata, path, exceptions, &mut check);
665 if let Some((crates, permitted_deps, location)) = crates_and_deps {
666 let descr = crates.get(0).unwrap_or(&path);
667 check_permitted_dependencies(
668 &metadata,
669 descr,
670 permitted_deps,
671 crates,
672 location,
673 &mut check,
674 );
675 }
676
677 if path == "library" {
678 check_runtime_license_exceptions(&metadata, &mut check);
679 check_runtime_no_duplicate_dependencies(&metadata, &mut check);
680 check_runtime_no_proc_macros(&metadata, &mut check);
681 checked_runtime_licenses = true;
682 }
683 }
684
685 assert!(checked_runtime_licenses);
688}
689
690fn check_proc_macro_dep_list(root: &Path, cargo: &Path, bless: bool, check: &mut RunningCheck) {
692 let mut cmd = cargo_metadata::MetadataCommand::new();
693 cmd.cargo_path(cargo)
694 .manifest_path(root.join("Cargo.toml"))
695 .features(cargo_metadata::CargoOpt::AllFeatures)
696 .other_options(vec!["--locked".to_owned()]);
697 let metadata = t!(cmd.exec());
698 let is_proc_macro_pkg = |pkg: &Package| pkg.targets.iter().any(|target| target.is_proc_macro());
699
700 let mut proc_macro_deps = HashSet::new();
701 for pkg in metadata.packages.iter().filter(|pkg| is_proc_macro_pkg(pkg)) {
702 deps_of(&metadata, &pkg.id, &mut proc_macro_deps);
703 }
704 proc_macro_deps.retain(|pkg| !is_proc_macro_pkg(&metadata[pkg]));
706
707 let proc_macro_deps: HashSet<_> =
708 proc_macro_deps.into_iter().map(|dep| metadata[dep].name.as_ref()).collect();
709 let expected = proc_macro_deps::CRATES.iter().copied().collect::<HashSet<_>>();
710
711 let needs_blessing = proc_macro_deps.difference(&expected).next().is_some()
712 || expected.difference(&proc_macro_deps).next().is_some();
713
714 if needs_blessing && bless {
715 let mut proc_macro_deps: Vec<_> = proc_macro_deps.into_iter().collect();
716 proc_macro_deps.sort();
717 let mut file = File::create(root.join("src/bootstrap/src/utils/proc_macro_deps.rs"))
718 .expect("`proc_macro_deps` should exist");
719 writeln!(
720 &mut file,
721 "/// Do not update manually - use `./x.py test tidy --bless`
722/// Holds all direct and indirect dependencies of proc-macro crates in tree.
723/// See <https://github.com/rust-lang/rust/issues/134863>
724pub static CRATES: &[&str] = &[
725 // tidy-alphabetical-start"
726 )
727 .unwrap();
728 for dep in proc_macro_deps {
729 writeln!(&mut file, " {dep:?},").unwrap();
730 }
731 writeln!(
732 &mut file,
733 " // tidy-alphabetical-end
734];"
735 )
736 .unwrap();
737 } else {
738 let mut error_found = false;
739
740 for missing in proc_macro_deps.difference(&expected) {
741 error_found = true;
742 check.error(format!(
743 "proc-macro crate dependency `{missing}` is not registered in `src/bootstrap/src/utils/proc_macro_deps.rs`",
744 ));
745 }
746 for extra in expected.difference(&proc_macro_deps) {
747 error_found = true;
748 check.error(format!(
749 "`{extra}` is registered in `src/bootstrap/src/utils/proc_macro_deps.rs`, but is not a proc-macro crate dependency",
750 ));
751 }
752 if error_found {
753 check.message("Run `./x.py test tidy --bless` to regenerate the list");
754 }
755 }
756}
757
758pub fn has_missing_submodule(root: &Path, submodules: &[&str]) -> bool {
762 !CiEnv::is_ci()
763 && submodules.iter().any(|submodule| {
764 let path = root.join(submodule);
765 !path.exists()
766 || read_dir(path).unwrap().next().is_none()
768 })
769}
770
771fn check_runtime_license_exceptions(metadata: &Metadata, check: &mut RunningCheck) {
776 for pkg in &metadata.packages {
777 if pkg.source.is_none() {
778 continue;
780 }
781 let license = match &pkg.license {
782 Some(license) => license,
783 None => {
784 check
785 .error(format!("dependency `{}` does not define a license expression", pkg.id));
786 continue;
787 }
788 };
789 if !LICENSES.contains(&license.as_str()) {
790 if *pkg.name == "fortanix-sgx-abi" && pkg.license.as_deref() == Some("MPL-2.0") {
795 continue;
796 }
797
798 check.error(format!("invalid license `{}` in `{}`", license, pkg.id));
799 }
800 }
801}
802
803fn check_license_exceptions(
807 metadata: &Metadata,
808 workspace: &str,
809 exceptions: &[(&str, &str)],
810 check: &mut RunningCheck,
811) {
812 for (name, license) in exceptions {
814 if !metadata.packages.iter().any(|p| *p.name == *name) {
816 check.error(format!(
817 "could not find exception package `{name}` in workspace `{workspace}`\n\
818 Remove from EXCEPTIONS list if it is no longer used.",
819 ));
820 }
821 for pkg in metadata.packages.iter().filter(|p| *p.name == *name) {
823 match &pkg.license {
824 None => {
825 check.error(format!(
826 "dependency exception `{}` in workspace `{workspace}` does not declare a license expression",
827 pkg.id
828 ));
829 }
830 Some(pkg_license) => {
831 if pkg_license.as_str() != *license {
832 check.error(format!(r#"dependency exception `{name}` license in workspace `{workspace}` has changed
833 previously `{license}` now `{pkg_license}`
834 update EXCEPTIONS for the new license
835"#));
836 }
837 }
838 }
839 }
840 if LICENSES.contains(license) || LICENSES_TOOLS.contains(license) {
841 check.error(format!(
842 "dependency exception `{name}` is not necessary. `{license}` is an allowed license"
843 ));
844 }
845 }
846
847 let exception_names: Vec<_> = exceptions.iter().map(|(name, _license)| *name).collect();
848
849 for pkg in &metadata.packages {
851 if pkg.source.is_none() {
852 continue;
854 }
855 if exception_names.contains(&pkg.name.as_str()) {
856 continue;
857 }
858 let license = match &pkg.license {
859 Some(license) => license,
860 None => {
861 check.error(format!(
862 "dependency `{}` in workspace `{workspace}` does not define a license expression",
863 pkg.id
864 ));
865 continue;
866 }
867 };
868 if !LICENSES.contains(&license.as_str()) && !LICENSES_TOOLS.contains(&license.as_str()) {
869 check.error(format!(
870 "invalid license `{}` for package `{}` in workspace `{workspace}`",
871 license, pkg.id
872 ));
873 }
874 }
875}
876
877fn check_runtime_no_duplicate_dependencies(metadata: &Metadata, check: &mut RunningCheck) {
878 let mut seen_pkgs = HashSet::new();
879 for pkg in &metadata.packages {
880 if pkg.source.is_none() {
881 continue;
882 }
883
884 if pkg.name.to_string() != "wasi" && !seen_pkgs.insert(&*pkg.name) {
888 check.error(format!(
889 "duplicate package `{}` is not allowed for the standard library",
890 pkg.name
891 ));
892 }
893 }
894}
895
896fn check_runtime_no_proc_macros(metadata: &Metadata, check: &mut RunningCheck) {
897 for pkg in &metadata.packages {
898 if pkg.targets.iter().any(|target| target.is_proc_macro()) {
899 check.error(format!(
900 "proc macro `{}` is not allowed as standard library dependency.\n\
901 Using proc macros in the standard library would break cross-compilation \
902 as proc-macros don't get shipped for the host tuple.",
903 pkg.name
904 ));
905 }
906 }
907}
908
909fn check_permitted_dependencies(
914 metadata: &Metadata,
915 descr: &str,
916 permitted_dependencies: &[&'static str],
917 restricted_dependency_crates: &[&'static str],
918 permitted_location: ListLocation,
919 check: &mut RunningCheck,
920) {
921 let mut has_permitted_dep_error = false;
922 let mut deps = HashSet::new();
923 for to_check in restricted_dependency_crates {
924 let to_check = pkg_from_name(metadata, to_check);
925 deps_of(metadata, &to_check.id, &mut deps);
926 }
927
928 for permitted in permitted_dependencies {
930 fn compare(pkg: &Package, permitted: &str) -> bool {
931 if let Some((name, version)) = permitted.split_once("@") {
932 let Ok(version) = Version::parse(version) else {
933 return false;
934 };
935 *pkg.name == name && pkg.version == version
936 } else {
937 *pkg.name == permitted
938 }
939 }
940 if !deps.iter().any(|dep_id| compare(pkg_from_id(metadata, dep_id), permitted)) {
941 check.error(format!(
942 "could not find allowed package `{permitted}`\n\
943 Remove from PERMITTED_DEPENDENCIES list if it is no longer used.",
944 ));
945 has_permitted_dep_error = true;
946 }
947 }
948
949 let permitted_dependencies: HashMap<_, _> = permitted_dependencies
951 .iter()
952 .map(|s| {
953 if let Some((name, version)) = s.split_once('@') {
954 (name, Version::parse(version).ok())
955 } else {
956 (*s, None)
957 }
958 })
959 .collect();
960
961 for dep in deps {
962 let dep = pkg_from_id(metadata, dep);
963 if dep.source.is_some() {
965 let is_eq = if let Some(version) = permitted_dependencies.get(dep.name.as_str()) {
966 if let Some(version) = version { version == &dep.version } else { true }
967 } else {
968 false
969 };
970 if !is_eq {
971 check.error(format!("Dependency for {descr} not explicitly permitted: {}", dep.id));
972 has_permitted_dep_error = true;
973 }
974 }
975 }
976
977 if has_permitted_dep_error {
978 eprintln!("Go to `{}:{}` for the list.", permitted_location.path, permitted_location.line);
979 }
980}
981
982fn pkg_from_name<'a>(metadata: &'a Metadata, name: &'static str) -> &'a Package {
984 let mut i = metadata.packages.iter().filter(|p| *p.name == name);
985 let result =
986 i.next().unwrap_or_else(|| panic!("could not find package `{name}` in package list"));
987 assert!(i.next().is_none(), "more than one package found for `{name}`");
988 result
989}
990
991fn pkg_from_id<'a>(metadata: &'a Metadata, id: &PackageId) -> &'a Package {
992 metadata.packages.iter().find(|p| &p.id == id).unwrap()
993}
994
995fn deps_of<'a>(metadata: &'a Metadata, pkg_id: &'a PackageId, result: &mut HashSet<&'a PackageId>) {
997 if !result.insert(pkg_id) {
998 return;
999 }
1000 let node = metadata
1001 .resolve
1002 .as_ref()
1003 .unwrap()
1004 .nodes
1005 .iter()
1006 .find(|n| &n.id == pkg_id)
1007 .unwrap_or_else(|| panic!("could not find `{pkg_id}` in resolve"));
1008 for dep in &node.deps {
1009 deps_of(metadata, &dep.pkg, result);
1010 }
1011}