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