diff options
author | H. Peter Anvin <hpa@zytor.com> | 2017-08-02 18:37:54 -0700 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2017-08-02 18:37:54 -0700 |
commit | 24f7b5c3e4e357b2cd6d4bd16703e6eb3be1c59e (patch) | |
tree | 322fb68569f184aadef176463ba45b6b9b9b8ade | |
parent | f7e39739c39571bfb4a28b15159521349b1504a5 (diff) | |
download | nasm-24f7b5c3e4e357b2cd6d4bd16703e6eb3be1c59e.tar.gz |
timestamp: centralize handing of compilation timestamp
Do all the generation and conversion of the compiler timestamp in one
place and make it available to modules.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r-- | asm/nasm.c | 118 | ||||
-rw-r--r-- | include/nasm.h | 12 | ||||
-rw-r--r-- | output/outieee.c | 11 |
3 files changed, 84 insertions, 57 deletions
@@ -43,7 +43,6 @@ #include <string.h> #include <ctype.h> #include <limits.h> -#include <time.h> #include "nasm.h" #include "nasmlib.h" @@ -93,7 +92,7 @@ static int pass1, pass2; /* XXX: Get rid of these, they are redundant */ int globalrel = 0; int globalbnd = 0; -static time_t official_compile_time; +struct compile_time official_compile_time; static char inname[FILENAME_MAX]; static char outname[FILENAME_MAX]; @@ -165,69 +164,35 @@ static void nasm_fputs(const char *line, FILE * outfile) puts(line); } -/* Convert a struct tm to a POSIX-style time constant */ -static int64_t make_posix_time(struct tm *tm) -{ - int64_t t; - int64_t y = tm->tm_year; - - /* See IEEE 1003.1:2004, section 4.14 */ - - t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400; - t += tm->tm_yday; - t *= 24; - t += tm->tm_hour; - t *= 60; - t += tm->tm_min; - t *= 60; - t += tm->tm_sec; - - return t; -} - static void define_macros_early(void) { + const struct compile_time * const oct = &official_compile_time; char temp[128]; - struct tm lt, *lt_p, gm, *gm_p; - int64_t posix_time; - - lt_p = localtime(&official_compile_time); - if (lt_p) { - lt = *lt_p; - strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", <); + if (oct->have_local) { + strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local); preproc->pre_define(temp); - strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", <); + strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local); preproc->pre_define(temp); - strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", <); + strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local); preproc->pre_define(temp); - strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", <); + strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local); preproc->pre_define(temp); } - gm_p = gmtime(&official_compile_time); - if (gm_p) { - gm = *gm_p; - - strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm); + if (oct->have_gm) { + strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm); preproc->pre_define(temp); - strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm); + strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm); preproc->pre_define(temp); - strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm); + strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm); preproc->pre_define(temp); - strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm); + strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm); preproc->pre_define(temp); } - if (gm_p) - posix_time = make_posix_time(&gm); - else if (lt_p) - posix_time = make_posix_time(<); - else - posix_time = 0; - - if (posix_time) { - snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time); + if (oct->have_posix) { + snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix); preproc->pre_define(temp); } } @@ -287,11 +252,64 @@ static void emit_dependencies(StrList *list) fclose(deps); } +/* Convert a struct tm to a POSIX-style time constant */ +static int64_t make_posix_time(const struct tm *tm) +{ + int64_t t; + int64_t y = tm->tm_year; + + /* See IEEE 1003.1:2004, section 4.14 */ + + t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400; + t += tm->tm_yday; + t *= 24; + t += tm->tm_hour; + t *= 60; + t += tm->tm_min; + t *= 60; + t += tm->tm_sec; + + return t; +} + +static void timestamp(void) +{ + struct compile_time * const oct = &official_compile_time; + const struct tm *tp, *best_gm; + + time(&oct->t); + + best_gm = NULL; + + tp = localtime(&oct->t); + if (tp) { + oct->local = *tp; + best_gm = &oct->local; + oct->have_local = true; + } + + tp = gmtime(&oct->t); + if (tp) { + oct->gm = *tp; + best_gm = &oct->gm; + oct->have_gm = true; + if (!oct->have_local) + oct->local = oct->gm; + } else { + oct->gm = oct->local; + } + + if (best_gm) { + oct->posix = make_posix_time(best_gm); + oct->have_posix = true; + } +} + int main(int argc, char **argv) { StrList *depend_list = NULL, **depend_ptr; - time(&official_compile_time); + timestamp(); iflag_set(&cpu, IF_PLEVEL); iflag_set(&cmd_cpu, IF_PLEVEL); diff --git a/include/nasm.h b/include/nasm.h index a50062ef..82318649 100644 --- a/include/nasm.h +++ b/include/nasm.h @@ -41,6 +41,8 @@ #include "compiler.h" #include <stdio.h> +#include <time.h> + #include "nasmlib.h" #include "strlist.h" #include "preproc.h" @@ -49,6 +51,16 @@ #include "opflags.h" #include "regs.h" +/* Time stamp for the official start of compilation */ +struct compile_time { + time_t t; + bool have_local, have_gm, have_posix; + int64_t posix; + struct tm local; + struct tm gm; +}; +extern struct compile_time official_compile_time; + #define NO_SEG -1L /* null segment value */ #define SEG_ABS 0x40000000L /* mask for far-absolute segments */ diff --git a/output/outieee.c b/output/outieee.c index 80c07852..e96c723b 100644 --- a/output/outieee.c +++ b/output/outieee.c @@ -1,5 +1,5 @@ /* ----------------------------------------------------------------------- * - * + * * Copyright 1996-2016 The NASM Authors - All Rights Reserved * See the file AUTHORS included with the NASM distribution for * the specific copyright holders. @@ -14,7 +14,7 @@ * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF @@ -835,7 +835,7 @@ ieee_directive(enum directive directive, char *value, int pass) case D_UPPERCASE: ieee_uppercase = true; return DIRR_OK; - + default: return DIRR_UNKNOWN; } @@ -897,8 +897,7 @@ static void ieee_filename(char *inname, char *outname) static void ieee_write_file(void) { - struct tm *thetime; - time_t reltime; + const struct tm * const thetime = &official_compile_time.local; struct FileName *fn; struct ieeeSection *seg; struct ieeePublic *pub, *loc; @@ -927,8 +926,6 @@ static void ieee_write_file(void) /* * date and time */ - time(&reltime); - thetime = localtime(&reltime); ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n", 1900 + thetime->tm_year, thetime->tm_mon + 1, thetime->tm_mday, thetime->tm_hour, thetime->tm_min, |