1 // SPDX-License-Identifier: GPL-2.0
3 //! The custom target specification file generator for `rustc`.
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.
11 //! [RFC 131]: https://rust-lang.github.io/rfcs/0131-target-specification.html
15 fmt::{Display, Formatter, Result},
26 type Object = Vec<(String, Value)>;
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 {
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)?;
42 write!(formatter, "\"{}\": {}", last.0, last.1)?;
44 formatter.write_str("}")
50 struct TargetSpec(Object);
53 fn new() -> TargetSpec {
54 TargetSpec(Vec::new())
59 fn push(&mut self, key: &str, value: T);
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)));
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)));
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)));
80 impl Push<&str> for TargetSpec {
81 fn push(&mut self, key: &str, value: &str) {
82 self.push(key, value.to_string());
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)));
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)?;
100 write!(formatter, " \"{}\": {}\n", last.0, last.1)?;
102 formatter.write_str("}")
106 struct KernelConfig(HashMap<String, String>);
109 /// Parses `include/config/auto.conf` from `stdin`.
110 fn from_stdin() -> KernelConfig {
111 let mut result = HashMap::new();
113 let stdin = std::io::stdin();
114 let mut handle = stdin.lock();
115 let mut line = String::new();
120 if handle.read_line(&mut line).unwrap() == 0 {
124 if line.starts_with('#') {
128 let (key, value) = line.split_once('=').expect("Missing `=` in line.");
129 result.insert(key.to_string(), value.trim_end_matches('\n').to_string());
135 /// Does the option exist in the configuration (any value)?
137 /// The argument must be passed without the `CONFIG_` prefix.
138 /// This avoids repetition and it also avoids `fixdep` making us
140 fn has(&self, option: &str) -> bool {
141 let option = "CONFIG_".to_owned() + option;
142 self.0.contains_key(&option)
147 let cfg = KernelConfig::from_stdin();
148 let mut ts = TargetSpec::new();
150 // `llvm-target`s are taken from `scripts/Makefile.clang`.
151 if cfg.has("X86_64") {
152 ts.push("arch", "x86_64");
155 "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
157 let mut features = "-3dnow,-3dnowa,-mmx,+soft-float".to_string();
158 if cfg.has("RETPOLINE") {
159 features += ",+retpoline-external-thunk";
161 ts.push("features", features);
162 ts.push("llvm-target", "x86_64-linux-gnu");
163 ts.push("target-pointer-width", "64");
165 panic!("Unsupported architecture");
168 ts.push("emit-debug-gdb-scripts", false);
169 ts.push("frame-pointer", "may-omit");
172 vec![("kind".to_string(), Value::String("none".to_string()))],
175 // Everything else is LE, whether `CPU_LITTLE_ENDIAN` is declared or not
176 // (e.g. x86). It is also `rustc`'s default.
177 if cfg.has("CPU_BIG_ENDIAN") {
178 ts.push("target-endian", "big");