]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/unbound/acx_nlnetlabs.m4
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / unbound / acx_nlnetlabs.m4
1 # acx_nlnetlabs.m4 - common macros for configure checks
2 # Copyright 2009, Wouter Wijngaards, NLnet Labs.   
3 # BSD licensed.
4 #
5 # Version 30
6 # 2015-11-18 spelling check fix.
7 # 2015-11-05 ACX_SSL_CHECKS no longer adds -ldl needlessly.
8 # 2015-08-28 ACX_CHECK_PIE and ACX_CHECK_RELRO_NOW added.
9 # 2015-03-17 AHX_CONFIG_REALLOCARRAY added
10 # 2013-09-19 FLTO help text improved.
11 # 2013-07-18 Enable ACX_CHECK_COMPILER_FLAG to test for -Wstrict-prototypes
12 # 2013-06-25 FLTO has --disable-flto option.
13 # 2013-05-03 Update W32_SLEEP for newer mingw that links but not defines it.
14 # 2013-03-22 Fix ACX_RSRC_VERSION for long version numbers.
15 # 2012-02-09 Fix AHX_MEMCMP_BROKEN with undef in compat/memcmp.h.
16 # 2012-01-20 Fix COMPILER_FLAGS_UNBOUND for gcc 4.6.2 assigned-not-used-warns.
17 # 2011-12-05 Fix getaddrinfowithincludes on windows with fedora16 mingw32-gcc.
18 #            Fix ACX_MALLOC for redefined malloc error.
19 #            Fix GETADDRINFO_WITH_INCLUDES to add -lws2_32
20 # 2011-11-10 Fix FLTO test to not drop a.out in current directory.
21 # 2011-11-01 Fix FLTO test for llvm on Lion.
22 # 2011-08-01 Fix nonblock test (broken at v13).
23 # 2011-08-01 Fix autoconf 2.68 warnings
24 # 2011-06-23 Add ACX_CHECK_FLTO to check -flto.
25 # 2010-08-16 Fix FLAG_OMITTED for AS_TR_CPP changes in autoconf-2.66.
26 # 2010-07-02 Add check for ss_family (for minix).
27 # 2010-04-26 Fix to use CPPFLAGS for CHECK_COMPILER_FLAGS.
28 # 2010-03-01 Fix RPATH using CONFIG_COMMANDS to run at the very end.
29 # 2010-02-18 WITH_SSL outputs the LIBSSL_LDFLAGS, LIBS, CPPFLAGS separate, -ldl
30 # 2010-02-01 added ACX_CHECK_MEMCMP_SIGNED, AHX_MEMCMP_BROKEN
31 # 2010-01-20 added AHX_COONFIG_STRLCAT
32 # 2009-07-14 U_CHAR detection improved for windows crosscompile.
33 #            added ACX_FUNC_MALLOC
34 #            fixup some #if to #ifdef
35 #            NONBLOCKING test for mingw crosscompile.
36 # 2009-07-13 added ACX_WITH_SSL_OPTIONAL
37 # 2009-07-03 fixup LDFLAGS for empty ssl dir.
38 #
39 # Automates some of the checking constructs.  Aims at portability for POSIX.
40 # Documentation for functions is below.
41 #
42 # the following macro's are provided in this file:
43 # (see below for details on each macro).
44 #
45 # ACX_ESCAPE_BACKSLASH          - escape backslashes in var for C-preproc.
46 # ACX_RSRC_VERSION              - create windows resource version number.
47 # ACX_CHECK_COMPILER_FLAG       - see if cc supports a flag.
48 # ACX_CHECK_ERROR_FLAGS         - see which flag is -werror (used below).
49 # ACX_CHECK_COMPILER_FLAG_NEEDED - see if flags make the code compile cleanly.
50 # ACX_DEPFLAG                   - find cc dependency flags.
51 # ACX_DETERMINE_EXT_FLAGS_UNBOUND - find out which flags enable BSD and POSIX.
52 # ACX_CHECK_FORMAT_ATTRIBUTE    - find cc printf format syntax.
53 # ACX_CHECK_UNUSED_ATTRIBUTE    - find cc variable unused syntax.
54 # ACX_CHECK_FLTO                - see if cc supports -flto and use it if so.
55 # ACX_LIBTOOL_C_ONLY            - create libtool for C only, improved.
56 # ACX_TYPE_U_CHAR               - u_char type.
57 # ACX_TYPE_RLIM_T               - rlim_t type.
58 # ACX_TYPE_SOCKLEN_T            - socklen_t type.
59 # ACX_TYPE_IN_ADDR_T            - in_addr_t type.
60 # ACX_TYPE_IN_PORT_T            - in_port_t type.
61 # ACX_ARG_RPATH                 - add --disable-rpath option.
62 # ACX_WITH_SSL                  - add --with-ssl option, link -lcrypto.
63 # ACX_WITH_SSL_OPTIONAL         - add --with-ssl option, link -lcrypto,
64 #                                 where --without-ssl is also accepted
65 # ACX_LIB_SSL                   - setup to link -lssl.
66 # ACX_SYS_LARGEFILE             - improved sys_largefile, fseeko, >2G files.
67 # ACX_CHECK_GETADDRINFO_WITH_INCLUDES - find getaddrinfo, portably.
68 # ACX_FUNC_DEPRECATED           - see if func is deprecated.
69 # ACX_CHECK_NONBLOCKING_BROKEN  - see if nonblocking sockets really work.
70 # ACX_MKDIR_ONE_ARG             - determine mkdir(2) number of arguments.
71 # ACX_FUNC_IOCTLSOCKET          - find ioctlsocket, portably.
72 # ACX_FUNC_MALLOC               - check malloc, define replacement .
73 # AHX_CONFIG_FORMAT_ATTRIBUTE   - config.h text for format.
74 # AHX_CONFIG_UNUSED_ATTRIBUTE   - config.h text for unused.
75 # AHX_CONFIG_FSEEKO             - define fseeko, ftello fallback.
76 # AHX_CONFIG_RAND_MAX           - define RAND_MAX if needed.
77 # AHX_CONFIG_MAXHOSTNAMELEN     - define MAXHOSTNAMELEN if needed.
78 # AHX_CONFIG_IPV6_MIN_MTU       - define IPV6_MIN_MTU if needed.
79 # AHX_CONFIG_SNPRINTF           - snprintf compat prototype
80 # AHX_CONFIG_INET_PTON          - inet_pton compat prototype
81 # AHX_CONFIG_INET_NTOP          - inet_ntop compat prototype
82 # AHX_CONFIG_INET_ATON          - inet_aton compat prototype
83 # AHX_CONFIG_MEMMOVE            - memmove compat prototype
84 # AHX_CONFIG_STRLCAT            - strlcat compat prototype
85 # AHX_CONFIG_STRLCPY            - strlcpy compat prototype
86 # AHX_CONFIG_GMTIME_R           - gmtime_r compat prototype
87 # AHX_CONFIG_W32_SLEEP          - w32 compat for sleep
88 # AHX_CONFIG_W32_USLEEP         - w32 compat for usleep
89 # AHX_CONFIG_W32_RANDOM         - w32 compat for random
90 # AHX_CONFIG_W32_SRANDOM        - w32 compat for srandom
91 # AHX_CONFIG_W32_FD_SET_T       - w32 detection of FD_SET_T.
92 # ACX_CFLAGS_STRIP              - strip one flag from CFLAGS
93 # ACX_STRIP_EXT_FLAGS           - strip extension flags from CFLAGS
94 # AHX_CONFIG_FLAG_OMITTED       - define omitted flag
95 # AHX_CONFIG_FLAG_EXT           - define omitted extension flag
96 # AHX_CONFIG_EXT_FLAGS          - define the stripped extension flags
97 # ACX_CHECK_MEMCMP_SIGNED       - check if memcmp uses signed characters.
98 # AHX_MEMCMP_BROKEN             - replace memcmp func for CHECK_MEMCMP_SIGNED.
99 # ACX_CHECK_SS_FAMILY           - check for sockaddr_storage.ss_family
100 # ACX_CHECK_PIE                 - add --enable-pie option and check if works
101 # ACX_CHECK_RELRO_NOW           - add --enable-relro-now option and check it
102 #
103
104 dnl Escape backslashes as \\, for C:\ paths, for the C preprocessor defines.
105 dnl for example, ACX_ESCAPE_BACKSLASH($from_var, to_var)
106 dnl $1: the text to change. 
107 dnl $2: the result.
108 AC_DEFUN([ACX_ESCAPE_BACKSLASH], [$2="`echo $1 | sed -e 's/\\\\/\\\\\\\\/g'`"
109 ])
110
111 dnl Calculate comma separated windows-resource numbers from package version.
112 dnl Picks the first three(,0) or four numbers out of the name.
113 dnl $1: variable for the result
114 AC_DEFUN([ACX_RSRC_VERSION], 
115 [$1=[`echo $PACKAGE_VERSION | sed -e 's/^[^0-9]*\([0-9][0-9]*\)[^0-9][^0-9]*\([0-9][0-9]*\)[^0-9][^0-9]*\([0-9][0-9]*\)[^0-9][^0-9]*\([0-9][0-9]*\).*$/\1,\2,\3,\4/' -e 's/^[^0-9]*\([0-9][0-9]*\)[^0-9][^0-9]*\([0-9][0-9]*\)[^0-9][^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1,\2,\3,0/' `]
116 ])
117
118 dnl Routine to help check for compiler flags.
119 dnl Checks if the compiler will accept the flag.
120 dnl $1: the flag without a - in front, so g to check -g.
121 dnl $2: executed if yes
122 dnl $3: executed if no
123 AC_DEFUN([ACX_CHECK_COMPILER_FLAG], 
124 [
125 AC_REQUIRE([AC_PROG_CC])
126 AC_MSG_CHECKING(whether $CC supports -$1)
127 cache=`echo $1 | sed 'y%.=/+-%___p_%'`
128 AC_CACHE_VAL(cv_prog_cc_flag_$cache,
129 [
130 echo 'void f(void){}' >conftest.c
131 if test -z "`$CC $CPPFLAGS $CFLAGS -$1 -c conftest.c 2>&1`"; then
132 eval "cv_prog_cc_flag_$cache=yes"
133 else
134 eval "cv_prog_cc_flag_$cache=no"
135 fi
136 rm -f conftest conftest.o conftest.c
137 ])
138 if eval "test \"`echo '$cv_prog_cc_flag_'$cache`\" = yes"; then
139 AC_MSG_RESULT(yes)
140 :
141 $2
142 else
143 AC_MSG_RESULT(no)
144 :
145 $3
146 fi
147 ])
148
149 dnl setup flags for ACX_CHECK_COMPILER_FLAG_NEEDED
150 dnl ERRFLAG: result, compiler flag to turn warnings into errors
151 AC_DEFUN([ACX_CHECK_ERROR_FLAGS],
152 [
153 ACX_CHECK_COMPILER_FLAG(Werror, [ERRFLAG="-Werror"], [ERRFLAG="-errwarn"])
154 ACX_CHECK_COMPILER_FLAG(Wall, [ERRFLAG="$ERRFLAG -Wall"],
155                                 [ERRFLAG="$ERRFLAG -errfmt"])
156 ])
157
158 dnl Routine to help check for needed compiler flags.
159 dnl $1: flags for CC
160 dnl $2: the includes and code
161 dnl $3: if the given code only compiles with the flag, execute argument 3
162 dnl $4: if the given code compiles without the flag, execute argument 4
163 dnl $5: with and without flag the compile fails, execute argument 5.
164 AC_DEFUN([ACX_CHECK_COMPILER_FLAG_NEEDED],
165 [
166 AC_REQUIRE([AC_PROG_CC])
167 AC_REQUIRE([ACX_CHECK_ERROR_FLAGS])
168 AC_MSG_CHECKING(whether we need $1 as a flag for $CC)
169 cache=AS_TR_SH($1)
170 dnl cache=`echo $1 | sed 'y%.=/+- %___p__%'`
171 AC_CACHE_VAL(cv_prog_cc_flag_needed_$cache,
172 [
173 echo '$2' > conftest.c
174 echo 'void f(){}' >>conftest.c
175 if test -z "`$CC $CPPFLAGS $CFLAGS $ERRFLAG -c conftest.c 2>&1`"; then
176 eval "cv_prog_cc_flag_needed_$cache=no"
177 else
178 [
179 if test -z "`$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1`"; then
180 eval "cv_prog_cc_flag_needed_$cache=yes"
181 else
182 eval "cv_prog_cc_flag_needed_$cache=fail"
183 #echo 'Test with flag fails too!'
184 #cat conftest.c
185 #echo "$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1"
186 #echo `$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1`
187 #exit 1
188 fi
189 ]
190 fi
191 rm -f conftest conftest.c conftest.o
192 ])
193 if eval "test \"`echo '$cv_prog_cc_flag_needed_'$cache`\" = yes"; then
194 AC_MSG_RESULT(yes)
195 :
196 $3
197 else
198 if eval "test \"`echo '$cv_prog_cc_flag_needed_'$cache`\" = no"; then
199 AC_MSG_RESULT(no)
200 #echo 'Test with flag is no!'
201 #cat conftest.c
202 #echo "$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1"
203 #echo `$CC $CPPFLAGS $CFLAGS $1 $ERRFLAG -c conftest.c 2>&1`
204 #exit 1
205 :
206 $4
207 else
208 AC_MSG_RESULT(failed)
209 :
210 $5
211 fi
212 fi
213 ])
214
215 dnl Check for CC dependency flag
216 dnl DEPFLAG: set to flag that generates dependencies.
217 AC_DEFUN([ACX_DEPFLAG],
218 [
219 AC_MSG_CHECKING([$CC dependency flag])
220 echo 'void f(){}' >conftest.c
221 if test "`$CC -MM conftest.c 2>&1`" = "conftest.o: conftest.c"; then
222         DEPFLAG="-MM"
223 else 
224   if test "`$CC -xM1 conftest.c 2>&1`" = "conftest.o: conftest.c"; then
225         DEPFLAG="-xM1"
226   else
227         DEPFLAG="-MM"  # dunno do something
228   fi 
229 fi
230 AC_MSG_RESULT($DEPFLAG)
231 rm -f conftest.c
232 AC_SUBST(DEPFLAG)
233 ])
234
235 dnl Determine flags that gives POSIX and BSD functionality.
236 dnl CFLAGS is modified for the result.
237 AC_DEFUN([ACX_DETERMINE_EXT_FLAGS_UNBOUND],
238 [
239 ACX_CHECK_COMPILER_FLAG(std=c99, [C99FLAG="-std=c99"])
240 ACX_CHECK_COMPILER_FLAG(xc99, [C99FLAG="-xc99"])
241
242 AC_CHECK_HEADERS([getopt.h time.h],,, [AC_INCLUDES_DEFAULT])
243
244 ACX_CHECK_COMPILER_FLAG_NEEDED($C99FLAG -D__EXTENSIONS__ -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=1 -D_ALL_SOURCE,
245 [
246 #include "confdefs.h"
247 #include <stdlib.h>
248 #include <ctype.h>
249 #include <sys/time.h>
250 #ifdef HAVE_TIME_H
251 #include <time.h>
252 #endif
253 #include <unistd.h>
254 #include <netdb.h>
255 #ifdef HAVE_GETOPT_H
256 #include <getopt.h>
257 #endif
258
259 int test() {
260         int a;
261         char **opts = NULL;
262         struct timeval tv;
263         char *t;
264         time_t time = 0;
265         char *buf = NULL;
266         const char* str = NULL;
267         struct msghdr msg;
268         msg.msg_control = 0;
269         t = ctime_r(&time, buf);
270         tv.tv_usec = 10;
271         srandom(32);
272         a = getopt(2, opts, "a");
273         a = isascii(32);
274         str = gai_strerror(0);
275         if(str && t && tv.tv_usec && msg.msg_control)
276                 a = 0;
277         return a;
278 }
279 ], [CFLAGS="$CFLAGS $C99FLAG -D__EXTENSIONS__ -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=1 -D_ALL_SOURCE"])
280
281 ACX_CHECK_COMPILER_FLAG_NEEDED($C99FLAG -D__EXTENSIONS__ -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_ALL_SOURCE,
282 [
283 #include "confdefs.h"
284 #include <stdlib.h>
285 #include <ctype.h>
286 #include <sys/time.h>
287 #ifdef HAVE_TIME_H
288 #include <time.h>
289 #endif
290 #include <unistd.h>
291 #include <netdb.h>
292 #ifdef HAVE_GETOPT_H
293 #include <getopt.h>
294 #endif
295
296 int test() {
297         int a;
298         char **opts = NULL;
299         struct timeval tv;
300         char *t;
301         time_t time = 0;
302         char *buf = NULL;
303         const char* str = NULL;
304         struct msghdr msg;
305         msg.msg_control = 0;
306         t = ctime_r(&time, buf);
307         tv.tv_usec = 10;
308         srandom(32);
309         a = getopt(2, opts, "a");
310         a = isascii(32);
311         str = gai_strerror(0);
312         if(str && t && tv.tv_usec && msg.msg_control)
313                 a = 0;
314         return a;
315 }
316 ], [CFLAGS="$CFLAGS $C99FLAG -D__EXTENSIONS__ -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -D_ALL_SOURCE"])
317
318 ACX_CHECK_COMPILER_FLAG_NEEDED($C99FLAG,
319 [
320 #include <stdbool.h>
321 #include <ctype.h>
322 int test() {
323         int a = 0;
324         return a;
325 }
326 ], [CFLAGS="$CFLAGS $C99FLAG"])
327
328 ACX_CHECK_COMPILER_FLAG_NEEDED(-D_BSD_SOURCE,
329 [
330 #include <ctype.h>
331
332 int test() {
333         int a;
334         a = isascii(32);
335         return a;
336 }
337 ], [CFLAGS="$CFLAGS -D_BSD_SOURCE"])
338
339 ACX_CHECK_COMPILER_FLAG_NEEDED(-D_GNU_SOURCE,
340 [
341 #include <netinet/in.h>
342
343 int test() {
344         struct in6_pktinfo inf;
345         int a = (int)sizeof(inf);
346         return a;
347 }
348 ], [CFLAGS="$CFLAGS -D_GNU_SOURCE"])
349
350 # check again for GNU_SOURCE for setresgid. May fail if setresgid
351 # is not available at all. -D_FRSRESGID is to make this check unique.
352 # otherwise we would get the previous cached result.
353 ACX_CHECK_COMPILER_FLAG_NEEDED(-D_GNU_SOURCE -D_FRSRESGID,
354 [
355 #include <unistd.h>
356
357 int test() {
358         int a = setresgid(0,0,0);
359         a = setresuid(0,0,0);
360         return a;
361 }
362 ], [CFLAGS="$CFLAGS -D_GNU_SOURCE"])
363
364 ACX_CHECK_COMPILER_FLAG_NEEDED(-D_POSIX_C_SOURCE=200112,
365 [
366 #include "confdefs.h"
367 #ifdef HAVE_TIME_H
368 #include <time.h>
369 #endif
370 #include <netdb.h>
371
372 int test() {
373         int a = 0;
374         char *t;
375         time_t time = 0;
376         char *buf = NULL;
377         const char* str = NULL;
378         t = ctime_r(&time, buf);
379         str = gai_strerror(0);
380         if(t && str)
381                 a = 0;
382         return a;
383 }
384 ], [CFLAGS="$CFLAGS -D_POSIX_C_SOURCE=200112"])
385
386 ACX_CHECK_COMPILER_FLAG_NEEDED(-D__EXTENSIONS__,
387 [
388 #include "confdefs.h"
389 #include <stdlib.h>
390 #include <ctype.h>
391 #include <sys/time.h>
392 #ifdef HAVE_TIME_H
393 #include <time.h>
394 #endif
395 #include <unistd.h>
396 #ifdef HAVE_GETOPT_H
397 #include <getopt.h>
398 #endif
399
400 int test() {
401         int a;
402         char **opts = NULL;
403         struct timeval tv;
404         tv.tv_usec = 10;
405         srandom(32);
406         a = getopt(2, opts, "a");
407         a = isascii(32);
408         if(tv.tv_usec)
409                 a = 0;
410         return a;
411 }
412 ], [CFLAGS="$CFLAGS -D__EXTENSIONS__"])
413
414 ])dnl End of ACX_DETERMINE_EXT_FLAGS_UNBOUND
415
416 dnl Check if CC supports -flto.
417 dnl in a way that supports clang and suncc (that flag does something else,
418 dnl but fails to link).  It sets it in CFLAGS if it works.
419 AC_DEFUN([ACX_CHECK_FLTO], [
420     AC_ARG_ENABLE([flto], AS_HELP_STRING([--disable-flto], [Disable link-time optimization (gcc specific option)]))
421     AS_IF([test "x$enable_flto" != "xno"], [
422         AC_MSG_CHECKING([if $CC supports -flto])
423         BAKCFLAGS="$CFLAGS"
424         CFLAGS="$CFLAGS -flto"
425         AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [
426             if $CC $CFLAGS -o conftest conftest.c 2>&1 | grep "warning: no debug symbols in executable" >/dev/null; then
427                 CFLAGS="$BAKCFLAGS"
428                 AC_MSG_RESULT(no)
429             else
430                 AC_MSG_RESULT(yes)
431             fi
432             rm -f conftest conftest.c conftest.o
433         ], [CFLAGS="$BAKCFLAGS" ; AC_MSG_RESULT(no)])
434     ])
435 ])
436
437 dnl Check the printf-format attribute (if any)
438 dnl result in HAVE_ATTR_FORMAT.  
439 dnl Make sure you also include the AHX_CONFIG_FORMAT_ATTRIBUTE.
440 AC_DEFUN([ACX_CHECK_FORMAT_ATTRIBUTE],
441 [AC_REQUIRE([AC_PROG_CC])
442 AC_MSG_CHECKING(whether the C compiler (${CC-cc}) accepts the "format" attribute)
443 AC_CACHE_VAL(ac_cv_c_format_attribute,
444 [ac_cv_c_format_attribute=no
445 AC_TRY_COMPILE(
446 [#include <stdio.h>
447 void f (char *format, ...) __attribute__ ((format (printf, 1, 2)));
448 void (*pf) (char *format, ...) __attribute__ ((format (printf, 1, 2)));
449 ], [
450    f ("%s", "str");
451 ],
452 [ac_cv_c_format_attribute="yes"],
453 [ac_cv_c_format_attribute="no"])
454 ])
455
456 AC_MSG_RESULT($ac_cv_c_format_attribute)
457 if test $ac_cv_c_format_attribute = yes; then
458   AC_DEFINE(HAVE_ATTR_FORMAT, 1, [Whether the C compiler accepts the "format" attribute])
459 fi
460 ])dnl End of ACX_CHECK_FORMAT_ATTRIBUTE
461
462 dnl Setup ATTR_FORMAT config.h parts.
463 dnl make sure you call ACX_CHECK_FORMAT_ATTRIBUTE also.
464 AC_DEFUN([AHX_CONFIG_FORMAT_ATTRIBUTE],
465
466 #ifdef HAVE_ATTR_FORMAT
467 #  define ATTR_FORMAT(archetype, string_index, first_to_check) \
468     __attribute__ ((format (archetype, string_index, first_to_check)))
469 #else /* !HAVE_ATTR_FORMAT */
470 #  define ATTR_FORMAT(archetype, string_index, first_to_check) /* empty */
471 #endif /* !HAVE_ATTR_FORMAT */
472 ])
473
474 dnl Check how to mark function arguments as unused.
475 dnl result in HAVE_ATTR_UNUSED.  
476 dnl Make sure you include AHX_CONFIG_UNUSED_ATTRIBUTE also.
477 AC_DEFUN([ACX_CHECK_UNUSED_ATTRIBUTE],
478 [AC_REQUIRE([AC_PROG_CC])
479 AC_MSG_CHECKING(whether the C compiler (${CC-cc}) accepts the "unused" attribute)
480 AC_CACHE_VAL(ac_cv_c_unused_attribute,
481 [ac_cv_c_unused_attribute=no
482 AC_TRY_COMPILE(
483 [#include <stdio.h>
484 void f (char *u __attribute__((unused)));
485 ], [
486    f ("x");
487 ],
488 [ac_cv_c_unused_attribute="yes"],
489 [ac_cv_c_unused_attribute="no"])
490 ])
491
492 dnl Setup ATTR_UNUSED config.h parts.
493 dnl make sure you call ACX_CHECK_UNUSED_ATTRIBUTE also.
494 AC_DEFUN([AHX_CONFIG_UNUSED_ATTRIBUTE],
495 [
496 #if defined(DOXYGEN)
497 #  define ATTR_UNUSED(x)  x
498 #elif defined(__cplusplus)
499 #  define ATTR_UNUSED(x)
500 #elif defined(HAVE_ATTR_UNUSED)
501 #  define ATTR_UNUSED(x)  x __attribute__((unused))
502 #else /* !HAVE_ATTR_UNUSED */
503 #  define ATTR_UNUSED(x)  x
504 #endif /* !HAVE_ATTR_UNUSED */
505 ])
506
507 AC_MSG_RESULT($ac_cv_c_unused_attribute)
508 if test $ac_cv_c_unused_attribute = yes; then
509   AC_DEFINE(HAVE_ATTR_UNUSED, 1, [Whether the C compiler accepts the "unused" attribute])
510 fi
511 ])dnl
512
513 dnl Pre-fun for ACX_LIBTOOL_C_ONLY
514 AC_DEFUN([ACX_LIBTOOL_C_PRE], [
515 # skip these tests, we do not need them.
516 AC_DEFUN([AC_PROG_F77], [:])
517 AC_DEFUN([AC_PROG_FC], [:])
518 AC_DEFUN([AC_PROG_CXX], [:])
519 AC_DEFUN([AC_PROG_CXXCPP], [:])
520 AC_DEFUN([AC_PROG_OBJC], [:])
521 AC_DEFUN([AC_PROG_OBJCCPP], [:])
522 AC_DEFUN([AC_LIBTOOL_CXX], [:])
523 AC_DEFUN([AC_LIBTOOL_F77], [:])
524 # always use ./libtool unless override from commandline (libtool=mylibtool)
525 if test -z "$libtool"; then
526         libtool="./libtool"
527 fi
528 AC_SUBST(libtool)
529 # avoid libtool max commandline length test on systems that fork slowly.
530 AC_CANONICAL_HOST
531 if echo "$host_os" | grep "sunos4" >/dev/null; then
532         lt_cv_sys_max_cmd_len=32750;
533 fi
534 AC_PATH_TOOL(AR, ar, [false])
535 if test $AR = false; then
536         AC_MSG_ERROR([Cannot find 'ar', please extend PATH to include it])
537 fi
538 ])
539
540 dnl Perform libtool check, portably, only for C
541 AC_DEFUN([ACX_LIBTOOL_C_ONLY], [
542 dnl as a requirement so that is gets called before LIBTOOL
543 dnl because libtools 'AC_REQUIRE' names are right after this one, before
544 dnl this function contents.
545 AC_REQUIRE([ACX_LIBTOOL_C_PRE])
546 AC_PROG_LIBTOOL
547 ])
548
549 dnl Detect if u_char type is defined, otherwise define it.
550 AC_DEFUN([ACX_TYPE_U_CHAR], 
551 [AC_CHECK_TYPE([u_char], ,
552         [AC_DEFINE([u_char], [unsigned char], [Define to 'unsigned char if not defined])], [
553 AC_INCLUDES_DEFAULT
554 #ifdef HAVE_WINSOCK2_H
555 #  include <winsock2.h>
556 #endif
557 ]) ])
558
559 dnl Detect if rlim_t type is defined, otherwise define it.
560 AC_DEFUN([ACX_TYPE_RLIM_T],
561 [AC_CHECK_TYPE(rlim_t, , 
562         [AC_DEFINE([rlim_t], [unsigned long], [Define to 'int' if not defined])], [
563 AC_INCLUDES_DEFAULT
564 #ifdef HAVE_SYS_RESOURCE_H
565 #  include <sys/resource.h>
566 #endif
567 ]) ])
568
569 dnl Detect if socklen_t type is defined, otherwise define it.
570 AC_DEFUN([ACX_TYPE_SOCKLEN_T],
571 [
572 AC_CHECK_TYPE(socklen_t, , 
573         [AC_DEFINE([socklen_t], [int], [Define to 'int' if not defined])], [
574 AC_INCLUDES_DEFAULT
575 #ifdef HAVE_SYS_SOCKET_H
576 #  include <sys/socket.h>
577 #endif
578 #ifdef HAVE_WS2TCPIP_H
579 #  include <ws2tcpip.h>
580 #endif
581 ]) ])
582
583 dnl Detect if in_addr_t type is defined, otherwise define it.
584 AC_DEFUN([ACX_TYPE_IN_ADDR_T],
585 [ AC_CHECK_TYPE(in_addr_t, [], [AC_DEFINE([in_addr_t], [uint32_t], [in_addr_t])], [
586 AC_INCLUDES_DEFAULT
587 #ifdef HAVE_SYS_TYPES_H
588 # include <sys/types.h>
589 #endif
590 #ifdef HAVE_NETINET_IN_H
591 # include <netinet/in.h>
592 #endif
593 ]) ])
594
595 dnl Detect if in_port_t type is defined, otherwise define it.
596 AC_DEFUN([ACX_TYPE_IN_PORT_T],
597 [ AC_CHECK_TYPE(in_port_t, [], [AC_DEFINE([in_port_t], [uint16_t], [in_port_t])], [
598 AC_INCLUDES_DEFAULT
599 #ifdef HAVE_SYS_TYPES_H
600 # include <sys/types.h>
601 #endif
602 #ifdef HAVE_NETINET_IN_H
603 # include <netinet/in.h>
604 #endif
605 ]) ])
606
607 dnl Add option to disable the evil rpath. Check whether to use rpath or not.
608 dnl Adds the --disable-rpath option. Uses trick to edit the ./libtool.
609 AC_DEFUN([ACX_ARG_RPATH],
610 [
611 AC_ARG_ENABLE(rpath,
612         [  --disable-rpath         disable hardcoded rpath (default=enabled)],
613         enable_rpath=$enableval, enable_rpath=yes)
614 if test "x$enable_rpath" = xno; then
615         dnl AC_MSG_RESULT([Fixing libtool for -rpath problems.])
616         AC_CONFIG_COMMANDS([disable-rpath], [
617         sed < libtool > libtool-2 \
618         's/^hardcode_libdir_flag_spec.*$'/'hardcode_libdir_flag_spec=" -D__LIBTOOL_RPATH_SED__ "/'
619         mv libtool-2 libtool
620         chmod 755 libtool
621         libtool="./libtool"
622         ])
623 fi
624 ])
625
626 dnl Add a -R to the RUNTIME_PATH.  Only if rpath is enabled and it is
627 dnl an absolute path.
628 dnl $1: the pathname to add.
629 AC_DEFUN([ACX_RUNTIME_PATH_ADD], [
630         if test "x$enable_rpath" = xyes; then
631                 if echo "$1" | grep "^/" >/dev/null; then
632                         RUNTIME_PATH="$RUNTIME_PATH -R$1"
633                 fi
634         fi
635 ])
636
637 dnl Common code for both ACX_WITH_SSL and ACX_WITH_SSL_OPTIONAL
638 dnl Takes one argument; the withval checked in those 2 functions
639 dnl sets up the environment for the given openssl path
640 AC_DEFUN([ACX_SSL_CHECKS], [
641     withval=$1
642     if test x_$withval != x_no; then
643         AC_MSG_CHECKING(for SSL)
644         if test x_$withval = x_ -o x_$withval = x_yes; then
645             withval="/usr/local/ssl /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /opt/local /usr/sfw /usr"
646         fi
647         for dir in $withval; do
648             ssldir="$dir"
649             if test -f "$dir/include/openssl/ssl.h"; then
650                 found_ssl="yes"
651                 AC_DEFINE_UNQUOTED([HAVE_SSL], [], [Define if you have the SSL libraries installed.])
652                 dnl assume /usr/include is already in the include-path.
653                 if test "$ssldir" != "/usr"; then
654                         CPPFLAGS="$CPPFLAGS -I$ssldir/include"
655                         LIBSSL_CPPFLAGS="$LIBSSL_CPPFLAGS -I$ssldir/include"
656                 fi
657                 break;
658             fi
659         done
660         if test x_$found_ssl != x_yes; then
661             AC_MSG_ERROR(Cannot find the SSL libraries in $withval)
662         else
663             AC_MSG_RESULT(found in $ssldir)
664             HAVE_SSL=yes
665             dnl assume /usr is already in the lib and dynlib paths.
666             if test "$ssldir" != "/usr" -a "$ssldir" != ""; then
667                 LDFLAGS="$LDFLAGS -L$ssldir/lib"
668                 LIBSSL_LDFLAGS="$LIBSSL_LDFLAGS -L$ssldir/lib"
669                 ACX_RUNTIME_PATH_ADD([$ssldir/lib])
670             fi
671         
672             AC_MSG_CHECKING([for HMAC_CTX_init in -lcrypto])
673             LIBS="$LIBS -lcrypto"
674             LIBSSL_LIBS="$LIBSSL_LIBS -lcrypto"
675             AC_TRY_LINK(, [
676                 int HMAC_CTX_init(void);
677                 (void)HMAC_CTX_init();
678               ], [
679                 AC_MSG_RESULT(yes)
680                 AC_DEFINE([HAVE_HMAC_CTX_INIT], 1, 
681                           [If you have HMAC_CTX_init])
682               ], [
683                 AC_MSG_RESULT(no)
684                 # check if -lwsock32 or -lgdi32 are needed.     
685                 BAKLIBS="$LIBS"
686                 BAKSSLLIBS="$LIBSSL_LIBS"
687                 LIBS="$LIBS -lgdi32"
688                 LIBSSL_LIBS="$LIBSSL_LIBS -lgdi32"
689                 AC_MSG_CHECKING([if -lcrypto needs -lgdi32])
690                 AC_TRY_LINK([], [
691                     int HMAC_CTX_init(void);
692                     (void)HMAC_CTX_init();
693                   ],[
694                     AC_DEFINE([HAVE_HMAC_CTX_INIT], 1, 
695                         [If you have HMAC_CTX_init])
696                     AC_MSG_RESULT(yes) 
697                   ],[
698                     AC_MSG_RESULT(no)
699                     LIBS="$BAKLIBS"
700                     LIBSSL_LIBS="$BAKSSLLIBS"
701                     LIBS="$LIBS -ldl"
702                     LIBSSL_LIBS="$LIBSSL_LIBS -ldl"
703                     AC_MSG_CHECKING([if -lcrypto needs -ldl])
704                     AC_TRY_LINK([], [
705                         int HMAC_CTX_init(void);
706                         (void)HMAC_CTX_init();
707                       ],[
708                         AC_DEFINE([HAVE_HMAC_CTX_INIT], 1, 
709                             [If you have HMAC_CTX_init])
710                         AC_MSG_RESULT(yes) 
711                       ],[
712                         AC_MSG_RESULT(no)
713                     AC_MSG_ERROR([OpenSSL found in $ssldir, but version 0.9.7 or higher is required])
714                     ])
715                 ])
716             ])
717         fi
718         AC_SUBST(HAVE_SSL)
719         AC_SUBST(RUNTIME_PATH)
720     fi
721 AC_CHECK_HEADERS([openssl/ssl.h],,, [AC_INCLUDES_DEFAULT])
722 AC_CHECK_HEADERS([openssl/err.h],,, [AC_INCLUDES_DEFAULT])
723 AC_CHECK_HEADERS([openssl/rand.h],,, [AC_INCLUDES_DEFAULT])
724 ])dnl End of ACX_SSL_CHECKS
725
726 dnl Check for SSL, where SSL is mandatory
727 dnl Adds --with-ssl option, searches for openssl and defines HAVE_SSL if found
728 dnl Setup of CPPFLAGS, CFLAGS.  Adds -lcrypto to LIBS. 
729 dnl Checks main header files of SSL.
730 dnl
731 AC_DEFUN([ACX_WITH_SSL],
732 [
733 AC_ARG_WITH(ssl, AC_HELP_STRING([--with-ssl=pathname],
734                                     [enable SSL (will check /usr/local/ssl
735                             /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /opt/local /usr/sfw /usr)]),[
736         ],[
737             withval="yes"
738         ])
739     if test x_$withval = x_no; then
740         AC_MSG_ERROR([Need SSL library to do digital signature cryptography])
741     fi
742     ACX_SSL_CHECKS($withval)
743 ])dnl End of ACX_WITH_SSL
744
745 dnl Check for SSL, where ssl is optional (--without-ssl is allowed)
746 dnl Adds --with-ssl option, searches for openssl and defines HAVE_SSL if found
747 dnl Setup of CPPFLAGS, CFLAGS.  Adds -lcrypto to LIBS. 
748 dnl Checks main header files of SSL.
749 dnl
750 AC_DEFUN([ACX_WITH_SSL_OPTIONAL],
751 [
752 AC_ARG_WITH(ssl, AC_HELP_STRING([--with-ssl=pathname],
753                                 [enable SSL (will check /usr/local/ssl
754                                 /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /opt/local /usr/sfw /usr)]),[
755         ],[
756             withval="yes"
757         ])
758     ACX_SSL_CHECKS($withval)
759 ])dnl End of ACX_WITH_SSL_OPTIONAL
760
761 dnl Setup to use -lssl
762 dnl To use -lcrypto, use the ACX_WITH_SSL setup (before this one).
763 AC_DEFUN([ACX_LIB_SSL],
764 [
765 # check if libssl needs libdl
766 BAKLIBS="$LIBS"
767 LIBS="-lssl $LIBS"
768 AC_MSG_CHECKING([if libssl needs libdl])
769 AC_TRY_LINK_FUNC([SSL_CTX_new], [
770         AC_MSG_RESULT([no])
771         LIBS="$BAKLIBS"
772 ] , [
773         AC_MSG_RESULT([yes])
774         LIBS="$BAKLIBS"
775         AC_SEARCH_LIBS([dlopen], [dl])
776 ]) ])dnl End of ACX_LIB_SSL
777
778 dnl Setup to use very large files (>2Gb).
779 dnl setups fseeko and its own
780 AC_DEFUN([ACX_SYS_LARGEFILE],
781 [
782 AC_SYS_LARGEFILE
783 dnl try to see if an additional _LARGEFILE_SOURCE 1 is needed to get fseeko
784 ACX_CHECK_COMPILER_FLAG_NEEDED(-D_LARGEFILE_SOURCE=1,
785 [
786 #include <stdio.h>
787 int test() {
788         int a = fseeko(stdin, 0, 0);
789         return a;
790 }
791 ], [CFLAGS="$CFLAGS -D_LARGEFILE_SOURCE=1"])
792 ])
793
794 dnl Check getaddrinfo.
795 dnl Works on linux, solaris, bsd and windows(links winsock).
796 dnl defines HAVE_GETADDRINFO, USE_WINSOCK.
797 AC_DEFUN([ACX_CHECK_GETADDRINFO_WITH_INCLUDES],
798 [AC_REQUIRE([AC_PROG_CC])
799 AC_MSG_CHECKING(for getaddrinfo)
800 ac_cv_func_getaddrinfo=no
801 AC_LINK_IFELSE(
802 [AC_LANG_SOURCE([[
803 #ifdef __cplusplus
804 extern "C"
805 {
806 #endif
807 char* getaddrinfo();
808 char* (*f) () = getaddrinfo;
809 #ifdef __cplusplus
810 }
811 #endif
812 int main() {
813         ;
814         return 0;
815 }
816 ]])],
817 dnl this case on linux, solaris, bsd
818 [ac_cv_func_getaddrinfo="yes"
819 dnl see if on windows
820 if test "$ac_cv_header_windows_h" = "yes"; then
821         AC_DEFINE(USE_WINSOCK, 1, [Whether the windows socket API is used])
822         USE_WINSOCK="1"
823         LIBS="$LIBS -lws2_32"
824 fi
825 ],
826 dnl no quick getaddrinfo, try mingw32 and winsock2 library.
827 ORIGLIBS="$LIBS"
828 LIBS="$LIBS -lws2_32"
829 AC_LINK_IFELSE(
830 [AC_LANG_PROGRAM(
831 [
832 #ifdef HAVE_WS2TCPIP_H
833 #include <ws2tcpip.h>
834 #endif
835 ],
836 [
837         (void)getaddrinfo(NULL, NULL, NULL, NULL);
838 ]
839 )],
840 [
841 ac_cv_func_getaddrinfo="yes"
842 dnl already: LIBS="$LIBS -lws2_32"
843 AC_DEFINE(USE_WINSOCK, 1, [Whether the windows socket API is used])
844 USE_WINSOCK="1"
845 ],
846 [
847 ac_cv_func_getaddrinfo="no"
848 LIBS="$ORIGLIBS"
849 ])
850 )
851
852 AC_MSG_RESULT($ac_cv_func_getaddrinfo)
853 if test $ac_cv_func_getaddrinfo = yes; then
854   AC_DEFINE(HAVE_GETADDRINFO, 1, [Whether getaddrinfo is available])
855 fi
856 ])dnl Endof AC_CHECK_GETADDRINFO_WITH_INCLUDES
857
858 dnl check if a function is deprecated. defines DEPRECATED_func in config.h.
859 dnl $1: function name
860 dnl $2: C-statement that calls the function.
861 dnl $3: includes for the program.
862 dnl $4: executes if yes
863 dnl $5: executes if no
864 AC_DEFUN([ACX_FUNC_DEPRECATED],
865 [
866 AC_REQUIRE([AC_PROG_CC])
867 AC_MSG_CHECKING(if $1 is deprecated)
868 cache=`echo $1 | sed 'y%.=/+-%___p_%'`
869 AC_CACHE_VAL(cv_cc_deprecated_$cache,
870 [
871 echo '$3' >conftest.c
872 echo 'void f(){ $2 }' >>conftest.c
873 if test -z "`$CC -c conftest.c 2>&1 | grep deprecated`"; then
874 eval "cv_cc_deprecated_$cache=no"
875 else
876 eval "cv_cc_deprecated_$cache=yes"
877 fi
878 rm -f conftest conftest.o conftest.c
879 ])
880 if eval "test \"`echo '$cv_cc_deprecated_'$cache`\" = yes"; then
881 AC_MSG_RESULT(yes)
882 AC_DEFINE_UNQUOTED(AS_TR_CPP([DEPRECATED_$1]), 1, [Whether $1 is deprecated])
883 :
884 $4
885 else
886 AC_MSG_RESULT(no)
887 :
888 $5
889 fi
890 ])dnl end of ACX_FUNC_DEPRECATED
891
892 dnl check if select and nonblocking sockets actually work.
893 dnl Needs fork(2) and select(2).
894 dnl defines NONBLOCKING_IS_BROKEN, and if that is true multiple reads from
895 dnl a nonblocking socket do not work, a new call to select is necessary.
896 AC_DEFUN([ACX_CHECK_NONBLOCKING_BROKEN],
897 [
898 AC_MSG_CHECKING([if nonblocking sockets work])
899 if echo $target | grep mingw32 >/dev/null; then 
900         AC_MSG_RESULT([no (windows)])
901         AC_DEFINE([NONBLOCKING_IS_BROKEN], 1, [Define if the network stack does not fully support nonblocking io (causes lower performance).])
902 else
903 AC_RUN_IFELSE([
904 AC_LANG_SOURCE([[
905 #include <stdio.h>
906 #include <string.h>
907 #include <stdlib.h>
908 #include <fcntl.h>
909 #include <errno.h>
910 #ifdef HAVE_SYS_TYPES_H
911 #include <sys/types.h>
912 #endif
913 #ifdef HAVE_SYS_SOCKET_H
914 #include <sys/socket.h>
915 #endif
916 #ifdef HAVE_NETINET_IN_H
917 #include <netinet/in.h>
918 #endif
919 #ifdef HAVE_ARPA_INET_H
920 #include <arpa/inet.h>
921 #endif
922 #ifdef HAVE_UNISTD_H
923 #include <unistd.h>
924 #endif
925 #ifdef HAVE_TIME_H
926 #include <time.h>
927 #endif
928
929 int main(void)
930 {
931         int port;
932         int sfd, cfd;
933         int num = 10;
934         int i, p;
935         struct sockaddr_in a;
936         /* test if select and nonblocking reads work well together */
937         /* open port.
938            fork child to send 10 messages.
939            select to read.
940            then try to nonblocking read the 10 messages
941            then, nonblocking read must give EAGAIN
942         */
943
944         port = 12345 + (time(0)%32);
945         sfd = socket(PF_INET, SOCK_DGRAM, 0);
946         if(sfd == -1) {
947                 perror("socket");
948                 return 1;
949         }
950         memset(&a, 0, sizeof(a));
951         a.sin_family = AF_INET;
952         a.sin_port = htons(port);
953         a.sin_addr.s_addr = inet_addr("127.0.0.1");
954         if(bind(sfd, (struct sockaddr*)&a, sizeof(a)) < 0) {
955                 perror("bind");
956                 return 1;
957         }
958         if(fcntl(sfd, F_SETFL, O_NONBLOCK) == -1) {
959                 perror("fcntl");
960                 return 1;
961         }
962
963         cfd = socket(PF_INET, SOCK_DGRAM, 0);
964         if(cfd == -1) {
965                 perror("client socket");
966                 return 1;
967         }
968         a.sin_port = 0;
969         if(bind(cfd, (struct sockaddr*)&a, sizeof(a)) < 0) {
970                 perror("client bind");
971                 return 1;
972         }
973         a.sin_port = htons(port);
974
975         /* no handler, causes exit in 10 seconds */
976         alarm(10);
977
978         /* send and receive on the socket */
979         if((p=fork()) == 0) {
980                 for(i=0; i<num; i++) {
981                         if(sendto(cfd, &i, sizeof(i), 0, 
982                                 (struct sockaddr*)&a, sizeof(a)) < 0) {
983                                 perror("sendto");
984                                 return 1;
985                         }
986                 }
987         } else {
988                 /* parent */
989                 fd_set rset;
990                 int x;
991                 if(p == -1) {
992                         perror("fork");
993                         return 1;
994                 }
995                 FD_ZERO(&rset);
996                 FD_SET(sfd, &rset);
997                 if(select(sfd+1, &rset, NULL, NULL, NULL) < 1) {
998                         perror("select");
999                         return 1;
1000                 }
1001                 i = 0;
1002                 while(i < num) {
1003                         if(recv(sfd, &x, sizeof(x), 0) != sizeof(x)) {
1004                                 if(errno == EAGAIN)
1005                                         continue;
1006                                 perror("recv");
1007                                 return 1;
1008                         }
1009                         i++;
1010                 }
1011                 /* now we want to get EAGAIN: nonblocking goodness */
1012                 errno = 0;
1013                 recv(sfd, &x, sizeof(x), 0);
1014                 if(errno != EAGAIN) {
1015                         perror("trying to recv again");
1016                         return 1;
1017                 }
1018                 /* EAGAIN encountered */
1019         }
1020
1021         close(sfd);
1022         close(cfd);
1023         return 0;
1024 }
1025 ]])], [
1026         AC_MSG_RESULT([yes])
1027 ], [
1028         AC_MSG_RESULT([no])
1029         AC_DEFINE([NONBLOCKING_IS_BROKEN], 1, [Define if the network stack does not fully support nonblocking io (causes lower performance).])
1030 ], [
1031         AC_MSG_RESULT([crosscompile(yes)])
1032 ])
1033 fi
1034 ])dnl End of ACX_CHECK_NONBLOCKING_BROKEN
1035
1036 dnl Check if mkdir has one or two arguments.
1037 dnl defines MKDIR_HAS_ONE_ARG
1038 AC_DEFUN([ACX_MKDIR_ONE_ARG],
1039 [
1040 AC_MSG_CHECKING([whether mkdir has one arg])
1041 AC_TRY_COMPILE([
1042 #include <stdio.h>
1043 #include <unistd.h>
1044 #ifdef HAVE_WINSOCK2_H
1045 #include <winsock2.h>
1046 #endif
1047 #ifdef HAVE_SYS_STAT_H
1048 #include <sys/stat.h>
1049 #endif
1050 ], [
1051         (void)mkdir("directory");
1052 ],
1053 AC_MSG_RESULT(yes)
1054 AC_DEFINE(MKDIR_HAS_ONE_ARG, 1, [Define if mkdir has one argument.])
1055 ,
1056 AC_MSG_RESULT(no)
1057 )
1058 ])dnl end of ACX_MKDIR_ONE_ARG
1059
1060 dnl Check for ioctlsocket function. works on mingw32 too.
1061 AC_DEFUN([ACX_FUNC_IOCTLSOCKET],
1062 [
1063 # check ioctlsocket
1064 AC_MSG_CHECKING(for ioctlsocket)
1065 AC_LINK_IFELSE([AC_LANG_PROGRAM([
1066 #ifdef HAVE_WINSOCK2_H
1067 #include <winsock2.h>
1068 #endif
1069 ], [
1070         (void)ioctlsocket(0, 0, NULL);
1071 ])], [
1072 AC_MSG_RESULT(yes)
1073 AC_DEFINE(HAVE_IOCTLSOCKET, 1, [if the function 'ioctlsocket' is available])
1074 ],[AC_MSG_RESULT(no)])
1075 ])dnl end of ACX_FUNC_IOCTLSOCKET
1076
1077 dnl detect malloc and provide malloc compat prototype.
1078 dnl $1: unique name for compat code
1079 AC_DEFUN([ACX_FUNC_MALLOC],
1080 [
1081         AC_MSG_CHECKING([for GNU libc compatible malloc])
1082         AC_RUN_IFELSE([AC_LANG_PROGRAM(
1083 [[#if defined STDC_HEADERS || defined HAVE_STDLIB_H
1084 #include <stdlib.h>
1085 #else
1086 char *malloc ();
1087 #endif
1088 ]], [ if(malloc(0) != 0) return 1;])
1089 ],
1090         [AC_MSG_RESULT([no])
1091         AC_LIBOBJ(malloc)
1092         AC_DEFINE_UNQUOTED([malloc], [rpl_malloc_$1], [Define if  replacement function should be used.])] ,
1093         [AC_MSG_RESULT([yes])
1094         AC_DEFINE([HAVE_MALLOC], 1, [If have GNU libc compatible malloc])],
1095         [AC_MSG_RESULT([no (crosscompile)])
1096         AC_LIBOBJ(malloc)
1097         AC_DEFINE_UNQUOTED([malloc], [rpl_malloc_$1], [Define if  replacement function should be used.])] )
1098 ])
1099
1100 dnl Define fallback for fseeko and ftello if needed.
1101 AC_DEFUN([AHX_CONFIG_FSEEKO],
1102 [
1103 #ifndef HAVE_FSEEKO
1104 #define fseeko fseek
1105 #define ftello ftell
1106 #endif /* HAVE_FSEEKO */
1107 ])
1108
1109 dnl Define RAND_MAX if not defined
1110 AC_DEFUN([AHX_CONFIG_RAND_MAX],
1111 [
1112 #ifndef RAND_MAX
1113 #define RAND_MAX        2147483647
1114 #endif
1115 ])
1116
1117 dnl Define MAXHOSTNAMELEN if not defined
1118 AC_DEFUN([AHX_CONFIG_MAXHOSTNAMELEN],
1119 [
1120 #ifndef MAXHOSTNAMELEN
1121 #define MAXHOSTNAMELEN 256
1122 #endif
1123 ])
1124
1125 dnl Define IPV6_MIN_MTU if not defined
1126 AC_DEFUN([AHX_CONFIG_IPV6_MIN_MTU],
1127 [
1128 #ifndef IPV6_MIN_MTU
1129 #define IPV6_MIN_MTU 1280
1130 #endif /* IPV6_MIN_MTU */
1131 ])
1132
1133 dnl provide snprintf, vsnprintf compat prototype
1134 dnl $1: unique name for compat code
1135 AC_DEFUN([AHX_CONFIG_SNPRINTF],
1136 [
1137 #ifndef HAVE_SNPRINTF
1138 #define snprintf snprintf_$1
1139 #define vsnprintf vsnprintf_$1
1140 #include <stdarg.h>
1141 int snprintf (char *str, size_t count, const char *fmt, ...);
1142 int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
1143 #endif /* HAVE_SNPRINTF */
1144 ])
1145
1146 dnl provide inet_pton compat prototype.
1147 dnl $1: unique name for compat code
1148 AC_DEFUN([AHX_CONFIG_INET_PTON],
1149 [
1150 #ifndef HAVE_INET_PTON
1151 #define inet_pton inet_pton_$1
1152 int inet_pton(int af, const char* src, void* dst);
1153 #endif /* HAVE_INET_PTON */
1154 ])
1155
1156 dnl provide inet_ntop compat prototype.
1157 dnl $1: unique name for compat code
1158 AC_DEFUN([AHX_CONFIG_INET_NTOP],
1159 [
1160 #ifndef HAVE_INET_NTOP
1161 #define inet_ntop inet_ntop_$1
1162 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
1163 #endif
1164 ])
1165
1166 dnl provide inet_aton compat prototype.
1167 dnl $1: unique name for compat code
1168 AC_DEFUN([AHX_CONFIG_INET_ATON],
1169 [
1170 #ifndef HAVE_INET_ATON
1171 #define inet_aton inet_aton_$1
1172 int inet_aton(const char *cp, struct in_addr *addr);
1173 #endif
1174 ])
1175
1176 dnl provide memmove compat prototype.
1177 dnl $1: unique name for compat code
1178 AC_DEFUN([AHX_CONFIG_MEMMOVE],
1179 [
1180 #ifndef HAVE_MEMMOVE
1181 #define memmove memmove_$1
1182 void *memmove(void *dest, const void *src, size_t n);
1183 #endif
1184 ])
1185
1186 dnl provide strlcat compat prototype.
1187 dnl $1: unique name for compat code
1188 AC_DEFUN([AHX_CONFIG_STRLCAT],
1189 [
1190 #ifndef HAVE_STRLCAT
1191 #define strlcat strlcat_$1
1192 size_t strlcat(char *dst, const char *src, size_t siz);
1193 #endif
1194 ])
1195
1196 dnl provide strlcpy compat prototype.
1197 dnl $1: unique name for compat code
1198 AC_DEFUN([AHX_CONFIG_STRLCPY],
1199 [
1200 #ifndef HAVE_STRLCPY
1201 #define strlcpy strlcpy_$1
1202 size_t strlcpy(char *dst, const char *src, size_t siz);
1203 #endif
1204 ])
1205
1206 dnl provide gmtime_r compat prototype.
1207 dnl $1: unique name for compat code
1208 AC_DEFUN([AHX_CONFIG_GMTIME_R],
1209 [
1210 #ifndef HAVE_GMTIME_R
1211 #define gmtime_r gmtime_r_$1
1212 struct tm *gmtime_r(const time_t *timep, struct tm *result);
1213 #endif
1214 ])
1215
1216 dnl provide reallocarray compat prototype.
1217 dnl $1: unique name for compat code
1218 AC_DEFUN([AHX_CONFIG_REALLOCARRAY],
1219 [
1220 #ifndef HAVE_REALLOCARRAY
1221 #define reallocarray reallocarray$1
1222 void* reallocarray(void *ptr, size_t nmemb, size_t size);
1223 #endif
1224 ])
1225
1226 dnl provide w32 compat definition for sleep
1227 AC_DEFUN([AHX_CONFIG_W32_SLEEP],
1228 [
1229 #if !defined(HAVE_SLEEP) || defined(HAVE_WINDOWS_H)
1230 #define sleep(x) Sleep((x)*1000) /* on win32 */
1231 #endif /* HAVE_SLEEP */
1232 ])
1233
1234 dnl provide w32 compat definition for usleep
1235 AC_DEFUN([AHX_CONFIG_W32_USLEEP],
1236 [
1237 #ifndef HAVE_USLEEP
1238 #define usleep(x) Sleep((x)/1000 + 1) /* on win32 */
1239 #endif /* HAVE_USLEEP */
1240 ])
1241
1242 dnl provide w32 compat definition for random
1243 AC_DEFUN([AHX_CONFIG_W32_RANDOM],
1244 [
1245 #ifndef HAVE_RANDOM
1246 #define random rand /* on win32, for tests only (bad random) */
1247 #endif /* HAVE_RANDOM */
1248 ])
1249
1250 dnl provide w32 compat definition for srandom
1251 AC_DEFUN([AHX_CONFIG_W32_SRANDOM],
1252 [
1253 #ifndef HAVE_SRANDOM
1254 #define srandom(x) srand(x) /* on win32, for tests only (bad random) */
1255 #endif /* HAVE_SRANDOM */
1256 ])
1257
1258 dnl provide w32 compat definition for FD_SET_T
1259 AC_DEFUN([AHX_CONFIG_W32_FD_SET_T],
1260 [
1261 /* detect if we need to cast to unsigned int for FD_SET to avoid warnings */
1262 #ifdef HAVE_WINSOCK2_H
1263 #define FD_SET_T (u_int)
1264 #else
1265 #define FD_SET_T 
1266 #endif
1267 ])
1268
1269 dnl Remove an extension flag from CFLAGS, define replacement to be made.
1270 dnl Used by ACX_STRIP_EXT_FLAGS.
1271 dnl $1: the name of the flag, for example -D_GNU_SOURCE.
1272 AC_DEFUN([ACX_CFLAGS_STRIP],
1273 [
1274   if echo $CFLAGS | grep " $1" >/dev/null 2>&1; then
1275     CFLAGS="`echo $CFLAGS | sed -e 's/ $1//g'`"
1276     AC_DEFINE(m4_bpatsubst(OMITTED_$1,[[-=]],_), 1, Put $1 define in config.h)
1277   fi
1278 ])
1279
1280 dnl Remove EXT flags from the CFLAGS and set them to be defined in config.h
1281 dnl use with ACX_DETERMINE_EXT_FLAGS.
1282 AC_DEFUN([ACX_STRIP_EXT_FLAGS],
1283 [
1284   AC_MSG_NOTICE([Stripping extension flags...])
1285   ACX_CFLAGS_STRIP(-D_GNU_SOURCE)
1286   ACX_CFLAGS_STRIP(-D_BSD_SOURCE)
1287   ACX_CFLAGS_STRIP(-D__EXTENSIONS__)
1288   ACX_CFLAGS_STRIP(-D_POSIX_C_SOURCE=200112)
1289   ACX_CFLAGS_STRIP(-D_XOPEN_SOURCE=600)
1290   ACX_CFLAGS_STRIP(-D_XOPEN_SOURCE_EXTENDED=1)
1291   ACX_CFLAGS_STRIP(-D_ALL_SOURCE)
1292   ACX_CFLAGS_STRIP(-D_LARGEFILE_SOURCE=1)
1293 ]) dnl End of ACX_STRIP_EXT_FLAGS
1294
1295 dnl define one omitted flag for config.h
1296 dnl $1: flag name. -D_GNU_SOURCE
1297 dnl $2: replacement define. _GNU_SOURCE
1298 dnl $3: define value, 1
1299 AC_DEFUN([AHX_CONFIG_FLAG_OMITTED],
1300 [#if defined($1) && !defined($2)
1301 #define $2 $3
1302 [#]endif ])
1303
1304 dnl Wrapper for AHX_CONFIG_FLAG_OMITTED for -D style flags
1305 dnl $1: the -DNAME or -DNAME=value string.
1306 AC_DEFUN([AHX_CONFIG_FLAG_EXT],
1307 [AHX_CONFIG_FLAG_OMITTED(m4_bpatsubst(OMITTED_$1,[[-=]],_),m4_bpatsubst(m4_bpatsubst($1,-D,),=.*$,),m4_if(m4_bregexp($1,=),-1,1,m4_bpatsubst($1,^.*=,)))
1308 ])
1309
1310 dnl config.h part to define omitted cflags, use with ACX_STRIP_EXT_FLAGS.
1311 AC_DEFUN([AHX_CONFIG_EXT_FLAGS],
1312 [AHX_CONFIG_FLAG_EXT(-D_GNU_SOURCE)
1313 AHX_CONFIG_FLAG_EXT(-D_BSD_SOURCE)
1314 AHX_CONFIG_FLAG_EXT(-D__EXTENSIONS__)
1315 AHX_CONFIG_FLAG_EXT(-D_POSIX_C_SOURCE=200112)
1316 AHX_CONFIG_FLAG_EXT(-D_XOPEN_SOURCE=600)
1317 AHX_CONFIG_FLAG_EXT(-D_XOPEN_SOURCE_EXTENDED=1)
1318 AHX_CONFIG_FLAG_EXT(-D_ALL_SOURCE)
1319 AHX_CONFIG_FLAG_EXT(-D_LARGEFILE_SOURCE=1)
1320 ])
1321
1322 dnl check if memcmp is using signed characters and replace if so.
1323 AC_DEFUN([ACX_CHECK_MEMCMP_SIGNED],
1324 [AC_MSG_CHECKING([if memcmp compares unsigned])
1325 AC_RUN_IFELSE([AC_LANG_SOURCE([[
1326 #include <stdio.h>
1327 #include <stdlib.h>
1328 #include <string.h>
1329 int main(void)
1330 {
1331         char a = 255, b = 0;
1332         if(memcmp(&a, &b, 1) < 0)
1333                 return 1;
1334         return 0;
1335 }
1336 ]])], [AC_MSG_RESULT([yes]) ],
1337 [ AC_MSG_RESULT([no])
1338   AC_DEFINE([MEMCMP_IS_BROKEN], [1], [Define if memcmp() does not compare unsigned bytes])
1339   AC_LIBOBJ([memcmp])
1340 ], [ AC_MSG_RESULT([cross-compile no])
1341   AC_DEFINE([MEMCMP_IS_BROKEN], [1], [Define if memcmp() does not compare unsigned bytes])
1342   AC_LIBOBJ([memcmp]) 
1343 ]) ])
1344
1345 dnl define memcmp to its replacement, pass unique id for program as arg
1346 AC_DEFUN([AHX_MEMCMP_BROKEN], [
1347 #ifdef MEMCMP_IS_BROKEN
1348 #include "compat/memcmp.h"
1349 #define memcmp memcmp_$1
1350 int memcmp(const void *x, const void *y, size_t n);
1351 #endif
1352 ])
1353
1354 dnl ACX_CHECK_SS_FAMILY           - check for sockaddr_storage.ss_family
1355 AC_DEFUN([ACX_CHECK_SS_FAMILY],
1356 [AC_CHECK_MEMBER([struct sockaddr_storage.ss_family], [], [
1357         AC_CHECK_MEMBER([struct sockaddr_storage.__ss_family], [
1358                 AC_DEFINE([ss_family], [__ss_family], [Fallback member name for socket family in struct sockaddr_storage])
1359         ],, [AC_INCLUDES_DEFAULT
1360 #ifdef HAVE_NETINET_IN_H
1361 #include <netinet/in.h>
1362 #endif
1363 #ifdef HAVE_SYS_SOCKET_H
1364 #include <sys/socket.h>
1365 #endif
1366 #ifdef HAVE_NETDB_H
1367 #include <netdb.h>
1368 #endif
1369 #ifdef HAVE_ARPA_INET_H
1370 #include <arpa/inet.h>
1371 #endif
1372     ])
1373 ], [AC_INCLUDES_DEFAULT
1374 #ifdef HAVE_NETINET_IN_H
1375 #include <netinet/in.h>
1376 #endif
1377 #ifdef HAVE_SYS_SOCKET_H
1378 #include <sys/socket.h>
1379 #endif
1380 #ifdef HAVE_NETDB_H
1381 #include <netdb.h>
1382 #endif
1383 #ifdef HAVE_ARPA_INET_H
1384 #include <arpa/inet.h>
1385 #endif
1386 ]) ])
1387
1388 dnl Check if CC and linker support -fPIE and -pie.
1389 dnl If so, sets them in CFLAGS / LDFLAGS.
1390 AC_DEFUN([ACX_CHECK_PIE], [
1391     AC_ARG_ENABLE([pie], AS_HELP_STRING([--enable-pie], [Enable Position-Independent Executable (eg. to fully benefit from ASLR, small performance penalty)]))
1392     AS_IF([test "x$enable_pie" = "xyes"], [
1393         AC_MSG_CHECKING([if $CC supports PIE])
1394         BAKLDFLAGS="$LDFLAGS"
1395         BAKCFLAGS="$CFLAGS"
1396         LDFLAGS="$LDFLAGS -pie"
1397         CFLAGS="$CFLAGS -fPIE"
1398         AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [
1399             if $CC $CFLAGS $LDFLAGS -o conftest conftest.c 2>&1 | grep "warning: no debug symbols in executable" >/dev/null; then
1400                 LDFLAGS="$BAKLDFLAGS"
1401                 AC_MSG_RESULT(no)
1402             else
1403                 AC_MSG_RESULT(yes)
1404             fi
1405             rm -f conftest conftest.c conftest.o
1406         ], [LDFLAGS="$BAKLDFLAGS" ; CFLAGS="$BAKCFLAGS" ; AC_MSG_RESULT(no)])
1407     ])
1408 ])
1409
1410 dnl Check if linker supports -Wl,-z,relro,-z,now.
1411 dnl If so, adds it to LDFLAGS.
1412 AC_DEFUN([ACX_CHECK_RELRO_NOW], [
1413     AC_ARG_ENABLE([relro_now], AS_HELP_STRING([--enable-relro-now], [Enable full relocation binding at load-time (RELRO NOW, to protect GOT and .dtor areas)]))
1414     AS_IF([test "x$enable_relro_now" = "xyes"], [
1415         AC_MSG_CHECKING([if $CC supports -Wl,-z,relro,-z,now])
1416         BAKLDFLAGS="$LDFLAGS"
1417         LDFLAGS="$LDFLAGS -Wl,-z,relro,-z,now"
1418         AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [
1419             if $CC $CFLAGS $LDFLAGS -o conftest conftest.c 2>&1 | grep "warning: no debug symbols in executable" >/dev/null; then
1420                 LDFLAGS="$BAKLDFLAGS"
1421                 AC_MSG_RESULT(no)
1422             else
1423                 AC_MSG_RESULT(yes)
1424             fi
1425             rm -f conftest conftest.c conftest.o
1426         ], [LDFLAGS="$BAKLDFLAGS" ; AC_MSG_RESULT(no)])
1427     ])
1428 ])
1429
1430 dnl End of file