sys_time_h.m4 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Configure a replacement for <sys/time.h>.
  2. # serial 12
  3. # Copyright (C) 2007, 2009-2023 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. # Written by Paul Eggert and Martin Lambers.
  8. AC_DEFUN_ONCE([gl_SYS_TIME_H],
  9. [
  10. dnl Use AC_REQUIRE here, so that the REPLACE_GETTIMEOFDAY=0 statement
  11. dnl below is expanded once only, before all REPLACE_GETTIMEOFDAY=1
  12. dnl statements that occur in other macros.
  13. AC_REQUIRE([gl_SYS_TIME_H_DEFAULTS])
  14. AC_REQUIRE([AC_C_RESTRICT])
  15. AC_CHECK_HEADERS_ONCE([sys/time.h])
  16. gl_CHECK_NEXT_HEADERS([sys/time.h])
  17. if test $ac_cv_header_sys_time_h != yes; then
  18. HAVE_SYS_TIME_H=0
  19. fi
  20. dnl On native Windows with MSVC, 'struct timeval' is defined in <winsock2.h>
  21. dnl only. So include that header in the list.
  22. gl_PREREQ_SYS_H_WINSOCK2
  23. AC_CACHE_CHECK([for struct timeval], [gl_cv_sys_struct_timeval],
  24. [AC_COMPILE_IFELSE(
  25. [AC_LANG_PROGRAM(
  26. [[#if HAVE_SYS_TIME_H
  27. #include <sys/time.h>
  28. #endif
  29. #include <time.h>
  30. #if HAVE_WINSOCK2_H
  31. # include <winsock2.h>
  32. #endif
  33. ]],
  34. [[static struct timeval x; x.tv_sec = x.tv_usec;]])],
  35. [gl_cv_sys_struct_timeval=yes],
  36. [gl_cv_sys_struct_timeval=no])
  37. ])
  38. if test $gl_cv_sys_struct_timeval != yes; then
  39. HAVE_STRUCT_TIMEVAL=0
  40. else
  41. dnl On native Windows with a 64-bit 'time_t', 'struct timeval' is defined
  42. dnl (in <sys/time.h> and <winsock2.h> for mingw64, in <winsock2.h> only
  43. dnl for MSVC) with a tv_sec field of type 'long' (32-bit!), which is
  44. dnl smaller than the 'time_t' type mandated by POSIX.
  45. dnl On OpenBSD 5.1 amd64, tv_sec is 64 bits and time_t 32 bits, but
  46. dnl that is good enough.
  47. AC_CACHE_CHECK([for wide-enough struct timeval.tv_sec member],
  48. [gl_cv_sys_struct_timeval_tv_sec],
  49. [AC_COMPILE_IFELSE(
  50. [AC_LANG_PROGRAM(
  51. [[#if HAVE_SYS_TIME_H
  52. #include <sys/time.h>
  53. #endif
  54. #include <time.h>
  55. #if HAVE_WINSOCK2_H
  56. # include <winsock2.h>
  57. #endif
  58. ]],
  59. [[static struct timeval x;
  60. typedef int verify_tv_sec_type[
  61. sizeof (time_t) <= sizeof x.tv_sec ? 1 : -1
  62. ];
  63. ]])],
  64. [gl_cv_sys_struct_timeval_tv_sec=yes],
  65. [gl_cv_sys_struct_timeval_tv_sec=no])
  66. ])
  67. if test $gl_cv_sys_struct_timeval_tv_sec != yes; then
  68. REPLACE_STRUCT_TIMEVAL=1
  69. fi
  70. fi
  71. dnl Check for declarations of anything we want to poison if the
  72. dnl corresponding gnulib module is not in use.
  73. gl_WARN_ON_USE_PREPARE([[
  74. #if HAVE_SYS_TIME_H
  75. # include <sys/time.h>
  76. #endif
  77. #include <time.h>
  78. ]], [gettimeofday])
  79. ])
  80. # gl_SYS_TIME_MODULE_INDICATOR([modulename])
  81. # sets the shell variable that indicates the presence of the given module
  82. # to a C preprocessor expression that will evaluate to 1.
  83. # This macro invocation must not occur in macros that are AC_REQUIREd.
  84. AC_DEFUN([gl_SYS_TIME_MODULE_INDICATOR],
  85. [
  86. dnl Ensure to expand the default settings once only.
  87. gl_SYS_TIME_H_REQUIRE_DEFAULTS
  88. gl_MODULE_INDICATOR_SET_VARIABLE([$1])
  89. dnl Define it also as a C macro, for the benefit of the unit tests.
  90. gl_MODULE_INDICATOR_FOR_TESTS([$1])
  91. ])
  92. # Initializes the default values for AC_SUBSTed shell variables.
  93. # This macro must not be AC_REQUIREd. It must only be invoked, and only
  94. # outside of macros or in macros that are not AC_REQUIREd.
  95. AC_DEFUN([gl_SYS_TIME_H_REQUIRE_DEFAULTS],
  96. [
  97. m4_defun(GL_MODULE_INDICATOR_PREFIX[_SYS_TIME_H_MODULE_INDICATOR_DEFAULTS], [
  98. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETTIMEOFDAY])
  99. ])
  100. m4_require(GL_MODULE_INDICATOR_PREFIX[_SYS_TIME_H_MODULE_INDICATOR_DEFAULTS])
  101. AC_REQUIRE([gl_SYS_TIME_H_DEFAULTS])
  102. ])
  103. AC_DEFUN([gl_SYS_TIME_H_DEFAULTS],
  104. [
  105. dnl Assume POSIX behavior unless another module says otherwise.
  106. HAVE_GETTIMEOFDAY=1; AC_SUBST([HAVE_GETTIMEOFDAY])
  107. HAVE_STRUCT_TIMEVAL=1; AC_SUBST([HAVE_STRUCT_TIMEVAL])
  108. HAVE_SYS_TIME_H=1; AC_SUBST([HAVE_SYS_TIME_H])
  109. REPLACE_GETTIMEOFDAY=0; AC_SUBST([REPLACE_GETTIMEOFDAY])
  110. REPLACE_STRUCT_TIMEVAL=0; AC_SUBST([REPLACE_STRUCT_TIMEVAL])
  111. ])