sys_time: Support for MSVC.
authorBruno Haible <bruno@clisp.org>
Wed, 14 Sep 2011 00:08:36 +0000 (02:08 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 14 Sep 2011 00:08:36 +0000 (02:08 +0200)
* m4/sys_time_h.m4 (gl_HEADER_SYS_TIME_H_BODY): Invoke
gl_PREREQ_SYS_H_WINSOCK2. When testing for 'struct timeval', also
include <winsock2.h>.
* lib/sys_time.in.h: On MSVC, include <winsock2.h> and hide its
function declarations that collide with POSIX.
* modules/sys_time (Files): Add m4/sys_socket_h.m4.
(Makefile.am): Substitute HAVE_WINSOCK2_H.

ChangeLog
lib/sys_time.in.h
m4/sys_time_h.m4
modules/sys_time

index cf150ec..53089d5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2011-09-13  Bruno Haible  <bruno@clisp.org>
 
+       sys_time: Support for MSVC.
+       * m4/sys_time_h.m4 (gl_HEADER_SYS_TIME_H_BODY): Invoke
+       gl_PREREQ_SYS_H_WINSOCK2. When testing for 'struct timeval', also
+       include <winsock2.h>.
+       * lib/sys_time.in.h: On MSVC, include <winsock2.h> and hide its
+       function declarations that collide with POSIX.
+       * modules/sys_time (Files): Add m4/sys_socket_h.m4.
+       (Makefile.am): Substitute HAVE_WINSOCK2_H.
+
+2011-09-13  Bruno Haible  <bruno@clisp.org>
+
        stat: Support for MSVC.
        * lib/stat.c: Include pathmax.h.
        * modules/stat (Depends-on): Add pathmax.
index ce70370..1fed8c4 100644 (file)
 #  include <time.h>
 # endif
 
+/* On native Windows with MSVC:
+   Get the 'struct timeval' type.  */
+# if defined _MSC_VER && @HAVE_WINSOCK2_H@ && !defined _GL_INCLUDING_WINSOCK2_H
+#  define _GL_INCLUDING_WINSOCK2_H
+#  include <winsock2.h>
+#  undef _GL_INCLUDING_WINSOCK2_H
+# endif
+
 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
 
 /* The definition of _GL_ARG_NONNULL is copied here.  */
@@ -98,4 +106,95 @@ _GL_WARN_ON_USE (gettimeofday, "gettimeofday is unportable - "
 #  endif
 # endif
 
+/* Hide some function declarations from <winsock2.h>.  */
+
+# if defined _MSC_VER && @HAVE_WINSOCK2_H@
+#  if !defined _@GUARD_PREFIX@_UNISTD_H
+#   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#    undef close
+#    define close close_used_without_including_unistd_h
+#   else
+     _GL_WARN_ON_USE (close,
+                      "close() used without including <unistd.h>");
+#   endif
+#   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#    undef gethostname
+#    define gethostname gethostname_used_without_including_unistd_h
+#   else
+     _GL_WARN_ON_USE (gethostname,
+                      "gethostname() used without including <unistd.h>");
+#   endif
+#  endif
+#  if !defined _@GUARD_PREFIX@_SYS_SOCKET_H
+#   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#    undef socket
+#    define socket              socket_used_without_including_sys_socket_h
+#    undef connect
+#    define connect             connect_used_without_including_sys_socket_h
+#    undef accept
+#    define accept              accept_used_without_including_sys_socket_h
+#    undef bind
+#    define bind                bind_used_without_including_sys_socket_h
+#    undef getpeername
+#    define getpeername         getpeername_used_without_including_sys_socket_h
+#    undef getsockname
+#    define getsockname         getsockname_used_without_including_sys_socket_h
+#    undef getsockopt
+#    define getsockopt          getsockopt_used_without_including_sys_socket_h
+#    undef listen
+#    define listen              listen_used_without_including_sys_socket_h
+#    undef recv
+#    define recv                recv_used_without_including_sys_socket_h
+#    undef send
+#    define send                send_used_without_including_sys_socket_h
+#    undef recvfrom
+#    define recvfrom            recvfrom_used_without_including_sys_socket_h
+#    undef sendto
+#    define sendto              sendto_used_without_including_sys_socket_h
+#    undef setsockopt
+#    define setsockopt          setsockopt_used_without_including_sys_socket_h
+#    undef shutdown
+#    define shutdown            shutdown_used_without_including_sys_socket_h
+#   else
+     _GL_WARN_ON_USE (socket,
+                      "socket() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (connect,
+                      "connect() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (accept,
+                      "accept() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (bind,
+                      "bind() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (getpeername,
+                      "getpeername() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (getsockname,
+                      "getsockname() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (getsockopt,
+                      "getsockopt() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (listen,
+                      "listen() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (recv,
+                      "recv() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (send,
+                      "send() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (recvfrom,
+                      "recvfrom() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (sendto,
+                      "sendto() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (setsockopt,
+                      "setsockopt() used without including <sys/socket.h>");
+     _GL_WARN_ON_USE (shutdown,
+                      "shutdown() used without including <sys/socket.h>");
+#   endif
+#  endif
+#  if !defined _@GUARD_PREFIX@_SYS_SELECT_H
+#   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#    undef select
+#    define select select_used_without_including_sys_select_h
+#   else
+     _GL_WARN_ON_USE (select,
+                      "select() used without including <sys/select.h>");
+#   endif
+#  endif
+# endif
+
 #endif /* _@GUARD_PREFIX@_SYS_TIME_H */
index 3a3adc8..064e0de 100644 (file)
@@ -1,5 +1,5 @@
 # Configure a replacement for <sys/time.h>.
-# serial 6
+# serial 7
 
 # Copyright (C) 2007, 2009-2011 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
@@ -27,6 +27,9 @@ AC_DEFUN([gl_HEADER_SYS_TIME_H_BODY],
     HAVE_SYS_TIME_H=0
   fi
 
+  dnl On native Windows with MSVC, 'struct timeval' is defined in <winsock2.h>
+  dnl only. So include that header in the list.
+  gl_PREREQ_SYS_H_WINSOCK2
   AC_CACHE_CHECK([for struct timeval], [gl_cv_sys_struct_timeval],
     [AC_COMPILE_IFELSE(
        [AC_LANG_PROGRAM(
@@ -34,6 +37,9 @@ AC_DEFUN([gl_HEADER_SYS_TIME_H_BODY],
              #include <sys/time.h>
             #endif
             #include <time.h>
+            #if HAVE_WINSOCK2_H
+            # include <winsock2.h>
+            #endif
           ]],
           [[static struct timeval x; x.tv_sec = x.tv_usec;]])],
        [gl_cv_sys_struct_timeval=yes],
index 18700e8..cdf0d46 100644 (file)
@@ -4,6 +4,7 @@ A <sys/time.h> that conforms better to POSIX.
 Files:
 lib/sys_time.in.h
 m4/sys_time_h.m4
+m4/sys_socket_h.m4
 
 Depends-on:
 include_next
@@ -31,6 +32,7 @@ sys/time.h: sys_time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNU
              -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
              -e 's|@''NEXT_SYS_TIME_H''@|$(NEXT_SYS_TIME_H)|g' \
              -e 's/@''GNULIB_GETTIMEOFDAY''@/$(GNULIB_GETTIMEOFDAY)/g' \
+             -e 's|@''HAVE_WINSOCK2_H''@|$(HAVE_WINSOCK2_H)|g' \
              -e 's/@''HAVE_GETTIMEOFDAY''@/$(HAVE_GETTIMEOFDAY)/g' \
              -e 's/@''HAVE_STRUCT_TIMEVAL''@/$(HAVE_STRUCT_TIMEVAL)/g' \
              -e 's/@''REPLACE_GETTIMEOFDAY''@/$(REPLACE_GETTIMEOFDAY)/g' \