Skip to main content

bootstrap/core/
backend.rs

1use std::str::FromStr;
2
3/// Represents a codegen backend.
4#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
5pub enum CodegenBackendKind {
6    #[default]
7    Llvm,
8    Cranelift,
9    Gcc,
10    Custom(String),
11}
12
13impl CodegenBackendKind {
14    /// Name of the codegen backend, as identified in the `compiler` directory
15    /// (`rustc_codegen_<name>`).
16    pub(crate) fn name(&self) -> &str {
17        match self {
18            CodegenBackendKind::Llvm => "llvm",
19            CodegenBackendKind::Cranelift => "cranelift",
20            CodegenBackendKind::Gcc => "gcc",
21            CodegenBackendKind::Custom(name) => name,
22        }
23    }
24
25    /// Name of the codegen backend's crate, e.g. `rustc_codegen_cranelift`.
26    pub(crate) fn crate_name(&self) -> String {
27        format!("rustc_codegen_{}", self.name())
28    }
29
30    pub(crate) fn is_llvm(&self) -> bool {
31        matches!(self, Self::Llvm)
32    }
33}
34
35/// FIXME(Zalathar): This is partly redundant with the parsing code in `parse_codegen_backends`.
36impl FromStr for CodegenBackendKind {
37    type Err = &'static str;
38
39    fn from_str(s: &str) -> Result<Self, Self::Err> {
40        match s.to_lowercase().as_str() {
41            "" => Err("Invalid empty backend name"),
42            "gcc" => Ok(Self::Gcc),
43            "llvm" => Ok(Self::Llvm),
44            "cranelift" => Ok(Self::Cranelift),
45            _ => Ok(Self::Custom(s.to_string())),
46        }
47    }
48}