2024-01-01 14:48:20 +00:00
|
|
|
use semver::BuildMetadata;
|
2022-09-28 19:13:40 +00:00
|
|
|
use std::process::Command;
|
|
|
|
use std::{env, str};
|
|
|
|
|
|
|
|
const BUILD_METADATA: &str = "SURREAL_BUILD_METADATA";
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
println!("cargo:rerun-if-env-changed={BUILD_METADATA}");
|
2024-08-22 10:26:03 +00:00
|
|
|
println!("cargo:rerun-if-changed=sdk");
|
2022-09-28 19:13:40 +00:00
|
|
|
println!("cargo:rerun-if-changed=src");
|
2024-01-22 17:51:49 +00:00
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
2022-09-28 19:13:40 +00:00
|
|
|
println!("cargo:rerun-if-changed=Cargo.toml");
|
|
|
|
println!("cargo:rerun-if-changed=Cargo.lock");
|
|
|
|
if let Some(metadata) = build_metadata() {
|
|
|
|
println!("cargo:rustc-env={BUILD_METADATA}={metadata}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_metadata() -> Option<String> {
|
2024-01-01 14:48:20 +00:00
|
|
|
if let Ok(input) = env::var(BUILD_METADATA) {
|
|
|
|
let metadata = input.trim();
|
|
|
|
if let Err(error) = BuildMetadata::new(metadata) {
|
|
|
|
panic!("invalid build metadata `{input}`: {error}");
|
|
|
|
}
|
|
|
|
return Some(metadata.to_owned());
|
2022-09-28 19:13:40 +00:00
|
|
|
}
|
|
|
|
let date = git()
|
|
|
|
.args(["show", "--no-patch", "--format=%ad", "--date=format:%Y%m%d"])
|
|
|
|
.output_string()?;
|
|
|
|
let rev = git().args(["rev-parse", "--short", "HEAD"]).output_string()?;
|
|
|
|
let repo_clean = git()
|
|
|
|
.args(["diff", "--quiet"])
|
|
|
|
.output()
|
|
|
|
.map(|output| output.status.success())
|
|
|
|
.unwrap_or_default();
|
|
|
|
let metadata = if repo_clean {
|
|
|
|
format!("{date}.{rev}")
|
|
|
|
} else {
|
|
|
|
format!("{date}.{rev}.dirty")
|
|
|
|
};
|
|
|
|
Some(metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn git() -> Command {
|
|
|
|
Command::new("git")
|
|
|
|
}
|
|
|
|
|
|
|
|
trait CommandExt {
|
|
|
|
fn output_string(&mut self) -> Option<String>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandExt for Command {
|
|
|
|
fn output_string(&mut self) -> Option<String> {
|
|
|
|
self.output()
|
|
|
|
.ok()
|
|
|
|
.filter(|output| output.status.success())
|
|
|
|
.and_then(|output| {
|
|
|
|
str::from_utf8(&output.stdout).ok().map(|output| output.trim().to_string())
|
|
|
|
})
|
|
|
|
.filter(|output| !output.is_empty())
|
|
|
|
}
|
|
|
|
}
|