xtask\tasks\fmt\house_rules/
cfg_target_arch.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use anyhow::anyhow;
5use fs_err::File;
6use std::io::BufRead;
7use std::io::BufReader;
8use std::path::Path;
9
10const SUPPRESS: &str = "xtask-fmt allow-target-arch";
11
12/// Using `target_arch` in order to execute CPU-specific intrinsics
13const SUPPRESS_REASON_CPU_INTRINSIC: &str = "cpu-intrinsic";
14/// Using `target_arch` in order to implement a '*-sys'-like crate (where the
15/// structure changes depending on the host-arch)
16const SUPPRESS_REASON_SYS_CRATE: &str = "sys-crate";
17/// A dependency of this crate will not build on other target architectures
18const SUPPRESS_REASON_DEPENDENCY: &str = "dependency";
19/// One off - support for the auto-arch selection logic in
20/// `build_rs_guest_arch`.
21const SUPPRESS_REASON_ONEOFF_GUEST_ARCH_IMPL: &str = "oneoff-guest-arch-impl";
22/// One off - used as part of flowey CI infra
23const SUPPRESS_REASON_ONEOFF_FLOWEY: &str = "oneoff-flowey";
24/// One off - used by petri to select native test dependencies
25const SUPPRESS_REASON_ONEOFF_PETRI_NATIVE_TEST_DEPS: &str = "oneoff-petri-native-test-deps";
26/// Onee off - used by petri to return the host architecture for test filtering
27const SUPPRESS_REASON_ONEOFF_PETRI_HOST_ARCH: &str = "oneoff-petri-host-arch";
28
29fn has_suppress(s: &str) -> bool {
30    let Some((_, after)) = s.split_once(SUPPRESS) else {
31        return false;
32    };
33
34    let after = after.trim();
35    let justification = after.split(' ').next().unwrap();
36
37    let ok = matches!(
38        justification,
39        SUPPRESS_REASON_CPU_INTRINSIC
40            | SUPPRESS_REASON_SYS_CRATE
41            | SUPPRESS_REASON_DEPENDENCY
42            | SUPPRESS_REASON_ONEOFF_GUEST_ARCH_IMPL
43            | SUPPRESS_REASON_ONEOFF_FLOWEY
44            | SUPPRESS_REASON_ONEOFF_PETRI_NATIVE_TEST_DEPS
45            | SUPPRESS_REASON_ONEOFF_PETRI_HOST_ARCH
46    );
47
48    if !ok {
49        log::error!(
50            "invalid justification '{}' (must be one of [sys-crate, cpu-intrinsic]",
51            after.split(' ').next().unwrap()
52        );
53    }
54
55    ok
56}
57
58pub fn check_cfg_target_arch(path: &Path, _fix: bool) -> anyhow::Result<()> {
59    let ext = path
60        .extension()
61        .and_then(|e| e.to_str())
62        .unwrap_or_default();
63
64    if !matches!(ext, "rs") {
65        return Ok(());
66    }
67
68    // need to exclude self (and house_rules.rs, which includes help-text) from
69    // the lint
70    if path == Path::new(file!()) || path == Path::new(super::PATH_TO_HOUSE_RULES_RS) {
71        return Ok(());
72    }
73
74    // guest_test_uefi is a guest-side crate (the code runs in the guest), so
75    // target_arch here is actually referring to the guest_arch
76    //
77    // openhcl_boot uses target_arch liberally, since it runs in VTL2 entirely
78    // in-service to the VTL2 linux kernel, which will always be native-arch.
79    // Similar for the sidecar kernel and TMKs. And minimal_rt provides the
80    // (arch-specific) runtime for both of them.
81    //
82    // safe_intrinsics performs architecture-specific operations that require
83    // the use of target_arch
84    //
85    // the whp/kvm crates are inherently arch-specific, as they contain
86    // low-level bindings to a particular platform's virtualization APIs
87    //
88    // The TMK-related crates run in the guest and are inherently arch-specific.
89    if path.starts_with("guest_test_uefi")
90        || path.starts_with("openhcl/openhcl_boot")
91        || path.starts_with("openhcl/minimal_rt")
92        || path.starts_with("openhcl/sidecar")
93        || path.starts_with("support/safe_intrinsics")
94        || path.starts_with("tmk/simple_tmk")
95        || path.starts_with("tmk/tmk_core")
96        || path.starts_with("vm/whp")
97        || path.starts_with("vm/kvm")
98    {
99        return Ok(());
100    }
101
102    let mut error = false;
103
104    // TODO: this lint really ought to be a dynlint / clippy lint
105    let f = BufReader::new(File::open(path)?);
106    let mut prev_line = String::new();
107    for (i, line) in f.lines().enumerate() {
108        let line = line?;
109        if line.contains("target_arch =") || line.contains("CARGO_CFG_TARGET_ARCH") {
110            // check if current line contains valid suppress, or is commented out
111            if !line.trim().starts_with("//") && !has_suppress(&line) && !has_suppress(&prev_line) {
112                error = true;
113                log::error!(
114                    "unjustified `cfg(target_arch = ...)`: {}:{}",
115                    path.display(),
116                    i + 1
117                );
118            }
119        }
120        prev_line = line;
121    }
122
123    if error {
124        Err(anyhow!(
125            "found unjustified uses of `cfg(target_arch = ...)` in {}",
126            path.display()
127        ))
128    } else {
129        Ok(())
130    }
131}