Skip to content

Commit 0b60b02

Browse files
committed
Use Cargo's target information when possible
rustc's target triples generally only have a vague resemblance to each other and to the information needed by `cc`. Let's instead prefer `CARGO_CFG_*` variables when available, since these contain the information directly from the compiler itself. In the cases where it isn't available (i.e. when running outside of a build script), we fall back to parsing the target triple, but instead of doing it in an ad-hoc fashion with string manipulation, we do it in a more structured fashion up front.
1 parent 268fb9b commit 0b60b02

File tree

5 files changed

+715
-429
lines changed

5 files changed

+715
-429
lines changed

dev-tools/gen-target-info/src/main.rs

+2-33
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,11 @@
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::{fs::File, io::Write as _};
32

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

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-
}
21-
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);
34-
}
35-
368
fn main() {
37-
let target_specs = get_target_specs_from_json();
38-
399
// Open file to write to
4010
let manifest_dir = env!("CARGO_MANIFEST_DIR");
4111

@@ -45,8 +15,7 @@ fn main() {
4515
f.write_all(PRELUDE.as_bytes()).unwrap();
4616

4717
// Start generating
48-
generate_riscv_arch_mapping(&mut f, &target_specs);
49-
generate_windows_triple_mapping(&mut f, &target_specs);
18+
// TODO
5019

5120
// Flush the data onto disk
5221
f.flush().unwrap();

0 commit comments

Comments
 (0)