Loading...
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
13use std::{
14 collections::HashMap,
15 fmt::{Display, Formatter, Result},
16 io::BufRead,
17};
18
19enum Value {
20 Boolean(bool),
21 Number(i32),
22 String(String),
23 Object(Object),
24}
25
26type Object = Vec<(String, Value)>;
27
28/// Minimal "almost JSON" generator (e.g. no `null`s, no arrays, no escaping),
29/// enough for this purpose.
30impl 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
50struct TargetSpec(Object);
51
52impl TargetSpec {
53 fn new() -> TargetSpec {
54 TargetSpec(Vec::new())
55 }
56}
57
58trait Push<T> {
59 fn push(&mut self, key: &str, value: T);
60}
61
62impl 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
68impl 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
74impl 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
80impl Push<&str> for TargetSpec {
81 fn push(&mut self, key: &str, value: &str) {
82 self.push(key, value.to_string());
83 }
84}
85
86impl 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
92impl 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
106struct KernelConfig(HashMap<String, String>);
107
108impl 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
146fn 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("X86_64") {
152 ts.push("arch", "x86_64");
153 ts.push(
154 "data-layout",
155 "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
156 );
157 let mut features = "-3dnow,-3dnowa,-mmx,+soft-float".to_string();
158 if cfg.has("RETPOLINE") {
159 features += ",+retpoline-external-thunk";
160 }
161 ts.push("features", features);
162 ts.push("llvm-target", "x86_64-linux-gnu");
163 ts.push("target-pointer-width", "64");
164 } else if cfg.has("LOONGARCH") {
165 ts.push("arch", "loongarch64");
166 ts.push("data-layout", "e-m:e-p:64:64-i64:64-i128:128-n64-S128");
167 ts.push("features", "-f,-d");
168 ts.push("llvm-target", "loongarch64-linux-gnusf");
169 ts.push("llvm-abiname", "lp64s");
170 ts.push("target-pointer-width", "64");
171 } else {
172 panic!("Unsupported architecture");
173 }
174
175 ts.push("emit-debug-gdb-scripts", false);
176 ts.push("frame-pointer", "may-omit");
177 ts.push(
178 "stack-probes",
179 vec![("kind".to_string(), Value::String("none".to_string()))],
180 );
181
182 // Everything else is LE, whether `CPU_LITTLE_ENDIAN` is declared or not
183 // (e.g. x86). It is also `rustc`'s default.
184 if cfg.has("CPU_BIG_ENDIAN") {
185 ts.push("target-endian", "big");
186 }
187
188 println!("{}", ts);
189}
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
13use std::{
14 collections::HashMap,
15 fmt::{Display, Formatter, Result},
16 io::BufRead,
17};
18
19enum Value {
20 Boolean(bool),
21 Number(i32),
22 String(String),
23 Array(Vec<Value>),
24 Object(Object),
25}
26
27type Object = Vec<(String, Value)>;
28
29fn comma_sep<T>(
30 seq: &[T],
31 formatter: &mut Formatter<'_>,
32 f: impl Fn(&mut Formatter<'_>, &T) -> Result,
33) -> Result {
34 if let [ref rest @ .., ref last] = seq[..] {
35 for v in rest {
36 f(formatter, v)?;
37 formatter.write_str(",")?;
38 }
39 f(formatter, last)?;
40 }
41 Ok(())
42}
43
44/// Minimal "almost JSON" generator (e.g. no `null`s, no escaping),
45/// enough for this purpose.
46impl Display for Value {
47 fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
48 match self {
49 Value::Boolean(boolean) => write!(formatter, "{}", boolean),
50 Value::Number(number) => write!(formatter, "{}", number),
51 Value::String(string) => write!(formatter, "\"{}\"", string),
52 Value::Array(values) => {
53 formatter.write_str("[")?;
54 comma_sep(&values[..], formatter, |formatter, v| v.fmt(formatter))?;
55 formatter.write_str("]")
56 }
57 Value::Object(object) => {
58 formatter.write_str("{")?;
59 comma_sep(&object[..], formatter, |formatter, v| {
60 write!(formatter, "\"{}\": {}", v.0, v.1)
61 })?;
62 formatter.write_str("}")
63 }
64 }
65 }
66}
67
68impl From<bool> for Value {
69 fn from(value: bool) -> Self {
70 Self::Boolean(value)
71 }
72}
73
74impl From<i32> for Value {
75 fn from(value: i32) -> Self {
76 Self::Number(value)
77 }
78}
79
80impl From<String> for Value {
81 fn from(value: String) -> Self {
82 Self::String(value)
83 }
84}
85
86impl From<&str> for Value {
87 fn from(value: &str) -> Self {
88 Self::String(value.to_string())
89 }
90}
91
92impl From<Object> for Value {
93 fn from(object: Object) -> Self {
94 Self::Object(object)
95 }
96}
97
98impl<T: Into<Value>, const N: usize> From<[T; N]> for Value {
99 fn from(i: [T; N]) -> Self {
100 Self::Array(i.into_iter().map(|v| v.into()).collect())
101 }
102}
103
104struct TargetSpec(Object);
105
106impl TargetSpec {
107 fn new() -> TargetSpec {
108 TargetSpec(Vec::new())
109 }
110
111 fn push(&mut self, key: &str, value: impl Into<Value>) {
112 self.0.push((key.to_string(), value.into()));
113 }
114}
115
116impl Display for TargetSpec {
117 fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
118 // We add some newlines for clarity.
119 formatter.write_str("{\n")?;
120 if let [ref rest @ .., ref last] = self.0[..] {
121 for (key, value) in rest {
122 write!(formatter, " \"{}\": {},\n", key, value)?;
123 }
124 write!(formatter, " \"{}\": {}\n", last.0, last.1)?;
125 }
126 formatter.write_str("}")
127 }
128}
129
130struct KernelConfig(HashMap<String, String>);
131
132impl KernelConfig {
133 /// Parses `include/config/auto.conf` from `stdin`.
134 fn from_stdin() -> KernelConfig {
135 let mut result = HashMap::new();
136
137 let stdin = std::io::stdin();
138 let mut handle = stdin.lock();
139 let mut line = String::new();
140
141 loop {
142 line.clear();
143
144 if handle.read_line(&mut line).unwrap() == 0 {
145 break;
146 }
147
148 if line.starts_with('#') {
149 continue;
150 }
151
152 let (key, value) = line.split_once('=').expect("Missing `=` in line.");
153 result.insert(key.to_string(), value.trim_end_matches('\n').to_string());
154 }
155
156 KernelConfig(result)
157 }
158
159 /// Does the option exist in the configuration (any value)?
160 ///
161 /// The argument must be passed without the `CONFIG_` prefix.
162 /// This avoids repetition and it also avoids `fixdep` making us
163 /// depend on it.
164 fn has(&self, option: &str) -> bool {
165 let option = "CONFIG_".to_owned() + option;
166 self.0.contains_key(&option)
167 }
168
169 /// Is the rustc version at least `major.minor.patch`?
170 fn rustc_version_atleast(&self, major: u32, minor: u32, patch: u32) -> bool {
171 let check_version = 100000 * major + 100 * minor + patch;
172 let actual_version = self
173 .0
174 .get("CONFIG_RUSTC_VERSION")
175 .unwrap()
176 .parse::<u32>()
177 .unwrap();
178 check_version <= actual_version
179 }
180}
181
182fn main() {
183 let cfg = KernelConfig::from_stdin();
184 let mut ts = TargetSpec::new();
185
186 // `llvm-target`s are taken from `scripts/Makefile.clang`.
187 if cfg.has("ARM64") {
188 panic!("arm64 uses the builtin rustc aarch64-unknown-none target");
189 } else if cfg.has("RISCV") {
190 if cfg.has("64BIT") {
191 panic!("64-bit RISC-V uses the builtin rustc riscv64-unknown-none-elf target");
192 } else {
193 panic!("32-bit RISC-V is an unsupported architecture");
194 }
195 } else if cfg.has("X86_64") {
196 ts.push("arch", "x86_64");
197 if cfg.rustc_version_atleast(1, 86, 0) {
198 ts.push("rustc-abi", "x86-softfloat");
199 }
200 ts.push(
201 "data-layout",
202 "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
203 );
204 let mut features = "-mmx,+soft-float".to_string();
205 if cfg.has("MITIGATION_RETPOLINE") {
206 // The kernel uses `-mretpoline-external-thunk` (for Clang), which Clang maps to the
207 // target feature of the same name plus the other two target features in
208 // `clang/lib/Driver/ToolChains/Arch/X86.cpp`. These should be eventually enabled via
209 // `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated
210 // flag); see https://github.com/rust-lang/rust/issues/116852.
211 features += ",+retpoline-external-thunk";
212 features += ",+retpoline-indirect-branches";
213 features += ",+retpoline-indirect-calls";
214 }
215 if cfg.has("MITIGATION_SLS") {
216 // The kernel uses `-mharden-sls=all`, which Clang maps to both these target features in
217 // `clang/lib/Driver/ToolChains/Arch/X86.cpp`. These should be eventually enabled via
218 // `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated
219 // flag); see https://github.com/rust-lang/rust/issues/116851.
220 features += ",+harden-sls-ijmp";
221 features += ",+harden-sls-ret";
222 }
223 ts.push("features", features);
224 ts.push("llvm-target", "x86_64-linux-gnu");
225 ts.push("supported-sanitizers", ["kcfi", "kernel-address"]);
226 ts.push("target-pointer-width", "64");
227 } else if cfg.has("X86_32") {
228 // This only works on UML, as i386 otherwise needs regparm support in rustc
229 if !cfg.has("UML") {
230 panic!("32-bit x86 only works under UML");
231 }
232 ts.push("arch", "x86");
233 if cfg.rustc_version_atleast(1, 86, 0) {
234 ts.push("rustc-abi", "x86-softfloat");
235 }
236 ts.push(
237 "data-layout",
238 "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",
239 );
240 let mut features = "-mmx,+soft-float".to_string();
241 if cfg.has("MITIGATION_RETPOLINE") {
242 features += ",+retpoline-external-thunk";
243 }
244 ts.push("features", features);
245 ts.push("llvm-target", "i386-unknown-linux-gnu");
246 ts.push("target-pointer-width", "32");
247 } else if cfg.has("LOONGARCH") {
248 panic!("loongarch uses the builtin rustc loongarch64-unknown-none-softfloat target");
249 } else {
250 panic!("Unsupported architecture");
251 }
252
253 ts.push("emit-debug-gdb-scripts", false);
254 ts.push("frame-pointer", "may-omit");
255 ts.push(
256 "stack-probes",
257 vec![("kind".to_string(), Value::String("none".to_string()))],
258 );
259
260 // Everything else is LE, whether `CPU_LITTLE_ENDIAN` is declared or not
261 // (e.g. x86). It is also `rustc`'s default.
262 if cfg.has("CPU_BIG_ENDIAN") {
263 ts.push("target-endian", "big");
264 }
265
266 println!("{}", ts);
267}