bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: Christian Biesinger <cbiesinger@google.com>
Cc: bug-gnulib@gnu.org, Christian Biesinger <cbiesinger@gmail.com>
Subject: Re: time_r module does not work on MingW
Date: Sat, 16 Nov 2019 13:15:30 +0100	[thread overview]
Message-ID: <2192288.myS54ITcZ4@omega> (raw)
In-Reply-To: <1576792.iqhKLmYyPN@omega>

This patch fixes the issue. The cause was that the contents of <time.h>
in mingw depends on whether <unistd.h> or <pthread.h> was included before.


2019-11-16  Bruno Haible  <bruno@clisp.org>

	time_r: Fix for mingw.
	Reported by Christian Biesinger <cbiesinger@google.com> in
	<https://lists.gnu.org/archive/html/bug-gnulib/2019-11/msg00014.html>.
	* lib/time.in.h: On mingw, include <unistd.h>.
	* m4/time_r.m4 (gl_TIME_R): On mingw, include <unistd.h> before
	<time.h>. Test for localtime_r in a way that works when it is defined
	as an inline function.

diff --git a/lib/time.in.h b/lib/time.in.h
index 94e1da3..dc00503 100644
--- a/lib/time.in.h
+++ b/lib/time.in.h
@@ -37,6 +37,12 @@
 
 # define _@GUARD_PREFIX@_TIME_H
 
+/* mingw's <time.h> provides the functions asctime_r, ctime_r, gmtime_r,
+   localtime_r only if <unistd.h> or <pthread.h> has been included before.  */
+# if defined __MINGW32__
+#  include <unistd.h>
+# endif
+
 # @INCLUDE_NEXT@ @NEXT_TIME_H@
 
 /* NetBSD 5.0 mis-defines NULL.  */
diff --git a/m4/time_r.m4 b/m4/time_r.m4
index 5caeca7..72cc975 100644
--- a/m4/time_r.m4
+++ b/m4/time_r.m4
@@ -17,19 +17,54 @@ AC_DEFUN([gl_TIME_R],
 
   dnl Some systems don't declare localtime_r() and gmtime_r() if _REENTRANT is
   dnl not defined.
-  AC_CHECK_DECLS([localtime_r], [], [], [[#include <time.h>]])
+  AC_CHECK_DECLS([localtime_r], [], [],
+    [[/* mingw's <time.h> provides the functions asctime_r, ctime_r,
+         gmtime_r, localtime_r only if <unistd.h> or <pthread.h> has
+         been included before.  */
+      #if defined __MINGW32__
+      # include <unistd.h>
+      #endif
+      #include <time.h>
+    ]])
   if test $ac_cv_have_decl_localtime_r = no; then
     HAVE_DECL_LOCALTIME_R=0
   fi
 
-  AC_CHECK_FUNCS_ONCE([localtime_r])
-  if test $ac_cv_func_localtime_r = yes; then
+  dnl We can't use AC_CHECK_FUNC here, because localtime_r() is defined as an
+  dnl inline function on mingw.
+  AC_CACHE_CHECK([for localtime_r], [gl_cv_func_localtime_r],
+    [AC_LINK_IFELSE(
+       [AC_LANG_PROGRAM(
+          [[/* mingw's <time.h> provides the functions asctime_r, ctime_r,
+               gmtime_r, localtime_r only if <unistd.h> or <pthread.h> has
+               been included before.  */
+            #if defined __MINGW32__
+            # include <unistd.h>
+            #endif
+            #include <time.h>
+          ]],
+          [[time_t a;
+            struct tm r;
+            localtime_r (&a, &r);
+          ]])
+       ],
+       [gl_cv_func_localtime_r=yes],
+       [gl_cv_func_localtime_r=no])
+    ])
+  if test $gl_cv_func_localtime_r = yes; then
     HAVE_LOCALTIME_R=1
     AC_CACHE_CHECK([whether localtime_r is compatible with its POSIX signature],
       [gl_cv_time_r_posix],
       [AC_COMPILE_IFELSE(
          [AC_LANG_PROGRAM(
-            [[#include <time.h>]],
+            [[/* mingw's <time.h> provides the functions asctime_r, ctime_r,
+                 gmtime_r, localtime_r only if <unistd.h> or <pthread.h> has
+                 been included before.  */
+              #if defined __MINGW32__
+              # include <unistd.h>
+              #endif
+              #include <time.h>
+            ]],
             [[/* We don't need to append 'restrict's to the argument types,
                  even though the POSIX signature has the 'restrict's,
                  since C99 says they can't affect type compatibility.  */



  reply	other threads:[~2019-11-16 12:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-11 18:56 time_r module does not work on MingW Christian Biesinger
2019-11-11 19:53 ` Paul Eggert
2019-11-11 19:59 ` Bruno Haible
2019-11-11 20:47   ` Christian Biesinger
2019-11-12  2:28     ` Bruno Haible
2019-11-16 12:15       ` Bruno Haible [this message]
2019-11-16 22:41         ` wctype.h compile error on mingw with GNULIB_NAMESPACE Christian Biesinger
2019-11-17  3:05           ` Bruno Haible
2019-11-17  4:40             ` Christian Biesinger
2019-11-24 19:15               ` Bruno Haible
2019-11-25 23:28                 ` Christian Biesinger
2019-11-25 23:53                   ` Bruno Haible
2019-11-24 14:06         ` time_r module does not work on MingW Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2192288.myS54ITcZ4@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=cbiesinger@gmail.com \
    --cc=cbiesinger@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).