From 65969a2b73ce93db4c80f1706677351b0e57fa72 Mon Sep 17 00:00:00 2001
From: Sofiane HAMAM <sofiane.hamam@smile.fr>
Date: Fri, 28 Mar 2025 12:31:17 +0100
Subject: [PATCH] Makefile.am: Use SOURCE_DATE_EPOCH environment variable

This package defines PKGBLDDATE as build timestamp which makes
it non reproducible. Use SOURCE_DATE_EPOCH if it is found, otherwise
use build timestamp. Following best practices, see :
https://reproducible-builds.org/docs/source-date-epoch/

The patch is BSD compatible too.

Change-Id: I26c1a00f2e8059ae31fe72a794b5962af5a84f44
Co-developed-by: Yoann Congal <yoann.congal@smile.fr>
Signed-off-by: Yoann Congal <yoann.congal@smile.fr>
Signed-off-by: Sofiane HAMAM <sofiane.hamam@smile.fr>

Upstream-Status: Backport [https://repo.or.cz/openocd.git?a=commit;h=6834f022b96fb1c7f5829166578e01a0ac223cb0]
---
 src/Makefile.am | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 6d79cd631..9ca697407 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -30,7 +30,10 @@ if RELEASE
 else
 %C%_libopenocd_la_CPPFLAGS += -DRELSTR=\"`$(top_srcdir)/guess-rev.sh $(top_srcdir)`\"
 %C%_libopenocd_la_CPPFLAGS += -DGITVERSION=\"`cd $(top_srcdir) && git describe`\"
-%C%_libopenocd_la_CPPFLAGS += -DPKGBLDDATE=\"`date +%F-%R`\"
+%C%_libopenocd_la_CPPFLAGS += -DPKGBLDDATE=\"`DATE_FMT=+%F-%R; \
+	SOURCE_DATE_EPOCH="$${SOURCE_DATE_EPOCH:-$$(date +%s)}"; \
+	date -u -d "@$$SOURCE_DATE_EPOCH" "$$DATE_FMT" 2>/dev/null || \
+	date -u -r "$$SOURCE_DATE_EPOCH" "$$DATE_FMT" 2>/dev/null || date -u "$$DATE_FMT"`\"
 endif
 
 # add default CPPFLAGS
