enable reproducible builds
[debian/cc1111] / debian / patches / reproducible_build
1 --- cc1111-2.9.0.orig/as/asranlib/asranlib.c
2 +++ cc1111-2.9.0/as/asranlib/asranlib.c
3 @@ -22,6 +22,8 @@ along with this program.  If not, see <h
4  #include <assert.h>
5  #include <time.h>
6  #include <sys/stat.h>
7 +#include <errno.h>
8 +#include <limits.h>
9  #include "dbuf_string.h"
10  #include "lkar.h"
11  
12 @@ -593,6 +595,47 @@ do_ranlib (const char *archive)
13        char tmpfile[] = "arXXXXXX";
14        struct stat stat_buf;
15        int can_stat;
16 +      time_t now;
17 +      char *endptr;
18 +      char *source_date_epoch;
19 +      unsigned long long epoch;
20 +
21 +      source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
22 +
23 +      if (source_date_epoch)
24 +        {
25 +          errno = 0;
26 +          epoch = strtoull (source_date_epoch, &endptr, 10);
27 +
28 +          if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0))
29 +                        || (errno != 0 && epoch == 0))
30 +            {
31 +              fprintf (stderr, "Environment variable $SOURCE_DATE_EPOCH: strtoull: %s\n", strerror(errno));
32 +              exit (1);
33 +            }
34 +
35 +          if (endptr == source_date_epoch)
36 +            {
37 +              fprintf (stderr, "Environment variable $SOURCE_DATE_EPOCH: No digits were found: %s\n", endptr);
38 +              exit (1);
39 +            }
40 +
41 +          if (*endptr != '\0')
42 +            {
43 +              fprintf (stderr, "Environment variable $SOURCE_DATE_EPOCH: Trailing garbage: %s\n", endptr);
44 +              exit (1);
45 +            }
46 +
47 +          if (epoch > ULONG_MAX)
48 +            {
49 +              fprintf (stderr, "Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to: %lu but was found to be: %llu \n", ULONG_MAX, epoch);
50 +              exit (1);
51 +            }
52 +
53 +          now = epoch;
54 +        }
55 +      else
56 +        now = time (NULL);
57  
58  #ifdef _WIN32
59        if (NULL == _mktemp (tmpfile) || NULL == (outfp = fopen (tmpfile, "wb")))
60 @@ -628,7 +671,7 @@ do_ranlib (const char *archive)
61  
62        symtab_size = 4 + 4 * nsym + str_length;
63  
64 -      fprintf (outfp, ARMAG AR_SYMBOL_TABLE_NAME "%-12d%-6d%-6d%-8d%-10d" ARFMAG, (int) time (NULL), 0, 0, 0, symtab_size);
65 +      fprintf (outfp, ARMAG AR_SYMBOL_TABLE_NAME "%-12d%-6d%-6d%-8d%-10d" ARFMAG, (int) now, 0, 0, 0, symtab_size);
66  
67        if (symtab_size & 1)
68          {