use std::env;
use std::path::Path;
use std::process::Command;

// xtrain-tensor calls GPU kernels (via xtrain-cuda's FFI), so it gates those
// call sites behind `not(no_cuda)` — the same convention xtrain-cuda uses. This
// build script only detects nvcc and emits that cfg; it compiles no CUDA itself
// (the kernels are built by xtrain-cuda's build.rs).
fn main() {
    println!("cargo:rustc-check-cfg=cfg(no_cuda)");

    let cuda_path = env::var("CUDA_HOME")
        .or_else(|_| env::var("CUDA_PATH"))
        .unwrap_or_else(|_| "/usr/local/cuda".to_string());

    if !nvcc_available(&cuda_path) {
        println!("cargo:rustc-cfg=no_cuda");
    }
}

fn nvcc_available(cuda_path: &str) -> bool {
    if Command::new("nvcc").arg("--version").output().is_ok() {
        return true;
    }
    Path::new(&format!("{cuda_path}/bin/nvcc")).exists()
}
