xref: /linux/scripts/generate_rust_target.rs (revision c4d7f546dd9aa9780716cdb07416ca97264dce43)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! The custom target specification file generator for `rustc`.
4 //!
5 //! To configure a target from scratch, a JSON-encoded file has to be passed
6 //! to `rustc` (introduced in [RFC 131]). These options and the file itself are
7 //! unstable. Eventually, `rustc` should provide a way to do this in a stable
8 //! manner. For instance, via command-line arguments. Therefore, this file
9 //! should avoid using keys which can be set via `-C` or `-Z` options.
10 //!
11 //! [RFC 131]: https://rust-lang.github.io/rfcs/0131-target-specification.html
12 
13 use std::{
14     collections::HashMap,
15     fmt::{Display, Formatter, Result},
16     io::BufRead,
17 };
18 
19 enum Value {
20     Boolean(bool),
21     Number(i32),
22     String(String),
23     Object(Object),
24 }
25 
26 type Object = Vec<(String, Value)>;
27 
28 /// Minimal "almost JSON" generator (e.g. no `null`s, no arrays, no escaping),
29 /// enough for this purpose.
30 impl Display for Value {
31     fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
32         match self {
33             Value::Boolean(boolean) => write!(formatter, "{}", boolean),
34             Value::Number(number) => write!(formatter, "{}", number),
35             Value::String(string) => write!(formatter, "\"{}\"", string),
36             Value::Object(object) => {
37                 formatter.write_str("{")?;
38                 if let [ref rest @ .., ref last] = object[..] {
39                     for (key, value) in rest {
40                         write!(formatter, "\"{}\": {},", key, value)?;
41                     }
42                     write!(formatter, "\"{}\": {}", last.0, last.1)?;
43                 }
44                 formatter.write_str("}")
45             }
46         }
47     }
48 }
49 
50 struct TargetSpec(Object);
51 
52 impl TargetSpec {
53     fn new() -> TargetSpec {
54         TargetSpec(Vec::new())
55     }
56 }
57 
58 trait Push<T> {
59     fn push(&mut self, key: &str, value: T);
60 }
61 
62 impl Push<bool> for TargetSpec {
63     fn push(&mut self, key: &str, value: bool) {
64         self.0.push((key.to_string(), Value::Boolean(value)));
65     }
66 }
67 
68 impl Push<i32> for TargetSpec {
69     fn push(&mut self, key: &str, value: i32) {
70         self.0.push((key.to_string(), Value::Number(value)));
71     }
72 }
73 
74 impl Push<String> for TargetSpec {
75     fn push(&mut self, key: &str, value: String) {
76         self.0.push((key.to_string(), Value::String(value)));
77     }
78 }
79 
80 impl Push<&str> for TargetSpec {
81     fn push(&mut self, key: &str, value: &str) {
82         self.push(key, value.to_string());
83     }
84 }
85 
86 impl Push<Object> for TargetSpec {
87     fn push(&mut self, key: &str, value: Object) {
88         self.0.push((key.to_string(), Value::Object(value)));
89     }
90 }
91 
92 impl Display for TargetSpec {
93     fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
94         // We add some newlines for clarity.
95         formatter.write_str("{\n")?;
96         if let [ref rest @ .., ref last] = self.0[..] {
97             for (key, value) in rest {
98                 write!(formatter, "    \"{}\": {},\n", key, value)?;
99             }
100             write!(formatter, "    \"{}\": {}\n", last.0, last.1)?;
101         }
102         formatter.write_str("}")
103     }
104 }
105 
106 struct KernelConfig(HashMap<String, String>);
107 
108 impl KernelConfig {
109     /// Parses `include/config/auto.conf` from `stdin`.
110     fn from_stdin() -> KernelConfig {
111         let mut result = HashMap::new();
112 
113         let stdin = std::io::stdin();
114         let mut handle = stdin.lock();
115         let mut line = String::new();
116 
117         loop {
118             line.clear();
119 
120             if handle.read_line(&mut line).unwrap() == 0 {
121                 break;
122             }
123 
124             if line.starts_with('#') {
125                 continue;
126             }
127 
128             let (key, value) = line.split_once('=').expect("Missing `=` in line.");
129             result.insert(key.to_string(), value.trim_end_matches('\n').to_string());
130         }
131 
132         KernelConfig(result)
133     }
134 
135     /// Does the option exist in the configuration (any value)?
136     ///
137     /// The argument must be passed without the `CONFIG_` prefix.
138     /// This avoids repetition and it also avoids `fixdep` making us
139     /// depend on it.
140     fn has(&self, option: &str) -> bool {
141         let option = "CONFIG_".to_owned() + option;
142         self.0.contains_key(&option)
143     }
144 }
145 
146 fn main() {
147     let cfg = KernelConfig::from_stdin();
148     let mut ts = TargetSpec::new();
149 
150     // `llvm-target`s are taken from `scripts/Makefile.clang`.
151     if cfg.has("ARM64") {
152         panic!("arm64 uses the builtin rustc aarch64-unknown-none target");
153     } else if cfg.has("RISCV") {
154         if cfg.has("64BIT") {
155             panic!("64-bit RISC-V uses the builtin rustc riscv64-unknown-none-elf target");
156         } else {
157             panic!("32-bit RISC-V is an unsupported architecture");
158         }
159     } else if cfg.has("X86_64") {
160         ts.push("arch", "x86_64");
161         ts.push(
162             "data-layout",
163             "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
164         );
165         let mut features = "-mmx,+soft-float".to_string();
166         if cfg.has("MITIGATION_RETPOLINE") {
167             // The kernel uses `-mretpoline-external-thunk` (for Clang), which Clang maps to the
168             // target feature of the same name plus the other two target features in
169             // `clang/lib/Driver/ToolChains/Arch/X86.cpp`. These should be eventually enabled via
170             // `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated
171             // flag); see https://github.com/rust-lang/rust/issues/116852.
172             features += ",+retpoline-external-thunk";
173             features += ",+retpoline-indirect-branches";
174             features += ",+retpoline-indirect-calls";
175         }
176         if cfg.has("MITIGATION_SLS") {
177             // The kernel uses `-mharden-sls=all`, which Clang maps to both these target features in
178             // `clang/lib/Driver/ToolChains/Arch/X86.cpp`. These should be eventually enabled via
179             // `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated
180             // flag); see https://github.com/rust-lang/rust/issues/116851.
181             features += ",+harden-sls-ijmp";
182             features += ",+harden-sls-ret";
183         }
184         ts.push("features", features);
185         ts.push("llvm-target", "x86_64-linux-gnu");
186         ts.push("target-pointer-width", "64");
187     } else if cfg.has("X86_32") {
188         // This only works on UML, as i386 otherwise needs regparm support in rustc
189         if !cfg.has("UML") {
190             panic!("32-bit x86 only works under UML");
191         }
192         ts.push("arch", "x86");
193         ts.push(
194             "data-layout",
195             "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128",
196         );
197         let mut features = "-mmx,+soft-float".to_string();
198         if cfg.has("MITIGATION_RETPOLINE") {
199             features += ",+retpoline-external-thunk";
200         }
201         ts.push("features", features);
202         ts.push("llvm-target", "i386-unknown-linux-gnu");
203         ts.push("target-pointer-width", "32");
204     } else if cfg.has("LOONGARCH") {
205         panic!("loongarch uses the builtin rustc loongarch64-unknown-none-softfloat target");
206     } else {
207         panic!("Unsupported architecture");
208     }
209 
210     ts.push("emit-debug-gdb-scripts", false);
211     ts.push("frame-pointer", "may-omit");
212     ts.push(
213         "stack-probes",
214         vec![("kind".to_string(), Value::String("none".to_string()))],
215     );
216 
217     // Everything else is LE, whether `CPU_LITTLE_ENDIAN` is declared or not
218     // (e.g. x86). It is also `rustc`'s default.
219     if cfg.has("CPU_BIG_ENDIAN") {
220         ts.push("target-endian", "big");
221     }
222 
223     println!("{}", ts);
224 }
225