qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] oslib-win32: only provide localtime_r/gmtime_r if missing
@ 2015-09-08  9:26 Daniel P. Berrange
  2015-09-22 14:10 ` Denis V. Lunev
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel P. Berrange @ 2015-09-08  9:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Stefan Weil, Paolo Bonzini

The oslib-win32 file currently provides a localtime_r and
gmtime_r replacement unconditionally. Some versions of
Mingw64 would provide crude macros for localtime_r/gmtime_r
which QEMU takes care to disable. Latest versions of Mingw64
now provide actual functions for localtime_r/gmtime_r, but
with a twist that you have to include unistd.h or pthread.h
before including time.h.  By luck some files in QEMU have
such an include order, resulting in compile errors:

  CC    util/osdep.o
In file included from include/qemu-common.h:48:0,
                 from util/osdep.c:48:
include/sysemu/os-win32.h:77:12: error: redundant redeclaration of 'gmtime_r' [-Werror=redundant-decls]
 struct tm *gmtime_r(const time_t *timep, struct tm *result);
            ^
In file included from include/qemu-common.h:35:0,
                 from util/osdep.c:48:
/usr/i686-w64-mingw32/sys-root/mingw/include/time.h:272:107: note: previous definition of 'gmtime_r' was here
In file included from include/qemu-common.h:48:0,
                 from util/osdep.c:48:
include/sysemu/os-win32.h:79:12: error: redundant redeclaration of 'localtime_r' [-Werror=redundant-decls]
 struct tm *localtime_r(const time_t *timep, struct tm *result);
            ^
In file included from include/qemu-common.h:35:0,
                 from util/osdep.c:48:
/usr/i686-w64-mingw32/sys-root/mingw/include/time.h:269:107: note: previous definition of 'localtime_r' was here

This change adds a configure test to see if localtime_r
exits, and only enables the QEMU impl if missing. We also
re-arrange qemu-common.h try attempt to guarantee that all
source files get unistd.h before time.h and thus see the
localtime_r/gmtime_r defs.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---

Changes since v1:

 - Rebase to deal with Peter's re-arrangement of defs
   across osdep.h/qemu-common.h

 configure                 | 34 ++++++++++++++++++++++++++++++++++
 include/qemu/osdep.h      |  4 +++-
 include/sysemu/os-win32.h |  2 ++
 util/oslib-win32.c        |  2 ++
 4 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index d854936..c324e62 100755
--- a/configure
+++ b/configure
@@ -1733,6 +1733,37 @@ else
 fi
 
 ##########################################
+# Mingw64 localtime_r/gmtime_r check
+
+if test "$mingw32" = "yes"; then
+    # Some versions of Mingw32/64 lack localtime_r
+    # and gmtime_r entirely
+    #
+    # Some versions of Mingw64 define a macro for
+    # localtime_r/gmtime_r/etc
+    #
+    # Some versions of Ming64 will define functions
+    # for localtime_r/gmtime_r, but only if you have
+    # _POSIX_THREAD_SAFE_FUNCTIONS defined. For fun
+    # though, unistd.h and pthread.h both define
+    # that for you.
+    #
+    # So this #undef localtime_r and #include <unistd.h>
+    # are not in fact redundant
+cat > $TMPC << EOF
+#include <unistd.h>
+#include <time.h>
+#undef localtime_r
+int main(void) { localtime_r(NULL, NULL); return 0; }
+EOF
+    if compile_prog "" "" ; then
+        localtime_r="yes"
+    else
+	localtime_r="no"
+    fi
+fi
+
+##########################################
 # pkg-config probe
 
 if ! has "$pkg_config_exe"; then
@@ -5005,6 +5036,9 @@ fi
 if test "$zero_malloc" = "yes" ; then
   echo "CONFIG_ZERO_MALLOC=y" >> $config_host_mak
 fi
+if test "$localtime_r" = "yes" ; then
+  echo "CONFIG_LOCALTIME_R=y" >> $config_host_mak
+fi
 if test "$qom_cast_debug" = "yes" ; then
   echo "CONFIG_QOM_CAST_DEBUG=y" >> $config_host_mak
 fi
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index ab3c876..e490028 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -38,10 +38,12 @@
 #include <strings.h>
 #include <inttypes.h>
 #include <limits.h>
+/* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
+ * function availability on recentish Mingw64 platforms */
+#include <unistd.h>
 #include <time.h>
 #include <ctype.h>
 #include <errno.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/time.h>
diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
index 706d85a..13dcef6 100644
--- a/include/sysemu/os-win32.h
+++ b/include/sysemu/os-win32.h
@@ -73,10 +73,12 @@
 #define siglongjmp(env, val) longjmp(env, val)
 
 /* Missing POSIX functions. Don't use MinGW-w64 macros. */
+#ifndef CONFIG_LOCALTIME_R
 #undef gmtime_r
 struct tm *gmtime_r(const time_t *timep, struct tm *result);
 #undef localtime_r
 struct tm *localtime_r(const time_t *timep, struct tm *result);
+#endif /* CONFIG_LOCALTIME_R */
 
 
 static inline void os_setup_signal_handling(void) {}
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 730a670..08f5a9c 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -95,6 +95,7 @@ void qemu_anon_ram_free(void *ptr, size_t size)
     }
 }
 
+#ifndef CONFIG_LOCALTIME_R
 /* FIXME: add proper locking */
 struct tm *gmtime_r(const time_t *timep, struct tm *result)
 {
@@ -118,6 +119,7 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
     }
     return p;
 }
+#endif /* CONFIG_LOCALTIME_R */
 
 void qemu_set_block(int fd)
 {
-- 
2.4.3

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH v2] oslib-win32: only provide localtime_r/gmtime_r if missing
  2015-09-08  9:26 [Qemu-devel] [PATCH v2] oslib-win32: only provide localtime_r/gmtime_r if missing Daniel P. Berrange
@ 2015-09-22 14:10 ` Denis V. Lunev
  0 siblings, 0 replies; 2+ messages in thread
From: Denis V. Lunev @ 2015-09-22 14:10 UTC (permalink / raw)
  To: Daniel P. Berrange, qemu-devel; +Cc: Peter Maydell, Paolo Bonzini, Stefan Weil

On 09/08/2015 12:26 PM, Daniel P. Berrange wrote:
> The oslib-win32 file currently provides a localtime_r and
> gmtime_r replacement unconditionally. Some versions of
> Mingw64 would provide crude macros for localtime_r/gmtime_r
> which QEMU takes care to disable. Latest versions of Mingw64
> now provide actual functions for localtime_r/gmtime_r, but
> with a twist that you have to include unistd.h or pthread.h
> before including time.h.  By luck some files in QEMU have
> such an include order, resulting in compile errors:
>
>    CC    util/osdep.o
> In file included from include/qemu-common.h:48:0,
>                   from util/osdep.c:48:
> include/sysemu/os-win32.h:77:12: error: redundant redeclaration of 'gmtime_r' [-Werror=redundant-decls]
>   struct tm *gmtime_r(const time_t *timep, struct tm *result);
>              ^
> In file included from include/qemu-common.h:35:0,
>                   from util/osdep.c:48:
> /usr/i686-w64-mingw32/sys-root/mingw/include/time.h:272:107: note: previous definition of 'gmtime_r' was here
> In file included from include/qemu-common.h:48:0,
>                   from util/osdep.c:48:
> include/sysemu/os-win32.h:79:12: error: redundant redeclaration of 'localtime_r' [-Werror=redundant-decls]
>   struct tm *localtime_r(const time_t *timep, struct tm *result);
>              ^
> In file included from include/qemu-common.h:35:0,
>                   from util/osdep.c:48:
> /usr/i686-w64-mingw32/sys-root/mingw/include/time.h:269:107: note: previous definition of 'localtime_r' was here
>
> This change adds a configure test to see if localtime_r
> exits, and only enables the QEMU impl if missing. We also
> re-arrange qemu-common.h try attempt to guarantee that all
> source files get unistd.h before time.h and thus see the
> localtime_r/gmtime_r defs.
>
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> ---
>
> Changes since v1:
>
>   - Rebase to deal with Peter's re-arrangement of defs
>     across osdep.h/qemu-common.h
>
>   configure                 | 34 ++++++++++++++++++++++++++++++++++
>   include/qemu/osdep.h      |  4 +++-
>   include/sysemu/os-win32.h |  2 ++
>   util/oslib-win32.c        |  2 ++
>   4 files changed, 41 insertions(+), 1 deletion(-)
>
> diff --git a/configure b/configure
> index d854936..c324e62 100755
> --- a/configure
> +++ b/configure
> @@ -1733,6 +1733,37 @@ else
>   fi
>   
>   ##########################################
> +# Mingw64 localtime_r/gmtime_r check
> +
> +if test "$mingw32" = "yes"; then
> +    # Some versions of Mingw32/64 lack localtime_r
> +    # and gmtime_r entirely
> +    #
> +    # Some versions of Mingw64 define a macro for
> +    # localtime_r/gmtime_r/etc
> +    #
> +    # Some versions of Ming64 will define functions
> +    # for localtime_r/gmtime_r, but only if you have
> +    # _POSIX_THREAD_SAFE_FUNCTIONS defined. For fun
> +    # though, unistd.h and pthread.h both define
> +    # that for you.
> +    #
> +    # So this #undef localtime_r and #include <unistd.h>
> +    # are not in fact redundant
> +cat > $TMPC << EOF
> +#include <unistd.h>
> +#include <time.h>
> +#undef localtime_r
> +int main(void) { localtime_r(NULL, NULL); return 0; }
> +EOF
> +    if compile_prog "" "" ; then
> +        localtime_r="yes"
> +    else
> +	localtime_r="no"
> +    fi
> +fi
> +
> +##########################################
>   # pkg-config probe
>   
>   if ! has "$pkg_config_exe"; then
> @@ -5005,6 +5036,9 @@ fi
>   if test "$zero_malloc" = "yes" ; then
>     echo "CONFIG_ZERO_MALLOC=y" >> $config_host_mak
>   fi
> +if test "$localtime_r" = "yes" ; then
> +  echo "CONFIG_LOCALTIME_R=y" >> $config_host_mak
> +fi
>   if test "$qom_cast_debug" = "yes" ; then
>     echo "CONFIG_QOM_CAST_DEBUG=y" >> $config_host_mak
>   fi
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index ab3c876..e490028 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -38,10 +38,12 @@
>   #include <strings.h>
>   #include <inttypes.h>
>   #include <limits.h>
> +/* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
> + * function availability on recentish Mingw64 platforms */
> +#include <unistd.h>
>   #include <time.h>
>   #include <ctype.h>
>   #include <errno.h>
> -#include <unistd.h>
>   #include <fcntl.h>
>   #include <sys/stat.h>
>   #include <sys/time.h>
> diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
> index 706d85a..13dcef6 100644
> --- a/include/sysemu/os-win32.h
> +++ b/include/sysemu/os-win32.h
> @@ -73,10 +73,12 @@
>   #define siglongjmp(env, val) longjmp(env, val)
>   
>   /* Missing POSIX functions. Don't use MinGW-w64 macros. */
> +#ifndef CONFIG_LOCALTIME_R
>   #undef gmtime_r
>   struct tm *gmtime_r(const time_t *timep, struct tm *result);
>   #undef localtime_r
>   struct tm *localtime_r(const time_t *timep, struct tm *result);
> +#endif /* CONFIG_LOCALTIME_R */
>   
>   
>   static inline void os_setup_signal_handling(void) {}
> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> index 730a670..08f5a9c 100644
> --- a/util/oslib-win32.c
> +++ b/util/oslib-win32.c
> @@ -95,6 +95,7 @@ void qemu_anon_ram_free(void *ptr, size_t size)
>       }
>   }
>   
> +#ifndef CONFIG_LOCALTIME_R
>   /* FIXME: add proper locking */
>   struct tm *gmtime_r(const time_t *timep, struct tm *result)
>   {
> @@ -118,6 +119,7 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
>       }
>       return p;
>   }
> +#endif /* CONFIG_LOCALTIME_R */
>   
>   void qemu_set_block(int fd)
>   {
Reviewed-by: Denis V. Lunev <den@openvz.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-09-22 14:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-08  9:26 [Qemu-devel] [PATCH v2] oslib-win32: only provide localtime_r/gmtime_r if missing Daniel P. Berrange
2015-09-22 14:10 ` Denis V. Lunev

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 NNTP newsgroup(s).