-
Notifications
You must be signed in to change notification settings - Fork 507
feat: Query rustc for clang target triples instead of hardcoding them #1004
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d45f70
Add new workspace crate `gen-target-info`
NobodyXu d6161e8
Fix msrv CI: Pass `--locked` to `cargo` to use constructed lockfile
NobodyXu 6541ea5
Fix msrv: Use edition 2018 in workspace gen-target-info
NobodyXu 22ef3fe
Fix `gen-target-info`: Generate formatted rust code
NobodyXu 5505ce0
Fix `gen-target-info`
NobodyXu f7b04cf
Format `Cargo.toml`
NobodyXu f1baca9
Rename `write_string_mapping_to_file` to `write_target_tuple_mapping`
NobodyXu 2cc4f4a
Refactor: Extract new fn `generate_riscv_arch_mapping`
NobodyXu 2894654
Add doc for the `target_info.rs` to warn against manually editing the…
NobodyXu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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(); | ||
NobodyXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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 hidden or 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"), | ||
]; | ||
NobodyXu marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.