#[cfg(all(
feature = "cuda",
any(feature = "fft", feature = "multiexp"),
not(feature = "cargo-clippy")
))]
fn main() {
use std::path::PathBuf;
use std::process::Command;
use std::{env, fs};
use blstrs::Bls12;
use sha2::{Digest, Sha256};
#[path = "src/source.rs"]
mod source;
let kernel_source = source::gen_source::<Bls12, source::Limb32>();
let out_dir = env::var("OUT_DIR").expect("OUT_DIR was not set.");
let mut nvcc = match env::var("EC_GPU_CUDA_NVCC_ARGS") {
Ok(args) => execute::command(format!("nvcc {}", args)),
Err(_) => {
let mut command = Command::new("nvcc");
command
.arg("--optimize=6")
.arg("--threads=0")
.arg("--fatbin")
.arg("--gpu-architecture=sm_86")
.arg("--generate-code=arch=compute_86,code=sm_86")
.arg("--generate-code=arch=compute_80,code=sm_80")
.arg("--generate-code=arch=compute_75,code=sm_75");
command
}
};
let mut hasher = Sha256::new();
hasher.update(kernel_source.as_bytes());
hasher.update(&format!("{:?}", &nvcc));
let kernel_digest = hex::encode(hasher.finalize());
let source_path: PathBuf = [&out_dir, &format!("{}.cu", &kernel_digest)]
.iter()
.collect();
let fatbin_path: PathBuf = [&out_dir, &format!("{}.fatbin", &kernel_digest)]
.iter()
.collect();
fs::write(&source_path, &kernel_source).unwrap_or_else(|_| {
panic!(
"Cannot write kernel source at {}.",
source_path.to_str().unwrap()
)
});
if !fatbin_path.as_path().exists() {
let status = nvcc
.arg("--output-file")
.arg(&fatbin_path)
.arg(&source_path)
.status()
.expect("Cannot run nvcc.");
if !status.success() {
panic!(
"nvcc failed. See the kernel source at {}",
source_path.to_str().unwrap()
);
}
}
println!(
"cargo:rustc-env=CUDA_KERNEL_FATBIN={}",
fatbin_path.to_str().unwrap()
);
}
#[cfg(not(all(
feature = "cuda",
any(feature = "fft", feature = "multiexp"),
not(feature = "cargo-clippy")
)))]
fn main() {
println!("cargo:rustc-env=CUDA_KERNEL_FATBIN=../build.rs");
}