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 Mar 19, 2023
1 parent 06c1289 commit 23303fc
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1958,25 +1958,37 @@ impl Build {
let mut parts = target.split('-');
if let Some(arch) = parts.next() {
let arch = &arch[5..];
if target.contains("linux") && arch.starts_with("64") {
cmd.args.push(("-march=rv64gc").into());
cmd.args.push("-mabi=lp64d".into());
} else if target.contains("freebsd") && arch.starts_with("64") {
cmd.args.push(("-march=rv64gc").into());
cmd.args.push("-mabi=lp64d".into());
} else if target.contains("openbsd") && arch.starts_with("64") {
cmd.args.push(("-march=rv64gc").into());
cmd.args.push("-mabi=lp64d".into());
} else if target.contains("linux") && arch.starts_with("32") {
cmd.args.push(("-march=rv32gc").into());
cmd.args.push("-mabi=ilp32d".into());
} else if arch.starts_with("64") {
cmd.args.push(("-march=rv".to_owned() + arch).into());
cmd.args.push("-mabi=lp64".into());

// 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 riscv_implements = |ext: &str| -> bool {
let pattern = |c| ['_', 'z', 's'].contains(&c);
let single_letter = arch.split(pattern).next().expect("Empty arch string?");
single_letter.contains(ext)
};

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") {
cmd.args.push(("-mabi=lp64".to_owned() + float_abi).into());
} else {
cmd.args.push(("-march=rv".to_owned() + arch).into());
cmd.args.push("-mabi=ilp32".into());
cmd.args.push(("-mabi=ilp32".to_owned() + float_abi).into());
}

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

0 comments on commit 23303fc

Please sign in to comment.