From 24846718507fe1565d64742a0a8c68acb680c640 Mon Sep 17 00:00:00 2001 From: "John W. Eaton" Date: Tue, 17 Dec 2013 11:04:37 -0800 Subject: [PATCH] gettimeofday: fix C++ crosscompilation Never replace gmtime and localtime by macros when compiling with C++, this prevents from being included. * m4/gettimeofday.m4 (gl_GETTIMEOFDAY_REPLACE_LOCALTIME): Do not define gmtime and localtime as preprocessor macros. Instead define some HAVE_GMTIME, HAVE_LOCALTIME, REPLACE_GMTIME, and REPLACE_LOCALTIME substitutions. * lib/time.in.h: Declare gmtime and localtime when needed. * m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): AC_SUBST HAVE_GMTIME, HAVE_LOCALTIME, REPLACE_GMTIME, and REPLACE_LOCALTIME. * modules/time: Depend on gettimeofday, and substitute the above variables in time.h. --- ChangeLog | 17 +++++++++++++++++ lib/time.in.h | 34 ++++++++++++++++++++++++++++++++++ m4/gettimeofday.m4 | 13 +++++++++---- m4/time_h.m4 | 4 ++++ modules/time | 5 +++++ 5 files changed, 69 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index e4525aa7a..f3e5cedf0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2013-12-17 John W. Eaton + + gettimeofday: fix C++ crosscompilation + + Never replace gmtime and localtime by macros when compiling with + C++, this prevents from being included. + + * m4/gettimeofday.m4 (gl_GETTIMEOFDAY_REPLACE_LOCALTIME): Do not + define gmtime and localtime as preprocessor macros. Instead + define some HAVE_GMTIME, HAVE_LOCALTIME, REPLACE_GMTIME, and + REPLACE_LOCALTIME substitutions. + * lib/time.in.h: Declare gmtime and localtime when needed. + * m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): AC_SUBST HAVE_GMTIME, + HAVE_LOCALTIME, REPLACE_GMTIME, and REPLACE_LOCALTIME. + * modules/time: Depend on gettimeofday, and substitute the above + variables in time.h. + 2013-12-17 Paul Eggert qacl: port to Windows better diff --git a/lib/time.in.h b/lib/time.in.h index ceea2ee4f..157896f84 100644 --- a/lib/time.in.h +++ b/lib/time.in.h @@ -187,6 +187,40 @@ _GL_CXXALIASWARN (gmtime_r); # endif # endif +/* Convert TIMER to RESULT, assuming local time and UTC respectively. See + and + . */ +# if @REPLACE_LOCALTIME@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef localtime +# define localtime rpl_localtime +# endif +_GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer) + _GL_ARG_NONNULL ((1))); +_GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer)); +# else +# if ! @HAVE_LOCALTIME@ +_GL_FUNCDECL_SYS (localtime, struct tm *, (time_t const *__timer) + _GL_ARG_NONNULL ((1))); +# endif +_GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer)); +# endif +# if @REPLACE_GMTIME@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef gmtime +# define gmtime rpl_gmtime +# endif +_GL_FUNCDECL_RPL (gmtime, struct tm *, (time_t const *__timer) + _GL_ARG_NONNULL ((1))); +_GL_CXXALIAS_RPL (gmtime, struct tm *, (time_t const *__timer)); +# else +# if ! @HAVE_GMTIME@ +_GL_FUNCDECL_SYS (gmtime, struct tm *, (time_t const *__timer) + _GL_ARG_NONNULL ((1))); +# endif +_GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer)); +# endif + /* Parse BUF as a time stamp, assuming FORMAT specifies its layout, and store the resulting broken-down time into TM. See . */ diff --git a/m4/gettimeofday.m4 b/m4/gettimeofday.m4 index 3c05e59f0..e4682bd99 100644 --- a/m4/gettimeofday.m4 +++ b/m4/gettimeofday.m4 @@ -127,10 +127,15 @@ AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER], ]) AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [ - AC_DEFINE([gmtime], [rpl_gmtime], - [Define to rpl_gmtime if the replacement function should be used.]) - AC_DEFINE([localtime], [rpl_localtime], - [Define to rpl_localtime if the replacement function should be used.]) + AC_CHECK_FUNCS_ONCE([gmtime localtime]) + if test $ac_cv_func_gmtime != yes; then + HAVE_GMTIME=0 + fi + if test $ac_cv_func_localtime != yes; then + HAVE_LOCALTIME=0 + fi + REPLACE_GMTIME=1 + REPLACE_LOCALTIME=1 ]) # Prerequisites of lib/gettimeofday.c. diff --git a/m4/time_h.m4 b/m4/time_h.m4 index 3b8390053..8675dd966 100644 --- a/m4/time_h.m4 +++ b/m4/time_h.m4 @@ -96,12 +96,16 @@ AC_DEFUN([gl_HEADER_TIME_H_DEFAULTS], GNULIB_TIME_R=0; AC_SUBST([GNULIB_TIME_R]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_DECL_LOCALTIME_R=1; AC_SUBST([HAVE_DECL_LOCALTIME_R]) + HAVE_GMTIME=1; AC_SUBST([HAVE_GMTIME]) + HAVE_LOCALTIME=1; AC_SUBST([HAVE_LOCALTIME]) HAVE_NANOSLEEP=1; AC_SUBST([HAVE_NANOSLEEP]) HAVE_STRPTIME=1; AC_SUBST([HAVE_STRPTIME]) HAVE_TIMEGM=1; AC_SUBST([HAVE_TIMEGM]) dnl If another module says to replace or to not replace, do that. dnl Otherwise, replace only if someone compiles with -DGNULIB_PORTCHECK; dnl this lets maintainers check for portability. + REPLACE_GMTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_GMTIME]) + REPLACE_LOCALTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_LOCALTIME]) REPLACE_LOCALTIME_R=GNULIB_PORTCHECK; AC_SUBST([REPLACE_LOCALTIME_R]) REPLACE_MKTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_MKTIME]) REPLACE_NANOSLEEP=GNULIB_PORTCHECK; AC_SUBST([REPLACE_NANOSLEEP]) diff --git a/modules/time b/modules/time index 8e946fcff..c58b93316 100644 --- a/modules/time +++ b/modules/time @@ -7,6 +7,7 @@ m4/time_h.m4 Depends-on: extensions +gettimeofday include_next snippet/arg-nonnull snippet/c++defs @@ -35,9 +36,13 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's/@''GNULIB_TIMEGM''@/$(GNULIB_TIMEGM)/g' \ -e 's/@''GNULIB_TIME_R''@/$(GNULIB_TIME_R)/g' \ -e 's|@''HAVE_DECL_LOCALTIME_R''@|$(HAVE_DECL_LOCALTIME_R)|g' \ + -e 's|@''HAVE_LOCALTIME''@|$(HAVE_LOCALTIME)|g' \ + -e 's|@''HAVE_GMTIME''@|$(HAVE_GMTIME)|g' \ -e 's|@''HAVE_NANOSLEEP''@|$(HAVE_NANOSLEEP)|g' \ -e 's|@''HAVE_STRPTIME''@|$(HAVE_STRPTIME)|g' \ -e 's|@''HAVE_TIMEGM''@|$(HAVE_TIMEGM)|g' \ + -e 's|@''REPLACE_GMTIME''@|$(REPLACE_GMTIME)|g' \ + -e 's|@''REPLACE_LOCALTIME''@|$(REPLACE_LOCALTIME)|g' \ -e 's|@''REPLACE_LOCALTIME_R''@|$(REPLACE_LOCALTIME_R)|g' \ -e 's|@''REPLACE_MKTIME''@|$(REPLACE_MKTIME)|g' \ -e 's|@''REPLACE_NANOSLEEP''@|$(REPLACE_NANOSLEEP)|g' \ -- 2.11.0