Fix guard detection of _WIN32_WINNT for MingW in CURL_FUNC_GETNAMEINFO_ARGTYPES
[platform/upstream/c-ares.git] / acinclude.m4
1
2
3 dnl CURL_CHECK_HEADER_WINDOWS
4 dnl -------------------------------------------------
5 dnl Check for compilable and valid windows.h header 
6
7 AC_DEFUN([CURL_CHECK_HEADER_WINDOWS], [
8   AC_CACHE_CHECK([for windows.h], [ac_cv_header_windows_h], [
9     AC_COMPILE_IFELSE([
10       AC_LANG_PROGRAM([
11 #undef inline
12 #define WIN32_LEAN_AND_MEAN
13 #include <windows.h>
14       ],[
15         int dummy=2*WINVER;
16       ])
17     ],[
18       ac_cv_header_windows_h="yes"
19     ],[
20       ac_cv_header_windows_h="no"
21     ])
22   ])
23   if test "x$ac_cv_header_windows_h" = "xyes"; then
24     AC_DEFINE_UNQUOTED(HAVE_WINDOWS_H, 1,
25       [Define to 1 if you have the windows.h header file.])
26     AC_DEFINE_UNQUOTED(WIN32_LEAN_AND_MEAN, 1,
27       [Define to avoid automatic inclusion of winsock.h])
28   fi
29 ])
30
31
32 dnl CURL_CHECK_HEADER_WINSOCK
33 dnl -------------------------------------------------
34 dnl Check for compilable and valid winsock.h header 
35
36 AC_DEFUN([CURL_CHECK_HEADER_WINSOCK], [
37   AC_REQUIRE([CURL_CHECK_HEADER_WINDOWS])dnl
38   AC_CACHE_CHECK([for winsock.h], [ac_cv_header_winsock_h], [
39     AC_COMPILE_IFELSE([
40       AC_LANG_PROGRAM([
41 #undef inline
42 #define WIN32_LEAN_AND_MEAN
43 #include <windows.h>
44 #include <winsock.h>
45       ],[
46         int dummy=WSACleanup();
47       ])
48     ],[
49       ac_cv_header_winsock_h="yes"
50     ],[
51       ac_cv_header_winsock_h="no"
52     ])
53   ])
54   if test "x$ac_cv_header_winsock_h" = "xyes"; then
55     AC_DEFINE_UNQUOTED(HAVE_WINSOCK_H, 1,
56       [Define to 1 if you have the winsock.h header file.])
57   fi
58 ])
59
60
61 dnl CURL_CHECK_HEADER_WINSOCK2
62 dnl -------------------------------------------------
63 dnl Check for compilable and valid winsock2.h header 
64
65 AC_DEFUN([CURL_CHECK_HEADER_WINSOCK2], [
66   AC_REQUIRE([CURL_CHECK_HEADER_WINDOWS])dnl
67   AC_CACHE_CHECK([for winsock2.h], [ac_cv_header_winsock2_h], [
68     AC_COMPILE_IFELSE([
69       AC_LANG_PROGRAM([
70 #undef inline
71 #define WIN32_LEAN_AND_MEAN
72 #include <windows.h>
73 #include <winsock2.h>
74       ],[
75         int dummy=2*IPPROTO_ESP;
76       ])
77     ],[
78       ac_cv_header_winsock2_h="yes"
79     ],[
80       ac_cv_header_winsock2_h="no"
81     ])
82   ])
83   if test "x$ac_cv_header_winsock2_h" = "xyes"; then
84     AC_DEFINE_UNQUOTED(HAVE_WINSOCK2_H, 1,
85       [Define to 1 if you have the winsock2.h header file.])
86   fi
87 ])
88
89
90 dnl CURL_CHECK_HEADER_WS2TCPIP
91 dnl -------------------------------------------------
92 dnl Check for compilable and valid ws2tcpip.h header
93
94 AC_DEFUN([CURL_CHECK_HEADER_WS2TCPIP], [
95   AC_REQUIRE([CURL_CHECK_HEADER_WINSOCK2])dnl
96   AC_CACHE_CHECK([for ws2tcpip.h], [ac_cv_header_ws2tcpip_h], [
97     AC_COMPILE_IFELSE([
98       AC_LANG_PROGRAM([
99 #undef inline
100 #define WIN32_LEAN_AND_MEAN
101 #include <windows.h>
102 #include <winsock2.h>
103 #include <ws2tcpip.h>
104       ],[
105         int dummy=2*IP_PKTINFO;
106       ])
107     ],[
108       ac_cv_header_ws2tcpip_h="yes"
109     ],[
110       ac_cv_header_ws2tcpip_h="no"
111     ])
112   ])
113   if test "x$ac_cv_header_ws2tcpip_h" = "xyes"; then
114     AC_DEFINE_UNQUOTED(HAVE_WS2TCPIP_H, 1,
115       [Define to 1 if you have the ws2tcpip.h header file.])
116   fi
117 ])
118
119
120 dnl CURL_CHECK_TYPE_SOCKLEN_T
121 dnl -------------------------------------------------
122 dnl Check for existing socklen_t type, and provide
123 dnl an equivalent type if socklen_t not available
124
125 AC_DEFUN([CURL_CHECK_TYPE_SOCKLEN_T], [
126   AC_REQUIRE([CURL_CHECK_HEADER_WS2TCPIP])dnl
127   AC_CHECK_TYPE([socklen_t], ,[
128     AC_CACHE_CHECK([for socklen_t equivalent], 
129       [curl_cv_socklen_t_equiv], [
130       curl_cv_socklen_t_equiv="unknown"
131       for arg2 in "struct sockaddr" void; do
132         for t in int size_t unsigned long "unsigned long"; do
133           AC_COMPILE_IFELSE([
134             AC_LANG_PROGRAM([
135 #undef inline
136 #ifdef HAVE_WINDOWS_H
137 #define WIN32_LEAN_AND_MEAN
138 #include <windows.h>
139 #ifdef HAVE_WINSOCK2_H
140 #include <winsock2.h>
141 #else
142 #ifdef HAVE_WINSOCK_H
143 #include <winsock.h>
144 #endif
145 #endif
146 #else
147 #ifdef HAVE_SYS_TYPES_H
148 #include <sys/types.h>
149 #endif
150 #ifdef HAVE_SYS_SOCKET_H
151 #include <sys/socket.h>
152 #endif
153 #endif
154               int getpeername (int, $arg2 *, $t *);
155             ],[
156               $t len=0;
157               getpeername(0,0,&len);
158             ])
159           ],[
160              curl_cv_socklen_t_equiv="$t"
161              break 2
162           ])
163         done
164       done
165     ])
166     if test "$curl_cv_socklen_t_equiv" = "unknown"; then
167       AC_MSG_ERROR([Cannot find a type to use in place of socklen_t])
168     else
169       AC_DEFINE_UNQUOTED(socklen_t, $curl_cv_socklen_t_equiv,
170         [type to use in place of socklen_t if not defined])
171     fi
172   ],[
173 #ifdef HAVE_WINDOWS_H
174 #ifdef HAVE_WS2TCPIP_H
175 #include <ws2tcpip.h>
176 #endif
177 #else
178 #ifdef HAVE_SYS_TYPES_H
179 #include <sys/types.h>
180 #endif
181 #ifdef HAVE_SYS_SOCKET_H
182 #include <sys/socket.h>
183 #endif
184 #endif
185   ])
186 ])
187
188
189 dnl CURL_FUNC_GETNAMEINFO_ARGTYPES
190 dnl -------------------------------------------------
191 dnl Check the type to be passed to five of the arguments
192 dnl of getnameinfo function, and define those types in  
193 dnl GETNAMEINFO_TYPE_ARG1, GETNAMEINFO_TYPE_ARG2,
194 dnl GETNAMEINFO_TYPE_ARG46 and GETNAMEINFO_TYPE_ARG7.
195 dnl This function is experimental and its results shall
196 dnl not be trusted while this notice is in place ------
197
198 AC_DEFUN([CURL_FUNC_GETNAMEINFO_ARGTYPES], [
199   AC_REQUIRE([CURL_CHECK_HEADER_WS2TCPIP])dnl
200   AC_REQUIRE([CURL_CHECK_TYPE_SOCKLEN_T])dnl
201   AC_CHECK_HEADERS(sys/types.h sys/socket.h netdb.h)
202   AC_CACHE_CHECK([types of arguments for getnameinfo],
203     [curl_cv_func_getnameinfo_args], [
204     curl_cv_func_getnameinfo_args="unknown"
205     for gni_arg1 in 'struct sockaddr *' 'const struct sockaddr *' 'void *'; do
206       for gni_arg2 in 'socklen_t' 'size_t' 'int'; do
207         for gni_arg46 in 'size_t' 'int' 'socklen_t' 'unsigned int'; do
208           for gni_arg7 in 'int' 'unsigned int'; do
209             AC_COMPILE_IFELSE([
210               AC_LANG_PROGRAM([
211 #undef inline
212 #ifdef HAVE_WINDOWS_H
213 #define WIN32_LEAN_AND_MEAN
214 #if defined(__MINGW32__) && ( (!defined(_WIN32_WINNT)) || (_WIN32_WINNT < 0x0501) )
215 #undef _WIN32_WINNT
216 #define _WIN32_WINNT 0x0501
217 #endif
218 #include <windows.h>
219 #ifdef HAVE_WINSOCK2_H
220 #include <winsock2.h>
221 #endif
222 #ifdef HAVE_WS2TCPIP_H
223 #include <ws2tcpip.h>
224 #endif
225 #else
226 #ifdef HAVE_SYS_TYPES_H
227 #include <sys/types.h>
228 #endif
229 #ifdef HAVE_SYS_SOCKET_H
230 #include <sys/socket.h>
231 #endif
232 #ifdef HAVE_NETDB_H
233 #include <netdb.h>
234 #endif
235 #endif
236                 extern int getnameinfo($gni_arg1, $gni_arg2,
237                                        char *, $gni_arg46,
238                                        char *, $gni_arg46,
239                                        $gni_arg7);
240               ],[
241                 $gni_arg2 salen=0;
242                 $gni_arg46 hostlen=0;
243                 $gni_arg46 servlen=0;
244                 $gni_arg7 flags=0;
245                 int res = getnameinfo(0, salen, 0, hostlen, 0, servlen, flags);
246               ])
247             ],[
248                curl_cv_func_getnameinfo_args="$gni_arg1,$gni_arg2,$gni_arg46,$gni_arg7"
249                break 4
250             ])
251           done
252         done
253       done
254     done
255   ])
256   if test "$curl_cv_func_getnameinfo_args" = "unknown"; then
257     AC_MSG_WARN([Cannot find proper types to use for getnameinfo args])
258   else
259     gni_prev_IFS=$IFS; IFS=','
260     set dummy `echo "$curl_cv_func_getnameinfo_args" | sed 's/\*/\*/g'`
261     IFS=$gni_prev_IFS
262     shift
263     AC_DEFINE_UNQUOTED(GETNAMEINFO_TYPE_ARG1, $[1],
264       [Define to the type of arg 1 for `getnameinfo'.])
265     AC_DEFINE_UNQUOTED(GETNAMEINFO_TYPE_ARG2, $[2],
266       [Define to the type of arg 2 for `getnameinfo'.])
267     AC_DEFINE_UNQUOTED(GETNAMEINFO_TYPE_ARG46, $[3],
268       [Define to the type of args 4 and 6 for `getnameinfo'.])
269     AC_DEFINE_UNQUOTED(GETNAMEINFO_TYPE_ARG7, $[4],
270       [Define to the type of arg 7 for `getnameinfo'.])
271   fi
272 ])
273
274
275 dnl Check for how to set a socket to non-blocking state. There seems to exist
276 dnl four known different ways, with the one used almost everywhere being POSIX
277 dnl and XPG3, while the other different ways for different systems (old BSD,
278 dnl Windows and Amiga).
279 dnl
280 dnl There are two known platforms (AIX 3.x and SunOS 4.1.x) where the
281 dnl O_NONBLOCK define is found but does not work. This condition is attempted
282 dnl to get caught in this script by using an excessive number of #ifdefs...
283 dnl
284 AC_DEFUN([CURL_CHECK_NONBLOCKING_SOCKET],
285 [
286   AC_MSG_CHECKING([non-blocking sockets style])
287
288   AC_TRY_COMPILE([
289 /* headers for O_NONBLOCK test */
290 #include <sys/types.h>
291 #include <unistd.h>
292 #include <fcntl.h>
293 ],[
294 /* try to compile O_NONBLOCK */
295
296 #if defined(sun) || defined(__sun__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
297 # if defined(__SVR4) || defined(__srv4__)
298 #  define PLATFORM_SOLARIS
299 # else
300 #  define PLATFORM_SUNOS4
301 # endif
302 #endif
303 #if (defined(_AIX) || defined(__xlC__)) && !defined(_AIX4)
304 # define PLATFORM_AIX_V3
305 #endif
306
307 #if defined(PLATFORM_SUNOS4) || defined(PLATFORM_AIX_V3) || defined(__BEOS__)
308 #error "O_NONBLOCK does not work on this platform"
309 #endif
310   int socket;
311   int flags = fcntl(socket, F_SETFL, flags | O_NONBLOCK);
312 ],[
313 dnl the O_NONBLOCK test was fine
314 nonblock="O_NONBLOCK"
315 AC_DEFINE(HAVE_O_NONBLOCK, 1, [use O_NONBLOCK for non-blocking sockets])
316 ],[
317 dnl the code was bad, try a different program now, test 2
318
319   AC_TRY_COMPILE([
320 /* headers for FIONBIO test */
321 #include <unistd.h>
322 #include <stropts.h>
323 ],[
324 /* FIONBIO source test (old-style unix) */
325  int socket;
326  int flags = ioctl(socket, FIONBIO, &flags);
327 ],[
328 dnl FIONBIO test was good
329 nonblock="FIONBIO"
330 AC_DEFINE(HAVE_FIONBIO, 1, [use FIONBIO for non-blocking sockets])
331 ],[
332 dnl FIONBIO test was also bad
333 dnl the code was bad, try a different program now, test 3
334
335   AC_TRY_COMPILE([
336 /* headers for ioctlsocket test (cygwin?) */
337 #include <windows.h>
338 ],[
339 /* ioctlsocket source code */
340  int socket;
341  unsigned long flags = ioctlsocket(socket, FIONBIO, &flags);
342 ],[
343 dnl ioctlsocket test was good
344 nonblock="ioctlsocket"
345 AC_DEFINE(HAVE_IOCTLSOCKET, 1, [use ioctlsocket() for non-blocking sockets])
346 ],[
347 dnl ioctlsocket didnt compile!, go to test 4
348
349   AC_TRY_LINK([
350 /* headers for IoctlSocket test (Amiga?) */
351 #include <sys/ioctl.h>
352 ],[
353 /* IoctlSocket source code */
354  int socket;
355  int flags = IoctlSocket(socket, FIONBIO, (long)1);
356 ],[
357 dnl ioctlsocket test was good
358 nonblock="IoctlSocket"
359 AC_DEFINE(HAVE_IOCTLSOCKET_CASE, 1, [use Ioctlsocket() for non-blocking sockets])
360 ],[
361 dnl Ioctlsocket didnt compile, do test 5!
362   AC_TRY_COMPILE([
363 /* headers for SO_NONBLOCK test (BeOS) */
364 #include <socket.h>
365 ],[
366 /* SO_NONBLOCK source code */
367  long b = 1;
368  int socket;
369  int flags = setsockopt(socket, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
370 ],[
371 dnl the SO_NONBLOCK test was good
372 nonblock="SO_NONBLOCK"
373 AC_DEFINE(HAVE_SO_NONBLOCK, 1, [use SO_NONBLOCK for non-blocking sockets])
374 ],[
375 dnl test 5 didnt compile!
376 nonblock="nada"
377 AC_DEFINE(HAVE_DISABLED_NONBLOCKING, 1, [disabled non-blocking sockets])
378 ])
379 dnl end of fifth test
380
381 ])
382 dnl end of forth test
383
384 ])
385 dnl end of third test
386
387 ])
388 dnl end of second test
389
390 ])
391 dnl end of non-blocking try-compile test
392   AC_MSG_RESULT($nonblock)
393
394   if test "$nonblock" = "nada"; then
395     AC_MSG_WARN([non-block sockets disabled])
396   fi
397 ])
398
399 dnl We create a function for detecting which compiler we use and then set as
400 dnl pendantic compiler options as possible for that particular compiler. The
401 dnl options are only used for debug-builds.
402
403 dnl This is a copy of the original found in curl's configure script. Don't
404 dnl modify this one, edit the one in curl and copy it back here when that one
405 dnl is changed.
406
407 AC_DEFUN([CURL_CC_DEBUG_OPTS],
408 [
409     if test "$GCC" = "yes"; then
410
411        dnl figure out gcc version!
412        AC_MSG_CHECKING([gcc version])
413        gccver=`$CC -dumpversion`
414        num1=`echo $gccver | cut -d . -f1`
415        num2=`echo $gccver | cut -d . -f2`
416        gccnum=`(expr $num1 "*" 100 + $num2) 2>/dev/null`
417        AC_MSG_RESULT($gccver)
418
419        AC_MSG_CHECKING([if this is icc in disguise])
420        AC_EGREP_CPP([^__INTEL_COMPILER], [__INTEL_COMPILER],
421          dnl action if the text is found, this it has not been replaced by the
422          dnl cpp
423          ICC="no"
424          AC_MSG_RESULT([no]),
425          dnl the text was not found, it was replaced by the cpp
426          ICC="yes"
427          AC_MSG_RESULT([yes])
428        )
429
430        if test "$ICC" = "yes"; then
431          dnl this is icc, not gcc.
432
433          dnl ICC warnings we ignore:
434          dnl * 279 warns on static conditions in while expressions
435          dnl * 269 warns on our "%Od" printf formatters for curl_off_t output:
436          dnl   "invalid format string conversion"
437
438          WARN="-wd279,269"
439
440          if test "$gccnum" -gt "600"; then
441             dnl icc 6.0 and older doesn't have the -Wall flag
442             WARN="-Wall $WARN"
443          fi
444        else dnl $ICC = yes
445          dnl 
446          WARN="-W -Wall -Wwrite-strings -pedantic -Wno-long-long -Wundef -Wpointer-arith -Wnested-externs -Winline -Wmissing-declarations -Wmissing-prototypes -Wsign-compare"
447
448          dnl -Wcast-align is a bit too annoying ;-)
449
450          if test "$gccnum" -ge "296"; then
451            dnl gcc 2.96 or later
452            WARN="$WARN -Wfloat-equal"
453
454            if test "$gccnum" -gt "296"; then
455              dnl this option does not exist in 2.96
456              WARN="$WARN -Wno-format-nonliteral"
457            fi
458
459            dnl -Wunreachable-code seems totally unreliable on my gcc 3.3.2 on
460            dnl on i686-Linux as it gives us heaps with false positives
461            if test "$gccnum" -ge "303"; then
462              dnl gcc 3.3 and later
463              WARN="$WARN -Wendif-labels -Wstrict-prototypes"
464            fi
465          fi
466
467          for flag in $CPPFLAGS; do
468            case "$flag" in
469             -I*)
470               dnl include path
471               add=`echo $flag | sed 's/^-I/-isystem /g'`
472               WARN="$WARN $add"
473               ;;
474            esac
475          done
476
477        fi dnl $ICC = no
478
479        CFLAGS="$CFLAGS $WARN"
480
481     fi dnl $GCC = yes
482
483     dnl strip off optimizer flags
484     NEWFLAGS=""
485     for flag in $CFLAGS; do
486       case "$flag" in
487       -O*)
488         dnl echo "cut off $flag"
489         ;;
490       *)
491         NEWFLAGS="$NEWFLAGS $flag"
492         ;;
493       esac
494     done
495     CFLAGS=$NEWFLAGS
496
497 ]) dnl end of AC_DEFUN()
498
499
500 dnl This macro determines if the specified struct exists in the specified file
501 dnl Syntax:
502 dnl CARES_CHECK_STRUCT(headers, struct name, if found, [if not found])
503
504 AC_DEFUN([CARES_CHECK_STRUCT], [
505   AC_MSG_CHECKING([for struct $2])
506   AC_TRY_COMPILE([$1], 
507     [
508       struct $2 struct_instance;
509     ], ac_struct="yes", ac_found="no")
510   if test "$ac_struct" = "yes" ; then
511     AC_MSG_RESULT(yes)
512     $3
513   else
514     AC_MSG_RESULT(no)
515     $4
516   fi
517 ])
518
519 dnl This macro determines if the specified constant exists in the specified file
520 dnl Syntax:
521 dnl CARES_CHECK_CONSTANT(headers, constant name, if found, [if not found])
522
523 AC_DEFUN([CARES_CHECK_CONSTANT], [
524   AC_MSG_CHECKING([for $2])
525   AC_EGREP_CPP(VARIABLEWASDEFINED,
526    [
527       $1
528
529       #ifdef $2
530         VARIABLEWASDEFINED
531       #else
532         NJET
533       #endif
534     ], ac_constant="yes", ac_constant="no"
535   )
536   if test "$ac_constant" = "yes" ; then
537     AC_MSG_RESULT(yes)
538     $3
539   else
540     AC_MSG_RESULT(no)
541     $4
542   fi
543 ])
544
545
546 dnl This macro determines how many parameters getservbyport_r takes
547 AC_DEFUN([CARES_CHECK_GETSERVBYPORT_R], [
548   AC_MSG_CHECKING([how many arguments getservbyport_r takes])
549   AC_TRY_COMPILE(
550     [#include <netdb.h>],
551     [
552       int p1, p5;
553       char *p2, p4[4096];
554       struct servent *p3, *p6;
555       getservbyport_r(p1, p2, p3, p4, p5, &p6);
556     ], ac_func_getservbyport_r=6,
557     [AC_TRY_COMPILE(
558       [#include <netdb.h>],
559       [
560         int p1, p5;
561         char *p2, p4[4096];
562         struct servent *p3;
563         getservbyport_r(p1, p2, p3, p4, p5);
564       ], ac_func_getservbyport_r=5,
565       [AC_TRY_COMPILE(
566         [#include <netdb.h>],
567         [
568           int p1;
569           char *p2;
570           struct servent *p3;
571           struct servent_data p4;
572           getservbyport_r(p1, p2, p3, &p4);
573         ], ac_func_getservbyport_r=4, ac_func_getservbyport_r=0
574       )]
575     )]
576   )
577 if test $ac_func_getservbyport_r != "0" ; then
578   AC_MSG_RESULT($ac_func_getservbyport_r)
579   AC_DEFINE(HAVE_GETSERVBYPORT_R, 1, [Specifies whether getservbyport_r is present])
580   AC_DEFINE_UNQUOTED(GETSERVBYPORT_R_ARGS, $ac_func_getservbyport_r, [Specifies the number of arguments to 
581 getservbyport_r])
582   if test $ac_func_getservbyport_r = "4" ; then
583    AC_DEFINE(GETSERVBYPORT_R_BUFSIZE, sizeof(struct servent_data), [Specifies the size of the buffer to pass to 
584 getservbyport_r])
585   else
586    AC_DEFINE(GETSERVBYPORT_R_BUFSIZE, 4096, [Specifies the size of the buffer to pass to getservbyport_r])
587   fi
588 else
589   AC_MSG_RESULT([not found])
590 fi
591 ])
592