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