Tests of module 'sys_socket' in C++ mode.
[gnulib.git] / m4 / sys_socket_h.m4
1 # sys_socket_h.m4 serial 15
2 dnl Copyright (C) 2005-2010 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl From Simon Josefsson.
8
9 AC_DEFUN([gl_HEADER_SYS_SOCKET],
10 [
11   AC_REQUIRE([gl_SYS_SOCKET_H_DEFAULTS])
12   AC_REQUIRE([AC_C_INLINE])
13
14   AC_CACHE_CHECK([whether <sys/socket.h> is self-contained],
15     [gl_cv_header_sys_socket_h_selfcontained],
16     [
17       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/socket.h>]], [[]])],
18         [gl_cv_header_sys_socket_h_selfcontained=yes],
19         [gl_cv_header_sys_socket_h_selfcontained=no])
20     ])
21   if test $gl_cv_header_sys_socket_h_selfcontained = yes; then
22     SYS_SOCKET_H=''
23     dnl If the shutdown function exists, <sys/socket.h> should define
24     dnl SHUT_RD, SHUT_WR, SHUT_RDWR.
25     AC_CHECK_FUNCS([shutdown])
26     if test $ac_cv_func_shutdown = yes; then
27       AC_CACHE_CHECK([whether <sys/socket.h> defines the SHUT_* macros],
28         [gl_cv_header_sys_socket_h_shut],
29         [
30           AC_COMPILE_IFELSE(
31             [AC_LANG_PROGRAM([[#include <sys/socket.h>]],
32                [[int a[] = { SHUT_RD, SHUT_WR, SHUT_RDWR };]])],
33             [gl_cv_header_sys_socket_h_shut=yes],
34             [gl_cv_header_sys_socket_h_shut=no])
35         ])
36       if test $gl_cv_header_sys_socket_h_shut = no; then
37         SYS_SOCKET_H='sys/socket.h'
38       fi
39     fi
40   else
41     SYS_SOCKET_H='sys/socket.h'
42   fi
43   # We need to check for ws2tcpip.h now.
44   gl_PREREQ_SYS_H_SOCKET
45   AC_CHECK_TYPES([struct sockaddr_storage, sa_family_t],,,[
46   /* sys/types.h is not needed according to POSIX, but the
47      sys/socket.h in i386-unknown-freebsd4.10 and
48      powerpc-apple-darwin5.5 required it. */
49 #include <sys/types.h>
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h>
52 #endif
53 #ifdef HAVE_WS2TCPIP_H
54 #include <ws2tcpip.h>
55 #endif
56 ])
57   if test $ac_cv_type_struct_sockaddr_storage = no; then
58     HAVE_STRUCT_SOCKADDR_STORAGE=0
59     SYS_SOCKET_H='sys/socket.h'
60   fi
61   if test $ac_cv_type_sa_family_t = no; then
62     HAVE_SA_FAMILY_T=0
63     SYS_SOCKET_H='sys/socket.h'
64   fi
65   if test -n "$SYS_SOCKET_H"; then
66     gl_PREREQ_SYS_H_WINSOCK2
67   fi
68   AC_SUBST([SYS_SOCKET_H])
69
70   dnl Check for declarations of anything we want to poison if the
71   dnl corresponding gnulib module is not in use.
72   gl_WARN_ON_USE_PREPARE([[
73 /* Some systems require prerequisite headers.  */
74 #include <sys/types.h>
75 #if !defined __GLIBC__ && HAVE_SYS_TIME_H
76 # include <sys/time.h>
77 #endif
78 #include <sys/select.h>
79     ]], [socket connect accept bind getpeername getsockname getsockopt
80     listen recv send recvfrom sendto setsockopt shutdown accept4])
81 ])
82
83 AC_DEFUN([gl_PREREQ_SYS_H_SOCKET],
84 [
85   dnl Check prerequisites of the <sys/socket.h> replacement.
86   gl_CHECK_NEXT_HEADERS([sys/socket.h])
87   if test $ac_cv_header_sys_socket_h = yes; then
88     HAVE_SYS_SOCKET_H=1
89     HAVE_WS2TCPIP_H=0
90   else
91     HAVE_SYS_SOCKET_H=0
92     dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make
93     dnl the check for those headers unconditional; yet cygwin reports
94     dnl that the headers are present but cannot be compiled (since on
95     dnl cygwin, all socket information should come from sys/socket.h).
96     AC_CHECK_HEADERS([ws2tcpip.h])
97     if test $ac_cv_header_ws2tcpip_h = yes; then
98       HAVE_WS2TCPIP_H=1
99     else
100       HAVE_WS2TCPIP_H=0
101     fi
102   fi
103   AC_SUBST([HAVE_SYS_SOCKET_H])
104   AC_SUBST([HAVE_WS2TCPIP_H])
105 ])
106
107 # Common prerequisites of the <sys/socket.h> replacement and of the
108 # <sys/select.h> replacement.
109 # Sets and substitutes HAVE_WINSOCK2_H.
110 AC_DEFUN([gl_PREREQ_SYS_H_WINSOCK2],
111 [
112   m4_ifdef([gl_UNISTD_H_DEFAULTS], [AC_REQUIRE([gl_UNISTD_H_DEFAULTS])])
113   m4_ifdef([gl_SYS_IOCTL_H_DEFAULTS], [AC_REQUIRE([gl_SYS_IOCTL_H_DEFAULTS])])
114   AC_CHECK_HEADERS_ONCE([sys/socket.h])
115   if test $ac_cv_header_sys_socket_h != yes; then
116     dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make
117     dnl the check for those headers unconditional; yet cygwin reports
118     dnl that the headers are present but cannot be compiled (since on
119     dnl cygwin, all socket information should come from sys/socket.h).
120     AC_CHECK_HEADERS([winsock2.h])
121   fi
122   if test "$ac_cv_header_winsock2_h" = yes; then
123     HAVE_WINSOCK2_H=1
124     UNISTD_H_HAVE_WINSOCK2_H=1
125     SYS_IOCTL_H_HAVE_WINSOCK2_H=1
126   else
127     HAVE_WINSOCK2_H=0
128   fi
129   AC_SUBST([HAVE_WINSOCK2_H])
130 ])
131
132 AC_DEFUN([gl_SYS_SOCKET_MODULE_INDICATOR],
133 [
134   dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
135   AC_REQUIRE([gl_SYS_SOCKET_H_DEFAULTS])
136   GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1
137   dnl Define it also as a C macro, for the benefit of the unit tests.
138   gl_MODULE_INDICATOR([$1])
139 ])
140
141 AC_DEFUN([gl_SYS_SOCKET_H_DEFAULTS],
142 [
143   GNULIB_SOCKET=0;      AC_SUBST([GNULIB_SOCKET])
144   GNULIB_CONNECT=0;     AC_SUBST([GNULIB_CONNECT])
145   GNULIB_ACCEPT=0;      AC_SUBST([GNULIB_ACCEPT])
146   GNULIB_BIND=0;        AC_SUBST([GNULIB_BIND])
147   GNULIB_GETPEERNAME=0; AC_SUBST([GNULIB_GETPEERNAME])
148   GNULIB_GETSOCKNAME=0; AC_SUBST([GNULIB_GETSOCKNAME])
149   GNULIB_GETSOCKOPT=0;  AC_SUBST([GNULIB_GETSOCKOPT])
150   GNULIB_LISTEN=0;      AC_SUBST([GNULIB_LISTEN])
151   GNULIB_RECV=0;        AC_SUBST([GNULIB_RECV])
152   GNULIB_SEND=0;        AC_SUBST([GNULIB_SEND])
153   GNULIB_RECVFROM=0;    AC_SUBST([GNULIB_RECVFROM])
154   GNULIB_SENDTO=0;      AC_SUBST([GNULIB_SENDTO])
155   GNULIB_SETSOCKOPT=0;  AC_SUBST([GNULIB_SETSOCKOPT])
156   GNULIB_SHUTDOWN=0;    AC_SUBST([GNULIB_SHUTDOWN])
157   GNULIB_ACCEPT4=0;     AC_SUBST([GNULIB_ACCEPT4])
158   HAVE_STRUCT_SOCKADDR_STORAGE=1; AC_SUBST([HAVE_STRUCT_SOCKADDR_STORAGE])
159   HAVE_SA_FAMILY_T=1;   AC_SUBST([HAVE_SA_FAMILY_T])
160   HAVE_ACCEPT4=1;       AC_SUBST([HAVE_ACCEPT4])
161 ])