scripts/generate_rust_target.rs

Source file repositories/reference/linux-study-clean/scripts/generate_rust_target.rs

File Facts

System
Linux kernel
Corpus path
scripts/generate_rust_target.rs
Extension
.rs
Size
9438 bytes
Lines
284
Domain
Support Tooling And Documentation
Bucket
scripts
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

fn push(&mut self, key: &str, value: impl Into<Value>) {
        self.0.push((key.to_string(), value.into()));
    }
}

impl Display for TargetSpec {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
        // We add some newlines for clarity.
        formatter.write_str("{\n")?;
        if let [ref rest @ .., ref last] = self.0[..] {
            for (key, value) in rest {
                write!(formatter, "    \"{}\": {},\n", key, value)?;
            }
            write!(formatter, "    \"{}\": {}\n", last.0, last.1)?;
        }
        formatter.write_str("}")
    }
}

struct KernelConfig(HashMap<String, String>);

impl KernelConfig {
    /// Parses `include/config/auto.conf` from `stdin`.
    fn from_stdin() -> KernelConfig {
        let mut result = HashMap::new();

        let stdin = std::io::stdin();
        let mut handle = stdin.lock();
        let mut line = String::new();

        loop {
            line.clear();

            if handle.read_line(&mut line).unwrap() == 0 {
                break;
            }

            if line.starts_with('#') {
                continue;
            }

            let (key, value) = line.split_once('=').expect("Missing `=` in line.");
            result.insert(key.to_string(), value.trim_end_matches('\n').to_string());
        }

        KernelConfig(result)
    }

    /// Does the option exist in the configuration (any value)?
    ///
    /// The argument must be passed without the `CONFIG_` prefix.
    /// This avoids repetition and it also avoids `fixdep` making us
    /// depend on it.
    fn has(&self, option: &str) -> bool {
        let option = "CONFIG_".to_owned() + option;
        self.0.contains_key(&option)
    }

    /// Is the rustc version at least `major.minor.patch`?
    fn rustc_version_atleast(&self, major: u32, minor: u32, patch: u32) -> bool {
        let check_version = 100000 * major + 100 * minor + patch;
        let actual_version = self
            .0
            .get("CONFIG_RUSTC_VERSION")
            .unwrap()
            .parse::<u32>()
            .unwrap();
        check_version <= actual_version
    }
}

fn main() {
    let cfg = KernelConfig::from_stdin();
    let mut ts = TargetSpec::new();

    // `llvm-target`s are taken from `scripts/Makefile.clang`.
    if cfg.has("ARM") {
        panic!("arm uses the builtin rustc target");
    } else if cfg.has("ARM64") {
        panic!("arm64 uses the builtin rustc aarch64-unknown-none target");
    } else if cfg.has("RISCV") {
        if cfg.has("64BIT") {
            panic!("64-bit RISC-V uses the builtin rustc riscv64-unknown-none-elf target");
        } else {
            panic!("32-bit RISC-V is an unsupported architecture");
        }
    } else if cfg.has("X86_64") {
        ts.push("arch", "x86_64");
        if cfg.rustc_version_atleast(1, 98, 0) {
            ts.push("rustc-abi", "softfloat");

Annotation

Implementation Notes