summaryrefslogtreecommitdiffstats
path: root/development/fpc/patches/honor_SOURCE_DATE_EPOCH_in_date.patch
blob: ffe63809b2c315e46d8e183eead9837b77020730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Description: Reproducible builds requires that the build time stamp is not
 recorded in binaries. In FPC they are fuild in via the $INCLUDE %DATE%
 directive which calls getdatestr in globals.pas. To allow reproducible builds
 we should honor the SOURCE_DATE_EPOCH environment variable. To not depend on
 the dateutil unit, we include the required code from that package here.
Author: Paul Gevers <elbrus@debian.org>
Author: Abou Al Montacir <abou.almontacir@sfr.fr>

Index: fpc/fpcsrc/compiler/globals.pas
===================================================================
--- fpc.orig/fpcsrc/compiler/globals.pas
+++ fpc/fpcsrc/compiler/globals.pas
@@ -559,6 +559,7 @@ interface
       startsystime : TSystemTime;
 
     function getdatestr:string;
+    Function UnixToDateTime(const AValue: Int64): TDateTime;
     function gettimestr:string;
     function filetimestring( t : longint) : string;
     function getrealtime(const st: TSystemTime) : real;
@@ -816,12 +817,34 @@ implementation
      get the current date in a string YY/MM/DD
    }
       var
+        Year,Month,Day: Word;
         st: TSystemTime;
+        SourceDateEpoch: string;
       begin
+        SourceDateEpoch := GetEnvironmentVariable('SOURCE_DATE_EPOCH');
+        if Length(SourceDateEpoch)>0 then
+           begin
+             DecodeDate(UnixToDateTime(StrToInt64(SourceDateEpoch)),Year,Month,Day);
+             getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
+           end
+        else
+           begin
         GetLocalTime(st);
         getdatestr:=L0(st.Year)+'/'+L0(st.Month)+'/'+L0(st.Day);
+           end;
       end;
 
+   Function UnixToDateTime(const AValue: Int64): TDateTime;
+   { Code copied from fpcsrc/packages/rtl-objpas/src/inc/dateutil.inc and
+   fpcsrc/rtl/objpas/sysutils/datih.inc }
+   const
+      TDateTimeEpsilon = 2.2204460493e-16 ;
+      UnixEpoch = TDateTime(-2415018.5) + TDateTime(2440587.5) ;
+   begin
+      Result:=UnixEpoch + AValue/SecsPerDay;
+      if (UnixEpoch>=0) and (Result<-TDateTimeEpsilon) then
+         Result:=int(Result-1.0+TDateTimeEpsilon)-frac(1.0+frac(Result));
+   end;
 
    function  filetimestring( t : longint) : string;
    {