Home | History | Annotate | Line # | Download | only in m4
      1  1.1  christos # year2038.m4 serial 7
      2  1.1  christos dnl Copyright (C) 2017-2022 Free Software Foundation, Inc.
      3  1.1  christos dnl This file is free software; the Free Software Foundation
      4  1.1  christos dnl gives unlimited permission to copy and/or distribute it,
      5  1.1  christos dnl with or without modifications, as long as this notice is preserved.
      6  1.1  christos 
      7  1.1  christos dnl Attempt to ensure that 'time_t' can go past the year 2038 and that
      8  1.1  christos dnl the functions 'time', 'stat', etc. work with post-2038 timestamps.
      9  1.1  christos 
     10  1.1  christos AC_DEFUN([gl_YEAR2038_EARLY],
     11  1.1  christos [
     12  1.1  christos   AC_REQUIRE([AC_CANONICAL_HOST])
     13  1.1  christos   case "$host_os" in
     14  1.1  christos     mingw*)
     15  1.1  christos       AC_DEFINE([__MINGW_USE_VC2005_COMPAT], [1],
     16  1.1  christos         [For 64-bit time_t on 32-bit mingw.])
     17  1.1  christos       ;;
     18  1.1  christos   esac
     19  1.1  christos ])
     20  1.1  christos 
     21  1.1  christos # gl_YEAR2038_TEST_INCLUDES
     22  1.1  christos # -------------------------
     23  1.1  christos AC_DEFUN([gl_YEAR2038_TEST_INCLUDES],
     24  1.1  christos [[
     25  1.1  christos   #include <time.h>
     26  1.1  christos   /* Check that time_t can represent 2**32 - 1 correctly.  */
     27  1.1  christos   #define LARGE_TIME_T \\
     28  1.1  christos     ((time_t) (((time_t) 1 << 30) - 1 + 3 * ((time_t) 1 << 30)))
     29  1.1  christos   int verify_time_t_range[(LARGE_TIME_T / 65537 == 65535
     30  1.1  christos                            && LARGE_TIME_T % 65537 == 0)
     31  1.1  christos                           ? 1 : -1];
     32  1.1  christos ]])
     33  1.1  christos 
     34  1.1  christos # gl_YEAR2038_BODY(REQUIRE-YEAR2038-SAFE)
     35  1.1  christos -----------------------------------------
     36  1.1  christos AC_DEFUN([gl_YEAR2038_BODY],
     37  1.1  christos [
     38  1.1  christos  AC_ARG_ENABLE([year2038],
     39  1.1  christos    [  --disable-year2038      omit support for timestamps past the year 2038])
     40  1.1  christos  AS_IF([test "$enable_year2038" != no],
     41  1.1  christos  [
     42  1.1  christos   dnl On many systems, time_t is already a 64-bit type.
     43  1.1  christos   dnl On those systems where time_t is still 32-bit, it requires kernel
     44  1.1  christos   dnl and libc support to make it 64-bit. For glibc 2.34 and later on Linux,
     45  1.1  christos   dnl defining _TIME_BITS=64 and _FILE_OFFSET_BITS=64 is needed on x86 and ARM.
     46  1.1  christos   dnl
     47  1.1  christos   dnl On native Windows, the system include files define types __time32_t
     48  1.1  christos   dnl and __time64_t. By default, time_t is an alias of
     49  1.1  christos   dnl   - __time32_t on 32-bit mingw,
     50  1.1  christos   dnl   - __time64_t on 64-bit mingw and on MSVC (since MSVC 8).
     51  1.1  christos   dnl But when compiling with -D__MINGW_USE_VC2005_COMPAT, time_t is an
     52  1.1  christos   dnl alias of __time64_t.
     53  1.1  christos   dnl And when compiling with -D_USE_32BIT_TIME_T, time_t is an alias of
     54  1.1  christos   dnl __time32_t.
     55  1.1  christos   AC_CACHE_CHECK([for time_t past the year 2038], [gl_cv_type_time_t_y2038],
     56  1.1  christos     [AC_COMPILE_IFELSE(
     57  1.1  christos        [AC_LANG_SOURCE([gl_YEAR2038_TEST_INCLUDES])],
     58  1.1  christos        [gl_cv_type_time_t_y2038=yes], [gl_cv_type_time_t_y2038=no])
     59  1.1  christos     ])
     60  1.1  christos   if test "$gl_cv_type_time_t_y2038" = no; then
     61  1.1  christos     AC_CACHE_CHECK([for 64-bit time_t with _TIME_BITS=64],
     62  1.1  christos       [gl_cv_type_time_t_bits_macro],
     63  1.1  christos       [AC_COMPILE_IFELSE(
     64  1.1  christos          [AC_LANG_SOURCE([[#define _TIME_BITS 64
     65  1.1  christos                            #define _FILE_OFFSET_BITS 64
     66  1.1  christos                            ]gl_YEAR2038_TEST_INCLUDES])],
     67  1.1  christos          [gl_cv_type_time_t_bits_macro=yes],
     68  1.1  christos          [gl_cv_type_time_t_bits_macro=no])
     69  1.1  christos       ])
     70  1.1  christos     if test "$gl_cv_type_time_t_bits_macro" = yes; then
     71  1.1  christos       AC_DEFINE([_TIME_BITS], [64],
     72  1.1  christos         [Number of bits in a timestamp, on hosts where this is settable.])
     73  1.1  christos       dnl AC_SYS_LARGFILE also defines this; it's OK if we do too.
     74  1.1  christos       AC_DEFINE([_FILE_OFFSET_BITS], [64],
     75  1.1  christos         [Number of bits in a file offset, on hosts where this is settable.])
     76  1.1  christos       gl_cv_type_time_t_y2038=yes
     77  1.1  christos     fi
     78  1.1  christos   fi
     79  1.1  christos   if test $gl_cv_type_time_t_y2038 = no; then
     80  1.1  christos     AC_COMPILE_IFELSE(
     81  1.1  christos       [AC_LANG_SOURCE(
     82  1.1  christos          [[#ifdef _USE_32BIT_TIME_T
     83  1.1  christos              int ok;
     84  1.1  christos            #else
     85  1.1  christos              error fail
     86  1.1  christos            #endif
     87  1.1  christos          ]])],
     88  1.1  christos       [AC_MSG_FAILURE(
     89  1.1  christos          [The 'time_t' type stops working after January 2038.
     90  1.1  christos           Remove _USE_32BIT_TIME_T from the compiler flags.])],
     91  1.1  christos       [# If not cross-compiling and $1 says we should check,
     92  1.1  christos        # and 'touch' works with a large timestamp, then evidently wider time_t
     93  1.1  christos        # is desired and supported, so fail and ask the builder to fix the
     94  1.1  christos        # problem.  Otherwise, just warn the builder.
     95  1.1  christos        m4_ifval([$1],
     96  1.1  christos          [if test $cross_compiling = no \
     97  1.1  christos              && TZ=UTC0 touch -t 210602070628.15 conftest.time 2>/dev/null; then
     98  1.1  christos             case `TZ=UTC0 LC_ALL=C ls -l conftest.time 2>/dev/null` in
     99  1.1  christos               *'Feb  7  2106'* | *'Feb  7 17:10'*)
    100  1.1  christos                 AC_MSG_FAILURE(
    101  1.1  christos                   [The 'time_t' type stops working after January 2038,
    102  1.1  christos                    and your system appears to support a wider 'time_t'.
    103  1.1  christos                    Try configuring with 'CC="${CC} -m64"'.
    104  1.1  christos                    To build with a 32-bit time_t anyway (not recommended),
    105  1.1  christos                    configure with '--disable-year2038'.]);;
    106  1.1  christos             esac
    107  1.1  christos             rm -f conftest.time
    108  1.1  christos           fi])
    109  1.1  christos        if test "$gl_warned_about_y2038" != yes; then
    110  1.1  christos          AC_MSG_WARN(
    111  1.1  christos            [The 'time_t' type stops working after January 2038,
    112  1.1  christos             and this package needs a wider 'time_t' type
    113  1.1  christos             if there is any way to access timestamps after that.
    114  1.1  christos             Configure with 'CC="${CC} -m64"' perhaps?])
    115  1.1  christos          gl_warned_about_y2038=yes
    116  1.1  christos        fi
    117  1.1  christos       ])
    118  1.1  christos   fi])
    119  1.1  christos ])
    120  1.1  christos 
    121  1.1  christos AC_DEFUN([gl_YEAR2038],
    122  1.1  christos [
    123  1.1  christos   gl_YEAR2038_BODY([require-year2038-safe])
    124  1.1  christos ])
    125