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