Skip to content

Commit

Permalink
Rework riscv -march and -mabi detection
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dramforever committed Jun 25, 2024
1 parent 6fa9ea6 commit 0526b06
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2361,29 +2361,41 @@ impl Build {
let mut parts = target.split('-');
if let Some(arch) = parts.next() {
let arch = &arch[5..];

// Assume that "rv{arch}" is a valid RISC-V ISA string.
// The compiler would error out otherwise, and we fix
// that later.
cmd.args.push(("-march=rv".to_owned() + arch).into());

// Detect single-letter extensions from `arch`, assuming
// no version numbers and canonical order
let single_letter = arch
.split(['_', 'z', 's'])
.next()
.expect("Empty arch string?");

let riscv_implements = |ext| single_letter.contains(ext);

// Detect ABI to select based on de facto standard

let float_abi = if riscv_implements("g") || riscv_implements("d") {
// Implements "d" (double-float), use double-float ABI
"d"
} else if riscv_implements("f") {
// Implements "f" (single-float), use single-float ABI
"f"
} else {
// No floating support, use soft-float ABI
""
};

if arch.starts_with("64") {
if target.contains("linux")
| target.contains("freebsd")
| target.contains("netbsd")
| target.contains("linux")
{
cmd.args.push(("-march=rv64gc").into());
cmd.args.push("-mabi=lp64d".into());
} else {
cmd.args.push(("-march=rv".to_owned() + arch).into());
cmd.args.push("-mabi=lp64".into());
}
} else if arch.starts_with("32") {
if target.contains("linux") {
cmd.args.push(("-march=rv32gc").into());
cmd.args.push("-mabi=ilp32d".into());
} else {
cmd.args.push(("-march=rv".to_owned() + arch).into());
cmd.args.push("-mabi=ilp32".into());
}
cmd.args.push(("-mabi=lp64".to_owned() + float_abi).into());
} else {
cmd.args.push("-mcmodel=medany".into());
cmd.args.push(("-mabi=ilp32".to_owned() + float_abi).into());
}

cmd.args.push("-mcmodel=medany".into());
}
}
}
Expand Down

0 comments on commit 0526b06

Please sign in to comment.