From 1bfa7880afc7cbbc1e3d73be5cfa77dc2fd216d2 Mon Sep 17 00:00:00 2001
From: Jaikiran Pai
Date: Sun, 20 Oct 2019 18:30:01 +0530
Subject: [PATCH] bz-62617 tstamp task - Honor SOURCE_DATE_EPOCH to allow
reproducible builds
---
CONTRIBUTORS | 1 +
WHATSNEW | 4 ++++
contributors.xml | 4 ++++
manual/Tasks/tstamp.html | 7 +++++++
.../org/apache/tools/ant/taskdefs/Tstamp.java | 19 +++++++++++++++++--
5 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index 4d7a8cf6a..80171e788 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -221,6 +221,7 @@ Joseph Walton
Josh Lucas
Juerg Wanner
Julian Simpson
+Julien Lepiller
Justin Vallon
Justyna Horwat
Karl Jansen
diff --git a/WHATSNEW b/WHATSNEW
index 1eaf76437..32424e484 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -20,6 +20,10 @@ Other changes:
* The runant.py script should now work with Python 3.
Github Pull Request #96
+ * tstamp task now honors SOURCE_DATE_EPOCH environment variable for
+ reproducible builds (https://reproducible-builds.org/specs/source-date-epoch/#idm55)
+ Bugzilla Report 62617
+
Changes from Ant 1.10.6 TO Ant 1.10.7
=====================================
diff --git a/contributors.xml b/contributors.xml
index e73660bc9..61040c2bd 100644
--- a/contributors.xml
+++ b/contributors.xml
@@ -923,6 +923,10 @@
Julian
Simpson
+
+ Julien
+ Lepiller
+
Justin
Vallon
diff --git a/manual/Tasks/tstamp.html b/manual/Tasks/tstamp.html
index 43609596f..1661392a1 100644
--- a/manual/Tasks/tstamp.html
+++ b/manual/Tasks/tstamp.html
@@ -44,6 +44,13 @@ you could also specify that value in ISO-8601 format (1972-04-17T08:07:00Z
specify a value in an invalid format an INFO message will be logged and the value will be
ignored.
+
+ Since Ant 1.10.8 the SOURCE_DATE_EPOCH
environment variable value (if set)
+ will be honoured for reproducible builds.
+ Ant will log a DEBUG message if an invalid value (value that cannot be parsed to an integer), is specified
+ for that environment variable and will instead use the "current" date.
+
+
Parameters
diff --git a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
index 226419c31..077052d44 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
@@ -50,6 +50,8 @@ import org.apache.tools.ant.types.EnumeratedAttribute;
*/
public class Tstamp extends Task {
+ private static final String ENV_SOURCE_DATE_EPOCH = "SOURCE_DATE_EPOCH";
+
private List customFormats = new Vector<>();
private String prefix = "";
@@ -75,8 +77,21 @@ public class Tstamp extends Task {
public void execute() throws BuildException {
try {
Date d = getNow();
-
- customFormats.forEach(cts -> cts.execute(getProject(), d, getLocation()));
+ // Honour reproducible builds https://reproducible-builds.org/specs/source-date-epoch/#idm55
+ final String epoch = System.getenv(ENV_SOURCE_DATE_EPOCH);
+ try {
+ if (epoch != null) {
+ // Value of SOURCE_DATE_EPOCH will be an integer, representing seconds.
+ d = new Date(Integer.parseInt(epoch) * 1000);
+ }
+ log("Honouring environment variable " + ENV_SOURCE_DATE_EPOCH + " which has been set to " + epoch);
+ } catch(NumberFormatException e) {
+ // ignore
+ log("Ignoring invalid value '" + epoch + "' for " + ENV_SOURCE_DATE_EPOCH
+ + " environment variable", Project.MSG_DEBUG);
+ }
+ final Date date = d;
+ customFormats.forEach(cts -> cts.execute(getProject(), date, getLocation()));
SimpleDateFormat dstamp = new SimpleDateFormat("yyyyMMdd");
setProperty("DSTAMP", dstamp.format(d));