Initial check-in.
[shibboleth/resolver.git] / acx_pthread.m4
1 ##### http://autoconf-archive.cryp.to/acx_pthread.html\r
2 #\r
3 # SYNOPSIS\r
4 #\r
5 #   ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])\r
6 #\r
7 # DESCRIPTION\r
8 #\r
9 #   This macro figures out how to build C programs using POSIX threads.\r
10 #   It sets the PTHREAD_LIBS output variable to the threads library and\r
11 #   linker flags, and the PTHREAD_CFLAGS output variable to any special\r
12 #   C compiler flags that are needed. (The user can also force certain\r
13 #   compiler flags/libs to be tested by setting these environment\r
14 #   variables.)\r
15 #\r
16 #   Also sets PTHREAD_CC to any special C compiler that is needed for\r
17 #   multi-threaded programs (defaults to the value of CC otherwise).\r
18 #   (This is necessary on AIX to use the special cc_r compiler alias.)\r
19 #\r
20 #   NOTE: You are assumed to not only compile your program with these\r
21 #   flags, but also link it with them as well. e.g. you should link\r
22 #   with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS\r
23 #   $LIBS\r
24 #\r
25 #   If you are only building threads programs, you may wish to use\r
26 #   these variables in your default LIBS, CFLAGS, and CC:\r
27 #\r
28 #          LIBS="$PTHREAD_LIBS $LIBS"\r
29 #          CFLAGS="$CFLAGS $PTHREAD_CFLAGS"\r
30 #          CC="$PTHREAD_CC"\r
31 #\r
32 #   In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute\r
33 #   constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to\r
34 #   that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).\r
35 #\r
36 #   ACTION-IF-FOUND is a list of shell commands to run if a threads\r
37 #   library is found, and ACTION-IF-NOT-FOUND is a list of commands to\r
38 #   run it if it is not found. If ACTION-IF-FOUND is not specified, the\r
39 #   default action will define HAVE_PTHREAD.\r
40 #\r
41 #   Please let the authors know if this macro fails on any platform, or\r
42 #   if you have any other suggestions or comments. This macro was based\r
43 #   on work by SGJ on autoconf scripts for FFTW (http://www.fftw.org/)\r
44 #   (with help from M. Frigo), as well as ac_pthread and hb_pthread\r
45 #   macros posted by Alejandro Forero Cuervo to the autoconf macro\r
46 #   repository. We are also grateful for the helpful feedback of\r
47 #   numerous users.\r
48 #\r
49 # LAST MODIFICATION\r
50 #\r
51 #   2006-05-29\r
52 #\r
53 # COPYLEFT\r
54 #\r
55 #   Copyright (c) 2006 Steven G. Johnson <stevenj@alum.mit.edu>\r
56 #\r
57 #   This program is free software; you can redistribute it and/or\r
58 #   modify it under the terms of the GNU General Public License as\r
59 #   published by the Free Software Foundation; either version 2 of the\r
60 #   License, or (at your option) any later version.\r
61 #\r
62 #   This program is distributed in the hope that it will be useful, but\r
63 #   WITHOUT ANY WARRANTY; without even the implied warranty of\r
64 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
65 #   General Public License for more details.\r
66 #\r
67 #   You should have received a copy of the GNU General Public License\r
68 #   along with this program; if not, write to the Free Software\r
69 #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA\r
70 #   02111-1307, USA.\r
71 #\r
72 #   As a special exception, the respective Autoconf Macro's copyright\r
73 #   owner gives unlimited permission to copy, distribute and modify the\r
74 #   configure scripts that are the output of Autoconf when processing\r
75 #   the Macro. You need not follow the terms of the GNU General Public\r
76 #   License when using or distributing such scripts, even though\r
77 #   portions of the text of the Macro appear in them. The GNU General\r
78 #   Public License (GPL) does govern all other use of the material that\r
79 #   constitutes the Autoconf Macro.\r
80 #\r
81 #   This special exception to the GPL applies to versions of the\r
82 #   Autoconf Macro released by the Autoconf Macro Archive. When you\r
83 #   make and distribute a modified version of the Autoconf Macro, you\r
84 #   may extend this special exception to the GPL to apply to your\r
85 #   modified version as well.\r
86 \r
87 AC_DEFUN([ACX_PTHREAD], [\r
88 AC_REQUIRE([AC_CANONICAL_HOST])\r
89 AC_LANG_SAVE\r
90 AC_LANG_C\r
91 acx_pthread_ok=no\r
92 \r
93 # We used to check for pthread.h first, but this fails if pthread.h\r
94 # requires special compiler flags (e.g. on True64 or Sequent).\r
95 # It gets checked for in the link test anyway.\r
96 \r
97 # First of all, check if the user has set any of the PTHREAD_LIBS,\r
98 # etcetera environment variables, and if threads linking works using\r
99 # them:\r
100 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then\r
101         save_CFLAGS="$CFLAGS"\r
102         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"\r
103         save_LIBS="$LIBS"\r
104         LIBS="$PTHREAD_LIBS $LIBS"\r
105         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])\r
106         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)\r
107         AC_MSG_RESULT($acx_pthread_ok)\r
108         if test x"$acx_pthread_ok" = xno; then\r
109                 PTHREAD_LIBS=""\r
110                 PTHREAD_CFLAGS=""\r
111         fi\r
112         LIBS="$save_LIBS"\r
113         CFLAGS="$save_CFLAGS"\r
114 fi\r
115 \r
116 # We must check for the threads library under a number of different\r
117 # names; the ordering is very important because some systems\r
118 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the\r
119 # libraries is broken (non-POSIX).\r
120 \r
121 # Create a list of thread flags to try.  Items starting with a "-" are\r
122 # C compiler flags, and other items are library names, except for "none"\r
123 # which indicates that we try without any flags at all, and "pthread-config"\r
124 # which is a program returning the flags for the Pth emulation library.\r
125 \r
126 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"\r
127 \r
128 # The ordering *is* (sometimes) important.  Some notes on the\r
129 # individual items follow:\r
130 \r
131 # pthreads: AIX (must check this before -lpthread)\r
132 # none: in case threads are in libc; should be tried before -Kthread and\r
133 #       other compiler flags to prevent continual compiler warnings\r
134 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)\r
135 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)\r
136 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)\r
137 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)\r
138 # -pthreads: Solaris/gcc\r
139 # -mthreads: Mingw32/gcc, Lynx/gcc\r
140 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it\r
141 #      doesn't hurt to check since this sometimes defines pthreads too;\r
142 #      also defines -D_REENTRANT)\r
143 #      ... -mt is also the pthreads flag for HP/aCC\r
144 # pthread: Linux, etcetera\r
145 # --thread-safe: KAI C++\r
146 # pthread-config: use pthread-config program (for GNU Pth library)\r
147 \r
148 case "${host_cpu}-${host_os}" in\r
149         *solaris*)\r
150 \r
151         # On Solaris (at least, for some versions), libc contains stubbed\r
152         # (non-functional) versions of the pthreads routines, so link-based\r
153         # tests will erroneously succeed.  (We need to link with -pthreads/-mt/\r
154         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather\r
155         # a function called by this macro, so we could check for that, but\r
156         # who knows whether they'll stub that too in a future libc.)  So,\r
157         # we'll just look for -pthreads and -lpthread first:\r
158 \r
159         acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"\r
160         ;;\r
161 esac\r
162 \r
163 if test x"$acx_pthread_ok" = xno; then\r
164 for flag in $acx_pthread_flags; do\r
165 \r
166         case $flag in\r
167                 none)\r
168                 AC_MSG_CHECKING([whether pthreads work without any flags])\r
169                 ;;\r
170 \r
171                 -*)\r
172                 AC_MSG_CHECKING([whether pthreads work with $flag])\r
173                 PTHREAD_CFLAGS="$flag"\r
174                 ;;\r
175 \r
176                 pthread-config)\r
177                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)\r
178                 if test x"$acx_pthread_config" = xno; then continue; fi\r
179                 PTHREAD_CFLAGS="`pthread-config --cflags`"\r
180                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"\r
181                 ;;\r
182 \r
183                 *)\r
184                 AC_MSG_CHECKING([for the pthreads library -l$flag])\r
185                 PTHREAD_LIBS="-l$flag"\r
186                 ;;\r
187         esac\r
188 \r
189         save_LIBS="$LIBS"\r
190         save_CFLAGS="$CFLAGS"\r
191         LIBS="$PTHREAD_LIBS $LIBS"\r
192         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"\r
193 \r
194         # Check for various functions.  We must include pthread.h,\r
195         # since some functions may be macros.  (On the Sequent, we\r
196         # need a special flag -Kthread to make this header compile.)\r
197         # We check for pthread_join because it is in -lpthread on IRIX\r
198         # while pthread_create is in libc.  We check for pthread_attr_init\r
199         # due to DEC craziness with -lpthreads.  We check for\r
200         # pthread_cleanup_push because it is one of the few pthread\r
201         # functions on Solaris that doesn't have a non-functional libc stub.\r
202         # We try pthread_create on general principles.\r
203         AC_TRY_LINK([#include <pthread.h>],\r
204                     [pthread_t th; pthread_join(th, 0);\r
205                      pthread_attr_init(0); pthread_cleanup_push(0, 0);\r
206                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],\r
207                     [acx_pthread_ok=yes])\r
208 \r
209         LIBS="$save_LIBS"\r
210         CFLAGS="$save_CFLAGS"\r
211 \r
212         AC_MSG_RESULT($acx_pthread_ok)\r
213         if test "x$acx_pthread_ok" = xyes; then\r
214                 break;\r
215         fi\r
216 \r
217         PTHREAD_LIBS=""\r
218         PTHREAD_CFLAGS=""\r
219 done\r
220 fi\r
221 \r
222 # Various other checks:\r
223 if test "x$acx_pthread_ok" = xyes; then\r
224         save_LIBS="$LIBS"\r
225         LIBS="$PTHREAD_LIBS $LIBS"\r
226         save_CFLAGS="$CFLAGS"\r
227         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"\r
228 \r
229         # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.\r
230         AC_MSG_CHECKING([for joinable pthread attribute])\r
231         attr_name=unknown\r
232         for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do\r
233             AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],\r
234                         [attr_name=$attr; break])\r
235         done\r
236         AC_MSG_RESULT($attr_name)\r
237         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then\r
238             AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,\r
239                                [Define to necessary symbol if this constant\r
240                                 uses a non-standard name on your system.])\r
241         fi\r
242 \r
243         AC_MSG_CHECKING([if more special flags are required for pthreads])\r
244         flag=no\r
245         case "${host_cpu}-${host_os}" in\r
246                 *-aix* | *-freebsd*)     flag="-D_THREAD_SAFE";;\r
247                 *-osf* | *-hpux*) flag="-D_REENTRANT";;\r
248                 *solaris*)\r
249                 if test "$GCC" = "yes"; then\r
250                     flag="-D_REENTRANT"\r
251                 else\r
252                     flag="-mt -D_REENTRANT"\r
253                 fi\r
254                 ;;\r
255         esac\r
256         AC_MSG_RESULT(${flag})\r
257         if test "x$flag" != xno; then\r
258             PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"\r
259         fi\r
260 \r
261         LIBS="$save_LIBS"\r
262         CFLAGS="$save_CFLAGS"\r
263 \r
264         # More AIX lossage: must compile with xlc_r or cc_r\r
265         if test x"$GCC" != xyes; then\r
266           AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})\r
267         else\r
268           PTHREAD_CC=$CC\r
269         fi\r
270 else\r
271         PTHREAD_CC="$CC"\r
272 fi\r
273 \r
274 AC_SUBST(PTHREAD_LIBS)\r
275 AC_SUBST(PTHREAD_CFLAGS)\r
276 AC_SUBST(PTHREAD_CC)\r
277 \r
278 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:\r
279 if test x"$acx_pthread_ok" = xyes; then\r
280         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])\r
281         :\r
282 else\r
283         acx_pthread_ok=no\r
284         $2\r
285 fi\r
286 AC_LANG_RESTORE\r
287 ])dnl ACX_PTHREAD\r