From 436b9eb85d9c02f6cfbd285136dfea73cfaf8172 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Nov 2020 21:38:26 +0000 Subject: [PATCH] package: honor SOURCE_DATE_EPOCH For projects supporting reproducible builds, it's possible to set the timestamp used in artifacts by setting SOURCE_DATE_EPOCH to a decimal Unix timestamp. This is helpful because it allows users to produce the exact same artifact, regardless of when the project was built, and it also means that services which generate crates from source can generate a consistent crate without having store previously built artifacts. For all these reasons, let's honor the SOURCE_DATE_EPOCH environment variable if it's set and use the current timestamp if it's not. --- src/cargo/ops/cargo_package.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index e3f402514..2056f8cf6 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -473,6 +473,11 @@ fn check_repo_state( } fn timestamp() -> u64 { + if let Ok(var) = std::env::var("SOURCE_DATE_EPOCH") { + if let Ok(stamp) = var.parse() { + return stamp; + } + } SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap()