Skip to content

Commit 9b932c9

Browse files
dramforevertea
authored andcommitted
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 rust-lang#795
1 parent 1372bf5 commit 9b932c9

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/lib.rs

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

0 commit comments

Comments
 (0)