fastnn 0.3.3

A robust deep learning library in Rust with CUDA kernels
Documentation
use std::env;
use std::path::PathBuf;

fn main() {
    println!("cargo:rerun-if-changed=cuda/");

    if cfg!(feature = "cuda") {
        build_cuda();
    } else {
        build_cpu_stubs();
    }
}

fn build_cpu_stubs() {
    cc::Build::new()
        .file("cuda/stubs.c")
        .compile("fastnn_cuda_stubs");
}

fn build_cuda() {
    let cuda_path = env::var("CUDA_PATH")
        .or_else(|_| env::var("CUDA_HOME"))
        .unwrap_or_else(|_| {
            if cfg!(target_os = "windows") {
                "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.0".to_string()
            } else {
                "/usr/local/cuda".to_string()
            }
        });

    let cuda_include = PathBuf::from(&cuda_path).join("include");
    let cuda_lib = if cfg!(target_os = "windows") {
        PathBuf::from(&cuda_path).join("lib/x64")
    } else {
        PathBuf::from(&cuda_path).join("lib64")
    };

    let mut build = cc::Build::new();
    build
        .cuda(true)
        .cudart("shared")
        .flag("-gencode=arch=compute_75,code=sm_75") // Turing
        .flag("-gencode=arch=compute_80,code=sm_80") // Ampere
        .flag("-gencode=arch=compute_86,code=sm_86") // Ampere (RTX 30xx)
        .flag("-gencode=arch=compute_89,code=sm_89") // Ada Lovelace
        .flag("-gencode=arch=compute_90,code=sm_90") // Hopper
        .flag("--use_fast_math")
        .flag("--extended-lambda")
        .flag("-O3")
        .include("cuda/include")
        .include(&cuda_include)
        .file("cuda/kernels.cu");

    // nvcc rejects host compilers newer than it knows. Take FASTNN_NVCC_CCBIN, else g++-15 if installed.
    println!("cargo:rerun-if-env-changed=FASTNN_NVCC_CCBIN");
    if let Ok(ccbin) = env::var("FASTNN_NVCC_CCBIN") {
        build.flag(format!("-ccbin={ccbin}"));
    } else if std::process::Command::new("g++-15")
        .arg("--version")
        .output()
        .is_ok()
    {
        build.flag("-ccbin=g++-15");
    }

    build.compile("fastnn_cuda_kernels");

    println!("cargo:rustc-link-search=native={}", cuda_lib.display());
    println!("cargo:rustc-link-lib=dylib=cudart");
    println!("cargo:rustc-link-lib=dylib=cublas");
    println!("cargo:rustc-link-lib=dylib=curand");
}