gettimeofday: Move AC_LIBOBJ invocations to module description.
[gnulib.git] / m4 / gettimeofday.m4
1 # serial 17
2
3 # Copyright (C) 2001-2003, 2005, 2007, 2009-2011 Free Software Foundation, Inc.
4 # This file is free software; the Free Software Foundation
5 # gives unlimited permission to copy and/or distribute it,
6 # with or without modifications, as long as this notice is preserved.
7
8 dnl From Jim Meyering.
9
10 AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
11 [
12   AC_REQUIRE([AC_C_RESTRICT])
13   AC_REQUIRE([gl_HEADER_SYS_TIME_H])
14   AC_REQUIRE([gl_HEADER_SYS_TIME_H_DEFAULTS])
15   AC_CHECK_FUNCS_ONCE([gettimeofday])
16
17   gl_gettimeofday_timezone=void
18   if test $ac_cv_func_gettimeofday != yes; then
19     HAVE_GETTIMEOFDAY=0
20   else
21     gl_FUNC_GETTIMEOFDAY_CLOBBER
22     AC_CACHE_CHECK([for gettimeofday with POSIX signature],
23       [gl_cv_func_gettimeofday_posix_signature],
24       [AC_COMPILE_IFELSE(
25          [AC_LANG_PROGRAM(
26             [[#include <sys/time.h>
27               struct timeval c;
28               int gettimeofday (struct timeval *restrict, void *restrict);
29             ]],
30             [[/* glibc uses struct timezone * rather than the POSIX void *
31                  if _GNU_SOURCE is defined.  However, since the only portable
32                  use of gettimeofday uses NULL as the second parameter, and
33                  since the glibc definition is actually more typesafe, it is
34                  not worth wrapping this to get a compliant signature.  */
35               int (*f) (struct timeval *restrict, void *restrict)
36                 = gettimeofday;
37               int x = f (&c, 0);
38               return !(x | c.tv_sec | c.tv_usec);
39             ]])],
40           [gl_cv_func_gettimeofday_posix_signature=yes],
41           [AC_COMPILE_IFELSE(
42             [AC_LANG_PROGRAM(
43               [[#include <sys/time.h>
44 int gettimeofday (struct timeval *restrict, struct timezone *restrict);
45               ]])],
46             [gl_cv_func_gettimeofday_posix_signature=almost],
47             [gl_cv_func_gettimeofday_posix_signature=no])])])
48     if test $gl_cv_func_gettimeofday_posix_signature = almost; then
49       gl_gettimeofday_timezone='struct timezone'
50     elif test $gl_cv_func_gettimeofday_posix_signature != yes; then
51       REPLACE_GETTIMEOFDAY=1
52     fi
53     m4_ifdef([gl_FUNC_TZSET_CLOBBER], [
54       gl_FUNC_TZSET_CLOBBER
55       if test $gl_cv_func_tzset_clobber = yes; then
56         REPLACE_GETTIMEOFDAY=1
57         gl_GETTIMEOFDAY_REPLACE_LOCALTIME
58         AC_DEFINE([tzset], [rpl_tzset],
59           [Define to rpl_tzset if the wrapper function should be used.])
60         AC_DEFINE([TZSET_CLOBBERS_LOCALTIME], [1],
61           [Define if tzset clobbers localtime's static buffer.])
62       fi
63     ])
64   fi
65   AC_DEFINE_UNQUOTED([GETTIMEOFDAY_TIMEZONE], [$gl_gettimeofday_timezone],
66     [Define this to 'void' or 'struct timezone' to match the system's
67      declaration of the second argument to gettimeofday.])
68 ])
69
70
71 dnl See if gettimeofday clobbers the static buffer that localtime uses
72 dnl for its return value.  The gettimeofday function from Mac OS X 10.0.4
73 dnl (i.e., Darwin 1.3.7) has this problem.
74 dnl
75 dnl If it does, then arrange to use gettimeofday and localtime only via
76 dnl the wrapper functions that work around the problem.
77
78 AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER],
79 [
80  AC_REQUIRE([gl_HEADER_SYS_TIME_H])
81
82  AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
83   [gl_cv_func_gettimeofday_clobber],
84   [AC_RUN_IFELSE(
85      [AC_LANG_PROGRAM(
86         [[#include <string.h>
87           #include <sys/time.h>
88           #include <time.h>
89           #include <stdlib.h>
90         ]],
91         [[
92           time_t t = 0;
93           struct tm *lt;
94           struct tm saved_lt;
95           struct timeval tv;
96           lt = localtime (&t);
97           saved_lt = *lt;
98           gettimeofday (&tv, NULL);
99           return memcmp (lt, &saved_lt, sizeof (struct tm)) != 0;
100         ]])],
101      [gl_cv_func_gettimeofday_clobber=no],
102      [gl_cv_func_gettimeofday_clobber=yes],
103      dnl When crosscompiling, assume it is broken.
104      [gl_cv_func_gettimeofday_clobber=yes])])
105
106  if test $gl_cv_func_gettimeofday_clobber = yes; then
107    REPLACE_GETTIMEOFDAY=1
108    gl_GETTIMEOFDAY_REPLACE_LOCALTIME
109    AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], [1],
110      [Define if gettimeofday clobbers the localtime buffer.])
111  fi
112 ])
113
114 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
115   AC_DEFINE([gmtime], [rpl_gmtime],
116     [Define to rpl_gmtime if the replacement function should be used.])
117   AC_DEFINE([localtime], [rpl_localtime],
118     [Define to rpl_localtime if the replacement function should be used.])
119 ])
120
121 # Prerequisites of lib/gettimeofday.c.
122 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
123   AC_CHECK_HEADERS([sys/timeb.h])
124   AC_CHECK_FUNCS([_ftime])
125 ])