-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Query rustc for clang target triples instead of hardcoding them (…
…#1004) * Add new workspace crate `gen-target-info` For generating `src/target_info.rs`, and use it to simplify riscv target arch mapping logic in `Build::add_default_flags` Signed-off-by: Jiahao XU <[email protected]> * Fix msrv CI: Pass `--locked` to `cargo` to use constructed lockfile Signed-off-by: Jiahao XU <[email protected]> * Fix msrv: Use edition 2018 in workspace gen-target-info Signed-off-by: Jiahao XU <[email protected]> * Fix `gen-target-info`: Generate formatted rust code Signed-off-by: Jiahao XU <[email protected]> * Fix `gen-target-info` Signed-off-by: Jiahao XU <[email protected]> * Format `Cargo.toml` Signed-off-by: Jiahao XU <[email protected]> * Rename `write_string_mapping_to_file` to `write_target_tuple_mapping` Signed-off-by: Jiahao XU <[email protected]> * Refactor: Extract new fn `generate_riscv_arch_mapping` Signed-off-by: Jiahao XU <[email protected]> * Add doc for the `target_info.rs` to warn against manually editing the file Signed-off-by: Jiahao XU <[email protected]> --------- Signed-off-by: Jiahao XU <[email protected]>
- Loading branch information
Showing
10 changed files
with
163 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "gen-target-info" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
serde = { version = "1.0.163", features = ["derive"] } | ||
serde-tuple-vec-map = "1.0.1" | ||
serde_json = "1.0.107" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
mod target_specs; | ||
pub use target_specs::*; | ||
|
||
mod read; | ||
pub use read::get_target_specs_from_json; | ||
|
||
mod write; | ||
pub use write::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use gen_target_info::{get_target_specs_from_json, write_target_tuple_mapping, RustcTargetSpecs}; | ||
use std::{fs::File, io::Write as _}; | ||
|
||
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 mut 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::<Vec<_>>(); | ||
riscv_target_mapping.sort_unstable_by_key(|(arch, _)| &**arch); | ||
riscv_target_mapping.dedup(); | ||
write_target_tuple_mapping(f, "RISCV_ARCH_MAPPING", &riscv_target_mapping); | ||
} | ||
|
||
fn main() { | ||
let target_specs = get_target_specs_from_json(); | ||
|
||
// 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"); | ||
|
||
f.write_all(PRELUDE.as_bytes()).unwrap(); | ||
|
||
// Start generating | ||
generate_riscv_arch_mapping(&mut f, &target_specs); | ||
|
||
// Flush the data onto disk | ||
f.flush().unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::process; | ||
|
||
use crate::RustcTargetSpecs; | ||
|
||
pub fn get_target_specs_from_json() -> RustcTargetSpecs { | ||
let mut cmd = process::Command::new("rustc"); | ||
cmd.args([ | ||
"+nightly", | ||
"-Zunstable-options", | ||
"--print", | ||
"all-target-specs-json", | ||
]) | ||
.stdout(process::Stdio::piped()); | ||
|
||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct PreLinkArgs( | ||
/// First field in the linker name, | ||
/// second field is the args. | ||
#[serde(with = "tuple_vec_map")] | ||
pub Vec<(String, Vec<String>)>, | ||
); | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all(deserialize = "kebab-case"))] | ||
pub struct TargetSpec { | ||
pub arch: String, | ||
pub llvm_target: String, | ||
/// link env to remove, mostly for apple | ||
pub link_env_remove: Option<Vec<String>>, | ||
/// link env to set, mostly for apple, e.g. `ZERO_AR_DATE=1` | ||
pub link_env: Option<Vec<String>>, | ||
pub os: Option<String>, | ||
/// `apple`, `pc` | ||
pub vendor: Option<String>, | ||
pub pre_link_args: Option<PreLinkArgs>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct RustcTargetSpecs( | ||
/// First field in the tuple is the rustc target | ||
#[serde(with = "tuple_vec_map")] | ||
pub Vec<(String, TargetSpec)>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use std::{fmt::Write as _, fs, io::Write as _}; | ||
|
||
pub fn write_target_tuple_mapping(f: &mut fs::File, variable_name: &str, data: &[(&str, &str)]) { | ||
let mut content = format!("pub const {variable_name}: &[(&str, &str)] = &[\n"); | ||
|
||
for (f1, f2) in data { | ||
write!(&mut content, r#" ("{f1}", "{f2}"),"#).unwrap(); | ||
content.push('\n'); | ||
} | ||
|
||
content.push_str("];\n"); | ||
|
||
f.write_all(content.as_bytes()).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//! This file is generated code. Please edit the generator | ||
//! in dev-tools/gen-target-info if you need to make changes. | ||
|
||
pub const RISCV_ARCH_MAPPING: &[(&str, &str)] = &[ | ||
("riscv32gc", "riscv32"), | ||
("riscv32i", "riscv32"), | ||
("riscv32im", "riscv32"), | ||
("riscv32imac", "riscv32"), | ||
("riscv32imafc", "riscv32"), | ||
("riscv32imc", "riscv32"), | ||
("riscv64gc", "riscv64"), | ||
("riscv64imac", "riscv64"), | ||
]; |