1 //! Checks the licenses of third-party dependencies.
3 use cargo_metadata::{Metadata, Package, PackageId, Resolve};
4 use std::collections::{BTreeSet, HashSet};
7 /// These are licenses that are allowed for all crates, including the runtime,
9 const LICENSES: &[&str] = &[
16 "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT", // wasi license
21 "0BSD OR MIT OR Apache-2.0", // adler license
22 "Zlib OR Apache-2.0 OR MIT", // tinyvec
23 "MIT OR Apache-2.0 OR Zlib", // tinyvec_macros
24 "MIT OR Zlib OR Apache-2.0", // miniz_oxide
25 "(MIT OR Apache-2.0) AND Unicode-DFS-2016", // unicode_ident
26 "Unicode-DFS-2016", // tinystr and icu4x
29 /// These are exceptions to Rust's permissive licensing policy, and
30 /// should be considered bugs. Exceptions are only allowed in Rust
31 /// tooling. It is _crucial_ that no exception crates be dependencies
32 /// of the Rust runtime (std/test).
33 const EXCEPTIONS: &[(&str, &str)] = &[
34 ("ar_archive_writer", "Apache-2.0 WITH LLVM-exception"), // rustc
35 ("mdbook", "MPL-2.0"), // mdbook
36 ("openssl", "Apache-2.0"), // cargo, mdbook
37 ("colored", "MPL-2.0"), // rustfmt
38 ("ryu", "Apache-2.0 OR BSL-1.0"), // cargo/... (because of serde)
39 ("bytesize", "Apache-2.0"), // cargo
40 ("im-rc", "MPL-2.0+"), // cargo
41 ("sized-chunks", "MPL-2.0+"), // cargo via im-rc
42 ("bitmaps", "MPL-2.0+"), // cargo via im-rc
43 ("fiat-crypto", "MIT OR Apache-2.0 OR BSD-1-Clause"), // cargo via pasetors
44 ("subtle", "BSD-3-Clause"), // cargo via pasetors
45 ("instant", "BSD-3-Clause"), // rustc_driver/tracing-subscriber/parking_lot
46 ("snap", "BSD-3-Clause"), // rustc
47 ("fluent-langneg", "Apache-2.0"), // rustc (fluent translations)
48 ("self_cell", "Apache-2.0"), // rustc (fluent translations)
49 // FIXME: this dependency violates the documentation comment above:
50 ("fortanix-sgx-abi", "MPL-2.0"), // libstd but only for `sgx` target
51 ("dunce", "CC0-1.0"), // cargo (dev dependency)
52 ("similar", "Apache-2.0"), // cargo (dev dependency)
53 ("normalize-line-endings", "Apache-2.0"), // cargo (dev dependency)
54 ("dissimilar", "Apache-2.0"), // rustdoc, rustc_lexer (few tests) via expect-test, (dev deps)
57 const EXCEPTIONS_CRANELIFT: &[(&str, &str)] = &[
58 ("cranelift-bforest", "Apache-2.0 WITH LLVM-exception"),
59 ("cranelift-codegen", "Apache-2.0 WITH LLVM-exception"),
60 ("cranelift-codegen-meta", "Apache-2.0 WITH LLVM-exception"),
61 ("cranelift-codegen-shared", "Apache-2.0 WITH LLVM-exception"),
62 ("cranelift-egraph", "Apache-2.0 WITH LLVM-exception"),
63 ("cranelift-entity", "Apache-2.0 WITH LLVM-exception"),
64 ("cranelift-frontend", "Apache-2.0 WITH LLVM-exception"),
65 ("cranelift-isle", "Apache-2.0 WITH LLVM-exception"),
66 ("cranelift-jit", "Apache-2.0 WITH LLVM-exception"),
67 ("cranelift-module", "Apache-2.0 WITH LLVM-exception"),
68 ("cranelift-native", "Apache-2.0 WITH LLVM-exception"),
69 ("cranelift-object", "Apache-2.0 WITH LLVM-exception"),
70 ("mach", "BSD-2-Clause"),
71 ("regalloc2", "Apache-2.0 WITH LLVM-exception"),
72 ("target-lexicon", "Apache-2.0 WITH LLVM-exception"),
73 ("wasmtime-jit-icache-coherence", "Apache-2.0 WITH LLVM-exception"),
76 const EXCEPTIONS_BOOTSTRAP: &[(&str, &str)] = &[
77 ("ryu", "Apache-2.0 OR BSL-1.0"), // through serde
80 /// These are the root crates that are part of the runtime. The licenses for
81 /// these and all their dependencies *must not* be in the exception list.
82 const RUNTIME_CRATES: &[&str] = &["std", "core", "alloc", "test", "panic_abort", "panic_unwind"];
84 /// Crates rustc is allowed to depend on. Avoid adding to the list if possible.
86 /// This list is here to provide a speed-bump to adding a new dependency to
87 /// rustc. Please check with the compiler team before adding an entry.
88 const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
101 "bumpalo", // Included in Cargo's dep graph but only activated on wasm32-*-unknown.
109 "convert_case", // dependency of derive_more
129 "fallible-iterator", // dependency of `thorin`
149 "icu_provider_adapters",
150 "icu_provider_macros",
159 "js-sys", // Included in Cargo's dep graph but only activated on wasm32-*-unknown.
183 "perf-event-open-sys",
226 "stable_deref_trait",
229 "subtle", // dependency of cargo (via pasetors)
245 "tracing-attributes",
248 "tracing-subscriber",
253 "unic-char-property",
259 "unic-langid-macros",
260 "unic-langid-macros-impl",
263 "unicode-normalization",
272 // vvv Included in Cargo's dep graph but only activated on wasm32-*-unknown.
274 "wasm-bindgen-backend",
275 "wasm-bindgen-macro",
276 "wasm-bindgen-macro-support",
277 "wasm-bindgen-shared",
278 // ^^^ Included in Cargo's dep graph but only activated on wasm32-*-unknown.
280 "winapi-i686-pc-windows-gnu",
282 "winapi-x86_64-pc-windows-gnu",
284 // this is a false-positive: it's only used by rustfmt, but because it's enabled through a
285 // feature, tidy thinks it's used by rustc as well.
295 const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
306 "cranelift-codegen-meta",
307 "cranelift-codegen-shared",
310 "cranelift-frontend",
334 "stable_deref_trait",
338 "wasmtime-jit-icache-coherence",
340 "winapi-i686-pc-windows-gnu",
341 "winapi-x86_64-pc-windows-gnu",
343 "windows_aarch64_msvc",
346 "windows_x86_64_gnu",
347 "windows_x86_64_msvc",
350 const FORBIDDEN_TO_HAVE_DUPLICATES: &[&str] = &[
351 // This crate takes quite a long time to build, so don't allow two versions of them
352 // to accidentally sneak into our dependency graph, in order to ensure we keep our CI times
357 /// Dependency checks.
359 /// `root` is path to the directory with the root `Cargo.toml` (for the workspace). `cargo` is path
360 /// to the cargo executable.
361 pub fn check(root: &Path, cargo: &Path, bad: &mut bool) {
362 let mut cmd = cargo_metadata::MetadataCommand::new();
363 cmd.cargo_path(cargo)
364 .manifest_path(root.join("Cargo.toml"))
365 .features(cargo_metadata::CargoOpt::AllFeatures);
366 let metadata = t!(cmd.exec());
367 let runtime_ids = compute_runtime_crates(&metadata);
368 check_license_exceptions(&metadata, EXCEPTIONS, runtime_ids, bad);
369 check_permitted_dependencies(
372 PERMITTED_RUSTC_DEPENDENCIES,
373 &["rustc_driver", "rustc_codegen_llvm"],
376 check_crate_duplicate(&metadata, FORBIDDEN_TO_HAVE_DUPLICATES, bad);
377 check_rustfix(&metadata, bad);
379 // Check rustc_codegen_cranelift independently as it has it's own workspace.
380 let mut cmd = cargo_metadata::MetadataCommand::new();
381 cmd.cargo_path(cargo)
382 .manifest_path(root.join("compiler/rustc_codegen_cranelift/Cargo.toml"))
383 .features(cargo_metadata::CargoOpt::AllFeatures);
384 let metadata = t!(cmd.exec());
385 let runtime_ids = HashSet::new();
386 check_license_exceptions(&metadata, EXCEPTIONS_CRANELIFT, runtime_ids, bad);
387 check_permitted_dependencies(
390 PERMITTED_CRANELIFT_DEPENDENCIES,
391 &["rustc_codegen_cranelift"],
394 check_crate_duplicate(&metadata, &[], bad);
396 let mut cmd = cargo_metadata::MetadataCommand::new();
397 cmd.cargo_path(cargo)
398 .manifest_path(root.join("src/bootstrap/Cargo.toml"))
399 .features(cargo_metadata::CargoOpt::AllFeatures);
400 let metadata = t!(cmd.exec());
401 let runtime_ids = HashSet::new();
402 check_license_exceptions(&metadata, EXCEPTIONS_BOOTSTRAP, runtime_ids, bad);
405 /// Check that all licenses are in the valid list in `LICENSES`.
407 /// Packages listed in `exceptions` are allowed for tools.
408 fn check_license_exceptions(
410 exceptions: &[(&str, &str)],
411 runtime_ids: HashSet<&PackageId>,
414 // Validate the EXCEPTIONS list hasn't changed.
415 for (name, license) in exceptions {
416 // Check that the package actually exists.
417 if !metadata.packages.iter().any(|p| p.name == *name) {
420 "could not find exception package `{}`\n\
421 Remove from EXCEPTIONS list if it is no longer used.",
425 // Check that the license hasn't changed.
426 for pkg in metadata.packages.iter().filter(|p| p.name == *name) {
431 "dependency exception `{}` does not declare a license expression",
435 Some(pkg_license) => {
436 if pkg_license.as_str() != *license {
437 println!("dependency exception `{name}` license has changed");
438 println!(" previously `{license}` now `{pkg_license}`");
439 println!(" update EXCEPTIONS for the new license");
447 let exception_names: Vec<_> = exceptions.iter().map(|(name, _license)| *name).collect();
449 // Check if any package does not have a valid license.
450 for pkg in &metadata.packages {
451 if pkg.source.is_none() {
452 // No need to check local packages.
455 if !runtime_ids.contains(&pkg.id) && exception_names.contains(&pkg.name.as_str()) {
458 let license = match &pkg.license {
459 Some(license) => license,
461 tidy_error!(bad, "dependency `{}` does not define a license expression", pkg.id);
465 if !LICENSES.contains(&license.as_str()) {
466 if pkg.name == "fortanix-sgx-abi" {
467 // This is a specific exception because SGX is considered
468 // "third party". See
469 // https://github.com/rust-lang/rust/issues/62620 for more. In
470 // general, these should never be added.
473 tidy_error!(bad, "invalid license `{}` in `{}`", license, pkg.id);
478 /// Checks the dependency of `restricted_dependency_crates` at the given path. Changes `bad` to
479 /// `true` if a check failed.
481 /// Specifically, this checks that the dependencies are on the `permitted_dependencies`.
482 fn check_permitted_dependencies(
485 permitted_dependencies: &[&'static str],
486 restricted_dependency_crates: &[&'static str],
489 // Check that the PERMITTED_DEPENDENCIES does not have unused entries.
490 for name in permitted_dependencies {
491 if !metadata.packages.iter().any(|p| p.name == *name) {
494 "could not find allowed package `{}`\n\
495 Remove from PERMITTED_DEPENDENCIES list if it is no longer used.",
500 // Get the list in a convenient form.
501 let permitted_dependencies: HashSet<_> = permitted_dependencies.iter().cloned().collect();
503 // Check dependencies.
504 let mut visited = BTreeSet::new();
505 let mut unapproved = BTreeSet::new();
506 for &krate in restricted_dependency_crates.iter() {
507 let pkg = pkg_from_name(metadata, krate);
509 check_crate_dependencies(&permitted_dependencies, metadata, &mut visited, pkg);
510 unapproved.append(&mut bad);
513 if !unapproved.is_empty() {
514 tidy_error!(bad, "Dependencies for {} not explicitly permitted:", descr);
515 for dep in unapproved {
521 /// Checks the dependencies of the given crate from the given cargo metadata to see if they are on
522 /// the list of permitted dependencies. Returns a list of disallowed dependencies.
523 fn check_crate_dependencies<'a>(
524 permitted_dependencies: &'a HashSet<&'static str>,
525 metadata: &'a Metadata,
526 visited: &mut BTreeSet<&'a PackageId>,
528 ) -> BTreeSet<&'a PackageId> {
529 // This will contain bad deps.
530 let mut unapproved = BTreeSet::new();
532 // Check if we have already visited this crate.
533 if visited.contains(&krate.id) {
537 visited.insert(&krate.id);
539 // If this path is in-tree, we don't require it to be explicitly permitted.
540 if krate.source.is_some() {
541 // If this dependency is not on `PERMITTED_DEPENDENCIES`, add to bad set.
542 if !permitted_dependencies.contains(krate.name.as_str()) {
543 unapproved.insert(&krate.id);
547 // Do a DFS in the crate graph.
548 let to_check = deps_of(metadata, &krate.id);
550 for dep in to_check {
551 let mut bad = check_crate_dependencies(permitted_dependencies, metadata, visited, dep);
552 unapproved.append(&mut bad);
558 /// Prevents multiple versions of some expensive crates.
559 fn check_crate_duplicate(
561 forbidden_to_have_duplicates: &[&str],
564 for &name in forbidden_to_have_duplicates {
565 let matches: Vec<_> = metadata.packages.iter().filter(|pkg| pkg.name == name).collect();
566 match matches.len() {
570 "crate `{}` is missing, update `check_crate_duplicate` \
571 if it is no longer used",
579 "crate `{}` is duplicated in `Cargo.lock`, \
580 it is too expensive to build multiple times, \
581 so make sure only one version appears across all dependencies",
585 println!(" * {}", pkg.id);
592 /// Returns a list of dependencies for the given package.
593 fn deps_of<'a>(metadata: &'a Metadata, pkg_id: &'a PackageId) -> Vec<&'a Package> {
594 let resolve = metadata.resolve.as_ref().unwrap();
598 .find(|n| &n.id == pkg_id)
599 .unwrap_or_else(|| panic!("could not find `{pkg_id}` in resolve"));
603 metadata.packages.iter().find(|pkg| pkg.id == dep.pkg).unwrap_or_else(|| {
604 panic!("could not find dep `{}` for pkg `{}` in resolve", dep.pkg, pkg_id)
610 /// Finds a package with the given name.
611 fn pkg_from_name<'a>(metadata: &'a Metadata, name: &'static str) -> &'a Package {
612 let mut i = metadata.packages.iter().filter(|p| p.name == name);
614 i.next().unwrap_or_else(|| panic!("could not find package `{name}` in package list"));
615 assert!(i.next().is_none(), "more than one package found for `{name}`");
619 /// Finds all the packages that are in the rust runtime.
620 fn compute_runtime_crates<'a>(metadata: &'a Metadata) -> HashSet<&'a PackageId> {
621 let resolve = metadata.resolve.as_ref().unwrap();
622 let mut result = HashSet::new();
623 for name in RUNTIME_CRATES {
624 let id = &pkg_from_name(metadata, name).id;
625 normal_deps_of_r(resolve, id, &mut result);
630 /// Recursively find all normal dependencies.
631 fn normal_deps_of_r<'a>(
632 resolve: &'a Resolve,
633 pkg_id: &'a PackageId,
634 result: &mut HashSet<&'a PackageId>,
636 if !result.insert(pkg_id) {
642 .find(|n| &n.id == pkg_id)
643 .unwrap_or_else(|| panic!("could not find `{pkg_id}` in resolve"));
644 for dep in &node.deps {
645 normal_deps_of_r(resolve, &dep.pkg, result);
649 fn check_rustfix(metadata: &Metadata, bad: &mut bool) {
650 let cargo = pkg_from_name(metadata, "cargo");
651 let compiletest = pkg_from_name(metadata, "compiletest");
652 let cargo_deps = deps_of(metadata, &cargo.id);
653 let compiletest_deps = deps_of(metadata, &compiletest.id);
654 let cargo_rustfix = cargo_deps.iter().find(|p| p.name == "rustfix").unwrap();
655 let compiletest_rustfix = compiletest_deps.iter().find(|p| p.name == "rustfix").unwrap();
656 if cargo_rustfix.version != compiletest_rustfix.version {
659 "cargo's rustfix version {} does not match compiletest's rustfix version {}\n\
660 rustfix should be kept in sync, update the cargo side first, and then update \
661 compiletest along with cargo.",
662 cargo_rustfix.version,
663 compiletest_rustfix.version