Skip to content
build.rs 1.36 KiB
Newer Older
theMackabu's avatar
theMackabu committed
use chrono::Datelike;
use sha2::{Digest, Sha256};
theMackabu's avatar
theMackabu committed
use std::env;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::PathBuf;
theMackabu's avatar
theMackabu committed
use std::process::Command;

theMackabu's avatar
theMackabu committed
fn sha256_digest(path: &PathBuf) -> Result<String, anyhow::Error> {
    let input = File::open(path)?;
    let mut reader = BufReader::new(input);

    let digest = {
        let mut hasher = Sha256::new();
        let mut buffer = [0; 1024];
        loop {
            let count = reader.read(&mut buffer)?;
            if count == 0 {
                break;
            }
            hasher.update(&buffer[..count]);
        }
        hasher.finalize()
    };
    Ok(data_encoding::HEXLOWER.encode(digest.as_ref()))
}

theMackabu's avatar
theMackabu committed
fn main() {
theMackabu's avatar
theMackabu committed
    let date = chrono::Utc::now();
theMackabu's avatar
theMackabu committed
    println!("cargo:rustc-env=BUILD_DATE={}-{}-{}", date.year(), date.month(), date.day());

    let output = Command::new("git").args(&["rev-parse", "--short=10", "HEAD"]).output().unwrap();
    println!("cargo:rustc-env=GIT_HASH={}", String::from_utf8(output.stdout).unwrap());

    let output_full = Command::new("git").args(&["rev-parse", "HEAD"]).output().unwrap();
    println!("cargo:rustc-env=GIT_HASH_FULL={}", String::from_utf8(output_full.stdout).unwrap());
theMackabu's avatar
theMackabu committed
    println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
theMackabu's avatar
theMackabu committed
    println!("cargo:rustc-env=FILE_SHA={}", sha256_digest(&PathBuf::from("src/go/embed/external")).unwrap());
theMackabu's avatar
theMackabu committed
}