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-cargo/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_collections",
291 "icu_list",
292 "icu_locale",
293 "icu_locale_core",
294 "icu_locale_data",
295 "icu_provider",
296 "ident_case",
297 "indexmap",
298 "intl-memoizer",
299 "intl_pluralrules",
300 "itertools",
301 "itoa",
302 "jiff",
303 "jiff-static",
304 "jobserver",
305 "lazy_static",
306 "leb128",
307 "libc",
308 "libloading",
309 "linux-raw-sys",
310 "litemap",
311 "lock_api",
312 "log",
313 "matchers",
314 "md-5",
315 "measureme",
316 "memchr",
317 "memmap2",
318 "miniz_oxide",
319 "nix",
320 "nu-ansi-term",
321 "object",
322 "odht",
323 "once_cell",
324 "overload",
325 "parking_lot",
326 "parking_lot_core",
327 "pathdiff",
328 "perf-event-open-sys",
329 "pin-project-lite",
330 "polonius-engine",
331 "portable-atomic", "portable-atomic-util",
333 "potential_utf",
334 "ppv-lite86",
335 "proc-macro-hack",
336 "proc-macro2",
337 "psm",
338 "pulldown-cmark",
339 "pulldown-cmark-escape",
340 "punycode",
341 "quote",
342 "r-efi",
343 "rand",
344 "rand_chacha",
345 "rand_core",
346 "rand_xorshift", "rand_xoshiro",
348 "redox_syscall",
349 "regex",
350 "regex-automata",
351 "regex-syntax",
352 "rustc-demangle",
353 "rustc-hash",
354 "rustc-literal-escaper",
355 "rustc-stable-hash",
356 "rustc_apfloat",
357 "rustix",
358 "ruzstd", "ryu",
360 "scoped-tls",
361 "scopeguard",
362 "self_cell",
363 "serde",
364 "serde_derive",
365 "serde_json",
366 "serde_path_to_error",
367 "sha1",
368 "sha2",
369 "sharded-slab",
370 "shlex",
371 "smallvec",
372 "stable_deref_trait",
373 "stacker",
374 "static_assertions",
375 "strsim",
376 "syn",
377 "synstructure",
378 "tempfile",
379 "termcolor",
380 "termize",
381 "thin-vec",
382 "thiserror",
383 "thiserror-impl",
384 "thorin-dwp",
385 "thread_local",
386 "tikv-jemalloc-sys",
387 "tinystr",
388 "tinyvec",
389 "tinyvec_macros",
390 "tracing",
391 "tracing-attributes",
392 "tracing-core",
393 "tracing-log",
394 "tracing-subscriber",
395 "tracing-tree",
396 "twox-hash",
397 "type-map",
398 "typenum",
399 "unic-langid",
400 "unic-langid-impl",
401 "unic-langid-macros",
402 "unic-langid-macros-impl",
403 "unicase",
404 "unicode-ident",
405 "unicode-normalization",
406 "unicode-properties",
407 "unicode-script",
408 "unicode-security",
409 "unicode-width",
410 "unicode-xid",
411 "valuable",
412 "version_check",
413 "wasi",
414 "wasm-encoder",
415 "wasmparser",
416 "winapi",
417 "winapi-i686-pc-windows-gnu",
418 "winapi-util",
419 "winapi-x86_64-pc-windows-gnu",
420 "windows",
421 "windows-collections",
422 "windows-core",
423 "windows-future",
424 "windows-implement",
425 "windows-interface",
426 "windows-link",
427 "windows-numerics",
428 "windows-result",
429 "windows-strings",
430 "windows-sys",
431 "windows-targets",
432 "windows-threading",
433 "windows_aarch64_gnullvm",
434 "windows_aarch64_msvc",
435 "windows_i686_gnu",
436 "windows_i686_gnullvm",
437 "windows_i686_msvc",
438 "windows_x86_64_gnu",
439 "windows_x86_64_gnullvm",
440 "windows_x86_64_msvc",
441 "wit-bindgen-rt@0.39.0", "writeable",
443 "yoke",
444 "yoke-derive",
445 "zerocopy",
446 "zerocopy-derive",
447 "zerofrom",
448 "zerofrom-derive",
449 "zerotrie",
450 "zerovec",
451 "zerovec-derive",
452 ];
454
455const PERMITTED_STDLIB_DEPENDENCIES: &[&str] = &[
456 "addr2line",
458 "adler2",
459 "cc",
460 "cfg-if",
461 "compiler_builtins",
462 "dlmalloc",
463 "fortanix-sgx-abi",
464 "getopts",
465 "gimli",
466 "hashbrown",
467 "hermit-abi",
468 "libc",
469 "memchr",
470 "miniz_oxide",
471 "object",
472 "r-efi",
473 "r-efi-alloc",
474 "rand",
475 "rand_core",
476 "rand_xorshift",
477 "rustc-demangle",
478 "rustc-literal-escaper",
479 "shlex",
480 "unicode-width",
481 "unwinding",
482 "wasi",
483 "windows-sys",
484 "windows-targets",
485 "windows_aarch64_gnullvm",
486 "windows_aarch64_msvc",
487 "windows_i686_gnu",
488 "windows_i686_gnullvm",
489 "windows_i686_msvc",
490 "windows_x86_64_gnu",
491 "windows_x86_64_gnullvm",
492 "windows_x86_64_msvc",
493 "wit-bindgen",
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 check_runtime_no_duplicate_dependencies(&metadata, bad);
596 check_runtime_no_proc_macros(&metadata, bad);
597 checked_runtime_licenses = true;
598 }
599 }
600
601 assert!(checked_runtime_licenses);
604}
605
606fn check_proc_macro_dep_list(root: &Path, cargo: &Path, bless: bool, bad: &mut bool) {
608 let mut cmd = cargo_metadata::MetadataCommand::new();
609 cmd.cargo_path(cargo)
610 .manifest_path(root.join("Cargo.toml"))
611 .features(cargo_metadata::CargoOpt::AllFeatures)
612 .other_options(vec!["--locked".to_owned()]);
613 let metadata = t!(cmd.exec());
614 let is_proc_macro_pkg = |pkg: &Package| pkg.targets.iter().any(|target| target.is_proc_macro());
615
616 let mut proc_macro_deps = HashSet::new();
617 for pkg in metadata.packages.iter().filter(|pkg| is_proc_macro_pkg(pkg)) {
618 deps_of(&metadata, &pkg.id, &mut proc_macro_deps);
619 }
620 proc_macro_deps.retain(|pkg| !is_proc_macro_pkg(&metadata[pkg]));
622
623 let proc_macro_deps: HashSet<_> =
624 proc_macro_deps.into_iter().map(|dep| metadata[dep].name.as_ref()).collect();
625 let expected = proc_macro_deps::CRATES.iter().copied().collect::<HashSet<_>>();
626
627 let needs_blessing = proc_macro_deps.difference(&expected).next().is_some()
628 || expected.difference(&proc_macro_deps).next().is_some();
629
630 if needs_blessing && bless {
631 let mut proc_macro_deps: Vec<_> = proc_macro_deps.into_iter().collect();
632 proc_macro_deps.sort();
633 let mut file = File::create(root.join("src/bootstrap/src/utils/proc_macro_deps.rs"))
634 .expect("`proc_macro_deps` should exist");
635 writeln!(
636 &mut file,
637 "/// Do not update manually - use `./x.py test tidy --bless`
638/// Holds all direct and indirect dependencies of proc-macro crates in tree.
639/// See <https://github.com/rust-lang/rust/issues/134863>
640pub static CRATES: &[&str] = &[
641 // tidy-alphabetical-start"
642 )
643 .unwrap();
644 for dep in proc_macro_deps {
645 writeln!(&mut file, " {dep:?},").unwrap();
646 }
647 writeln!(
648 &mut file,
649 " // tidy-alphabetical-end
650];"
651 )
652 .unwrap();
653 } else {
654 let old_bad = *bad;
655
656 for missing in proc_macro_deps.difference(&expected) {
657 tidy_error!(
658 bad,
659 "proc-macro crate dependency `{missing}` is not registered in `src/bootstrap/src/utils/proc_macro_deps.rs`",
660 );
661 }
662 for extra in expected.difference(&proc_macro_deps) {
663 tidy_error!(
664 bad,
665 "`{extra}` is registered in `src/bootstrap/src/utils/proc_macro_deps.rs`, but is not a proc-macro crate dependency",
666 );
667 }
668 if *bad != old_bad {
669 eprintln!("Run `./x.py test tidy --bless` to regenerate the list");
670 }
671 }
672}
673
674pub fn has_missing_submodule(root: &Path, submodules: &[&str]) -> bool {
678 !CiEnv::is_ci()
679 && submodules.iter().any(|submodule| {
680 let path = root.join(submodule);
681 !path.exists()
682 || read_dir(path).unwrap().next().is_none()
684 })
685}
686
687fn check_runtime_license_exceptions(metadata: &Metadata, bad: &mut bool) {
692 for pkg in &metadata.packages {
693 if pkg.source.is_none() {
694 continue;
696 }
697 let license = match &pkg.license {
698 Some(license) => license,
699 None => {
700 tidy_error!(bad, "dependency `{}` does not define a license expression", pkg.id);
701 continue;
702 }
703 };
704 if !LICENSES.contains(&license.as_str()) {
705 if *pkg.name == "fortanix-sgx-abi" && pkg.license.as_deref() == Some("MPL-2.0") {
710 continue;
711 }
712
713 tidy_error!(bad, "invalid license `{}` in `{}`", license, pkg.id);
714 }
715 }
716}
717
718fn check_license_exceptions(
722 metadata: &Metadata,
723 workspace: &str,
724 exceptions: &[(&str, &str)],
725 bad: &mut bool,
726) {
727 for (name, license) in exceptions {
729 if !metadata.packages.iter().any(|p| *p.name == *name) {
731 tidy_error!(
732 bad,
733 "could not find exception package `{}` in workspace `{workspace}`\n\
734 Remove from EXCEPTIONS list if it is no longer used.",
735 name
736 );
737 }
738 for pkg in metadata.packages.iter().filter(|p| *p.name == *name) {
740 match &pkg.license {
741 None => {
742 tidy_error!(
743 bad,
744 "dependency exception `{}` in workspace `{workspace}` does not declare a license expression",
745 pkg.id
746 );
747 }
748 Some(pkg_license) => {
749 if pkg_license.as_str() != *license {
750 println!(
751 "dependency exception `{name}` license in workspace `{workspace}` has changed"
752 );
753 println!(" previously `{license}` now `{pkg_license}`");
754 println!(" update EXCEPTIONS for the new license");
755 *bad = true;
756 }
757 }
758 }
759 }
760 }
761
762 let exception_names: Vec<_> = exceptions.iter().map(|(name, _license)| *name).collect();
763
764 for pkg in &metadata.packages {
766 if pkg.source.is_none() {
767 continue;
769 }
770 if exception_names.contains(&pkg.name.as_str()) {
771 continue;
772 }
773 let license = match &pkg.license {
774 Some(license) => license,
775 None => {
776 tidy_error!(
777 bad,
778 "dependency `{}` in workspace `{workspace}` does not define a license expression",
779 pkg.id
780 );
781 continue;
782 }
783 };
784 if !LICENSES.contains(&license.as_str()) {
785 tidy_error!(
786 bad,
787 "invalid license `{}` for package `{}` in workspace `{workspace}`",
788 license,
789 pkg.id
790 );
791 }
792 }
793}
794
795fn check_runtime_no_duplicate_dependencies(metadata: &Metadata, bad: &mut bool) {
796 let mut seen_pkgs = HashSet::new();
797 for pkg in &metadata.packages {
798 if pkg.source.is_none() {
799 continue;
800 }
801
802 if pkg.name.to_string() != "wasi" && !seen_pkgs.insert(&*pkg.name) {
806 tidy_error!(
807 bad,
808 "duplicate package `{}` is not allowed for the standard library",
809 pkg.name
810 );
811 }
812 }
813}
814
815fn check_runtime_no_proc_macros(metadata: &Metadata, bad: &mut bool) {
816 for pkg in &metadata.packages {
817 if pkg.targets.iter().any(|target| target.is_proc_macro()) {
818 tidy_error!(
819 bad,
820 "proc macro `{}` is not allowed as standard library dependency.\n\
821 Using proc macros in the standard library would break cross-compilation \
822 as proc-macros don't get shipped for the host tuple.",
823 pkg.name
824 );
825 }
826 }
827}
828
829fn check_permitted_dependencies(
834 metadata: &Metadata,
835 descr: &str,
836 permitted_dependencies: &[&'static str],
837 restricted_dependency_crates: &[&'static str],
838 bad: &mut bool,
839) {
840 let mut has_permitted_dep_error = false;
841 let mut deps = HashSet::new();
842 for to_check in restricted_dependency_crates {
843 let to_check = pkg_from_name(metadata, to_check);
844 deps_of(metadata, &to_check.id, &mut deps);
845 }
846
847 for permitted in permitted_dependencies {
849 fn compare(pkg: &Package, permitted: &str) -> bool {
850 if let Some((name, version)) = permitted.split_once("@") {
851 let Ok(version) = Version::parse(version) else {
852 return false;
853 };
854 *pkg.name == name && pkg.version == version
855 } else {
856 *pkg.name == permitted
857 }
858 }
859 if !deps.iter().any(|dep_id| compare(pkg_from_id(metadata, dep_id), permitted)) {
860 tidy_error!(
861 bad,
862 "could not find allowed package `{permitted}`\n\
863 Remove from PERMITTED_DEPENDENCIES list if it is no longer used.",
864 );
865 has_permitted_dep_error = true;
866 }
867 }
868
869 let permitted_dependencies: HashMap<_, _> = permitted_dependencies
871 .iter()
872 .map(|s| {
873 if let Some((name, version)) = s.split_once('@') {
874 (name, Version::parse(version).ok())
875 } else {
876 (*s, None)
877 }
878 })
879 .collect();
880
881 for dep in deps {
882 let dep = pkg_from_id(metadata, dep);
883 if dep.source.is_some() {
885 let is_eq = if let Some(version) = permitted_dependencies.get(dep.name.as_str()) {
886 if let Some(version) = version { version == &dep.version } else { true }
887 } else {
888 false
889 };
890 if !is_eq {
891 tidy_error!(bad, "Dependency for {descr} not explicitly permitted: {}", dep.id);
892 has_permitted_dep_error = true;
893 }
894 }
895 }
896
897 if has_permitted_dep_error {
898 eprintln!("Go to `{PERMITTED_DEPS_LOCATION}` for the list.");
899 }
900}
901
902fn pkg_from_name<'a>(metadata: &'a Metadata, name: &'static str) -> &'a Package {
904 let mut i = metadata.packages.iter().filter(|p| *p.name == name);
905 let result =
906 i.next().unwrap_or_else(|| panic!("could not find package `{name}` in package list"));
907 assert!(i.next().is_none(), "more than one package found for `{name}`");
908 result
909}
910
911fn pkg_from_id<'a>(metadata: &'a Metadata, id: &PackageId) -> &'a Package {
912 metadata.packages.iter().find(|p| &p.id == id).unwrap()
913}
914
915fn deps_of<'a>(metadata: &'a Metadata, pkg_id: &'a PackageId, result: &mut HashSet<&'a PackageId>) {
917 if !result.insert(pkg_id) {
918 return;
919 }
920 let node = metadata
921 .resolve
922 .as_ref()
923 .unwrap()
924 .nodes
925 .iter()
926 .find(|n| &n.id == pkg_id)
927 .unwrap_or_else(|| panic!("could not find `{pkg_id}` in resolve"));
928 for dep in &node.deps {
929 deps_of(metadata, &dep.pkg, result);
930 }
931}