/ build.rs
build.rs
1 use std::env; 2 use std::process::Command; 3 4 fn main() -> Result<(), Box<dyn std::error::Error>> { 5 // Set a build-time `GIT_HEAD` env var which includes the commit id; 6 // such that we can tell which code is running. 7 let hash = env::var("GIT_HEAD").unwrap_or_else(|_| { 8 Command::new("git") 9 .arg("rev-parse") 10 .arg("--short") 11 .arg("HEAD") 12 .output() 13 .ok() 14 .and_then(|output| { 15 if output.status.success() { 16 String::from_utf8(output.stdout).ok() 17 } else { 18 None 19 } 20 }) 21 .unwrap_or("unknown".into()) 22 }); 23 24 let version = if let Ok(version) = env::var("RADICLE_VERSION") { 25 version 26 } else { 27 "pre-release".to_owned() 28 }; 29 30 // Set a build-time `SOURCE_DATE_EPOCH` env var which includes the commit time. 31 let commit_time = env::var("SOURCE_DATE_EPOCH").unwrap_or_else(|_| { 32 Command::new("git") 33 .arg("log") 34 .arg("-1") 35 .arg("--pretty=%ct") 36 .arg("HEAD") 37 .output() 38 .ok() 39 .and_then(|output| { 40 if output.status.success() { 41 String::from_utf8(output.stdout).ok() 42 } else { 43 None 44 } 45 }) 46 .unwrap_or(0.to_string()) 47 }); 48 49 println!("cargo::rustc-env=RADICLE_VERSION={version}"); 50 println!("cargo::rustc-env=SOURCE_DATE_EPOCH={commit_time}"); 51 println!("cargo::rustc-env=GIT_HEAD={hash}"); 52 53 Ok(()) 54 }