Description: handle ALI timestamps generated from SOURCE_DATE_EPOCH
 When the SOURCE_DATE_EPOCH environment variable is set,
 gcc replaces timestamps more recent than its value with its value
 when writing Ada Library Information (ALI) files.
 This allow reproducible builds from generated or patched Ada sources.
 https://reproducible-builds.org/specs/source-date-epoch/
 .
 Let gprbuild recognize this situation instead of always detecting the
 file as obsolete.
 .
 The patch should be kept in sync with the gcc-BV patch with the same name
 (hence Getenv instead of Ada.Environment_Variable.Value).
Author: Nicolas Boulenguez <nicolas@debian.org>

--- a/gpr/src/gpr-osint.adb
+++ b/gpr/src/gpr-osint.adb
@@ -523,4 +523,18 @@
 
 begin
    Reset_File_Attributes (Unknown_Attributes'Address);
+
+   --  Look for Source_Date_Epoch in the environment.
+   declare
+      Env_Var : String_Access;
+      Get_OK  : Boolean;
+      Epoch   : OS_Time;
+   begin
+      Env_Var := Getenv ("SOURCE_DATE_EPOCH");
+      Get_OS_Time_From_String (Env_Var.all, Get_OK, Epoch);
+      Free (Env_Var);
+      if Get_OK then
+         Source_Date_Epoch := OS_Time_To_GNAT_Time (Epoch);
+      end if;
+   end;
 end GPR.Osint;
--- a/gpr/src/gpr-osint.ads
+++ b/gpr/src/gpr-osint.ads
@@ -219,4 +219,15 @@
       --  A status of zero indicates normal completion, a non-zero status
       --  indicates abnormal termination.
 
+   Source_Date_Epoch : Time_Stamp_Type := Time_Stamp_Type'("99991231235959");
+   --  * gnat1 truncates to this date time stamps written to ALI files, making
+   --    their contents deterministic even for patched or generated sources.
+   --    See https://reproducible-builds.org/specs/source-date-epoch.
+   --  * When gnatmake reads this date from an ALI file, and the source file is
+   --    more recent, it ignores the dates and only considers checksums as if
+   --    Minimal_Recompilation was selected. Else, the source would always
+   --    be detected as requiring a recompilation.
+   --  The default value has no effect, but Initialize will assign it if
+   --  SOURCE_DATE_EPOCH in the environment represents a valid epoch.
+
 end GPR.Osint;
--- a/gpr/src/gpr-util.adb
+++ b/gpr/src/gpr-util.adb
@@ -4865,9 +4865,12 @@
                         then
                            Found := True;
 
-                           if Opt.Minimal_Recompilation
-                             and then ALI.Sdep.Table (D).Stamp /=
-                             Dep_Src.Source_TS
+                           if (Opt.Minimal_Recompilation
+                                 and then ALI.Sdep.Table (D).Stamp /=
+                                 Dep_Src.Source_TS)
+                             or else
+                             (ALI.Sdep.Table (D).Stamp = Source_Date_Epoch
+                                and then Source_Date_Epoch < Dep_Src.Source_TS)
                            then
                               --  If minimal recompilation is in action,
                               --  replace the stamp of the source file in
@@ -5172,9 +5175,12 @@
                      then
                         Found := True;
 
-                        if Opt.Minimal_Recompilation
-                          and then ALI.Sdep.Table (D).Stamp /=
-                          Dep_Src.Source_TS
+                        if (Opt.Minimal_Recompilation
+                              and then ALI.Sdep.Table (D).Stamp /=
+                              Dep_Src.Source_TS)
+                          or else
+                          (ALI.Sdep.Table (D).Stamp = Source_Date_Epoch
+                             and then Source_Date_Epoch < Dep_Src.Source_TS)
                         then
                            --  If minimal recompilation is in action, replace
                            --  the stamp of the source file in the table if
@@ -5388,9 +5394,12 @@
                         then
                            Found := True;
 
-                           if Opt.Minimal_Recompilation
-                             and then ALI.Sdep.Table (D).Stamp /=
-                             Dep_Src.Source_TS
+                           if (Opt.Minimal_Recompilation
+                                 and then ALI.Sdep.Table (D).Stamp /=
+                                 Dep_Src.Source_TS)
+                             or else
+                             (ALI.Sdep.Table (D).Stamp = Source_Date_Epoch
+                                and then Source_Date_Epoch < Dep_Src.Source_TS)
                            then
                               --  If minimal recompilation is in action,
                               --  replace the stamp of the source file in
