Skip to content

Commit 6a6c1cd

Browse files
committed
Refactor target_feature parsing
1 parent d60e71d commit 6a6c1cd

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

riscv-rt/build.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn add_linker_script(arch_width: u32) -> io::Result<()> {
1818
}
1919

2020
/// Parse the target RISC-V architecture and returns its bit width and the extension set
21-
fn parse_target(target: &str) -> (u32, HashSet<char>) {
21+
fn parse_target(target: &str, cargo_flags: &str) -> (u32, HashSet<char>) {
2222
// isolate bit width and extensions from the rest of the target information
2323
let arch = target
2424
.trim_start_matches("riscv")
@@ -43,30 +43,39 @@ fn parse_target(target: &str) -> (u32, HashSet<char>) {
4343
extensions.insert('d');
4444
}
4545

46-
(bits, extensions)
47-
}
48-
49-
fn main() {
50-
let target = env::var("TARGET").unwrap();
51-
let _name = env::var("CARGO_PKG_NAME").unwrap();
52-
53-
// This is required until target_feature risc-v work is stable and in-use (rust 1.75.0)
54-
let cargo_flags = env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();
5546
let cargo_flags = cargo_flags
5647
.split(0x1fu8 as char)
5748
.filter(|arg| !arg.is_empty());
58-
59-
let target_features = cargo_flags
49+
50+
cargo_flags
6051
.filter(|k| k.starts_with("target-feature=")).flat_map(|str| {
6152
let flags = str.split('=').collect::<Vec<&str>>()[1];
6253
flags.split(',')
54+
})
55+
.for_each(|feature| {
56+
let chars = feature.chars().collect::<Vec<char>>();
57+
match chars[0] {
58+
'+' => { extensions.insert(chars[1]); }
59+
'-' => { extensions.remove(&chars[1]); }
60+
_ => { panic!("Unsupported target feature operation"); }
61+
}
6362
});
6463

64+
(bits, extensions)
65+
}
66+
67+
fn main() {
68+
let target = env::var("TARGET").unwrap();
69+
let cargo_flags = env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();
70+
let _name = env::var("CARGO_PKG_NAME").unwrap();
71+
6572
// set configuration flags depending on the target
6673
if target.starts_with("riscv") {
6774
println!("cargo:rustc-cfg=riscv");
6875

69-
let (bits, mut extensions) = parse_target(&target);
76+
// This is required until target_arch & target_feature risc-v work is
77+
// stable and in-use (rust 1.75.0)
78+
let (bits, extensions) = parse_target(&target, &cargo_flags);
7079

7180
// generate the linker script and expose the ISA width
7281
let arch_width = match bits {
@@ -82,15 +91,6 @@ fn main() {
8291
};
8392
add_linker_script(arch_width).unwrap();
8493

85-
target_features.for_each(|feature| {
86-
let chars = feature.chars().collect::<Vec<char>>();
87-
if chars[0] == '+' {
88-
extensions.insert(chars[1]);
89-
} else if chars[0] == '-' {
90-
extensions.remove(&chars[1]);
91-
}
92-
});
93-
9494
// expose the ISA extensions
9595
for ext in &extensions {
9696
println!("cargo:rustc-cfg=riscv{}", ext);

0 commit comments

Comments
 (0)