Merge pull request #119126 from fabaff/pycomfoconnect
[NixPkgs.git] / pkgs / development / compilers / gcc / use-source-date-epoch.patch
blob65a5ab028c1c97830c3cbadc9fbb11525247df3b
1 https://gcc.gnu.org/ml/gcc-patches/2015-06/msg02210.html
3 diff --git a/libcpp/macro.c b/libcpp/macro.c
4 index 1e0a0b5..a52e3cb 100644
5 --- a/libcpp/macro.c
6 +++ b/libcpp/macro.c
7 @@ -349,14 +349,38 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
8 slow on some systems. */
9 time_t tt;
10 struct tm *tb = NULL;
11 + char *source_date_epoch;
13 - /* (time_t) -1 is a legitimate value for "number of seconds
14 - since the Epoch", so we have to do a little dance to
15 - distinguish that from a genuine error. */
16 - errno = 0;
17 - tt = time(NULL);
18 - if (tt != (time_t)-1 || errno == 0)
19 - tb = localtime (&tt);
20 + /* Allow the date and time to be set externally by an exported
21 + environment variable to enable reproducible builds. */
22 + source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
23 + if (source_date_epoch)
24 + {
25 + errno = 0;
26 + tt = (time_t) strtol (source_date_epoch, NULL, 10);
27 + if (errno == 0)
28 + {
29 + tb = gmtime (&tt);
30 + if (tb == NULL)
31 + cpp_error (pfile, CPP_DL_ERROR,
32 + "SOURCE_DATE_EPOCH=\"%s\" is not a valid date",
33 + source_date_epoch);
34 + }
35 + else
36 + cpp_error (pfile, CPP_DL_ERROR,
37 + "SOURCE_DATE_EPOCH=\"%s\" is not a valid number",
38 + source_date_epoch);
39 + }
40 + else
41 + {
42 + /* (time_t) -1 is a legitimate value for "number of seconds
43 + since the Epoch", so we have to do a little dance to
44 + distinguish that from a genuine error. */
45 + errno = 0;
46 + tt = time(NULL);
47 + if (tt != (time_t)-1 || errno == 0)
48 + tb = localtime (&tt);
49 + }
51 if (tb)