use std::{env, process::Command}; fn main() { for variable in [ "LATCHD_VERSION", "LATCHD_COMMIT", "LATCHD_BUILD_TIME", "SOURCE_DATE_EPOCH", ] { println!("cargo:rerun-if-env-changed={variable}"); } let version = env::var("LATCHD_VERSION") .ok() .filter(|value| !value.is_empty()) .unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_owned()); let commit = env::var("LATCHD_COMMIT") .ok() .filter(|value| !value.is_empty()) .or_else(|| git_value(["rev-parse", "--short=12", "HEAD"])) .unwrap_or_else(|| "unknown".to_owned()); let build_time = env::var("LATCHD_BUILD_TIME") .ok() .filter(|value| !value.is_empty()) .or_else(|| env::var("SOURCE_DATE_EPOCH").ok()) .unwrap_or_else(|| "unknown".to_owned()); println!("cargo:rustc-env=LATCHD_VERSION={version}"); println!("cargo:rustc-env=LATCHD_COMMIT={commit}"); println!("cargo:rustc-env=LATCHD_BUILD_TIME={build_time}"); } fn git_value(arguments: [&str; N]) -> Option { let output = Command::new("git").args(arguments).output().ok()?; if !output.status.success() { return None; } let value = String::from_utf8(output.stdout).ok()?; let value = value.trim(); (!value.is_empty()).then(|| value.to_owned()) }