Skip to content

Commit 6a1aff2

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 06c1289 commit 6a1aff2

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/lib.rs

+30-17
Original file line numberDiff line numberDiff line change
@@ -1958,25 +1958,38 @@ impl Build {
19581958
let mut parts = target.split('-');
19591959
if let Some(arch) = parts.next() {
19601960
let arch = &arch[5..];
1961-
if target.contains("linux") && arch.starts_with("64") {
1962-
cmd.args.push(("-march=rv64gc").into());
1963-
cmd.args.push("-mabi=lp64d".into());
1964-
} else if target.contains("freebsd") && arch.starts_with("64") {
1965-
cmd.args.push(("-march=rv64gc").into());
1966-
cmd.args.push("-mabi=lp64d".into());
1967-
} else if target.contains("openbsd") && arch.starts_with("64") {
1968-
cmd.args.push(("-march=rv64gc").into());
1969-
cmd.args.push("-mabi=lp64d".into());
1970-
} else if target.contains("linux") && arch.starts_with("32") {
1971-
cmd.args.push(("-march=rv32gc").into());
1972-
cmd.args.push("-mabi=ilp32d".into());
1973-
} else if arch.starts_with("64") {
1974-
cmd.args.push(("-march=rv".to_owned() + arch).into());
1975-
cmd.args.push("-mabi=lp64".into());
1961+
1962+
// Assume that "rv{arch}" is a valid RISC-V ISA string.
1963+
// The compiler would error out otherwise, and we fix
1964+
// that later.
1965+
cmd.args.push(("-march=rv".to_owned() + arch).into());
1966+
1967+
// Detect single-letter extensions from `arch`, assuming
1968+
// no version numbers and canonical order
1969+
let riscv_implements = |ext: &str| -> bool {
1970+
let pattern = |c| ['_', 'z', 's'].contains(&c);
1971+
let single_letter =
1972+
arch.split(pattern).next().expect("Empty arch string?");
1973+
single_letter.contains(ext)
1974+
};
1975+
1976+
let float_abi = if riscv_implements("g") || riscv_implements("d") {
1977+
// Implements "d" (double-float), use double-float ABI
1978+
"d"
1979+
} else if riscv_implements("f") {
1980+
// Implements "f" (single-float), use single-float ABI
1981+
"f"
1982+
} else {
1983+
// No floating support, use soft-float ABI
1984+
""
1985+
};
1986+
1987+
if arch.starts_with("64") {
1988+
cmd.args.push(("-mabi=lp64".to_owned() + float_abi).into());
19761989
} else {
1977-
cmd.args.push(("-march=rv".to_owned() + arch).into());
1978-
cmd.args.push("-mabi=ilp32".into());
1990+
cmd.args.push(("-mabi=ilp32".to_owned() + float_abi).into());
19791991
}
1992+
19801993
cmd.args.push("-mcmodel=medany".into());
19811994
}
19821995
}

0 commit comments

Comments
 (0)