|
1 | | -use gen_target_info::{get_target_specs_from_json, write_target_tuple_mapping, RustcTargetSpecs}; |
2 | | -use std::{collections::BTreeMap, fs::File, io::Write as _}; |
| 1 | +use std::io::Write as _; |
| 2 | +use std::{fs::File, io::BufRead}; |
| 3 | + |
| 4 | +use gen_target_info::{ |
| 5 | + get_target_spec_from_msrv, get_target_specs_from_json, get_targets_msrv, RustcTargetSpecs, |
| 6 | +}; |
3 | 7 |
|
4 | 8 | const PRELUDE: &str = r#"//! This file is generated code. Please edit the generator |
5 | 9 | //! in dev-tools/gen-target-info if you need to make changes. |
6 | 10 |
|
7 | 11 | "#; |
8 | 12 |
|
9 | | -fn generate_riscv_arch_mapping(f: &mut File, target_specs: &RustcTargetSpecs) { |
10 | | - let riscv_target_mapping = target_specs |
11 | | - .0 |
12 | | - .iter() |
13 | | - .filter_map(|(target, target_spec)| { |
14 | | - let arch = target.split_once('-').unwrap().0; |
15 | | - (arch.contains("riscv") && arch != target_spec.arch) |
16 | | - .then_some((arch, &*target_spec.arch)) |
17 | | - }) |
18 | | - .collect::<BTreeMap<_, _>>(); |
19 | | - write_target_tuple_mapping(f, "RISCV_ARCH_MAPPING", &riscv_target_mapping); |
20 | | -} |
| 13 | +fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std::io::Result<()> { |
| 14 | + writeln!(f, "use super::Target;")?; |
| 15 | + writeln!(f, "use std::borrow::Cow;")?; |
| 16 | + writeln!(f)?; |
| 17 | + writeln!(f, "pub(crate) const LIST: &[(&str, Target)] = &[")?; |
| 18 | + |
| 19 | + for (triple, spec) in &target_specs.0 { |
| 20 | + let full_arch = triple.split_once('-').unwrap().0; |
| 21 | + let arch = &spec.arch; |
| 22 | + let vendor = spec.vendor.as_deref().unwrap_or("unknown"); |
| 23 | + let os = spec.os.as_deref().unwrap_or("none"); |
| 24 | + let env = spec.env.as_deref().unwrap_or(""); |
| 25 | + let abi = spec.abi.as_deref().unwrap_or(""); |
| 26 | + |
| 27 | + writeln!(f, " (")?; |
| 28 | + writeln!(f, " {triple:?},")?; |
| 29 | + writeln!(f, " Target {{")?; |
| 30 | + writeln!(f, " full_arch: Cow::Borrowed({full_arch:?}),")?; |
| 31 | + writeln!(f, " arch: Cow::Borrowed({arch:?}),")?; |
| 32 | + writeln!(f, " vendor: Cow::Borrowed({vendor:?}),")?; |
| 33 | + writeln!(f, " os: Cow::Borrowed({os:?}),")?; |
| 34 | + writeln!(f, " env: Cow::Borrowed({env:?}),")?; |
| 35 | + writeln!(f, " abi: Cow::Borrowed({abi:?}),")?; |
| 36 | + writeln!(f, " }},")?; |
| 37 | + writeln!(f, " ),")?; |
| 38 | + } |
| 39 | + |
| 40 | + writeln!(f, "];")?; |
21 | 41 |
|
22 | | -fn generate_windows_triple_mapping(f: &mut File, target_specs: &RustcTargetSpecs) { |
23 | | - let windows_target_mapping = target_specs |
24 | | - .0 |
25 | | - .iter() |
26 | | - .filter_map(|(target, target_spec)| { |
27 | | - let rust_target_parts = target.splitn(4, '-').collect::<Vec<_>>(); |
28 | | - let os = *rust_target_parts.get(2)?; |
29 | | - (os.contains("windows") && target != &*target_spec.llvm_target) |
30 | | - .then_some((&**target, &*target_spec.llvm_target)) |
31 | | - }) |
32 | | - .collect::<BTreeMap<_, _>>(); |
33 | | - write_target_tuple_mapping(f, "WINDOWS_TRIPLE_MAPPING", &windows_target_mapping); |
| 42 | + Ok(()) |
34 | 43 | } |
35 | 44 |
|
36 | 45 | fn main() { |
37 | | - let target_specs = get_target_specs_from_json(); |
| 46 | + // Primarily use information from nightly. |
| 47 | + let mut target_specs = get_target_specs_from_json(); |
| 48 | + // Next, read from MSRV to support old, removed targets. |
| 49 | + for target_triple in get_targets_msrv().lines() { |
| 50 | + let target_triple = target_triple.unwrap(); |
| 51 | + let target_triple = target_triple.trim(); |
| 52 | + target_specs |
| 53 | + .0 |
| 54 | + .entry(target_triple.to_string()) |
| 55 | + .or_insert_with(|| get_target_spec_from_msrv(target_triple)); |
| 56 | + } |
38 | 57 |
|
39 | 58 | // Open file to write to |
40 | 59 | let manifest_dir = env!("CARGO_MANIFEST_DIR"); |
41 | 60 |
|
42 | | - let path = format!("{manifest_dir}/../../src/target_info.rs"); |
43 | | - let mut f = File::create(path).expect("failed to create src/target_info.rs"); |
| 61 | + let path = format!("{manifest_dir}/../../src/target/generated.rs"); |
| 62 | + let mut f = File::create(path).expect("failed to create src/target/generated.rs"); |
44 | 63 |
|
45 | 64 | f.write_all(PRELUDE.as_bytes()).unwrap(); |
46 | 65 |
|
47 | 66 | // Start generating |
48 | | - generate_riscv_arch_mapping(&mut f, &target_specs); |
49 | | - generate_windows_triple_mapping(&mut f, &target_specs); |
| 67 | + generate_target_mapping(&mut f, &target_specs).unwrap(); |
50 | 68 |
|
51 | 69 | // Flush the data onto disk |
52 | 70 | f.flush().unwrap(); |
|
0 commit comments