tets
[anytun.git] / openvpn / acinclude.m4
blobf164bacb2ffb70a024ea4e94ca296e6f4116f38a
1 dnl Special Autoconf Macros for OpenVPN
3 dnl OPENVPN_ADD_LIBS(LIB)
4 AC_DEFUN([OPENVPN_ADD_LIBS], [
5   LIBS="$1 $LIBS"
6 ])
8 dnl @synopsis AX_EMPTY_ARRAY
9 dnl
10 dnl Define EMPTY_ARRAY_SIZE to be either "0"
11 dnl or "" depending on which syntax the compiler
12 dnl prefers for empty arrays in structs.
13 dnl
14 dnl @version
15 dnl @author James Yonan <jim@yonan.net>
18 AC_DEFUN([AX_EMPTY_ARRAY], [
19   AC_MSG_RESULT([checking for C compiler empty array support])
20   AC_COMPILE_IFELSE(
21     [
22         struct { int foo; int bar[[0]]; } mystruct;
23     ], [
24         AC_DEFINE_UNQUOTED(EMPTY_ARRAY_SIZE, 0, [Dimension to use for empty array declaration])
25     ], [
26         AC_COMPILE_IFELSE(
27             [
28                 struct { int foo; int bar[[]]; } mystruct;
29             ], [
30                 AC_DEFINE_UNQUOTED(EMPTY_ARRAY_SIZE,, [Dimension to use for empty array declaration])
31             ], [
32                 AC_MSG_ERROR([C compiler is unable to creaty empty arrays])
33             ])
34     ])
35   ]
38 dnl @synopsis AX_CPP_VARARG_MACRO_GCC
39 dnl
40 dnl Test if the preprocessor understands GNU GCC-style vararg macros.
41 dnl If it does, defines HAVE_CPP_VARARG_MACRO_GCC to 1.
42 dnl
43 dnl @version
44 dnl @author James Yonan <jim@yonan.net>, Matthias Andree <matthias.andree@web.de>
45 AC_DEFUN([AX_CPP_VARARG_MACRO_GCC], [dnl
46     AS_VAR_PUSHDEF([VAR],[ax_cv_cpp_vararg_macro_gcc])dnl
47     AC_CACHE_CHECK([for GNU GCC vararg macro support], VAR, [dnl
48       AC_COMPILE_IFELSE([
49         #define macro(a, b...) func(a, b)
50         int func(int a, int b, int c);
51         int test() { return macro(1, 2, 3); }
52         ], [ VAR=yes ], [VAR=no])])
53     if test $VAR = yes ; then
54     AC_DEFINE([HAVE_CPP_VARARG_MACRO_GCC], 1, 
55       [Define to 1 if your compiler supports GNU GCC-style variadic macros])
56     fi
57     AS_VAR_POPDEF([VAR])dnl
60 dnl @synopsis AX_CPP_VARARG_MACRO_ISO
61 dnl
62 dnl Test if the preprocessor understands ISO C 1999 vararg macros.
63 dnl If it does, defines HAVE_CPP_VARARG_MACRO_ISO to 1.
64 dnl
65 dnl @version
66 dnl @author James Yonan <jim@yonan.net>, Matthias Andree <matthias.andree@web.de>
67 AC_DEFUN([AX_CPP_VARARG_MACRO_ISO], [dnl
68     AS_VAR_PUSHDEF([VAR],[ax_cv_cpp_vararg_macro_iso])dnl
69     AC_CACHE_CHECK([for ISO C 1999 vararg macro support], VAR, [dnl
70       AC_COMPILE_IFELSE([
71 #define macro(a, ...) func(a, __VA_ARGS__)
72         int func(int a, int b, int c);
73         int test() { return macro(1, 2, 3); }
74         ], [ VAR=yes ], [VAR=no])])
75     if test $VAR = yes ; then
76     AC_DEFINE([HAVE_CPP_VARARG_MACRO_ISO], 1, 
77       [Define to 1 if your compiler supports ISO C99 variadic macros])
78     fi
79     AS_VAR_POPDEF([VAR])dnl
82 dnl -- The following is taken from curl's acinclude.m4 --
83 dnl Check for socklen_t: historically on BSD it is an int, and in
84 dnl POSIX 1g it is a type of its own, but some platforms use different
85 dnl types for the argument to getsockopt, getpeername, etc.  So we
86 dnl have to test to find something that will work.
87 AC_DEFUN([TYPE_SOCKLEN_T],
89    AC_CHECK_TYPE([socklen_t], ,[
90       AC_MSG_CHECKING([for socklen_t equivalent])
91       AC_CACHE_VAL([curl_cv_socklen_t_equiv],
92       [
93          # Systems have either "struct sockaddr *" or
94          # "void *" as the second argument to getpeername
95          curl_cv_socklen_t_equiv=
96          for arg2 in "struct sockaddr" void; do
97             for t in int size_t unsigned long "unsigned long"; do
98                AC_TRY_COMPILE([
99                   #include <sys/types.h>
100                   #include <sys/socket.h>
102                   int getpeername (int, $arg2 *, $t *);
103                ],[
104                   $t len;
105                   getpeername(0,0,&len);
106                ],[
107                   curl_cv_socklen_t_equiv="$t"
108                   break
109                ])
110             done
111          done
113          if test "x$curl_cv_socklen_t_equiv" = x; then
114             AC_MSG_ERROR([Cannot find a type to use in place of socklen_t])
115          fi
116       ])
117       AC_MSG_RESULT($curl_cv_socklen_t_equiv)
118       AC_DEFINE_UNQUOTED(socklen_t, $curl_cv_socklen_t_equiv,
119                         [type to use in place of socklen_t if not defined])],
120       [#include <sys/types.h>
121 #include <sys/socket.h>])
124 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
126 dnl This macro figures out how to build C programs using POSIX
127 dnl threads.  It sets the PTHREAD_LIBS output variable to the threads
128 dnl library and linker flags, and the PTHREAD_CFLAGS output variable
129 dnl to any special C compiler flags that are needed.  (The user can also
130 dnl force certain compiler flags/libs to be tested by setting these
131 dnl environment variables.)
133 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
134 dnl multi-threaded programs (defaults to the value of CC otherwise).
135 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
137 dnl If you are only building threads programs, you may wish to
138 dnl use these variables in your default LIBS, CFLAGS, and CC:
140 dnl        LIBS="$PTHREAD_LIBS $LIBS"
141 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
142 dnl        CC="$PTHREAD_CC"
144 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
145 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE
146 dnl to that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
148 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
149 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands
150 dnl to run it if it is not found.  If ACTION-IF-FOUND is not specified,
151 dnl the default action will define HAVE_PTHREAD.
153 dnl Please let the authors know if this macro fails on any platform,
154 dnl or if you have any other suggestions or comments.  This macro was
155 dnl based on work by SGJ on autoconf scripts for FFTW (www.fftw.org)
156 dnl (with help from M. Frigo), as well as ac_pthread and hb_pthread
157 dnl macros posted by AFC to the autoconf macro repository.  We are also
158 dnl grateful for the helpful feedback of numerous users.
160 dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Alejandro Forero Cuervo <bachue@bachue.com>
162 AC_DEFUN([ACX_PTHREAD], [
163 AC_REQUIRE([AC_CANONICAL_HOST])
164 acx_pthread_ok=no
166 # We used to check for pthread.h first, but this fails if pthread.h
167 # requires special compiler flags (e.g. on True64 or Sequent).
168 # It gets checked for in the link test anyway.
170 # First of all, check if the user has set any of the PTHREAD_LIBS,
171 # etcetera environment variables, and if threads linking works using
172 # them:
173 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
174         save_CFLAGS="$CFLAGS"
175         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
176         save_LIBS="$LIBS"
177         LIBS="$PTHREAD_LIBS $LIBS"
178         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
179         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
180         AC_MSG_RESULT($acx_pthread_ok)
181         if test x"$acx_pthread_ok" = xno; then
182                 PTHREAD_LIBS=""
183                 PTHREAD_CFLAGS=""
184         fi
185         LIBS="$save_LIBS"
186         CFLAGS="$save_CFLAGS"
189 # We must check for the threads library under a number of different
190 # names; the ordering is very important because some systems
191 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
192 # libraries is broken (non-POSIX).
194 # Create a list of thread flags to try.  Items starting with a "-" are
195 # C compiler flags, and other items are library names, except for "none"
196 # which indicates that we try without any flags at all.
198 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt"
200 # The ordering *is* (sometimes) important.  Some notes on the
201 # individual items follow:
203 # pthreads: AIX (must check this before -lpthread)
204 # none: in case threads are in libc; should be tried before -Kthread and
205 #       other compiler flags to prevent continual compiler warnings
206 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
207 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
208 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
209 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
210 # -pthreads: Solaris/gcc
211 # -mthreads: Mingw32/gcc, Lynx/gcc
212 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
213 #      doesn't hurt to check since this sometimes defines pthreads too;
214 #      also defines -D_REENTRANT)
215 # pthread: Linux, etcetera
216 # --thread-safe: KAI C++
218 case "$target" in
219         *solaris*)
221         # On Solaris (at least, for some versions), libc contains stubbed
222         # (non-functional) versions of the pthreads routines, so link-based
223         # tests will erroneously succeed.  (We need to link with -pthread or
224         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
225         # a function called by this macro, so we could check for that, but
226         # who knows whether they'll stub that too in a future libc.)  So,
227         # we'll just look for -pthreads and -lpthread first:
229         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
230         ;;
231 esac
233 if test x"$acx_pthread_ok" = xno; then
234 for flag in $acx_pthread_flags; do
236         case $flag in
237                 none)
238                 AC_MSG_CHECKING([whether pthreads work without any flags])
239                 ;;
241                 -*)
242                 AC_MSG_CHECKING([whether pthreads work with $flag])
243                 PTHREAD_CFLAGS="$flag"
244                 ;;
246                 *)
247                 AC_MSG_CHECKING([for the pthreads library -l$flag])
248                 PTHREAD_LIBS="-l$flag"
249                 ;;
250         esac
252         save_LIBS="$LIBS"
253         save_CFLAGS="$CFLAGS"
254         LIBS="$PTHREAD_LIBS $LIBS"
255         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
257         # Check for various functions.  We must include pthread.h,
258         # since some functions may be macros.  (On the Sequent, we
259         # need a special flag -Kthread to make this header compile.)
260         # We check for pthread_join because it is in -lpthread on IRIX
261         # while pthread_create is in libc.  We check for pthread_attr_init
262         # due to DEC craziness with -lpthreads.  We check for
263         # pthread_cleanup_push because it is one of the few pthread
264         # functions on Solaris that doesn't have a non-functional libc stub.
265         # We try pthread_create on general principles.
266         AC_TRY_LINK([#include <pthread.h>],
267                     [pthread_t th; pthread_join(th, 0);
268                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
269                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
270                     [acx_pthread_ok=yes])
272         LIBS="$save_LIBS"
273         CFLAGS="$save_CFLAGS"
275         AC_MSG_RESULT($acx_pthread_ok)
276         if test "x$acx_pthread_ok" = xyes; then
277                 break;
278         fi
280         PTHREAD_LIBS=""
281         PTHREAD_CFLAGS=""
282 done
285 # Various other checks:
286 if test "x$acx_pthread_ok" = xyes; then
287         save_LIBS="$LIBS"
288         LIBS="$PTHREAD_LIBS $LIBS"
289         save_CFLAGS="$CFLAGS"
290         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
292         # Detect AIX lossage: threads are created detached by default
293         # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
294         AC_MSG_CHECKING([for joinable pthread attribute])
295         AC_TRY_LINK([#include <pthread.h>],
296                     [int attr=PTHREAD_CREATE_JOINABLE;],
297                     ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
298         if test x"$ok" = xunknown; then
299                 AC_TRY_LINK([#include <pthread.h>],
300                             [int attr=PTHREAD_CREATE_UNDETACHED;],
301                             ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
302         fi
303         if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
304                 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
305                           [Define to the necessary symbol if this constant
306                            uses a non-standard name on your system.])
307         fi
308         AC_MSG_RESULT(${ok})
309         if test x"$ok" = xunknown; then
310                 AC_MSG_WARN([we do not know how to create joinable pthreads])
311         fi
313         AC_MSG_CHECKING([if more special flags are required for pthreads])
314         flag=no
315         case "$target" in
316                 *-aix* | *-freebsd*)               flag="-D_THREAD_SAFE";;
317                 *solaris* | alpha*-osf* | *linux*) flag="-D_REENTRANT";;
318         esac
319         AC_MSG_RESULT(${flag})
320         if test "x$flag" != xno; then
321                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
322         fi
324         LIBS="$save_LIBS"
325         CFLAGS="$save_CFLAGS"
327         # More AIX lossage: must compile with cc_r
328         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
329 else
330         PTHREAD_CC="$CC"
333 AC_SUBST(PTHREAD_LIBS)
334 AC_SUBST(PTHREAD_CFLAGS)
335 AC_SUBST(PTHREAD_CC)
337 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
338 if test x"$acx_pthread_ok" = xyes; then
339         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
340         :
341 else
342         acx_pthread_ok=no
343         $2
346 ])dnl ACX_PTHREAD