use std::env;
use std::path::Path;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
struct Commit {
hash: String,
short_hash: String,
date: String,
}
fn get_commit_from_git() -> Option<Commit> {
if !Path::new("../.git").exists() {
return None;
}
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.arg("--abbrev=9")
.output()
{
Ok(output) if output.status.success() => output,
_ => return None,
};
let stdout = String::from_utf8(output.stdout).unwrap();
let mut parts = stdout.split_whitespace().map(|s| s.to_string());
Some(Commit {
hash: parts.next()?,
short_hash: parts.next()?,
date: parts.next()?,
})
}
fn main() {
let target = env::var("TARGET").unwrap_or_default();
println!("cargo:rustc-env=BUILD_PLATFORM={}", target);
let mut rustflags = env::var("RUSTFLAGS").unwrap_or_default();
let additional_rustflags = if target.contains("x86_64") {
"-C target-feature=+sse4.2"
} else {
""
};
if !additional_rustflags.is_empty() {
if !rustflags.is_empty() {
rustflags.push(' ');
}
rustflags.push_str(additional_rustflags);
println!("cargo:rustc-env=RUSTFLAGS={}", rustflags);
}
if let Ok(build_time) = SystemTime::now().duration_since(UNIX_EPOCH) {
println!("cargo:rustc-env=BUILD_TIMESTAMP={}", build_time.as_secs());
}
if let Some(commit) = get_commit_from_git() {
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit.hash);
println!(
"cargo:rustc-env=GIT_COMMIT_SHORT_HASH={}",
commit.short_hash
);
println!("cargo:rustc-env=GIT_COMMIT_DATE={}", commit.date);
}
}