]> git.donarmstrong.com Git - deb_pkgs/spamass-milter.git/blob - ac_acx_pthread.m4
[svn-inject] Installing original source of spamass-milter
[deb_pkgs/spamass-milter.git] / ac_acx_pthread.m4
1 dnl BEGIN http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html
2 dnl $Id: ac_acx_pthread.m4,v 1.3 2004/09/07 01:02:58 dnelson Exp $
3 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
4 dnl
5 dnl This macro figures out how to build C programs using POSIX
6 dnl threads.  It sets the PTHREAD_LIBS output variable to the threads
7 dnl library and linker flags, and the PTHREAD_CFLAGS output variable
8 dnl to any special C compiler flags that are needed.  (The user can also
9 dnl force certain compiler flags/libs to be tested by setting these
10 dnl environment variables.)
11 dnl
12 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
13 dnl multi-threaded programs (defaults to the value of CC otherwise).
14 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
15 dnl
16 dnl If you are only building threads programs, you may wish to
17 dnl use these variables in your default LIBS, CFLAGS, and CC:
18 dnl
19 dnl        LIBS="$PTHREAD_LIBS $LIBS"
20 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
21 dnl        CC="$PTHREAD_CC"
22 dnl
23 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
24 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE
25 dnl to that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
26 dnl
27 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
28 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands
29 dnl to run it if it is not found.  If ACTION-IF-FOUND is not specified,
30 dnl the default action will define HAVE_PTHREAD.
31 dnl
32 dnl Please let the authors know if this macro fails on any platform,
33 dnl or if you have any other suggestions or comments.  This macro was
34 dnl based on work by SGJ on autoconf scripts for FFTW (www.fftw.org)
35 dnl (with help from M. Frigo), as well as ac_pthread and hb_pthread
36 dnl macros posted by AFC to the autoconf macro repository.  We are also
37 dnl grateful for the helpful feedback of numerous users.
38 dnl
39 dnl @version Id: acx_pthread.m4,v 1.10 2004-06-16
40 dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Alejandro Forero Cuervo <bachue@bachue.com>
41
42 AC_DEFUN([ACX_PTHREAD], [
43 AC_REQUIRE([AC_CANONICAL_HOST])
44 AC_LANG_SAVE
45 AC_LANG_C
46 acx_pthread_ok=no
47
48 # We used to check for pthread.h first, but this fails if pthread.h
49 # requires special compiler flags (e.g. on True64 or Sequent).
50 # It gets checked for in the link test anyway.
51
52 # First of all, check if the user has set any of the PTHREAD_LIBS,
53 # etcetera environment variables, and if threads linking works using
54 # them:
55 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
56         save_CFLAGS="$CFLAGS"
57         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
58         save_LIBS="$LIBS"
59         LIBS="$PTHREAD_LIBS $LIBS"
60         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
61         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
62         AC_MSG_RESULT($acx_pthread_ok)
63         if test x"$acx_pthread_ok" = xno; then
64                 PTHREAD_LIBS=""
65                 PTHREAD_CFLAGS=""
66         fi
67         LIBS="$save_LIBS"
68         CFLAGS="$save_CFLAGS"
69 fi
70
71 # We must check for the threads library under a number of different
72 # names; the ordering is very important because some systems
73 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
74 # libraries is broken (non-POSIX).
75
76 # Create a list of thread flags to try.  Items starting with a "-" are
77 # C compiler flags, and other items are library names, except for "none"
78 # which indicates that we try without any flags at all, and "pthread-config"
79 # which is a program returning the flags for the Pth emulation library.
80
81 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
82
83 # The ordering *is* (sometimes) important.  Some notes on the
84 # individual items follow:
85
86 # pthreads: AIX (must check this before -lpthread)
87 # none: in case threads are in libc; should be tried before -Kthread and
88 #       other compiler flags to prevent continual compiler warnings
89 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
90 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
91 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
92 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
93 # -pthreads: Solaris/gcc
94 # -mthreads: Mingw32/gcc, Lynx/gcc
95 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
96 #      doesn't hurt to check since this sometimes defines pthreads too;
97 #      also defines -D_REENTRANT)
98 # pthread: Linux, etcetera
99 # --thread-safe: KAI C++
100 # pthread-config: use pthread-config program (for GNU Pth library)
101
102 case "${host_cpu}-${host_os}" in
103         *solaris*)
104
105         # On Solaris (at least, for some versions), libc contains stubbed
106         # (non-functional) versions of the pthreads routines, so link-based
107         # tests will erroneously succeed.  (We need to link with -pthread or
108         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
109         # a function called by this macro, so we could check for that, but
110         # who knows whether they'll stub that too in a future libc.)  So,
111         # we'll just look for -pthreads and -lpthread first:
112
113         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
114         ;;
115 esac
116
117 if test x"$acx_pthread_ok" = xno; then
118 for flag in $acx_pthread_flags; do
119
120         case $flag in
121                 none)
122                 AC_MSG_CHECKING([whether pthreads work without any flags])
123                 ;;
124
125                 -*)
126                 AC_MSG_CHECKING([whether pthreads work with $flag])
127                 PTHREAD_CFLAGS="$flag"
128                 ;;
129
130                 pthread-config)
131                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
132                 if test x"$acx_pthread_config" = xno; then continue; fi
133                 PTHREAD_CFLAGS="`pthread-config --cflags`"
134                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
135                 ;;
136
137                 *)
138                 AC_MSG_CHECKING([for the pthreads library -l$flag])
139                 PTHREAD_LIBS="-l$flag"
140                 ;;
141         esac
142
143         save_LIBS="$LIBS"
144         save_CFLAGS="$CFLAGS"
145         LIBS="$PTHREAD_LIBS $LIBS"
146         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
147
148         # Check for various functions.  We must include pthread.h,
149         # since some functions may be macros.  (On the Sequent, we
150         # need a special flag -Kthread to make this header compile.)
151         # We check for pthread_join because it is in -lpthread on IRIX
152         # while pthread_create is in libc.  We check for pthread_attr_init
153         # due to DEC craziness with -lpthreads.  We check for
154         # pthread_cleanup_push because it is one of the few pthread
155         # functions on Solaris that doesn't have a non-functional libc stub.
156         # We try pthread_create on general principles.
157         AC_TRY_LINK([#include <pthread.h>],
158                     [pthread_t th; pthread_join(th, 0);
159                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
160                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
161                     [acx_pthread_ok=yes])
162
163         LIBS="$save_LIBS"
164         CFLAGS="$save_CFLAGS"
165
166         AC_MSG_RESULT($acx_pthread_ok)
167         if test "x$acx_pthread_ok" = xyes; then
168                 break;
169         fi
170
171         PTHREAD_LIBS=""
172         PTHREAD_CFLAGS=""
173 done
174 fi
175
176 # Various other checks:
177 if test "x$acx_pthread_ok" = xyes; then
178         save_LIBS="$LIBS"
179         LIBS="$PTHREAD_LIBS $LIBS"
180         save_CFLAGS="$CFLAGS"
181         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
182
183         # Detect AIX lossage: threads are created detached by default
184         # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
185         AC_MSG_CHECKING([for joinable pthread attribute])
186         AC_TRY_LINK([#include <pthread.h>],
187                     [int attr=PTHREAD_CREATE_JOINABLE;],
188                     ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
189         if test x"$ok" = xunknown; then
190                 AC_TRY_LINK([#include <pthread.h>],
191                             [int attr=PTHREAD_CREATE_UNDETACHED;],
192                             ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
193         fi
194         if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
195                 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
196                           [Define to the necessary symbol if this constant
197                            uses a non-standard name on your system.])
198         fi
199         AC_MSG_RESULT(${ok})
200         if test x"$ok" = xunknown; then
201                 AC_MSG_WARN([we do not know how to create joinable pthreads])
202         fi
203
204         AC_MSG_CHECKING([if more special flags are required for pthreads])
205         flag=no
206         case "${host_cpu}-${host_os}" in
207                 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
208                 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
209         esac
210         AC_MSG_RESULT(${flag})
211         if test "x$flag" != xno; then
212                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
213         fi
214
215         LIBS="$save_LIBS"
216         CFLAGS="$save_CFLAGS"
217
218         # More AIX lossage: must compile with cc_r
219         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
220 else
221         PTHREAD_CC="$CC"
222 fi
223
224 AC_SUBST(PTHREAD_LIBS)
225 AC_SUBST(PTHREAD_CFLAGS)
226 AC_SUBST(PTHREAD_CC)
227
228 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
229 if test x"$acx_pthread_ok" = xyes; then
230         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
231         :
232 else
233         acx_pthread_ok=no
234         $2
235 fi
236 AC_LANG_RESTORE
237 ])dnl ACX_PTHREAD
238 dnl END http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html