use std::env;
fn main() {
if env::var("CARGO_FEATURE_PROVE").is_ok()
&& env::var("CARGO_CFG_TARGET_OS").is_ok_and(|os| os == "macos" || os == "ios")
{
println!(
"cargo:rustc-env=RECURSION_METAL_PATH={}",
env::var("DEP_RISC0_CIRCUIT_RECURSION_SYS_METAL_KERNEL").unwrap()
);
}
#[cfg(feature = "prove")]
download_zkr();
}
#[cfg(feature = "prove")]
fn download_zkr() {
use std::{
fs,
path::{Path, PathBuf},
str::FromStr,
};
use downloader::{verify, Download, DownloadSummary, Downloader};
use sha2::{Digest, Sha256};
const FILENAME: &str = "recursion_zkr.zip";
const SRC_PATH: &str = "src/recursion_zkr.zip";
const SHA256_HASH: &str = "ba5c4f8fae128d90ba6791d99d1927f2b4f73bad2860a2763d3db9ffa4270476";
fn check_sha2(path: &Path) -> bool {
let data = fs::read(path).unwrap();
hex::encode(Sha256::digest(data)) == SHA256_HASH
}
println!("cargo:rerun-if-env-changed=RECURSION_SRC_PATH");
let src_path = env::var("RECURSION_SRC_PATH").unwrap_or(SRC_PATH.to_string());
let src_path = PathBuf::from_str(src_path.as_str()).unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir);
let out_path = out_dir.join(FILENAME);
if env::var("DOCS_RS").is_ok() && !out_path.exists() {
fs::write(&out_path, b"").unwrap();
return;
}
if out_path.exists() {
if check_sha2(&out_path) {
return;
}
fs::remove_file(&out_path).unwrap();
}
if src_path.exists() && check_sha2(&src_path) {
fs::copy(&src_path, &out_path).unwrap();
return;
}
let mut downloader = Downloader::builder()
.download_folder(out_dir)
.build()
.unwrap();
let url = format!("https://risc0-artifacts.s3.us-west-2.amazonaws.com/zkr/{SHA256_HASH}.zip");
eprintln!("Downloading {url}");
let dl = Download::new(&url)
.file_name(&PathBuf::from_str(FILENAME).unwrap())
.verify(verify::with_digest::<Sha256>(
hex::decode(SHA256_HASH).unwrap(),
));
let results = downloader.download(&[dl]).unwrap();
for result in results {
let summary: DownloadSummary = result.unwrap();
eprintln!("{summary}");
}
}