Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cargo's target information when possible #1225

Merged
merged 18 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/regenerate-target-info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ jobs:
git checkout -b regenerate-target-info-${{ github.run_id }}

- name: Install rust
# Install both MSRV and current nightly
run: |
rustup toolchain install stable nightly --no-self-update --profile minimal
rustup toolchain install 1.63 stable nightly --no-self-update --profile minimal

- name: Create lockfile
run: cargo update

- uses: Swatinem/rust-cache@v2
with:
cache-all-crates: 'true'
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target
/target
Cargo.lock
.idea
*.iml
2 changes: 1 addition & 1 deletion dev-tools/gen-target-info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod target_specs;
pub use target_specs::*;

mod read;
pub use read::get_target_specs_from_json;
pub use read::*;

mod write;
pub use write::*;
80 changes: 49 additions & 31 deletions dev-tools/gen-target-info/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,70 @@
use gen_target_info::{get_target_specs_from_json, write_target_tuple_mapping, RustcTargetSpecs};
use std::{collections::BTreeMap, fs::File, io::Write as _};
use std::io::Write as _;
use std::{fs::File, io::BufRead};

use gen_target_info::{
get_target_spec_from_msrv, get_target_specs_from_json, get_targets_msrv, RustcTargetSpecs,
};

const PRELUDE: &str = r#"//! This file is generated code. Please edit the generator
//! in dev-tools/gen-target-info if you need to make changes.

"#;

fn generate_riscv_arch_mapping(f: &mut File, target_specs: &RustcTargetSpecs) {
let riscv_target_mapping = target_specs
.0
.iter()
.filter_map(|(target, target_spec)| {
let arch = target.split_once('-').unwrap().0;
(arch.contains("riscv") && arch != target_spec.arch)
.then_some((arch, &*target_spec.arch))
})
.collect::<BTreeMap<_, _>>();
write_target_tuple_mapping(f, "RISCV_ARCH_MAPPING", &riscv_target_mapping);
}
fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std::io::Result<()> {
writeln!(f, "use super::Target;")?;
writeln!(f, "use std::borrow::Cow;")?;
writeln!(f)?;
writeln!(f, "pub(crate) const LIST: &[(&str, Target)] = &[")?;

for (triple, spec) in &target_specs.0 {
let full_arch = triple.split_once('-').unwrap().0;
let arch = &spec.arch;
let vendor = spec.vendor.as_deref().unwrap_or("unknown");
let os = spec.os.as_deref().unwrap_or("none");
let env = spec.env.as_deref().unwrap_or("");
let abi = spec.abi.as_deref().unwrap_or("");

writeln!(f, " (")?;
writeln!(f, " {triple:?},")?;
writeln!(f, " Target {{")?;
writeln!(f, " full_arch: Cow::Borrowed({full_arch:?}),")?;
writeln!(f, " arch: Cow::Borrowed({arch:?}),")?;
writeln!(f, " vendor: Cow::Borrowed({vendor:?}),")?;
writeln!(f, " os: Cow::Borrowed({os:?}),")?;
writeln!(f, " env: Cow::Borrowed({env:?}),")?;
writeln!(f, " abi: Cow::Borrowed({abi:?}),")?;
writeln!(f, " }},")?;
writeln!(f, " ),")?;
}

writeln!(f, "];")?;

fn generate_windows_triple_mapping(f: &mut File, target_specs: &RustcTargetSpecs) {
let windows_target_mapping = target_specs
.0
.iter()
.filter_map(|(target, target_spec)| {
let rust_target_parts = target.splitn(4, '-').collect::<Vec<_>>();
let os = *rust_target_parts.get(2)?;
(os.contains("windows") && target != &*target_spec.llvm_target)
.then_some((&**target, &*target_spec.llvm_target))
})
.collect::<BTreeMap<_, _>>();
write_target_tuple_mapping(f, "WINDOWS_TRIPLE_MAPPING", &windows_target_mapping);
Ok(())
}

fn main() {
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
let target_specs = get_target_specs_from_json();
// Primarily use information from nightly.
let mut target_specs = get_target_specs_from_json();
// Next, read from MSRV to support old, removed targets.
for target_triple in get_targets_msrv().lines() {
let target_triple = target_triple.unwrap();
let target_triple = target_triple.trim();
target_specs
.0
.entry(target_triple.to_string())
.or_insert_with(|| get_target_spec_from_msrv(target_triple));
}

// Open file to write to
let manifest_dir = env!("CARGO_MANIFEST_DIR");

let path = format!("{manifest_dir}/../../src/target_info.rs");
let mut f = File::create(path).expect("failed to create src/target_info.rs");
let path = format!("{manifest_dir}/../../src/target/generated.rs");
let mut f = File::create(path).expect("failed to create src/target/generated.rs");

f.write_all(PRELUDE.as_bytes()).unwrap();

// Start generating
generate_riscv_arch_mapping(&mut f, &target_specs);
generate_windows_triple_mapping(&mut f, &target_specs);
generate_target_mapping(&mut f, &target_specs).unwrap();

// Flush the data onto disk
f.flush().unwrap();
Expand Down
45 changes: 42 additions & 3 deletions dev-tools/gen-target-info/src/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
use std::process;

use crate::RustcTargetSpecs;
use crate::{RustcTargetSpecs, TargetSpec};

pub fn get_targets_msrv() -> Vec<u8> {
let mut cmd = process::Command::new("rustc");
cmd.args(["+1.63", "--print", "target-list"]);
cmd.stdout(process::Stdio::piped());
cmd.stderr(process::Stdio::inherit());

let process::Output { status, stdout, .. } = cmd.output().unwrap();

if !status.success() {
panic!("{:?} failed with non-zero exit status: {}", cmd, status)
}

stdout
}

pub fn get_target_spec_from_msrv(target: &str) -> TargetSpec {
let mut cmd = process::Command::new("rustc");
cmd.args([
"+1.63",
"-Zunstable-options",
"--print",
"target-spec-json",
"--target",
target,
]);
cmd.env("RUSTC_BOOTSTRAP", "1");
cmd.stdout(process::Stdio::piped());
cmd.stderr(process::Stdio::inherit());

let process::Output { status, stdout, .. } = cmd.output().unwrap();

if !status.success() {
panic!("{:?} failed with non-zero exit status: {}", cmd, status)
}

serde_json::from_slice(&stdout).unwrap()
}

pub fn get_target_specs_from_json() -> RustcTargetSpecs {
let mut cmd = process::Command::new("rustc");
Expand All @@ -9,8 +47,9 @@ pub fn get_target_specs_from_json() -> RustcTargetSpecs {
"-Zunstable-options",
"--print",
"all-target-specs-json",
])
.stdout(process::Stdio::piped());
]);
cmd.stdout(process::Stdio::piped());
cmd.stderr(process::Stdio::inherit());

let process::Output { status, stdout, .. } = cmd.output().unwrap();

Expand Down
2 changes: 2 additions & 0 deletions dev-tools/gen-target-info/src/target_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct TargetSpec {
pub os: Option<String>,
/// `apple`, `pc`
pub vendor: Option<String>,
pub env: Option<String>,
pub abi: Option<String>,
pub pre_link_args: Option<PreLinkArgs>,
}

Expand Down
Loading