Skip to content

Commit 8eb234f

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 rust-lang#795
1 parent 06c1289 commit 8eb234f

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/lib.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,25 +1958,36 @@ 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 single_letter = arch.split(&['_', 'z', 's']).next().unwrap_or(arch);
1971+
single_letter.contains(ext)
1972+
};
1973+
1974+
let float_abi = if riscv_implements("g") || riscv_implements("d") {
1975+
// Implements "d" (double-float), use double-float ABI
1976+
"d"
1977+
} else if riscv_implements("f") {
1978+
// Implements "f" (single-float), use single-float ABI
1979+
"f"
1980+
} else {
1981+
// No floating support, use soft-float ABI
1982+
""
1983+
};
1984+
1985+
if arch.starts_with("64") {
1986+
cmd.args.push(("-mabi=lp64".to_owned() + float_abi).into());
19761987
} else {
1977-
cmd.args.push(("-march=rv".to_owned() + arch).into());
1978-
cmd.args.push("-mabi=ilp32".into());
1988+
cmd.args.push(("-mabi=ilp32".to_owned() + float_abi).into());
19791989
}
1990+
19801991
cmd.args.push("-mcmodel=medany".into());
19811992
}
19821993
}

0 commit comments

Comments
 (0)