gethostname: fix build on mingw
authorEric Blake <eblake@redhat.com>
Fri, 19 Mar 2010 22:57:28 +0000 (16:57 -0600)
committerEric Blake <eblake@redhat.com>
Mon, 22 Mar 2010 17:51:00 +0000 (11:51 -0600)
Since commit 4e86671a, gethostname failed to compile on mingw.
Gnulib's sys/socket.h includes winsock2.h, which then includes
unistd.h prior to declaring gethostname.  The fix is to ensure
that our replacement unistd.h does not declare any replacements
until we are sure that winsock2.h is completely included.

* lib/unistd.in.h (includes): Work around fact that mingw
<winsock2.h> re-includes <unistd.h>, by avoiding any
redeclarations if we are being included by <winsock2.h>.
Reported by Matthias Bolte.

Signed-off-by: Eric Blake <eblake@redhat.com>
ChangeLog
lib/unistd.in.h

index 78de306..e948579 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2010-03-22  Eric Blake  <eblake@redhat.com>
+
+       gethostname: fix build on mingw
+       * lib/unistd.in.h (includes): Work around fact that mingw
+       <winsock2.h> re-includes <unistd.h>, by avoiding any
+       redeclarations if we are being included by <winsock2.h>.
+       Reported by Matthias Bolte.
+
 2010-03-21  Bruno Haible  <bruno@clisp.org>
 
        forkpty: Provide replacement on AIX, HP-UX, IRIX, Solaris.
index c1732b8..beef6b0 100644 (file)
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-#ifndef _GL_UNISTD_H
+/* Special invocation convention:
+   - On mingw, several headers, including <winsock2.h>, include <unistd.h>,
+     but we need to ensure that both the system <unistd.h> and <winsock2.h>
+     are completely included before we replace gethostname.  */
+#if @UNISTD_H_HAVE_WINSOCK2_H@ && !defined _GL_WINSOCK2_H_WITNESS \
+  && defined _WINSOCK2_H
+/* <unistd.h> is being indirectly included for the first time from
+   <winsock2.h>; avoid declaring any overrides.  */
+# if @HAVE_UNISTD_H@
+#  @INCLUDE_NEXT@ @NEXT_UNISTD_H@
+# else
+#  error unexpected; report this to bug-gnulib@gnu.org
+# endif
+# define _GL_WINSOCK2_H_WITNESS
+
+/* Normal invocation.  */
+#elif !defined _GL_UNISTD_H
 
 #if __GNUC__ >= 3
 @PRAGMA_SYSTEM_HEADER@
 # @INCLUDE_NEXT@ @NEXT_UNISTD_H@
 #endif
 
-#ifndef _GL_UNISTD_H
+/* Get all possible declarations of gethostname().  */
+#if @UNISTD_H_HAVE_WINSOCK2_H@ && !defined _GL_INCLUDING_WINSOCK2_H
+# define _GL_INCLUDING_WINSOCK2_H
+# include <winsock2.h>
+# undef _GL_INCLUDING_WINSOCK2_H
+#endif
+
+#if !defined _GL_UNISTD_H && !defined _GL_INCLUDING_WINSOCK2_H
 #define _GL_UNISTD_H
 
 /* NetBSD 5.0 mis-defines NULL.  Also get size_t.  */
@@ -76,7 +99,6 @@
 #if @GNULIB_GETHOSTNAME@
 /* Get all possible declarations of gethostname().  */
 # if @UNISTD_H_HAVE_WINSOCK2_H@
-#  include <winsock2.h>
 #  if !defined _GL_SYS_SOCKET_H
 #   undef socket
 #   define socket               socket_used_without_including_sys_socket_h