//! Stamps the binary with `YYYY.MM.DD--` as `SLINUXM_VERSION`. //! //! Cargo.toml keeps a semver `version` (CalVer with zero-padded parts is not //! valid semver). Every part can be supplied through the environment, which //! is how a build without a `.git` directory (Nix, tarballs) gets them. use std::path::Path; use std::process::Command; use std::time::{SystemTime, UNIX_EPOCH}; #[path = "src/version.rs"] mod version; const ENV_VARS: [&str; 4] = [ "SLINUXM_VERSION", "SLINUXM_GIT_SHA", "SLINUXM_GIT_BRANCH", "SLINUXM_BUILD_DATE", ]; fn env(name: &str) -> Option { std::env::var(name).ok().filter(|v| !v.trim().is_empty()) } fn git(args: &[&str]) -> Option { let out = Command::new("git").args(args).output().ok()?; if !out.status.success() { return None; } let text = String::from_utf8(out.stdout).ok()?; let text = text.trim(); (!text.is_empty()).then(|| text.to_owned()) } /// Registers the files that change HEAD. `--git-path` resolves worktrees, /// where `.git` is a file and the refs live in the main repository. fn watch_git(branch: Option<&str>) { let mut paths = vec!["HEAD".to_owned(), "packed-refs".to_owned()]; if let Some(b) = branch.filter(|b| *b != "HEAD") { paths.push(format!("refs/heads/{b}")); } for p in paths { if let Some(resolved) = git(&["rev-parse", "--git-path", &p]) { println!("cargo:rerun-if-changed={resolved}"); } } } /// Wall-clock UTC unless `SLINUXM_BUILD_DATE` is set. `SOURCE_DATE_EPOCH` is /// deliberately ignored: Nix dev shells export it as a fixed 1980 timestamp. fn build_date() -> String { if let Some(d) = env("SLINUXM_BUILD_DATE") { return d; } let secs = SystemTime::now() .duration_since(UNIX_EPOCH) .map_or(0, |d| d.as_secs()); version::date_from_epoch(secs) } fn main() { for name in ENV_VARS { println!("cargo:rerun-if-env-changed={name}"); } // Naming any rerun path switches off cargo's default of watching the whole // package, so the sources are listed too: the dirty flag must be rechecked // whenever they change. for p in ["build.rs", "Cargo.toml", "Cargo.lock", "src"] { println!("cargo:rerun-if-changed={p}"); } let value = match env("SLINUXM_VERSION") { Some(v) => { assert!( version::is_valid(&v), "SLINUXM_VERSION {v:?} is not YYYY.MM.DD--" ); v } None => { let in_git = Path::new(".git").exists() || git(&["rev-parse", "--git-dir"]).is_some(); let git_branch = in_git .then(|| git(&["rev-parse", "--abbrev-ref", "HEAD"])) .flatten(); if in_git { watch_git(git_branch.as_deref()); } let branch = env("SLINUXM_GIT_BRANCH") .or(git_branch) .map_or_else(|| "unknown".to_owned(), |b| version::sanitize_branch(&b)); let sha = env("SLINUXM_GIT_SHA").or_else(|| { let sha = git(&["rev-parse", "--short=7", "HEAD"])?; let dirty = git(&["status", "--porcelain", "--untracked-files=no"]).is_some(); Some(if dirty { format!("{sha}-dirty") } else { sha }) }); let sha = sha.map_or_else(|| "unknown".to_owned(), |s| version::sanitize_sha(&s)); let v = version::compose(&build_date(), &branch, &sha); assert!(version::is_valid(&v), "built an invalid version: {v:?}"); v } }; println!("cargo:rustc-env=SLINUXM_VERSION={value}"); }