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