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. Therefore, unistd.h cannot replace gethostname until winsock2.h is complete, also fixing things if a .c includes <unistd.h> (or even <winsock2.h>) prior to <sys/socket.h>.
* lib/sys_socket.in.h (includes): Set witness when including winsock2.h. * lib/unistd.in.h (includes): Likewise, after hoisting <winsock2.h> inclusion. Don't replace anything when included by winsock2.h. Reported by Matthias Bolte. Signed-off-by: Eric Blake <ebl...@redhat.com> --- Next round of attempts. ChangeLog | 9 +++++++++ lib/sys_socket.in.h | 7 ++++++- lib/unistd.in.h | 24 +++++++++++++++++++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index cc3ef5a..eb4cf52 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2010-03-20 Eric Blake <ebl...@redhat.com> + + gethostname: fix build on mingw + * lib/sys_socket.in.h (includes): Set witness when including + winsock2.h. + * lib/unistd.in.h (includes): Likewise, after hoisting <winsock2.h> + inclusion. Don't replace anything when included by winsock2.h. + Reported by Matthias Bolte. + 2010-03-20 Bruno Haible <br...@clisp.org> * m4/unlink.m4 (gl_FUNC_UNLINK): Fix last commit. diff --git a/lib/sys_socket.in.h b/lib/sys_socket.in.h index 006a901..b34b506 100644 --- a/lib/sys_socket.in.h +++ b/lib/sys_socket.in.h @@ -28,7 +28,10 @@ - On Cygwin 1.5.x we have a sequence of nested includes <sys/socket.h> -> <cygwin/socket.h> -> <asm/socket.h> -> <cygwin/if.h>, and the latter includes <sys/socket.h>. In this situation, the functions - are not yet declared, therefore we cannot provide the C++ aliases. */ + are not yet declared, therefore we cannot provide the C++ aliases. + - On mingw, <sys/socket.h> -> <winsock2.h> -> <unistd.h>, and the latter + includes <winsock2.h>. In this situation, functions like gethostname + are not yet declared, therefore we must defer. */ #...@include_next@ @NEXT_SYS_SOCKET_H@ @@ -129,7 +132,9 @@ struct sockaddr_storage # if @HAVE_WINSOCK2_H@ +# define _GL_INCLUDING_WINSOCK2_H # include <winsock2.h> +# undef _GL_INCLUDING_WINSOCK2_H # endif # if @HAVE_WS2TCPIP_H@ # include <ws2tcpip.h> diff --git a/lib/unistd.in.h b/lib/unistd.in.h index b74484a..bc2416a 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -15,18 +15,37 @@ 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. */ +#ifdef _GL_INCLUDING_UNISTD_H && @HAVE_UNISTD_H@ +# @INCLUDE_NEXT@ @NEXT_UNISTD_H@ + +/* Normal invocation. */ +#elif !defined _GL_UNISTD_H #if __GNUC__ >= 3 @PRAGMA_SYSTEM_HEADER@ #endif +#define _GL_INCLUDING_UNISTD_H + /* The include_next requires a split double-inclusion guard. */ #if @HAVE_UNISTD_H@ # @INCLUDE_NEXT@ @NEXT_UNISTD_H@ #endif -#ifndef _GL_UNISTD_H +/* Get all possible declarations of gethostname(). */ +#if @UNISTD_H_HAVE_WINSOCK2_H@ +# define _GL_INCLUDING_WINSOCK2_H +# include <winsock2.h> +# undef _GL_INCLUDING_WINSOCK2_H +#endif + +#undef _GL_INCLUDING_UNISTD_H + +#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 +95,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 -- 1.6.6.1