From ad6f17cd93fb5d505240802d6cf53a285086594f Mon Sep 17 00:00:00 2001 From: Rangi Date: Wed, 16 Dec 2020 11:08:18 -0500 Subject: [PATCH] Support SOURCE_DATE_EPOCH for reproducible builds See https://reproducible-builds.org/docs/source-date-epoch/ Fixes #286 --- include/asm/symbol.h | 3 ++- src/asm/main.c | 14 ++++++++++++-- src/asm/rgbasm.5 | 6 ++++++ src/asm/symbol.c | 4 +--- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/asm/symbol.h b/include/asm/symbol.h index f9005bc2..63578234 100644 --- a/include/asm/symbol.h +++ b/include/asm/symbol.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "asm/section.h" @@ -140,7 +141,7 @@ struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, struct Symbol *sym_Ref(char const *symName); struct Symbol *sym_AddString(char const *symName, char const *value); void sym_Purge(char const *symName); -void sym_Init(void); +void sym_Init(time_t now); /* Functions to save and restore the current symbol scope. */ char const *sym_GetCurrentSymbolScope(void); diff --git a/src/asm/main.c b/src/asm/main.c index 47ee0019..16a251f6 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -218,7 +218,7 @@ static char *make_escape(const char *str) } /* Short options */ -static char const *optstring = "b:D:Eg:hi:LM:o:p:r:VvW:w"; +static const char *optstring = "b:D:Eg:hi:LM:o:p:r:VvW:w"; /* Variables for the long-only options */ static int depType; /* Variants of `-M` */ @@ -283,6 +283,16 @@ int main(int argc, char *argv[]) char *tzMainfile; + time_t now = time(NULL); + char *sourceDateEpoch = getenv("SOURCE_DATE_EPOCH"); + + /* + * Support SOURCE_DATE_EPOCH for reproducible builds + * https://reproducible-builds.org/docs/source-date-epoch/ + */ + if (sourceDateEpoch) + now = (time_t)strtoul(sourceDateEpoch, NULL, 0); + dependfile = NULL; /* Initial number of allocated elements in array */ @@ -481,7 +491,7 @@ int main(int argc, char *argv[]) nTotalLines = 0; nIFDepth = 0; - sym_Init(); + sym_Init(now); sym_SetExportAll(exportall); opt_ParseDefines(); diff --git a/src/asm/rgbasm.5 b/src/asm/rgbasm.5 index 568d1d68..d787d4fb 100644 --- a/src/asm/rgbasm.5 +++ b/src/asm/rgbasm.5 @@ -1092,6 +1092,12 @@ The following symbols are defined by the assembler: .It Ic EQU Ta Dv __RGBDS_MINOR__ Ta Minor version number of RGBDS .It Ic EQU Ta Dv __RGBDS_PATCH__ Ta Patch version number of RGBDS .El +.Pp +The current time values will be taken from the +.Dv SOURCE_DATE_EPOCH +environment variable if that is defined as a UNIX timestamp. +Refer to the spec at +.Lk https://reproducible-builds.org/docs/source-date-epoch/ . .Sh DEFINING DATA .Ss Declaring variables in a RAM section .Ic DS diff --git a/src/asm/symbol.c b/src/asm/symbol.c index 3ce5c221..699bbb4d 100644 --- a/src/asm/symbol.c +++ b/src/asm/symbol.c @@ -655,7 +655,7 @@ static inline struct Symbol *createBuiltinSymbol(char const *name) /* * Initialize the symboltable */ -void sym_Init(void) +void sym_Init(time_t now) { PCSymbol = createBuiltinSymbol("@"); struct Symbol *_NARGSymbol = createBuiltinSymbol("_NARG"); @@ -677,8 +677,6 @@ void sym_Init(void) sym_AddEqu("__RGBDS_MINOR__", PACKAGE_VERSION_MINOR)->isBuiltin = true; sym_AddEqu("__RGBDS_PATCH__", PACKAGE_VERSION_PATCH)->isBuiltin = true; - time_t now = time(NULL); - if (now == (time_t)-1) { warn("Couldn't determine current time"); /* Fall back by pretending we are at the Epoch */