and added files
[bcm963xx.git] / userapps / opensource / siproxd / acinclude.m4
1 dnl --------------------------------------------------------------------
2 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
3 dnl
4 dnl This macro figures out how to build C programs using POSIX
5 dnl threads.  It sets the PTHREAD_LIBS output variable to the threads
6 dnl library and linker flags, and the PTHREAD_CFLAGS output variable
7 dnl to any special C compiler flags that are needed.  (The user can also
8 dnl force certain compiler flags/libs to be tested by setting these
9 dnl environment variables.)
10 dnl
11 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
12 dnl multi-threaded programs (defaults to the value of CC otherwise).
13 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
14 dnl
15 dnl If you are only building threads programs, you may wish to
16 dnl use these variables in your default LIBS, CFLAGS, and CC:
17 dnl
18 dnl        LIBS="$PTHREAD_LIBS $LIBS"
19 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
20 dnl        CC="$PTHREAD_CC"
21 dnl
22 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
23 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE
24 dnl to that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
25 dnl
26 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
27 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands
28 dnl to run it if it is not found.  If ACTION-IF-FOUND is not specified,
29 dnl the default action will define HAVE_PTHREAD.
30 dnl
31 dnl Please let the authors know if this macro fails on any platform,
32 dnl or if you have any other suggestions or comments.  This macro was
33 dnl based on work by SGJ on autoconf scripts for FFTW (www.fftw.org)
34 dnl (with help from M. Frigo), as well as ac_pthread and hb_pthread
35 dnl macros posted by AFC to the autoconf macro repository.  We are also
36 dnl grateful for the helpful feedback of numerous users.
37 dnl
38 dnl @version $Id: acinclude.m4,v 1.6 2004/11/03 21:23:32 hb9xar Exp $
39 dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Alejandro Forero Cuervo <bachue@bachue.com>
40
41 AC_DEFUN([ACX_PTHREAD], [
42 AC_REQUIRE([AC_CANONICAL_HOST])
43 acx_pthread_ok=no
44
45 # We used to check for pthread.h first, but this fails if pthread.h
46 # requires special compiler flags (e.g. on True64 or Sequent).
47 # It gets checked for in the link test anyway.
48
49 # First of all, check if the user has set any of the PTHREAD_LIBS,
50 # etcetera environment variables, and if threads linking works using
51 # them:
52 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
53         save_CFLAGS="$CFLAGS"
54         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
55         save_LIBS="$LIBS"
56         LIBS="$PTHREAD_LIBS $LIBS"
57         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
58         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
59         AC_MSG_RESULT($acx_pthread_ok)
60         if test x"$acx_pthread_ok" = xno; then
61                 PTHREAD_LIBS=""
62                 PTHREAD_CFLAGS=""
63         fi
64         LIBS="$save_LIBS"
65         CFLAGS="$save_CFLAGS"
66 fi
67
68 # We must check for the threads library under a number of different
69 # names; the ordering is very important because some systems
70 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
71 # libraries is broken (non-POSIX).
72
73 # Create a list of thread flags to try.  Items starting with a "-" are
74 # C compiler flags, and other items are library names, except for "none"
75 # which indicates that we try without any flags at all.
76
77 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt"
78
79 # The ordering *is* (sometimes) important.  Some notes on the
80 # individual items follow:
81
82 # pthreads: AIX (must check this before -lpthread)
83 # none: in case threads are in libc; should be tried before -Kthread and
84 #       other compiler flags to prevent continual compiler warnings
85 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
86 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
87 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
88 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
89 # -pthreads: Solaris/gcc
90 # -mthreads: Mingw32/gcc, Lynx/gcc
91 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
92 #      doesn't hurt to check since this sometimes defines pthreads too;
93 #      also defines -D_REENTRANT)
94 # pthread: Linux, etcetera
95 # --thread-safe: KAI C++
96
97 case "${host_cpu}-${host_os}" in
98         *solaris*)
99
100         # On Solaris (at least, for some versions), libc contains stubbed
101         # (non-functional) versions of the pthreads routines, so link-based
102         # tests will erroneously succeed.  (We need to link with -pthread or
103         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
104         # a function called by this macro, so we could check for that, but
105         # who knows whether they'll stub that too in a future libc.)  So,
106         # we'll just look for -pthreads and -lpthread first:
107
108         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
109         ;;
110 esac
111
112 if test x"$acx_pthread_ok" = xno; then
113 for flag in $acx_pthread_flags; do
114
115         case $flag in
116                 none)
117                 AC_MSG_CHECKING([whether pthreads work without any flags])
118                 ;;
119
120                 -*)
121                 AC_MSG_CHECKING([whether pthreads work with $flag])
122                 PTHREAD_CFLAGS="$flag"
123                 ;;
124
125                 *)
126                 AC_MSG_CHECKING([for the pthreads library -l$flag])
127                 PTHREAD_LIBS="-l$flag"
128                 ;;
129         esac
130
131         save_LIBS="$LIBS"
132         save_CFLAGS="$CFLAGS"
133         LIBS="$PTHREAD_LIBS $LIBS"
134         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
135
136         # Check for various functions.  We must include pthread.h,
137         # since some functions may be macros.  (On the Sequent, we
138         # need a special flag -Kthread to make this header compile.)
139         # We check for pthread_join because it is in -lpthread on IRIX
140         # while pthread_create is in libc.  We check for pthread_attr_init
141         # due to DEC craziness with -lpthreads.  We check for
142         # pthread_cleanup_push because it is one of the few pthread
143         # functions on Solaris that doesn't have a non-functional libc stub.
144         # We try pthread_create on general principles.
145         AC_TRY_LINK([#include <pthread.h>],
146                     [pthread_t th; pthread_join(th, 0);
147                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
148                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
149                     [acx_pthread_ok=yes])
150
151         LIBS="$save_LIBS"
152         CFLAGS="$save_CFLAGS"
153
154         AC_MSG_RESULT($acx_pthread_ok)
155         if test "x$acx_pthread_ok" = xyes; then
156                 break;
157         fi
158
159         PTHREAD_LIBS=""
160         PTHREAD_CFLAGS=""
161 done
162 fi
163
164 # Various other checks:
165 if test "x$acx_pthread_ok" = xyes; then
166         save_LIBS="$LIBS"
167         LIBS="$PTHREAD_LIBS $LIBS"
168         save_CFLAGS="$CFLAGS"
169         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
170
171         # Detect AIX lossage: threads are created detached by default
172         # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
173         AC_MSG_CHECKING([for joinable pthread attribute])
174         AC_TRY_LINK([#include <pthread.h>],
175                     [int attr=PTHREAD_CREATE_JOINABLE;],
176                     ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
177         if test x"$ok" = xunknown; then
178                 AC_TRY_LINK([#include <pthread.h>],
179                             [int attr=PTHREAD_CREATE_UNDETACHED;],
180                             ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
181         fi
182         if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
183                 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
184                           [Define to the necessary symbol if this constant
185                            uses a non-standard name on your system.])
186         fi
187         AC_MSG_RESULT(${ok})
188         if test x"$ok" = xunknown; then
189                 AC_MSG_WARN([we do not know how to create joinable pthreads])
190         fi
191
192         AC_MSG_CHECKING([if more special flags are required for pthreads])
193         flag=no
194         case "${host_cpu}-${host_os}" in
195                 *-aix* | *-freebsd*)     flag="-D_THREAD_SAFE";;
196                 *solaris* | alpha*-osf*) flag="-D_REENTRANT";;
197         esac
198         AC_MSG_RESULT(${flag})
199         if test "x$flag" != xno; then
200                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
201         fi
202
203         LIBS="$save_LIBS"
204         CFLAGS="$save_CFLAGS"
205
206         # More AIX lossage: must compile with cc_r
207         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
208 else
209         PTHREAD_CC="$CC"
210 fi
211
212 AC_SUBST(PTHREAD_LIBS)
213 AC_SUBST(PTHREAD_CFLAGS)
214 AC_SUBST(PTHREAD_CC)
215
216 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
217 if test x"$acx_pthread_ok" = xyes; then
218         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
219         :
220 else
221         acx_pthread_ok=no
222         $2
223 fi
224
225 ])dnl ACX_PTHREAD
226
227 dnl @synopsis ACX_WHICH_GETHOSTBYNAME_R
228 dnl
229 dnl Provides a test to determine the correct 
230 dnl way to call gethostbyname_r
231 dnl
232 dnl defines HAVE_FUNC_GETHOSTBYNAME_R_6 if it needs 6 arguments (e.g linux)
233 dnl defines HAVE_FUNC_GETHOSTBYNAME_R_5 if it needs 5 arguments (e.g. solaris)
234 dnl defines HAVE_FUNC_GETHOSTBYNAME_R_3 if it needs 3 arguments (e.g. osf/1)
235 dnl
236 dnl if used in conjunction in gethostname.c the api demonstrated
237 dnl in test.c can be used regardless of which gethostbyname_r 
238 dnl exists. These example files found at
239 dnl http://www.csn.ul.ie/~caolan/publink/gethostbyname_r
240 dnl
241 dnl @version $Id: acinclude.m4,v 1.6 2004/11/03 21:23:32 hb9xar Exp $
242 dnl @author Caolan McNamara <caolan@skynet.ie>
243 dnl
244 dnl based on David Arnold's autoconf suggestion in the threads faq
245 dnl
246 AC_DEFUN([ACX_WHICH_GETHOSTBYNAME_R], [
247
248 AC_CHECK_FUNC(gethostbyname_r, [
249   AC_MSG_CHECKING(how many arguments takes gethostbyname_r)
250   AC_TRY_COMPILE([
251         #include <stdlib.h>
252         #include <netdb.h>], [
253         char *name;
254         struct hostent *he;
255         struct hostent_data data;
256         (void) gethostbyname_r(name, he, &data);
257   ],acx_which_gethostname_r=three, [
258 dnl
259     AC_TRY_COMPILE([
260         #include <stdlib.h>
261         #include <netdb.h>], [
262         char *name;
263         struct hostent *he, *res;
264         char buffer[2048];
265         int buflen = 2048;
266         int h_errnop;
267         (void) gethostbyname_r(name, he, buffer, buflen, &res, &h_errnop)
268     ],acx_which_gethostname_r=six, [
269 dnl
270       AC_TRY_COMPILE([
271         #include <stdlib.h>
272         #include <netdb.h>], [
273         char *name;
274         struct hostent *he;
275         char buffer[2048];
276         int buflen = 2048;
277         int h_errnop;
278         (void) gethostbyname_r(name, he, buffer, buflen, &h_errnop)
279       ],acx_which_gethostname_r=five,acx_which_gethostname_r=no)
280
281     ])
282   ])
283 ], acx_which_gethostname_r=no)
284
285 if test $acx_which_gethostname_r = six; then
286   AC_DEFINE(HAVE_FUNC_GETHOSTBYNAME_R_6,,[gethostbyname_r takes 6 arguments])
287 elif test $acx_which_gethostname_r = five; then
288   AC_DEFINE(HAVE_FUNC_GETHOSTBYNAME_R_5,,[gethostbyname_r takes 5 arguments])
289 elif test $acx_which_gethostname_r = three; then
290   AC_DEFINE(HAVE_FUNC_GETHOSTBYNAME_R_3,,[gethostbyname_r takes 3 arguments])
291
292 fi
293
294 AC_MSG_RESULT($acx_which_gethostname_r)
295
296 ])dnl ACX_WHICH_GETHOSTBYNAME_R
297