moved over some changes from cvs
[supertux.git] / mk / autoconf / acx_pthread.m4
1 AC_DEFUN([ACX_PTHREAD], [
2 AC_REQUIRE([AC_CANONICAL_HOST])
3 AC_LANG_SAVE
4 AC_LANG_C
5 acx_pthread_ok=no
6
7 # We used to check for pthread.h first, but this fails if pthread.h
8 # requires special compiler flags (e.g. on True64 or Sequent).
9 # It gets checked for in the link test anyway.
10
11 # First of all, check if the user has set any of the PTHREAD_LIBS,
12 # etcetera environment variables, and if threads linking works using
13 # them:
14 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
15         save_CFLAGS="$CFLAGS"
16         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
17         save_LIBS="$LIBS"
18         LIBS="$PTHREAD_LIBS $LIBS"
19         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
20         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
21         AC_MSG_RESULT($acx_pthread_ok)
22         if test x"$acx_pthread_ok" = xno; then
23                 PTHREAD_LIBS=""
24                 PTHREAD_CFLAGS=""
25         fi
26         LIBS="$save_LIBS"
27         CFLAGS="$save_CFLAGS"
28 fi
29
30 # We must check for the threads library under a number of different
31 # names; the ordering is very important because some systems
32 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
33 # libraries is broken (non-POSIX).
34
35 # Create a list of thread flags to try.  Items starting with a "-" are
36 # C compiler flags, and other items are library names, except for "none"
37 # which indicates that we try without any flags at all, and "pthread-config"
38 # which is a program returning the flags for the Pth emulation library.
39
40 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
41
42 # The ordering *is* (sometimes) important.  Some notes on the
43 # individual items follow:
44
45 # pthreads: AIX (must check this before -lpthread)
46 # none: in case threads are in libc; should be tried before -Kthread and
47 #       other compiler flags to prevent continual compiler warnings
48 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
49 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
50 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
51 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
52 # -pthreads: Solaris/gcc
53 # -mthreads: Mingw32/gcc, Lynx/gcc
54 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
55 #      doesn't hurt to check since this sometimes defines pthreads too;
56 #      also defines -D_REENTRANT)
57 # pthread: Linux, etcetera
58 # --thread-safe: KAI C++
59 # pthread-config: use pthread-config program (for GNU Pth library)
60
61 case "${host_cpu}-${host_os}" in
62         *solaris*)
63
64         # On Solaris (at least, for some versions), libc contains stubbed
65         # (non-functional) versions of the pthreads routines, so link-based
66         # tests will erroneously succeed.  (We need to link with -pthread or
67         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
68         # a function called by this macro, so we could check for that, but
69         # who knows whether they'll stub that too in a future libc.)  So,
70         # we'll just look for -pthreads and -lpthread first:
71
72         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
73         ;;
74 esac
75
76 if test x"$acx_pthread_ok" = xno; then
77 for flag in $acx_pthread_flags; do
78
79         case $flag in
80                 none)
81                 AC_MSG_CHECKING([whether pthreads work without any flags])
82                 ;;
83
84                 -*)
85                 AC_MSG_CHECKING([whether pthreads work with $flag])
86                 PTHREAD_CFLAGS="$flag"
87                 ;;
88
89                 pthread-config)
90                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
91                 if test x"$acx_pthread_config" = xno; then continue; fi
92                 PTHREAD_CFLAGS="`pthread-config --cflags`"
93                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
94                 ;;
95
96                 *)
97                 AC_MSG_CHECKING([for the pthreads library -l$flag])
98                 PTHREAD_LIBS="-l$flag"
99                 ;;
100         esac
101
102         save_LIBS="$LIBS"
103         save_CFLAGS="$CFLAGS"
104         LIBS="$PTHREAD_LIBS $LIBS"
105         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
106
107         # Check for various functions.  We must include pthread.h,
108         # since some functions may be macros.  (On the Sequent, we
109         # need a special flag -Kthread to make this header compile.)
110         # We check for pthread_join because it is in -lpthread on IRIX
111         # while pthread_create is in libc.  We check for pthread_attr_init
112         # due to DEC craziness with -lpthreads.  We check for
113         # pthread_cleanup_push because it is one of the few pthread
114         # functions on Solaris that doesn't have a non-functional libc stub.
115         # We try pthread_create on general principles.
116         AC_TRY_LINK([#include <pthread.h>],
117                     [pthread_t th; pthread_join(th, 0);
118                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
119                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
120                     [acx_pthread_ok=yes])
121
122         LIBS="$save_LIBS"
123         CFLAGS="$save_CFLAGS"
124
125         AC_MSG_RESULT($acx_pthread_ok)
126         if test "x$acx_pthread_ok" = xyes; then
127                 break;
128         fi
129
130         PTHREAD_LIBS=""
131         PTHREAD_CFLAGS=""
132 done
133 fi
134
135 # Various other checks:
136 if test "x$acx_pthread_ok" = xyes; then
137         save_LIBS="$LIBS"
138         LIBS="$PTHREAD_LIBS $LIBS"
139         save_CFLAGS="$CFLAGS"
140         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
141
142         # Detect AIX lossage: threads are created detached by default
143         # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
144         AC_MSG_CHECKING([for joinable pthread attribute])
145         AC_TRY_LINK([#include <pthread.h>],
146                     [int attr=PTHREAD_CREATE_JOINABLE;],
147                     ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
148         if test x"$ok" = xunknown; then
149                 AC_TRY_LINK([#include <pthread.h>],
150                             [int attr=PTHREAD_CREATE_UNDETACHED;],
151                             ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
152         fi
153         if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
154                 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
155                           [Define to the necessary symbol if this constant
156                            uses a non-standard name on your system.])
157         fi
158         AC_MSG_RESULT(${ok})
159         if test x"$ok" = xunknown; then
160                 AC_MSG_WARN([we do not know how to create joinable pthreads])
161         fi
162
163         AC_MSG_CHECKING([if more special flags are required for pthreads])
164         flag=no
165         case "${host_cpu}-${host_os}" in
166                 *-aix* | *-freebsd*)     flag="-D_THREAD_SAFE";;
167                 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
168         esac
169         AC_MSG_RESULT(${flag})
170         if test "x$flag" != xno; then
171                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
172         fi
173
174         LIBS="$save_LIBS"
175         CFLAGS="$save_CFLAGS"
176
177         # More AIX lossage: must compile with cc_r
178         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
179 else
180         PTHREAD_CC="$CC"
181 fi
182
183 AC_SUBST(PTHREAD_LIBS)
184 AC_SUBST(PTHREAD_CFLAGS)
185 AC_SUBST(PTHREAD_CC)
186
187 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
188 if test x"$acx_pthread_ok" = xyes; then
189         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
190         :
191 else
192         acx_pthread_ok=no
193         $2
194 fi
195 AC_LANG_RESTORE
196 ])dnl ACX_PTHREAD