Skip to content

Commit cb81972

Browse files
committed
tidy: Introduce WorkspaceInfo struct for deps information
1 parent 7ad23f4 commit cb81972

File tree

2 files changed

+73
-36
lines changed

2 files changed

+73
-36
lines changed

src/tools/tidy/src/deps.rs

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,74 @@ const LICENSES: &[&str] = &[
4848

4949
type ExceptionList = &'static [(&'static str, &'static str)];
5050

51+
pub(crate) struct WorkspaceInfo<'a> {
52+
/// Path to the directory containing the workspace root Cargo.toml file.
53+
pub(crate) path: &'a str,
54+
/// The list of license exceptions.
55+
pub(crate) exceptions: ExceptionList,
56+
/// Optionally:
57+
/// * A list of crates for which dependencies need to be explicitly allowed.
58+
/// * The list of allowed dependencies.
59+
pub(crate) crates_and_deps: Option<(&'a [&'a str], &'a [&'a str])>,
60+
/// Submodules required for the workspace
61+
pub(crate) submodules: &'a [&'a str],
62+
}
63+
64+
impl<'a> WorkspaceInfo<'a> {
65+
const fn new(
66+
path: &'a str,
67+
exceptions: ExceptionList,
68+
crates_and_deps: Option<(&'a [&str], &'a [&str])>,
69+
submodules: &'a [&str],
70+
) -> Self {
71+
Self { path, exceptions, crates_and_deps, submodules }
72+
}
73+
}
74+
5175
/// The workspaces to check for licensing and optionally permitted dependencies.
52-
///
53-
/// Each entry consists of a tuple with the following elements:
54-
///
55-
/// * The path to the workspace root Cargo.toml file.
56-
/// * The list of license exceptions.
57-
/// * Optionally a tuple of:
58-
/// * A list of crates for which dependencies need to be explicitly allowed.
59-
/// * The list of allowed dependencies.
60-
/// * Submodules required for the workspace.
6176
// FIXME auto detect all cargo workspaces
62-
pub(crate) const WORKSPACES: &[(&str, ExceptionList, Option<(&[&str], &[&str])>, &[&str])] = &[
77+
pub(crate) const WORKSPACES: &[WorkspaceInfo<'static>] = &[
6378
// The root workspace has to be first for check_rustfix to work.
64-
(".", EXCEPTIONS, Some((&["rustc-main"], PERMITTED_RUSTC_DEPENDENCIES)), &[]),
65-
("library", EXCEPTIONS_STDLIB, Some((&["sysroot"], PERMITTED_STDLIB_DEPENDENCIES)), &[]),
79+
WorkspaceInfo::new(".", EXCEPTIONS, Some((&["rustc-main"], PERMITTED_RUSTC_DEPENDENCIES)), &[]),
80+
WorkspaceInfo::new(
81+
"library",
82+
EXCEPTIONS_STDLIB,
83+
Some((&["sysroot"], PERMITTED_STDLIB_DEPENDENCIES)),
84+
&[],
85+
),
6686
// Outside of the alphabetical section because rustfmt formats it using multiple lines.
67-
(
87+
WorkspaceInfo::new(
6888
"compiler/rustc_codegen_cranelift",
6989
EXCEPTIONS_CRANELIFT,
7090
Some((&["rustc_codegen_cranelift"], PERMITTED_CRANELIFT_DEPENDENCIES)),
7191
&[],
7292
),
7393
// tidy-alphabetical-start
74-
("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/miri/test-cargo-miri", &[], None), // FIXME uncomment once all deps are vendored
78-
//("src/tools/miri/test_dependencies", &[], None), // FIXME uncomment once all deps are vendored
79-
("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, &[]),
94+
WorkspaceInfo::new("compiler/rustc_codegen_gcc", EXCEPTIONS_GCC, None, &[]),
95+
WorkspaceInfo::new("src/bootstrap", EXCEPTIONS_BOOTSTRAP, None, &[]),
96+
WorkspaceInfo::new("src/tools/cargo", EXCEPTIONS_CARGO, None, &["src/tools/cargo"]),
97+
//WorkspaceInfo::new("src/tools/miri/test-cargo-miri", &[], None), // FIXME uncomment once all deps are vendored
98+
//WorkspaceInfo::new("src/tools/miri/test_dependencies", &[], None), // FIXME uncomment once all deps are vendored
99+
WorkspaceInfo::new("src/tools/rust-analyzer", EXCEPTIONS_RUST_ANALYZER, None, &[]),
100+
WorkspaceInfo::new(
101+
"src/tools/rustbook",
102+
EXCEPTIONS_RUSTBOOK,
103+
None,
104+
&["src/doc/book", "src/doc/reference"],
105+
),
106+
WorkspaceInfo::new(
107+
"src/tools/rustc-perf",
108+
EXCEPTIONS_RUSTC_PERF,
109+
None,
110+
&["src/tools/rustc-perf"],
111+
),
112+
WorkspaceInfo::new("src/tools/test-float-parse", EXCEPTIONS, None, &[]),
113+
WorkspaceInfo::new(
114+
"tests/run-make-cargo/uefi-qemu/uefi_qemu_test",
115+
EXCEPTIONS_UEFI_QEMU_TEST,
116+
None,
117+
&[],
118+
),
84119
// tidy-alphabetical-end
85120
];
86121

@@ -567,29 +602,29 @@ pub fn check(root: &Path, cargo: &Path, bless: bool, bad: &mut bool) {
567602

568603
check_proc_macro_dep_list(root, cargo, bless, bad);
569604

570-
for &(workspace, exceptions, permitted_deps, submodules) in WORKSPACES {
605+
for &WorkspaceInfo { path, exceptions, crates_and_deps, submodules } in WORKSPACES {
571606
if has_missing_submodule(root, submodules) {
572607
continue;
573608
}
574609

575-
if !root.join(workspace).join("Cargo.lock").exists() {
576-
tidy_error!(bad, "the `{workspace}` workspace doesn't have a Cargo.lock");
610+
if !root.join(path).join("Cargo.lock").exists() {
611+
tidy_error!(bad, "the `{path}` workspace doesn't have a Cargo.lock");
577612
continue;
578613
}
579614

580615
let mut cmd = cargo_metadata::MetadataCommand::new();
581616
cmd.cargo_path(cargo)
582-
.manifest_path(root.join(workspace).join("Cargo.toml"))
617+
.manifest_path(root.join(path).join("Cargo.toml"))
583618
.features(cargo_metadata::CargoOpt::AllFeatures)
584619
.other_options(vec!["--locked".to_owned()]);
585620
let metadata = t!(cmd.exec());
586621

587-
check_license_exceptions(&metadata, workspace, exceptions, bad);
588-
if let Some((crates, permitted_deps)) = permitted_deps {
589-
check_permitted_dependencies(&metadata, workspace, permitted_deps, crates, bad);
622+
check_license_exceptions(&metadata, path, exceptions, bad);
623+
if let Some((crates, permitted_deps)) = crates_and_deps {
624+
check_permitted_dependencies(&metadata, path, permitted_deps, crates, bad);
590625
}
591626

592-
if workspace == "library" {
627+
if path == "library" {
593628
check_runtime_license_exceptions(&metadata, bad);
594629
check_runtime_no_duplicate_dependencies(&metadata, bad);
595630
check_runtime_no_proc_macros(&metadata, bad);

src/tools/tidy/src/extdeps.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use std::fs;
44
use std::path::Path;
55

6+
use crate::deps::WorkspaceInfo;
7+
68
/// List of allowed sources for packages.
79
const ALLOWED_SOURCES: &[&str] = &[
810
r#""registry+https://github.com/rust-lang/crates.io-index""#,
@@ -13,22 +15,22 @@ const ALLOWED_SOURCES: &[&str] = &[
1315
/// Checks for external package sources. `root` is the path to the directory that contains the
1416
/// workspace `Cargo.toml`.
1517
pub fn check(root: &Path, bad: &mut bool) {
16-
for &(workspace, _, _, submodules) in crate::deps::WORKSPACES {
18+
for &WorkspaceInfo { path, submodules, .. } in crate::deps::WORKSPACES {
1719
if crate::deps::has_missing_submodule(root, submodules) {
1820
continue;
1921
}
2022

2123
// FIXME check other workspaces too
2224
// `Cargo.lock` of rust.
23-
let path = root.join(workspace).join("Cargo.lock");
25+
let lockfile = root.join(path).join("Cargo.lock");
2426

25-
if !path.exists() {
26-
tidy_error!(bad, "the `{workspace}` workspace doesn't have a Cargo.lock");
27+
if !lockfile.exists() {
28+
tidy_error!(bad, "the `{path}` workspace doesn't have a Cargo.lock");
2729
continue;
2830
}
2931

3032
// Open and read the whole file.
31-
let cargo_lock = t!(fs::read_to_string(&path));
33+
let cargo_lock = t!(fs::read_to_string(&lockfile));
3234

3335
// Process each line.
3436
for line in cargo_lock.lines() {

0 commit comments

Comments
 (0)