Search code examples
cudalinkerrustffinvcc

Changing the compilation arguments passed to nvcc by Rust using cc


I am using cc to link a CUDA kernel written in C to Rust. Here is my build.rs file:

extern crate cc;

fn main() {
    cc::Build::new()
        .cuda(true)
        .flag("-cudart=shared")
        .flag("-gencode")
        .flag("arch=compute_61,code=sm_61")
        .file("kernel/kernel.cu")
        .compile("kernel/kernel.a");
}

I have this error :

running: "nvcc" "-ccbin=c++" "-O0" "-Xcompiler" "-ffunction-sections" "-Xcompiler" "-fdata-sections" "-Xcompiler" "-fPIC" "-G" "-Xcompiler" "-g" "-m64" "-Xcompiler" "-Wall" "-Xcompiler" "-Wextra" "-cudart=shared" "-gencode" "arch=compute_61,code=sm_61" "-o" "/home/ltei/Dev/Workspaces/rust_cudnn/target/debug/build/rust_cudnn-df924982e63c2363/out/kernel/kernel.o" "-c" "kernel/kernel.cu" cargo:warning=In file included from /usr/include/cuda_runtime.h:78:0, cargo:warning= from :0: cargo:warning=/usr/include/host_config.h:119:2: error: #error -- unsupported GNU version! gcc versions later than 5 are not supported! cargo:warning= #error -- unsupported GNU version! gcc versions later than 5 are not supported! cargo:warning= ^~~~~ exit code: 1

I know that it would work if I could change the -ccbin=c++ in the command into -ccbin=clang-3.8, but I have no idea how to do it.

I could also install another version of GCC, but I'd prefer the first solution.


Solution

  • You can set the CXX environment variable to whatever you want.

    CXX=this-is-my-cpp-compiler cargo build
    

    This will be used as the argument to ccbin:

    "nvcc" "-ccbin=this-is-my-cpp-compiler" "-O0" "-Xcompiler" "-ffunction-sections" "-Xcompiler" "-fdata-sections" "-Xcompiler" "-fPIC" "-G" "-Xcompiler" "-g" "-m64" "-Xcompiler" "-Wall" "-Xcompiler" "-Wextra" "-cudart=shared" "-gencode" "arch=compute_61,code=sm_61" "-o" "/private/tmp/c/target/debug/build/c-67ec4fdcff2f35d1/out/kernel/kernel.o" "-c" "kernel/kernel.cu"