From 9c0183724614517a67d0955065171465bab7154a Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Wed, 26 Apr 2023 18:21:48 -0700
Subject: [PATCH 20/29] Tone down year-2038 changes

New macro AC_SYS_YEAR2038_RECOMMENDED replaces new macro
AC_SYS_YEAR2038_REQUIRED, and gives the builder an out of
specifying --disable-year2038.  Remove new macro
AC_SYS_LARGEFILE_REQUIRED, which was added only for symmetry and
does not seem to have a great need.
* NEWS, doc/autoconf.texi: Document this.
* lib/autoconf/specific.m4:
Be more specific about mid-January 2038 than just Jan 2038.
(_AC_SYS_YEAR2038_PROBE): Ignore IF-NOT-DETECTED arg.
If support is not detected, merely set ac_have_year2038=no instead
of erroring out.  All callers changed.
(_AC_SYS_YEAR2038_OPT_IN): Remove.  All callers removed.
(AC_SYS_YEAR2038): Simplify by requiring AC_SYS_LARGEFILE
and then testing the result.
(AC_SYS_YEAR2038_REQUIRED, AC_SYS_LARGEFILE_REQUIRED): Remove.
(AC_SYS_YEAR2038_RECOMMENDED): New macro.
(_AC_SYS_LARGEFILE_PROBE): If support is not detected, merely set
ac_have_largefile=no instead of erroring out.  All callers changed.
Take on the burden of invoking year2038 probe as needed.
(AC_SYS_LARGEFILE): Simplify.

Upstream-Status: Backport
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 NEWS                     |  18 ++--
 doc/autoconf.texi        |  34 +++-----
 lib/autoconf/specific.m4 | 177 +++++++++++----------------------------
 3 files changed, 69 insertions(+), 160 deletions(-)

diff --git a/NEWS b/NEWS
index 8e4ecc1bf..a48a00858 100644
--- a/NEWS
+++ b/NEWS
@@ -51,7 +51,7 @@ GNU Autoconf NEWS - User visible changes.
 
 ** New features
 
-*** New macro AC_SYS_YEAR2038.
+*** New macros AC_SYS_YEAR2038 and AC_SYS_YEAR2038_RECOMMENDED.
   This causes 'configure' to widen time_t if possible on systems where
   time_t by default cannot represent file and other timestamps after
   January 2038.  Widening is possible only on 32-bit GNU/Linux x86 and
@@ -67,18 +67,14 @@ GNU Autoconf NEWS - User visible changes.
   This is similar to longstanding consistency requirements with
   --enable-largefile and --disable-largefile.
 
+  AC_SYS_YEAR2038_RECOMMENDED acts like AC_SYS_YEAR2038 except that
+  'configure' fails if the target lacks support for post-2038
+  timestamps and --disable-year2038 is not given.
+
 *** AC_SYS_LARGEFILE now optionally arranges to widen time_t.
   It now acts like AC_SYS_YEAR2038, except 'configure' defaults to
-  --disable-year2038 unless AC_SYS_YEAR2038 is also present.
-  As with AC_SYS_YEAR2038, application and library builders should
-  configure consistently.
-
-*** New macros AC_SYS_LARGEFILE_REQUIRED and AC_SYS_YEAR2038_REQUIRED.
-  These act like AC_SYS_LARGEFILE and AC_SYS_YEAR2038 respectively,
-  except that 'configure' fails if the target lacks support
-  for large files and for post-2038 timestamps, respectively.
-  As with AC_SYS_YEAR2038, application and library builders should
-  configure consistently.
+  --disable-year2038 unless either AC_SYS_YEAR2038 or
+  AC_SYS_YEAR2038_RECOMMENDED is also present.
 
 *** AC_USE_SYSTEM_EXTENSIONS now enables C23 Annex F extensions
   by defining __STDC_WANT_IEC_60559_EXT__.
diff --git a/doc/autoconf.texi b/doc/autoconf.texi
index b3c708e87..0f3047b31 100644
--- a/doc/autoconf.texi
+++ b/doc/autoconf.texi
@@ -8808,18 +8808,20 @@ if possible.  These types may include @code{blkcnt_t}, @code{dev_t},
 
 Also, arrange for a @command{configure} option @code{--enable-year2038}
 to request widening the type @code{time_t} as needed to represent file
-wand other timestamps after January 2038.  This widening is possible
+wand other timestamps after mid-January 2038.  This widening is possible
 only on 32-bit GNU/Linux x86 and ARM systems with glibc 2.34 or later.
 If year-2038 support is requested but @command{configure} fails to find a way
 to widen @code{time_t} and inspection of the system suggests that
 this feature is available somehow, @command{configure} will error out.
 If you want the default to be @code{--enable-year2038}, you can use
-@code{AC_SYS_YEAR2038} instead of @code{AC_SYS_LARGEFILE}.
+@code{AC_SYS_YEAR2038} or @code{AC_SYS_YEAR2038_RECOMMENDED}
+instead of @code{AC_SYS_LARGEFILE}.
 In other words, older packages that have long used @code{AC_SYS_LARGEFILE}
 can have year-2038 support on 32-bit GNU/Linux x86 and ARM systems either by
 regenerating @file{configure} with current Autoconf and configuring with
-@option{--enable-year2038}, or by using @code{AC_SYS_YEAR2038} and
-configuring without @option{--disable-year2038}.
+@option{--enable-year2038}, or by using @code{AC_SYS_YEAR2038} or
+@code{AC_SYS_YEAR2038_RECOMMENDED} and configuring without
+@option{--disable-year2038}.
 A future version of Autoconf might change the @code{AC_SYS_LARGEFILE}
 default to @code{--enable-year2038}; if and when that happens,
 @code{AC_SYS_LARGEFILE} and @code{AC_SYS_YEAR2038} will become equivalent.
@@ -8886,19 +8888,6 @@ library, enabling or disabling the application's large-file support may
 break binary compatibility with that library.
 @end defmac
 
-@defmac AC_SYS_LARGEFILE_REQUIRED
-@acindex{SYS_LARGEFILE_REQUIRED}
-This macro has the same effect as @code{AC_SYS_LARGEFILE},
-but also declares that the program being configured
-requires support for large files.
-If a large @code{off_t} is unavailable,
-@command{configure} will error out.
-The @option{--disable-largefile} option will not be available.
-
-Large-file and year-2038 support for applications and libraries should
-be configured compatibly.  @xref{AC_SYS_LARGEFILE}.
-@end defmac
-
 @anchor{AC_SYS_LONG_FILE_NAMES}
 @defmac AC_SYS_LONG_FILE_NAMES
 @acindex{SYS_LONG_FILE_NAMES}
@@ -8927,14 +8916,13 @@ applications and libraries should be configured compatibly.
 @xref{AC_SYS_LARGEFILE}.
 @end defmac
 
-@defmac AC_SYS_YEAR2038_REQUIRED
-@acindex{SYS_YEAR2038_REQUIRED}
+@defmac AC_SYS_YEAR2038_RECOMMENDED
+@acindex{SYS_YEAR2038_RECOMMENDED}
 This macro has the same effect as @code{AC_SYS_YEAR2038},
 but also declares that the program being configured
-requires support for timestamps after mid-January of 2038.
-If a large @code{time_t} is unavailable,
-@command{configure} will unconditionally error out.
-The @option{--disable-year2038} option will not be available.
+should support timestamps after mid-January 2038.
+If a large @code{time_t} is unavailable, @command{configure} will error
+out unless the @option{--disable-year2038} option is specified.
 
 Year-2038 support for applications and libraries should be configured
 compatibly.  @xref{AC_SYS_YEAR2038}.
diff --git a/lib/autoconf/specific.m4 b/lib/autoconf/specific.m4
index f411695fe..88e0479e8 100644
--- a/lib/autoconf/specific.m4
+++ b/lib/autoconf/specific.m4
@@ -115,8 +115,8 @@ m4_define([_AC_SYS_YEAR2038_OPTIONS], m4_normalize(
                                       dnl 32-bit MinGW (misconfiguration)
 ))
 
-# _AC_SYS_YEAR2038_PROBE([IF-NOT-DETECTED])
-# -----------------------------------------
+# _AC_SYS_YEAR2038_PROBE
+# ----------------------
 # Subroutine of AC_SYS_YEAR2038.  Probe for time_t that can represent
 # time points more than 2**31 - 1 seconds after the epoch (dates after
 # 2038-01-18, see above) and set the cache variable ac_cv_sys_year2038_opts
@@ -129,13 +129,10 @@ m4_define([_AC_SYS_YEAR2038_OPTIONS], m4_normalize(
 # AC_TRY_RUN.  Note also that some systems only support large time_t
 # together with large off_t.
 #
-# If support is not detected, the behavior depends on which of the
-# top-level AC_SYS_YEAR2038 macros was used (see below).
-#
 # If you change this macro you may also need to change
 # _AC_SYS_YEAR2038_OPTIONS.
 AC_DEFUN([_AC_SYS_YEAR2038_PROBE],
-[AC_CACHE_CHECK([for $CC option to enable timestamps after Jan 2038],
+[AC_CACHE_CHECK([for $CC option for timestamps after 2038],
   [ac_cv_sys_year2038_opts],
   [ac_save_CPPFLAGS="$CPPFLAGS"
   ac_opt_found=no
@@ -154,29 +151,7 @@ ac_have_year2038=yes
 AS_CASE([$ac_cv_sys_year2038_opts],
   ["none needed"], [],
   ["support not detected"],
-    [ac_have_year2038=no
-     AS_CASE([$ac_year2038_required,$enable_year2038],
-      [yes,*],
-        [AC_MSG_FAILURE([support for timestamps after Jan 2038 is required])],
-      [*,yes],
-        [# If we're not cross compiling and 'touch' works with a large
-        # timestamp, then we can presume the system supports wider time_t
-        # *somehow* and we just weren't able to detect it.  One common
-        # case that we deliberately *don't* probe for is a system that
-        # supports both 32- and 64-bit ABIs but only the 64-bit ABI offers
-        # wide time_t.  (It would be inappropriate for us to override an
-        # intentional use of -m32.)  Error out, demanding use of
-        # --disable-year2038 if this is intentional.
-        AS_IF([test $cross_compiling = no],
-          [AS_IF([TZ=UTC0 touch -t 210602070628.15 conftest.time 2>/dev/null],
-            [AS_CASE([`TZ=UTC0 LC_ALL=C ls -l conftest.time 2>/dev/null`],
-              [*'Feb  7  2106'* | *'Feb  7 17:10'*],
-              [AC_MSG_FAILURE(m4_text_wrap(
-      [this system appears to support timestamps after January 2038,
-       but no mechanism for enabling wide 'time_t' was detected.
-       Did you mean to build a 64-bit binary? (e.g. 'CC="${CC} -m64"'.)
-       To proceed with 32-bit time_t, configure with '--disable-year2038'.],
-      [], [], [55]))])])])])],
+    [ac_have_year2038=no],
 
   ["-D_TIME_BITS=64"],
     [AC_DEFINE([_TIME_BITS], [64],
@@ -189,7 +164,7 @@ AS_CASE([$ac_cv_sys_year2038_opts],
   ["-U_USE_32_BIT_TIME_T"*],
     [AC_MSG_FAILURE(m4_text_wrap(
       [the 'time_t' type is currently forced to be 32-bit.
-       It will stop working after January 2038.
+       It will stop working after mid-January 2038.
        Remove _USE_32BIT_TIME_T from the compiler flags.],
       [], [], [55]))],
 
@@ -199,44 +174,23 @@ AS_CASE([$ac_cv_sys_year2038_opts],
 
 # _AC_SYS_YEAR2038_ENABLE
 # -----------------------
-# Subroutine of AC_SYS_YEAR2038 and _AC_SYS_YEAR2038_OPT_IN.
 # Depending on which of the YEAR2038 macros was used, add either an
-# --enable-year2038, or a --disable-year2038, or no option at all to
-# the configure script.  Note that this is expanded very late and
+# --enable-year2038 or a --disable-year2038 to
+# the configure script.  This is expanded very late and
 # therefore there cannot be any code in the AC_ARG_ENABLE.  The
 # default value for 'enable_year2038' is emitted unconditionally
 # because the generated code always looks at this variable.
 m4_define([_AC_SYS_YEAR2038_ENABLE],
 [m4_divert_text([DEFAULTS],
-  m4_provide_if([AC_SYS_YEAR2038_REQUIRED],
-    [ac_year2038_required=yes],
-    [ac_year2038_required=no]))dnl
-m4_divert_text([DEFAULTS],
   m4_provide_if([AC_SYS_YEAR2038],
     [enable_year2038=yes],
     [enable_year2038=no]))]dnl
-[m4_provide_if([AC_SYS_YEAR2038_REQUIRED], [],
 [AC_ARG_ENABLE([year2038],
   m4_provide_if([AC_SYS_YEAR2038],
     [AS_HELP_STRING([--disable-year2038],
-      [omit support for dates after Jan 2038])],
+      [don't support timestamps after 2038])],
     [AS_HELP_STRING([--enable-year2038],
-      [include support for dates after Jan 2038])]))])])
-
-# _AC_SYS_YEAR2038_OPT_IN
-# -----------------------
-# If the --enable-year2038 option is given to configure, attempt to
-# detect and activate support for large time_t on 32-bit systems.
-# This macro is automatically invoked by AC_SYS_LARGEFILE when large
-# *file* support is detected.  It does not AC_REQUIRE AC_SYS_LARGEFILE
-# to avoid a dependency loop, and is therefore unsafe to expose as a
-# documented macro.
-AC_DEFUN([_AC_SYS_YEAR2038_OPT_IN],
-[m4_provide_if([_AC_SYS_YEAR2038_PROBE], [], [dnl
-  AS_IF([test "$ac_year2038_required,$enable_year2038" != no,no],
-    [_AC_SYS_YEAR2038_PROBE])
-  AC_CONFIG_COMMANDS_PRE([_AC_SYS_YEAR2038_ENABLE])
-])])
+      [support timestamps after 2038])]))])
 
 # AC_SYS_YEAR2038
 # ---------------
@@ -244,28 +198,39 @@ AC_DEFUN([_AC_SYS_YEAR2038_OPT_IN],
 # On systems where time_t is not always 64 bits, this probe can be
 # skipped by passing the --disable-year2038 option to configure.
 AC_DEFUN([AC_SYS_YEAR2038],
-[m4_provide_if([AC_SYS_LARGEFILE_REQUIRED], [],
-  [AC_REQUIRE([AC_SYS_LARGEFILE])])]dnl
-[m4_provide_if([_AC_SYS_YEAR2038_PROBE], [], [dnl
-  AS_IF([test "$ac_year2038_required,$enable_year2038" != no,no],
-    [_AC_SYS_YEAR2038_PROBE])
-  AC_CONFIG_COMMANDS_PRE([_AC_SYS_YEAR2038_ENABLE])
-])])
-
-# AC_SYS_YEAR2038_REQUIRED
-# ------------------------
-# Same as AC_SYS_YEAR2038, but declares that this program *requires*
-# support for large time_t.  If we cannot find any way to make time_t
-# capable of representing values larger than 2**31 - 1, configure will
-# error out.  Furthermore, no --enable-year2038 nor --disable-year2038
-# option will be available.
-AC_DEFUN([AC_SYS_YEAR2038_REQUIRED],
-[m4_provide_if([AC_SYS_LARGEFILE_REQUIRED], [],
-  [AC_REQUIRE([AC_SYS_LARGEFILE])])]dnl
-[m4_provide_if([_AC_SYS_YEAR2038_PROBE], [], [dnl
-  _AC_SYS_YEAR2038_PROBE
-  AC_CONFIG_COMMANDS_PRE([_AC_SYS_YEAR2038_ENABLE])
-])])
+[AC_REQUIRE([AC_SYS_LARGEFILE])dnl
+AS_IF([test "$enable_year2038,$ac_have_year2038,$cross_compiling" = yes,no,no],
+ [# If we're not cross compiling and 'touch' works with a large
+  # timestamp, then we can presume the system supports wider time_t
+  # *somehow* and we just weren't able to detect it.  One common
+  # case that we deliberately *don't* probe for is a system that
+  # supports both 32- and 64-bit ABIs but only the 64-bit ABI offers
+  # wide time_t.  (It would be inappropriate for us to override an
+  # intentional use of -m32.)  Error out, demanding use of
+  # --disable-year2038 if this is intentional.
+  AS_IF([TZ=UTC0 touch -t 210602070628.15 conftest.time 2>/dev/null],
+    [AS_CASE([`TZ=UTC0 LC_ALL=C ls -l conftest.time 2>/dev/null`],
+       [*'Feb  7  2106'* | *'Feb  7 17:10'*],
+       [AC_MSG_FAILURE(m4_text_wrap(
+	  [this system appears to support timestamps after mid-January 2038,
+	   but no mechanism for enabling wide 'time_t' was detected.
+	   Did you mean to build a 64-bit binary? (E.g., 'CC="${CC} -m64"'.)
+	   To proceed with 32-bit time_t, configure with '--disable-year2038'.],
+	  [], [], [55]))])])])])
+
+# AC_SYS_YEAR2038_RECOMMENDED
+# ---------------------------
+# Same as AC_SYS_YEAR2038, but recommend support for large time_t.
+# If we cannot find any way to make time_t capable of representing
+# values larger than 2**31 - 1, error out unless --disable-year2038 is given.
+AC_DEFUN([AC_SYS_YEAR2038_RECOMMENDED],
+[AC_REQUIRE([AC_SYS_YEAR2038])dnl
+AS_IF([test "$enable_year2038,$ac_have_year2038" = yes,no],
+   [AC_MSG_FAILURE(m4_text_wrap(
+      [could not enable timestamps after mid-January 2038.
+       Did you mean to build a 64-bit binary? (E.g., 'CC="${CC} -m64"'.)
+       To proceed with 32-bit time_t, configure with '--disable-year2038'.],
+      [], [], [55]))])])
 
 # _AC_SYS_LARGEFILE_TEST_CODE
 # ---------------------------
@@ -342,9 +307,7 @@ AS_CASE([$ac_cv_sys_largefile_opts],
   ["none needed"], [],
   ["supported through gnulib"], [],
   ["support not detected"],
-    [ac_have_largefile=no
-     AS_IF([test $ac_largefile_required,$ac_year2038_required != no,no],
-       [AC_MSG_FAILURE([support for large files is required])])],
+    [ac_have_largefile=no],
 
   ["-D_FILE_OFFSET_BITS=64"],
     [AC_DEFINE([_FILE_OFFSET_BITS], [64],
@@ -360,30 +323,9 @@ AS_CASE([$ac_cv_sys_largefile_opts],
   [AC_MSG_ERROR(
     [internal error: bad value for \$ac_cv_sys_largefile_opts])])
 
-_AC_SYS_YEAR2038_OPT_IN
-])
-
-# _AC_SYS_LARGEFILE_ENABLE
-# ------------------------
-# Subroutine of AC_SYS_LARGEFILE.  If AC_SYS_LARGEFILE_REQUIRED was
-# not used at any point in this configure script, add a
-# --disable-largefile option to the configure script.  Note that this
-# is expanded very late and therefore there cannot be any code in the
-# AC_ARG_ENABLE.  The default value for 'enable_largefile' is emitted
-# unconditionally because the generated shell code always looks at
-# this variable.
-m4_define([_AC_SYS_LARGEFILE_ENABLE],
-[m4_divert_text([DEFAULTS],
-  m4_provide_if([AC_SYS_LARGEFILE_REQUIRED],
-    [ac_largefile_required=yes],
-    [ac_largefile_required=no]))dnl
-m4_divert_text([DEFAULTS],
-  [enable_largefile=yes])]dnl
-[m4_provide_if([AC_SYS_LARGEFILE_REQUIRED], [],
-   [m4_provide_if([AC_SYS_YEAR2038_REQUIRED], [],
-      [AC_ARG_ENABLE([largefile],
-         [AS_HELP_STRING([--disable-largefile],
-	    [omit support for large files])])])])])
+AS_IF([test "$enable_year2038" != no],
+  [_AC_SYS_YEAR2038_PROBE])
+AC_CONFIG_COMMANDS_PRE([_AC_SYS_YEAR2038_ENABLE])])
 
 # AC_SYS_LARGEFILE
 # ----------------
@@ -394,29 +336,12 @@ m4_divert_text([DEFAULTS],
 # Additionally, on Linux file systems with 64-bit inodes a file that happens
 # to have a 64-bit inode number cannot be accessed by 32-bit applications on
 # Linux x86/x86_64.  This can occur with file systems such as XFS and NFS.
-# This macro allows configuration to continue if the system doesn't support
-# large files; see also AC_SYS_LARGEFILE_REQUIRED.
 AC_DEFUN([AC_SYS_LARGEFILE],
-[m4_provide_if([_AC_SYS_LARGEFILE_PROBE], [], [dnl
-  AS_IF([test "$ac_largefile_required,$enable_largefile,$ac_year2038_required" != no,no,no],
-    [_AC_SYS_LARGEFILE_PROBE])
-  AC_CONFIG_COMMANDS_PRE([_AC_SYS_LARGEFILE_ENABLE])
-])])
-
-# AC_SYS_LARGEFILE_REQUIRED
-# -------------------------
-# Same as AC_SYS_LARGEFILE, but declares that this program *requires*
-# support for large files.  If we cannot find a combination of compiler
-# options and #defines that makes 'off_t' capable of representing 2**63 - 1,
-# 'configure' will error out.  Furthermore, 'configure' will not offer a
-# --disable-largefile command line option.
-# If both AC_SYS_LARGEFILE and AC_SYS_LARGEFILE_REQUIRED are used in the
-# same configure script -- in either order -- AC_SYS_LARGEFILE_REQUIRED wins.
-AC_DEFUN([AC_SYS_LARGEFILE_REQUIRED],
-[m4_provide_if([_AC_SYS_LARGEFILE_PROBE], [], [dnl
-  _AC_SYS_LARGEFILE_PROBE
-  AC_CONFIG_COMMANDS_PRE([_AC_SYS_LARGEFILE_ENABLE])
-])])
+[AC_ARG_ENABLE([largefile],
+   [AS_HELP_STRING([--disable-largefile],
+      [omit support for large files])])dnl
+AS_IF([test "$enable_largefile,$enable_year2038" != no,no],
+  [_AC_SYS_LARGEFILE_PROBE])])
 
 # AC_SYS_LONG_FILE_NAMES
 # ----------------------
-- 
2.41.0

