diff mbox series

Allow to override build date with SOURCE_DATE_EPOCH

Message ID 20231023083949.26204-1-bwiedemann@suse.de
State Superseded
Headers show
Series Allow to override build date with SOURCE_DATE_EPOCH | expand

Commit Message

Bernhard M. Wiedemann Oct. 23, 2023, 8:39 a.m. UTC
in order to make builds reproducible.
See https://reproducible-builds.org/ for why this is good
and https://reproducible-builds.org/specs/source-date-epoch/
for the definition of this variable.

Without this patch, openSUSE's qemu package
always varied between builds.

This patch was done while working on reproducible builds for openSUSE.

This is an alternative to
https://lists.ozlabs.org/pipermail/slof/2023-October/002895.html

Signed-off-by: Bernhard M. Wiedemann <bwiedemann@suse.de>
---
 romfs/tools/create_crc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Comments

Thomas Huth Oct. 23, 2023, 9:03 a.m. UTC | #1
On 23/10/2023 10.39, Bernhard M. Wiedemann wrote:
> in order to make builds reproducible.
> See https://reproducible-builds.org/ for why this is good
> and https://reproducible-builds.org/specs/source-date-epoch/
> for the definition of this variable.
> 
> Without this patch, openSUSE's qemu package
> always varied between builds.
> 
> This patch was done while working on reproducible builds for openSUSE.
> 
> This is an alternative to
> https://lists.ozlabs.org/pipermail/slof/2023-October/002895.html
> 
> Signed-off-by: Bernhard M. Wiedemann <bwiedemann@suse.de>
> ---
>   romfs/tools/create_crc.c | 8 +++++++-
>   1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/romfs/tools/create_crc.c b/romfs/tools/create_crc.c
> index abc373b..9122e3c 100644
> --- a/romfs/tools/create_crc.c
> +++ b/romfs/tools/create_crc.c
> @@ -95,7 +95,13 @@ createHeaderImage(int notime)
>   
>   	if (!notime) {
>   		/* read time and write it into data stream */
> -		if ((caltime = time(NULL)) == -1) {
> +		char *source_date_epoch;
> +		/* This assumes that (if used) the SOURCE_DATE_EPOCH environment variable
> +		   will contain a correct, positive integer in the time_t range */
> +		if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) == NULL ||
> +			(caltime = (time_t)strtoll(source_date_epoch, NULL, 10)) <= 0)

Mostly a matter of taste, but I think it would be nicer to indent "(caltime" 
to the same level as "(source_date_epoch" in the previous line.

> +			time(&caltime);

Could you please use caltime = time(NULL) instead? ... I'm not quite sure 
whether time() will return -1 as error code via the pointer, too, or whether 
it only returns -1 as error via the return code.

With that change added:
Reviewed-by: Thomas Huth <thuth@redhat.com>


> +		if (caltime == -1) {
>   			printf("time error\n");
>   		}
>   		if ((tm = localtime(&caltime)) == NULL) {
diff mbox series

Patch

diff --git a/romfs/tools/create_crc.c b/romfs/tools/create_crc.c
index abc373b..9122e3c 100644
--- a/romfs/tools/create_crc.c
+++ b/romfs/tools/create_crc.c
@@ -95,7 +95,13 @@  createHeaderImage(int notime)
 
 	if (!notime) {
 		/* read time and write it into data stream */
-		if ((caltime = time(NULL)) == -1) {
+		char *source_date_epoch;
+		/* This assumes that (if used) the SOURCE_DATE_EPOCH environment variable
+		   will contain a correct, positive integer in the time_t range */
+		if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) == NULL ||
+			(caltime = (time_t)strtoll(source_date_epoch, NULL, 10)) <= 0)
+			time(&caltime);
+		if (caltime == -1) {
 			printf("time error\n");
 		}
 		if ((tm = localtime(&caltime)) == NULL) {