rust-lang / cc-rs

Rust library for build scripts to compile C/C++ code into a Rust library
https://docs.rs/cc
Apache License 2.0
1.81k stars 434 forks source link

Fix the clang targets for RISC-V Rust targets #993

Closed djkoloski closed 6 months ago

djkoloski commented 6 months ago

Rust supports a variety of RISC-V targets which may include optional features in the first segment of the target triple. When invoking clang for these targets, we need to remove these features since clang doesn't use them.

djkoloski commented 6 months ago

I'll take a second look tomorrow. I got it compiling but didn't test any feature functionality.

djkoloski commented 6 months ago

It looks like all of the extensions are getting passed through -march by slicing them off of the target name, with the exceptions of 32-bit linux targets and 64-bit linux, freebsd, and netbsd targets. Those override -march=rv32/64gc exactly, as per this PR. In the default case though, these extensions are getting passed through generically. Here's the snippet in question:

if target.starts_with("riscv32") || target.starts_with("riscv64") {
    // get the 32i/32imac/32imc/64gc/64imac/... part
    let mut parts = target.split('-');
    if let Some(arch) = parts.next() {
        let arch = &arch[5..];
        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());
            }
        } else {
            cmd.args.push("-mcmodel=medany".into());
        }
    }
}

So this PR should be ready to merge. I was testing with riscv32imc-unknown-none-elf so I should be hitting those branches.