tidy/
target_policy.rs

1//! Tests for target tier policy compliance.
2//!
3//! As of writing, only checks that sanity-check assembly test for targets doesn't miss any targets.
4
5use std::collections::HashSet;
6use std::path::Path;
7
8use crate::walk::{filter_not_rust, walk};
9
10const TARGET_DEFINITIONS_PATH: &str = "compiler/rustc_target/src/spec/targets/";
11const ASSEMBLY_TEST_PATH: &str = "tests/assembly/targets/";
12const REVISION_LINE_START: &str = "//@ revisions: ";
13const EXCEPTIONS: &[&str] = &[
14    // FIXME: disabled since it fails on CI saying the csky component is missing
15    "csky_unknown_linux_gnuabiv2",
16    "csky_unknown_linux_gnuabiv2hf",
17    // FIXME: disabled since it requires a custom LLVM until the upstream LLVM adds support for the target (https://github.com/espressif/llvm-project/issues/4)
18    "xtensa_esp32_none_elf",
19    "xtensa_esp32_espidf",
20    "xtensa_esp32s2_none_elf",
21    "xtensa_esp32s2_espidf",
22    "xtensa_esp32s3_none_elf",
23    "xtensa_esp32s3_espidf",
24];
25
26pub fn check(root_path: &Path, bad: &mut bool) {
27    let mut targets_to_find = HashSet::new();
28
29    let definitions_path = root_path.join(TARGET_DEFINITIONS_PATH);
30    for defn in ignore::WalkBuilder::new(&definitions_path)
31        .max_depth(Some(1))
32        .filter_entry(|e| !filter_not_rust(e.path()))
33        .build()
34    {
35        let defn = defn.unwrap();
36        // Skip directory itself.
37        if defn.path() == definitions_path {
38            continue;
39        }
40
41        let path = defn.path();
42        let target_name = path.file_stem().unwrap().to_string_lossy().into_owned();
43        let _ = targets_to_find.insert(target_name);
44    }
45
46    walk(&root_path.join(ASSEMBLY_TEST_PATH), |_, _| false, &mut |_, contents| {
47        for line in contents.lines() {
48            let Some(_) = line.find(REVISION_LINE_START) else {
49                continue;
50            };
51            let (_, target_name) = line.split_at(REVISION_LINE_START.len());
52            targets_to_find.remove(target_name);
53        }
54    });
55
56    for target in targets_to_find {
57        if !EXCEPTIONS.contains(&target.as_str()) {
58            tidy_error!(bad, "{ASSEMBLY_TEST_PATH}: missing assembly test for {target}")
59        }
60    }
61}