pub const GIT_HASH: &str = env!("PYWATT_GIT_HASH");
pub const BUILD_TIME_UTC: &str = env!("PYWATT_BUILD_TIME_UTC");
pub const RUSTC_VERSION: &str = env!("PYWATT_RUSTC_VERSION");
pub fn emit_build_info() {
use std::process::Command;
let git_hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.map(|output| {
if output.status.success() {
String::from_utf8_lossy(&output.stdout).trim().to_string()
} else {
"unknown".to_string()
}
})
.unwrap_or_else(|_| "unknown".to_string());
let build_time = chrono::Utc::now().to_rfc3339();
let rustc_version = Command::new("rustc")
.arg("--version")
.output()
.map(|output| {
if output.status.success() {
String::from_utf8_lossy(&output.stdout).trim().to_string()
} else {
"unknown".to_string()
}
})
.unwrap_or_else(|_| "unknown".to_string());
println!("cargo:rustc-env=PYWATT_GIT_HASH={}", git_hash);
println!("cargo:rustc-env=PYWATT_BUILD_TIME_UTC={}", build_time);
println!("cargo:rustc-env=PYWATT_RUSTC_VERSION={}", rustc_version);
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/refs/heads");
println!("cargo:rerun-if-changed=.git/index");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:warning=Build info: git={}, time={}, rustc={}",
git_hash, build_time, rustc_version);
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct BuildInfo {
pub git_hash: &'static str,
pub build_time_utc: &'static str,
pub rustc_version: &'static str,
}
impl BuildInfo {
pub fn new() -> Self {
Self {
git_hash: GIT_HASH,
build_time_utc: BUILD_TIME_UTC,
rustc_version: RUSTC_VERSION,
}
}
}
impl Default for BuildInfo {
fn default() -> Self {
Self::new()
}
}
pub fn get_build_info() -> BuildInfo {
BuildInfo::new()
}