Files
leleka/crates/deploy/build.rs
T

95 lines
3.8 KiB
Rust

//! Build identity for `leleka version` (release checklist docs/release/5090-v0.1-checklist.md, section 3).
//!
//! Embeds the git commit of the tree the binary was built from, whether that tree had uncommitted changes, and the
//! Strata upstream pin recorded in `docs/upstream.md`. The release packager (`scripts/release/package-5090.sh`) exports
//! `LELEKA_GIT_COMMIT` and `LELEKA_GIT_DIRTY` so the values do not depend on `git` being callable from the build
//! script; an ordinary developer build asks git itself and falls back to `unknown` (an exported tree has no `.git`).
//! Nothing here reads the network or changes the generated code of the engine.
use std::path::{Path, PathBuf};
use std::process::Command;
fn git(repo: &Path, args: &[&str]) -> Option<String> {
let output = Command::new("git")
.args(args)
.current_dir(repo)
.output()
.ok()?;
if !output.status.success() {
return None;
}
Some(String::from_utf8_lossy(&output.stdout).trim().to_owned())
}
/// The 40-hex commit on the `Upstream commit SHA` row of the pin table in `docs/upstream.md`.
fn upstream_pin(doc: &Path) -> Option<String> {
let text = std::fs::read_to_string(doc).ok()?;
let line = text
.lines()
.find(|line| line.contains("Upstream commit SHA"))?;
let start = line.find('`')? + 1;
let end = start + line[start..].find('`')?;
let sha = &line[start..end];
(sha.len() == 40 && sha.bytes().all(|b| b.is_ascii_hexdigit()))
.then(|| sha.to_ascii_lowercase())
}
fn main() {
let manifest = PathBuf::from(
std::env::var_os("CARGO_MANIFEST_DIR").expect("cargo sets CARGO_MANIFEST_DIR"),
);
let repo = manifest.join("..").join("..");
let doc = repo.join("docs").join("upstream.md");
emit_reruns(&repo, &doc);
let commit = std::env::var("LELEKA_GIT_COMMIT")
.ok()
.filter(|value| !value.trim().is_empty())
.or_else(|| git(&repo, &["rev-parse", "HEAD"]))
.unwrap_or_else(|| "unknown".to_owned());
let dirty = tree_state(&repo);
let pin = upstream_pin(&doc).unwrap_or_else(|| "unknown".to_owned());
println!("cargo:rustc-env=LELEKA_BUILD_COMMIT={commit}");
println!("cargo:rustc-env=LELEKA_BUILD_TREE={dirty}");
println!("cargo:rustc-env=LELEKA_BUILD_STRATA_PIN={pin}");
println!(
"cargo:rustc-env=LELEKA_BUILD_TARGET={}",
std::env::var("TARGET").unwrap_or_else(|_| "unknown".to_owned())
);
println!(
"cargo:rustc-env=LELEKA_BUILD_PROFILE={}",
std::env::var("PROFILE").unwrap_or_else(|_| "unknown".to_owned())
);
}
/// When cargo has to run this script again: the packager variables, the pin document, a commit or a branch switch.
fn emit_reruns(repo: &Path, doc: &Path) {
println!("cargo:rerun-if-env-changed=LELEKA_GIT_COMMIT");
println!("cargo:rerun-if-env-changed=LELEKA_GIT_DIRTY");
println!("cargo:rerun-if-changed={}", doc.display());
// A commit moves the reflog of HEAD; a branch switch moves HEAD itself.
for name in ["HEAD", "logs/HEAD"] {
if let Some(path) = git(repo, &["rev-parse", "--git-path", name]) {
println!("cargo:rerun-if-changed={}", repo.join(path).display());
}
}
}
/// `clean`, `dirty` or `unknown`: from the packager variable, else from git.
fn tree_state(repo: &Path) -> &'static str {
match std::env::var("LELEKA_GIT_DIRTY")
.ok()
.filter(|value| !value.trim().is_empty())
{
Some(value) => match value.trim() {
"0" | "no" | "false" | "clean" => "clean",
_ => "dirty",
},
None => match git(repo, &["status", "--porcelain", "--untracked-files=no"]) {
Some(status) if status.is_empty() => "clean",
Some(_) => "dirty",
None => "unknown",
},
}
}