changeset 8377:a8e9ed7caeae

Make <sys/socket.h> self-contained, so that it can be included without prior <sys/types.h>.
author Bruno Haible <bruno@clisp.org>
date Wed, 07 Mar 2007 23:04:53 +0000
parents 8144a2607d0a
children 0413e343def8
files ChangeLog lib/socket_.h m4/sys_socket_h.m4 modules/sys_socket
diffstat 4 files changed, 106 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2007-03-02  Bruno Haible  <bruno@clisp.org>
+
+	* lib/socket_.h: If sys/socket.h exists, include that and <sys/types.h>
+	before it. Turn HAVE_WINSOCK2_H and HAVE_WS2TCPIP_H into configute-time
+	values.
+	* m4/sys_socket_h.m4 (gl_HEADER_SYS_SOCKET): Test also whether
+	<sys/socket.h> is self-contained. Set ABSOLUTE_SYS_SOCKET_H,
+	HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H, HAVE_WS2TCPIP_H.
+	* modules/sys_socket (Depends-on): Add absolute-header.
+	(Makefile.am): Substitute ABSOLUTE_SYS_SOCKET_H, HAVE_SYS_SOCKET_H,
+	HAVE_WINSOCK2_H, HAVE_WS2TCPIP_H into sys/socket.h.
+	(Include): Remove requirement of inclusion of <sys/types.h>.
+
 2007-03-02  Bruno Haible  <bruno@clisp.org>
 
 	* lib/byteswap_.h (bswap_32): Fix formula.
--- a/lib/socket_.h
+++ b/lib/socket_.h
@@ -1,5 +1,5 @@
 /* Provide a sys/socket header file for systems lacking it (read: MinGW).
-   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
    Written by Simon Josefsson.
 
    This program is free software; you can redistribute it and/or modify
@@ -16,12 +16,25 @@
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-#ifndef _SYS_SOCKET_H
-#define _SYS_SOCKET_H
+#ifndef _GL_SYS_SOCKET_H
+#define _GL_SYS_SOCKET_H
+
+/* This file is supposed to be used on platforms that lack <sys/socket.h>
+   and on platforms where <sys/socket.h> cannot be included standalone.
+   It is intended to provide definitions and prototypes needed by an
+   application.  */
+
+#if @HAVE_SYS_SOCKET_H@
 
-/* This file is supposed to be used on platforms that lack
-   sys/socket.h.  It is intended to provide definitions and prototypes
-   needed by an application.
+/* On many platforms, <sys/socket.h> assumes prior inclusion of
+   <sys/types.h>.  */
+
+# include <sys/types.h>
+# include @ABSOLUTE_SYS_SOCKET_H@
+
+#else
+
+/* A platform that lacks <sys/socket.h>.
 
    Currently only MinGW is supported.  See the gnulib manual regarding
    Windows sockets.  MinGW has the header files winsock2.h and
@@ -39,32 +52,34 @@
    releases. */
 
 
-#if HAVE_WINSOCK2_H
-# include <winsock2.h>
-#endif
-#if HAVE_WS2TCPIP_H
-# include <ws2tcpip.h>
-#endif
+# if @HAVE_WINSOCK2_H@
+#  include <winsock2.h>
+# endif
+# if @HAVE_WS2TCPIP_H@
+#  include <ws2tcpip.h>
+# endif
 
 /* For shutdown(). */
-#if !defined SHUT_RD && defined SD_RECEIVE
-# define SHUT_RD SD_RECEIVE
-#endif
-#if !defined SHUT_WR && defined SD_SEND
-# define SHUT_WR SD_SEND
-#endif
-#if !defined SHUT_RDWR && defined SD_BOTH
-# define SHUT_RDWR SD_BOTH
-#endif
+# if !defined SHUT_RD && defined SD_RECEIVE
+#  define SHUT_RD SD_RECEIVE
+# endif
+# if !defined SHUT_WR && defined SD_SEND
+#  define SHUT_WR SD_SEND
+# endif
+# if !defined SHUT_RDWR && defined SD_BOTH
+#  define SHUT_RDWR SD_BOTH
+# endif
 
-#if defined _WIN32 || defined __WIN32__
-# define ENOTSOCK                WSAENOTSOCK
-# define EADDRINUSE              WSAEADDRINUSE
-# define ENETRESET               WSAENETRESET
-# define ECONNABORTED            WSAECONNABORTED
-# define ECONNRESET              WSAECONNRESET
-# define ENOTCONN                WSAENOTCONN
-# define ESHUTDOWN               WSAESHUTDOWN
-#endif
+# if defined _WIN32 || defined __WIN32__
+#  define ENOTSOCK                WSAENOTSOCK
+#  define EADDRINUSE              WSAEADDRINUSE
+#  define ENETRESET               WSAENETRESET
+#  define ECONNABORTED            WSAECONNABORTED
+#  define ECONNRESET              WSAECONNRESET
+#  define ENOTCONN                WSAENOTCONN
+#  define ESHUTDOWN               WSAESHUTDOWN
+# endif
 
-#endif /* _SYS_SOCKET_H */
+#endif /* HAVE_SYS_SOCKET_H */
+
+#endif /* _GL_SYS_SOCKET_H */
--- a/m4/sys_socket_h.m4
+++ b/m4/sys_socket_h.m4
@@ -1,5 +1,5 @@
-# sys_socket_h.m4 serial 2
-dnl Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+# sys_socket_h.m4 serial 3
+dnl Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
@@ -8,16 +8,47 @@
 
 AC_DEFUN([gl_HEADER_SYS_SOCKET],
 [
-  AC_CHECK_HEADERS_ONCE([sys/socket.h])
-  if test $ac_cv_header_sys_socket_h = yes; then
+  AC_CACHE_CHECK([whether <sys/socket.h> is self-contained],
+    [gl_cv_header_sys_socket_h_selfcontained],
+    [
+      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <sys/socket.h>], [])],
+        [gl_cv_header_sys_socket_h_selfcontained=yes],
+        [gl_cv_header_sys_socket_h_selfcontained=no])
+    ])
+  if test $gl_cv_header_sys_socket_h_selfcontained = yes; then
     SYS_SOCKET_H=''
   else
-    dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make
-    dnl the check for those headers unconditional; yet cygwin reports
-    dnl that the headers are present but cannot be compiled (since on
-    dnl cygwin, all socket information should come from sys/socket.h).
-    AC_CHECK_HEADERS([winsock2.h ws2tcpip.h])
     SYS_SOCKET_H='sys/socket.h'
+
+    AC_CHECK_HEADERS([sys/socket.h])
+    gl_ABSOLUTE_HEADER([sys/socket.h])
+    ABSOLUTE_SYS_SOCKET_H=\"$gl_cv_absolute_sys_socket_h\"
+    AC_SUBST([ABSOLUTE_SYS_SOCKET_H])
+    if test $ac_cv_header_sys_socket_h = yes; then
+      HAVE_SYS_SOCKET_H=1
+      HAVE_WINSOCK2_H=0
+      HAVE_WS2TCPIP_H=0
+    else
+      HAVE_SYS_SOCKET_H=0
+      dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make
+      dnl the check for those headers unconditional; yet cygwin reports
+      dnl that the headers are present but cannot be compiled (since on
+      dnl cygwin, all socket information should come from sys/socket.h).
+      AC_CHECK_HEADERS([winsock2.h ws2tcpip.h])
+      if test $ac_cv_header_winsock2_h = yes; then
+        HAVE_WINSOCK2_H=1
+      else
+        HAVE_WINSOCK2_H=0
+      fi
+      if test $ac_cv_header_ws2tcpip_h = yes; then
+        HAVE_WS2TCPIP_H=1
+      else
+        HAVE_WS2TCPIP_H=0
+      fi
+    fi
+    AC_SUBST([HAVE_SYS_SOCKET_H])
+    AC_SUBST([HAVE_WINSOCK2_H])
+    AC_SUBST([HAVE_WS2TCPIP_H])
   fi
-  AC_SUBST(SYS_SOCKET_H)
+  AC_SUBST([SYS_SOCKET_H])
 ])
--- a/modules/sys_socket
+++ b/modules/sys_socket
@@ -7,6 +7,7 @@
 m4/sockpfaf.m4
 
 Depends-on:
+absolute-header
 
 configure.ac:
 gl_HEADER_SYS_SOCKET
@@ -20,14 +21,17 @@
 sys/socket.h: socket_.h
 	@MKDIR_P@ sys
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
-	  cat $(srcdir)/socket_.h; \
+	  sed -e 's|@''ABSOLUTE_SYS_SOCKET_H''@|$(ABSOLUTE_SYS_SOCKET_H)|g' \
+	      -e 's|@''HAVE_SYS_SOCKET_H''@|$(HAVE_SYS_SOCKET_H)|g' \
+	      -e 's|@''HAVE_WINSOCK2_H''@|$(HAVE_WINSOCK2_H)|g' \
+	      -e 's|@''HAVE_WS2TCPIP_H''@|$(HAVE_WS2TCPIP_H)|g' \
+	      < $(srcdir)/socket_.h; \
 	} > $@-t
 	mv -f $@-t $@
 MOSTLYCLEANFILES += sys/socket.h sys/socket.h-t
 MOSTLYCLEANDIRS += sys
 
 Include:
-#include <sys/types.h>
 #include <sys/socket.h>
 
 License: