Permit reproducible builds (patch attached)

Discuss new features and functions
Posts: 44
Joined: 14 Aug 2022

daviank

Permit reproducible builds by use of SOURCE_DATE_EPOCH
Currently the code relies on __DATE__ and __TIME__ macros.

This prevents to have reproducible build because these values changes every
time we compile.

To achieve reproducible builds, make use of SOURCE_DATE_EPOCH as proposed
on https://reproducible-builds.org

See:
- https://reproducible-builds.org/docs/
- https://reproducible-builds.org/docs/source-date-epoch/

This could be achieved another way if you accept to replace the __DATE__ and __TIME__ macros by something "reproducible".

--- a/FreeFileSync/Source/Makefile
+++ b/FreeFileSync/Source/Makefile
@@ -39,6 +39,10 @@
 LDFLAGS += `$(PKG_CONFIG) --libs libselinux`
 endif
 
+ifneq ($(SOURCE_DATE_EPOCH),)
+cxxFlags += -DSOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH)
+endif
+
 cppFiles=
 cppFiles+=application.cpp
 cppFiles+=base_tools.cpp
--- a/FreeFileSync/Source/RealTimeSync/Makefile
+++ b/FreeFileSync/Source/RealTimeSync/Makefile
@@ -16,6 +16,10 @@
 #treat as system headers so that warnings are hidden:
 CXXFLAGS  += -isystem/usr/include/gtk-3.0
 
+ifneq ($(SOURCE_DATE_EPOCH),)
+cxxFlags += -DSOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH)
+endif
+
 cppFiles=
 cppFiles+=application.cpp
 cppFiles+=config.cpp
--- a/zen/time.h
+++ b/zen/time.h
@@ -251,8 +251,16 @@
 inline
 TimeComp getCompileTime()
 {
+#ifdef SOURCE_DATE_EPOCH
+    time_t build_date = SOURCE_DATE_EPOCH;
+    tm *ltm = std::localtime(&build_date);
+    char compileTime[100];
+    std::strftime(compileTime, 100, "%b %d %Y %T", ltm); //e.g. "Aug  1 2017 01:32:26"
+#else
     //https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
     char compileTime[] = __DATE__ " " __TIME__; //e.g. "Aug  1 2017 01:32:26"
+#endif
+
     if (compileTime[4] == ' ') //day is space-padded, but %d expects zero-padding
         compileTime[4] = '0';