From: Dhole <dhole@openmailbox.org>
To: Bernd Schmidt <bschmidt@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: Allow embedded timestamps by C/C++ macros to be set externally (3)
Date: Wed, 27 Apr 2016 15:57:00 -0000 [thread overview]
Message-ID: <20160427155633.GA21574@panther> (raw)
In-Reply-To: <571FFADB.3080209@redhat.com>
[-- Attachment #1.1: Type: text/plain, Size: 1389 bytes --]
Thanks again for the review Bernd,
On 16-04-27 01:33:47, Bernd Schmidt wrote:
> >+ epoch = strtoll (source_date_epoch, &endptr, 10);
> >+ if ((errno == ERANGE && (epoch == LLONG_MAX || epoch == LLONG_MIN))
> >+ || (errno != 0 && epoch == 0))
> >+ fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
> >+ "strtoll: %s\n", xstrerror(errno));
> >+ if (endptr == source_date_epoch)
> >+ fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
> >+ "No digits were found: %s\n", endptr);
> >+ if (*endptr != '\0')
> >+ fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
> >+ "Trailing garbage: %s\n", endptr);
> >+ if (epoch < 0)
> >+ fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
> >+ "Value must be nonnegative: %lld \n", epoch);
>
> These are somewhat unusual for error messages, but I think the general
> principle of no capitalization probably applies, so "No", "Trailing", and
> "Value" should be lowercase.
Done.
> >+ time_t source_date_epoch = (time_t) -1;
> >+
> >+ source_date_epoch = get_source_date_epoch ();
>
> First initialization seems unnecessary. Might want to merge the declaration
> with the initialization.
And done.
I'm attaching the updated patch with the two minor issues fixed.
Cheers,
--
Dhole
[-- Attachment #1.2: gcc-SOURCE_DATE_EPOCH-patch-2016_04_27.diff.txt --]
[-- Type: text/plain, Size: 6523 bytes --]
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index f2846bb..5315475 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -12741,4 +12741,37 @@ valid_array_size_p (location_t loc, tree type, tree name)
return true;
}
+/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
+ timestamp to replace embedded current dates to get reproducible
+ results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
+time_t
+get_source_date_epoch ()
+{
+ char *source_date_epoch;
+ long long epoch;
+ char *endptr;
+
+ source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
+ if (!source_date_epoch)
+ return (time_t) -1;
+
+ errno = 0;
+ epoch = strtoll (source_date_epoch, &endptr, 10);
+ if ((errno == ERANGE && (epoch == LLONG_MAX || epoch == LLONG_MIN))
+ || (errno != 0 && epoch == 0))
+ fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
+ "strtoll: %s\n", xstrerror(errno));
+ if (endptr == source_date_epoch)
+ fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
+ "no digits were found: %s\n", endptr);
+ if (*endptr != '\0')
+ fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
+ "trailing garbage: %s\n", endptr);
+ if (epoch < 0)
+ fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
+ "value must be nonnegative: %lld \n", epoch);
+
+ return (time_t) epoch;
+}
+
#include "gt-c-family-c-common.h"
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index fa3746c..656bc75 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1467,4 +1467,9 @@ extern bool reject_gcc_builtin (const_tree, location_t = UNKNOWN_LOCATION);
extern void warn_duplicated_cond_add_or_warn (location_t, tree, vec<tree> **);
extern bool valid_array_size_p (location_t, tree, tree);
+/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
+ timestamp to replace embedded current dates to get reproducible
+ results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
+extern time_t get_source_date_epoch (void);
+
#endif /* ! GCC_C_COMMON_H */
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index 96da4fc..bf1db6c 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -385,6 +385,9 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
enum cpp_ttype type;
unsigned char add_flags = 0;
enum overflow_type overflow = OT_NONE;
+ time_t source_date_epoch = get_source_date_epoch ();
+
+ cpp_init_source_date_epoch (parse_in, source_date_epoch);
timevar_push (TV_CPP);
retry:
diff --git a/gcc/doc/cppenv.texi b/gcc/doc/cppenv.texi
index 22c8cb3..e958e93 100644
--- a/gcc/doc/cppenv.texi
+++ b/gcc/doc/cppenv.texi
@@ -79,4 +79,21 @@ main input file is omitted.
@ifclear cppmanual
@xref{Preprocessor Options}.
@end ifclear
+
+@item SOURCE_DATE_EPOCH
+
+If this variable is set, its value specifies a UNIX timestamp to be
+used in replacement of the current date and time in the @code{__DATE__}
+and @code{__TIME__} macros, so that the embedded timestamps become
+reproducible.
+
+The value of @env{SOURCE_DATE_EPOCH} must be a UNIX timestamp,
+defined as the number of seconds (excluding leap seconds) since
+01 Jan 1970 00:00:00 represented in ASCII, identical to the output of
+@samp{@command{date +%s}}.
+
+The value should be a known timestamp such as the last modification
+time of the source or package and it should be set by the build
+process.
+
@end vtable
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 35b0375..4998b3a 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -784,6 +784,9 @@ extern void cpp_init_special_builtins (cpp_reader *);
/* Set up built-ins like __FILE__. */
extern void cpp_init_builtins (cpp_reader *, int);
+/* Initialize the source_date_epoch value. */
+extern void cpp_init_source_date_epoch (cpp_reader *, time_t);
+
/* This is called after options have been parsed, and partially
processed. */
extern void cpp_post_options (cpp_reader *);
diff --git a/libcpp/init.c b/libcpp/init.c
index 4343075..f5ff85b 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -533,8 +533,15 @@ cpp_init_builtins (cpp_reader *pfile, int hosted)
_cpp_define_builtin (pfile, "__OBJC__ 1");
}
+/* Initialize the source_date_epoch value. */
+void
+cpp_init_source_date_epoch (cpp_reader *pfile, time_t source_date_epoch)
+{
+ pfile->source_date_epoch = source_date_epoch;
+}
+
/* Sanity-checks are dependent on command-line options, so it is
- called as a subroutine of cpp_read_main_file (). */
+ called as a subroutine of cpp_read_main_file. */
#if CHECKING_P
static void sanity_checks (cpp_reader *);
static void sanity_checks (cpp_reader *pfile)
diff --git a/libcpp/internal.h b/libcpp/internal.h
index 9ce8707..e3eb26b 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -502,6 +502,10 @@ struct cpp_reader
const unsigned char *date;
const unsigned char *time;
+ /* Externally set timestamp to replace current date and time useful for
+ reproducibility. */
+ time_t source_date_epoch;
+
/* EOF token, and a token forcing paste avoidance. */
cpp_token avoid_paste;
cpp_token eof;
diff --git a/libcpp/macro.c b/libcpp/macro.c
index c251553..c2a8376 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -357,13 +357,20 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
time_t tt;
struct tm *tb = NULL;
- /* (time_t) -1 is a legitimate value for "number of seconds
- since the Epoch", so we have to do a little dance to
- distinguish that from a genuine error. */
- errno = 0;
- tt = time(NULL);
- if (tt != (time_t)-1 || errno == 0)
- tb = localtime (&tt);
+ /* Set a reproducible timestamp for __DATE__ and __TIME__ macro
+ usage if SOURCE_DATE_EPOCH is defined. */
+ if (pfile->source_date_epoch != (time_t) -1)
+ tb = gmtime (&pfile->source_date_epoch);
+ else
+ {
+ /* (time_t) -1 is a legitimate value for "number of seconds
+ since the Epoch", so we have to do a little dance to
+ distinguish that from a genuine error. */
+ errno = 0;
+ tt = time (NULL);
+ if (tt != (time_t)-1 || errno == 0)
+ tb = localtime (&tt);
+ }
if (tb)
{
[-- Attachment #1.3: ChangeLog --]
[-- Type: text/plain, Size: 1136 bytes --]
gcc/c-family/ChangeLog:
2016-04-27 Eduard Sanou <dhole@openmailbox.org>
Matthias Klose <doko@debian.org>
* c-common.c (get_source_date_epoch): New function, gets the environment
variable SOURCE_DATE_EPOCH and parses it as long long with error
handling.
* c-common.h (get_source_date_epoch): Prototype.
* c-lex.c (c_lex_with_flags): set parse_in->source_date_epoch.
gcc/ChangeLog:
2016-04-27 Eduard Sanou <dhole@openmailbox.org>
Matthias Klose <doko@debian.org>
* doc/cppenv.texi: Document SOURCE_DATE_EPOCH environment variable.
libcpp/ChangeLog:
2016-04-27 Eduard Sanou <dhole@openmailbox.org>
Matthias Klose <doko@debian.org>
* include/cpplib.h (cpp_init_source_date_epoch): Prototype.
* init.c (cpp_init_source_date_epoch): New function.
* internal.h: Added source_date_epoch variable to struct
cpp_reader to store a reproducible date.
* macro.c (_cpp_builtin_macro_text): Set pfile->date timestamp from
pfile->source_date_epoch instead of localtime if source_date_epoch is
set, to be used for __DATE__ and __TIME__ macros to help reproducible
builds.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]
next prev parent reply other threads:[~2016-04-27 15:57 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-18 12:28 Dhole
2016-04-18 13:05 ` Markus Trippelsdorf
2016-05-03 14:44 ` Dhole
2016-05-03 14:53 ` Markus Trippelsdorf
2016-04-25 10:16 ` Bernd Schmidt
2016-04-26 21:29 ` Dhole
2016-04-26 23:33 ` Bernd Schmidt
2016-04-27 15:57 ` Dhole [this message]
2016-04-28 9:20 ` Matthias Klose
2016-04-28 9:22 ` Bernd Schmidt
2016-04-28 10:08 ` Jakub Jelinek
2016-04-28 10:31 ` Bernd Schmidt
2016-04-28 10:35 ` Jakub Jelinek
2016-04-28 13:10 ` Bernd Schmidt
2016-04-28 13:14 ` Jakub Jelinek
2016-04-28 18:31 ` Dhole
2016-04-29 7:17 ` Jakub Jelinek
2016-05-05 23:28 ` Eduard Sanou
2016-05-06 6:26 ` Andreas Schwab
2016-05-10 11:14 ` Dhole
2016-05-10 11:24 ` Andreas Schwab
2016-05-10 11:32 ` Bernd Schmidt
2016-05-10 15:51 ` Joseph Myers
2016-05-12 0:38 ` Dhole
2016-05-12 9:17 ` Bernd Schmidt
2016-05-13 17:11 ` Dhole
2016-05-23 23:00 ` Dhole
2016-05-24 16:45 ` Jeff Law
2016-06-01 16:29 ` Bernd Schmidt
2016-06-01 16:59 ` Matthias Klose
2016-06-02 13:01 ` Christophe Lyon
2016-06-02 13:05 ` Jakub Jelinek
2016-06-02 13:21 ` Christophe Lyon
2016-06-02 13:24 ` Jakub Jelinek
2016-06-02 14:50 ` Christophe Lyon
2016-06-02 15:04 ` Jakub Jelinek
2016-06-02 15:27 ` Bernd Schmidt
2016-06-02 15:30 ` Christophe Lyon
2016-06-02 12:19 ` Fix up dg-set-compiler-env-var Jakub Jelinek
2016-06-02 12:26 ` Bernd Schmidt
2016-06-02 12:33 ` Jakub Jelinek
2016-05-05 23:39 ` Allow embedded timestamps by C/C++ macros to be set externally (3) Dhole
2016-05-09 10:24 ` Bernd Schmidt
2016-05-09 10:38 ` Bernd Schmidt
2016-05-09 10:42 ` Jakub Jelinek
2016-05-09 11:01 ` Bernd Schmidt
2016-05-10 11:18 ` Dhole
2016-04-28 18:57 ` Martin Sebor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160427155633.GA21574@panther \
--to=dhole@openmailbox.org \
--cc=bschmidt@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).