Allow to override build date with SOURCE_DATE_EPOCH

to make builds reproducible.
See https://reproducible-builds.org/ for why this is good
and https://reproducible-builds.org/specs/source-date-epoch/
for the definition of this variable.
Also use UTC to be independent of timezone.
This commit is contained in:
Bernhard M. Wiedemann 2024-10-24 09:32:18 +02:00
parent 79eb752552
commit 54a7a5069c
No known key found for this signature in database
GPG Key ID: 8D6B1CBF0689344C

View File

@ -1,7 +1,14 @@
use chrono::{TimeZone, Utc};
use std::env;
fn main() { fn main() {
// Add current date as a variable to be displayed in the 'Linux Toolbox' text. // Add current date as a variable to be displayed in the 'Linux Toolbox' text.
let now = match env::var("SOURCE_DATE_EPOCH") {
Ok(val) => { Utc.timestamp_opt(val.parse::<i64>().unwrap(), 0).unwrap() }
Err(_) => Utc::now(),
};
println!( println!(
"cargo:rustc-env=BUILD_DATE={}", "cargo:rustc-env=BUILD_DATE={}",
chrono::Local::now().format("%Y-%m-%d") now.format("%Y-%m-%d")
); );
} }