Skip to content

Commit 0fed19a

Browse files
committed
Rework riscv -march and -mabi detection
Instead of adding cases for all the operating systems, Use target architecture directly as ISA string, replacing "riscv" with "rv", and detect floating point ABI based on support for the D and F extensions. Fixes #795
1 parent 6fa9ea6 commit 0fed19a

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/lib.rs

+33-20
Original file line numberDiff line numberDiff line change
@@ -2361,29 +2361,42 @@ impl Build {
23612361
let mut parts = target.split('-');
23622362
if let Some(arch) = parts.next() {
23632363
let arch = &arch[5..];
2364+
2365+
// Assume that "rv{arch}" is a valid RISC-V ISA string.
2366+
// The compiler would error out otherwise, and we fix
2367+
// that later.
2368+
cmd.args.push(format!("-march=rv{arch}").into());
2369+
2370+
// Detect single-letter extensions from `arch`, assuming
2371+
// no version numbers and canonical order
2372+
let single_letter = arch
2373+
.split(['_', 'z', 's'])
2374+
.next()
2375+
// The arch string starts with 32 or 64
2376+
.expect("arch string cannot be empty");
2377+
2378+
let riscv_implements = |ext| single_letter.contains(ext);
2379+
2380+
// Detect ABI to select based on de facto standard
2381+
2382+
let float_abi = if riscv_implements("g") || riscv_implements("d") {
2383+
// Implements "d" (double-float), use double-float ABI
2384+
"d"
2385+
} else if riscv_implements("f") {
2386+
// Implements "f" (single-float), use single-float ABI
2387+
"f"
2388+
} else {
2389+
// No floating support, use soft-float ABI
2390+
""
2391+
};
2392+
23642393
if arch.starts_with("64") {
2365-
if target.contains("linux")
2366-
| target.contains("freebsd")
2367-
| target.contains("netbsd")
2368-
| target.contains("linux")
2369-
{
2370-
cmd.args.push(("-march=rv64gc").into());
2371-
cmd.args.push("-mabi=lp64d".into());
2372-
} else {
2373-
cmd.args.push(("-march=rv".to_owned() + arch).into());
2374-
cmd.args.push("-mabi=lp64".into());
2375-
}
2376-
} else if arch.starts_with("32") {
2377-
if target.contains("linux") {
2378-
cmd.args.push(("-march=rv32gc").into());
2379-
cmd.args.push("-mabi=ilp32d".into());
2380-
} else {
2381-
cmd.args.push(("-march=rv".to_owned() + arch).into());
2382-
cmd.args.push("-mabi=ilp32".into());
2383-
}
2394+
cmd.args.push(format!("-mabi=lp64{float_abi}").into());
23842395
} else {
2385-
cmd.args.push("-mcmodel=medany".into());
2396+
cmd.args.push(format!("-mabi=ilp32{float_abi}").into());
23862397
}
2398+
2399+
cmd.args.push("-mcmodel=medany".into());
23872400
}
23882401
}
23892402
}

0 commit comments

Comments
 (0)