Merge pull request #807 from sbellem/source-date-epoch

xtask: add SOURCE_DATE_EPOCH support
This commit is contained in:
bunnie 2026-02-11 01:58:25 +08:00 committed by GitHub
commit b2ee028fe2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,7 +6,7 @@ use std::fs::OpenOptions;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::process::Command; use std::process::Command;
use chrono::Local; use chrono::{Local, TimeZone, Utc};
pub(crate) fn generate_version(add_timestamp: bool) { pub(crate) fn generate_version(add_timestamp: bool) {
let output = if cfg!(target_os = "windows") { let output = if cfg!(target_os = "windows") {
@ -30,13 +30,16 @@ pub(crate) fn generate_version(add_timestamp: bool) {
let mut new_data = Vec::new(); let mut new_data = Vec::new();
print_header(&mut new_data); print_header(&mut new_data);
if add_timestamp { if add_timestamp {
let now = Local::now(); // Use SOURCE_DATE_EPOCH for reproducible builds if available
write!( // See: https://reproducible-builds.org/docs/source-date-epoch/
new_data, let timestamp_str = if let Ok(sde) = std::env::var("SOURCE_DATE_EPOCH") {
"#[allow(dead_code)]\npub const TIMESTAMP: &'static str = \"{}\";\n", let secs: i64 = sde.parse().expect("SOURCE_DATE_EPOCH must be a valid Unix timestamp");
now.to_rfc2822() Utc.timestamp_opt(secs, 0).single().expect("Invalid SOURCE_DATE_EPOCH timestamp").to_rfc2822()
) } else {
.expect("couldn't add our timestamp"); Local::now().to_rfc2822()
};
write!(new_data, "#[allow(dead_code)]\npub const TIMESTAMP: &'static str = \"{}\";\n", timestamp_str)
.expect("couldn't add our timestamp");
} else { } else {
write!(new_data, "#[allow(dead_code)]\npub const TIMESTAMP: &'static str = \"omitted\";\n") write!(new_data, "#[allow(dead_code)]\npub const TIMESTAMP: &'static str = \"omitted\";\n")
.expect("couldn't add our timestamp"); .expect("couldn't add our timestamp");