aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorEduard Sanou <dhole@openmailbox.org>2016-04-28 09:12:05 +0000
committerMatthias Klose <doko@gcc.gnu.org>2016-04-28 09:12:05 +0000
commit174f66220d4d39ed503ded1ec3e7ba514cc4283e (patch)
treea72db5c4b6147a63f588b215e341061ba7b18de5 /gcc
parenta564d350951a990136fe6a5010b7eaa165f17c58 (diff)
downloadgcc-174f66220d4d39ed503ded1ec3e7ba514cc4283e.zip
gcc-174f66220d4d39ed503ded1ec3e7ba514cc4283e.tar.gz
gcc-174f66220d4d39ed503ded1ec3e7ba514cc4283e.tar.bz2
c-common.c (get_source_date_epoch): New function...
gcc/c-family/ChangeLog: 2016-04-28 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-28 Eduard Sanou <dhole@openmailbox.org> Matthias Klose <doko@debian.org> * doc/cppenv.texi: Document SOURCE_DATE_EPOCH environment variable. libcpp/ChangeLog: 2016-04-28 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. Co-Authored-By: Matthias Klose <doko@debian.org> From-SVN: r235550
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog5
-rw-r--r--gcc/c-family/ChangeLog9
-rw-r--r--gcc/c-family/c-common.c33
-rw-r--r--gcc/c-family/c-common.h5
-rw-r--r--gcc/c-family/c-lex.c3
-rw-r--r--gcc/doc/cppenv.texi17
6 files changed, 72 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index face26b..a41b8e0 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2016-04-28 Eduard Sanou <dhole@openmailbox.org>
+ Matthias Klose <doko@debian.org>
+
+ * doc/cppenv.texi: Document SOURCE_DATE_EPOCH environment variable.
+
2016-04-28 Richard Biener <rguenther@suse.de>
PR middle-end/70777
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index ac3be53..8b1e764 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,12 @@
+2016-04-28 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.
+
2015-04-27 Ryan Burn <contact@rnburn.com>
PR c++/69024
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 1f0d76a..c086dee 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -12784,4 +12784,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 1309549..3a7805f 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1471,4 +1471,9 @@ extern bool valid_array_size_p (location_t, tree, tree);
extern bool cilk_ignorable_spawn_rhs_op (tree);
extern bool cilk_recognize_spawn (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 6b020a4..ff7eb25 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -388,6 +388,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