use singe_cuda_find::{
    emit_rerun_if_env_changed, find_cuda_driver_path, find_cuda_runtime, find_nvrtc,
};

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    emit_rerun_if_env_changed();

    link_libraries();
}

fn link_libraries() {
    if let Some(cuda_driver_path) = find_cuda_driver_path() {
        println!("cargo:rustc-link-search={}", cuda_driver_path.display());
        #[cfg(target_os = "linux")]
        println!(
            "cargo:rustc-link-arg=-Wl,-rpath,{}",
            cuda_driver_path.display()
        );
        println!("cargo:rustc-link-lib=cuda");
        // println!("cargo:rustc-link-lib=nvrtc");
    }

    if let Some(cudart) = find_cuda_runtime() {
        println!("cargo:rustc-link-search={}", cudart.library_path.display());
        // println!("cargo:rustc-link-lib=cudart");
        println!("cargo:rustc-link-lib=static=cudart_static");

        println!("cargo:include={}", cudart.include_path.display());
    }

    if let Some(nvrtc) = find_nvrtc() {
        println!("cargo:rustc-link-search={}", nvrtc.library_path.display());
        println!("cargo:rustc-link-lib=nvrtc");
    }
}
