]> git.meshlink.io Git - catta/blob - common/acx_pthread.m4
merge mips portability patch from Sjoerd Simmons (closes: #4)
[catta] / common / acx_pthread.m4
1 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
2 dnl
3 dnl This macro figures out how to build C programs using POSIX threads.
4 dnl It sets the PTHREAD_LIBS output variable to the threads library and
5 dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
6 dnl C compiler flags that are needed. (The user can also force certain
7 dnl compiler flags/libs to be tested by setting these environment
8 dnl variables.)
9 dnl
10 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
11 dnl multi-threaded programs (defaults to the value of CC otherwise).
12 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
13 dnl
14 dnl NOTE: You are assumed to not only compile your program with these
15 dnl flags, but also link it with them as well. e.g. you should link
16 dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
17 dnl $LIBS
18 dnl
19 dnl If you are only building threads programs, you may wish to use
20 dnl these variables in your default LIBS, CFLAGS, and CC:
21 dnl
22 dnl        LIBS="$PTHREAD_LIBS $LIBS"
23 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
24 dnl        CC="$PTHREAD_CC"
25 dnl
26 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
27 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
28 dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
29 dnl
30 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
31 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
32 dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
33 dnl default action will define HAVE_PTHREAD.
34 dnl
35 dnl Please let the authors know if this macro fails on any platform, or
36 dnl if you have any other suggestions or comments. This macro was based
37 dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
38 dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
39 dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
40 dnl We are also grateful for the helpful feedback of numerous users.
41 dnl
42 dnl @category InstalledPackages
43 dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
44 dnl @version 2005-01-14
45 dnl @license GPLWithACException
46 dnl 
47 dnl Checks for GCC shared/pthread inconsistency based on work by
48 dnl Marcin Owsiany <marcin@owsiany.pl>
49
50
51 AC_DEFUN([ACX_PTHREAD], [
52 AC_REQUIRE([AC_CANONICAL_HOST])
53 AC_LANG_SAVE
54 AC_LANG_C
55 acx_pthread_ok=no
56
57 # We used to check for pthread.h first, but this fails if pthread.h
58 # requires special compiler flags (e.g. on True64 or Sequent).
59 # It gets checked for in the link test anyway.
60
61 # First of all, check if the user has set any of the PTHREAD_LIBS,
62 # etcetera environment variables, and if threads linking works using
63 # them:
64 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
65         save_CFLAGS="$CFLAGS"
66         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
67         save_LIBS="$LIBS"
68         LIBS="$PTHREAD_LIBS $LIBS"
69         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
70         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
71         AC_MSG_RESULT($acx_pthread_ok)
72         if test x"$acx_pthread_ok" = xno; then
73                 PTHREAD_LIBS=""
74                 PTHREAD_CFLAGS=""
75         fi
76         LIBS="$save_LIBS"
77         CFLAGS="$save_CFLAGS"
78 fi
79
80 # We must check for the threads library under a number of different
81 # names; the ordering is very important because some systems
82 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
83 # libraries is broken (non-POSIX).
84
85 # Create a list of thread flags to try.  Items starting with a "-" are
86 # C compiler flags, and other items are library names, except for "none"
87 # which indicates that we try without any flags at all, and "pthread-config"
88 # which is a program returning the flags for the Pth emulation library.
89
90 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
91
92 # The ordering *is* (sometimes) important.  Some notes on the
93 # individual items follow:
94
95 # pthreads: AIX (must check this before -lpthread)
96 # none: in case threads are in libc; should be tried before -Kthread and
97 #       other compiler flags to prevent continual compiler warnings
98 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
99 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
100 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
101 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
102 # -pthreads: Solaris/gcc
103 # -mthreads: Mingw32/gcc, Lynx/gcc
104 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
105 #      doesn't hurt to check since this sometimes defines pthreads too;
106 #      also defines -D_REENTRANT)
107 # pthread: Linux, etcetera
108 # --thread-safe: KAI C++
109 # pthread-config: use pthread-config program (for GNU Pth library)
110
111 case "${host_cpu}-${host_os}" in
112         *solaris*)
113
114         # On Solaris (at least, for some versions), libc contains stubbed
115         # (non-functional) versions of the pthreads routines, so link-based
116         # tests will erroneously succeed.  (We need to link with -pthread or
117         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
118         # a function called by this macro, so we could check for that, but
119         # who knows whether they'll stub that too in a future libc.)  So,
120         # we'll just look for -pthreads and -lpthread first:
121
122         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
123         ;;
124 esac
125
126 if test x"$acx_pthread_ok" = xno; then
127 for flag in $acx_pthread_flags; do
128
129         case $flag in
130                 none)
131                 AC_MSG_CHECKING([whether pthreads work without any flags])
132                 ;;
133
134                 -*)
135                 AC_MSG_CHECKING([whether pthreads work with $flag])
136                 PTHREAD_CFLAGS="$flag"
137                 ;;
138
139                 pthread-config)
140                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
141                 if test x"$acx_pthread_config" = xno; then continue; fi
142                 PTHREAD_CFLAGS="`pthread-config --cflags`"
143                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
144                 ;;
145
146                 *)
147                 AC_MSG_CHECKING([for the pthreads library -l$flag])
148                 PTHREAD_LIBS="-l$flag"
149                 ;;
150         esac
151
152         save_LIBS="$LIBS"
153         save_CFLAGS="$CFLAGS"
154         LIBS="$PTHREAD_LIBS $LIBS"
155         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
156
157         # Check for various functions.  We must include pthread.h,
158         # since some functions may be macros.  (On the Sequent, we
159         # need a special flag -Kthread to make this header compile.)
160         # We check for pthread_join because it is in -lpthread on IRIX
161         # while pthread_create is in libc.  We check for pthread_attr_init
162         # due to DEC craziness with -lpthreads.  We check for
163         # pthread_cleanup_push because it is one of the few pthread
164         # functions on Solaris that doesn't have a non-functional libc stub.
165         # We try pthread_create on general principles.
166         AC_TRY_LINK([#include <pthread.h>],
167                     [pthread_t th; pthread_join(th, 0);
168                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
169                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
170                     [acx_pthread_ok=yes])
171
172         LIBS="$save_LIBS"
173         CFLAGS="$save_CFLAGS"
174
175         AC_MSG_RESULT($acx_pthread_ok)
176         if test "x$acx_pthread_ok" = xyes; then
177                 break;
178         fi
179
180         PTHREAD_LIBS=""
181         PTHREAD_CFLAGS=""
182 done
183 fi
184
185 # Various other checks:
186 if test "x$acx_pthread_ok" = xyes; then
187         save_LIBS="$LIBS"
188         LIBS="$PTHREAD_LIBS $LIBS"
189         save_CFLAGS="$CFLAGS"
190         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
191
192         # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
193         AC_MSG_CHECKING([for joinable pthread attribute])
194         attr_name=unknown
195         for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
196             AC_TRY_LINK([#include <pthread.h>], [int attr=$attr;],
197                         [attr_name=$attr; break])
198         done
199         AC_MSG_RESULT($attr_name)
200         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
201             AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
202                                [Define to necessary symbol if this constant
203                                 uses a non-standard name on your system.])
204         fi
205
206         AC_MSG_CHECKING([if more special flags are required for pthreads])
207         flag=no
208         case "${host_cpu}-${host_os}" in
209             *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
210             *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
211         esac
212         AC_MSG_RESULT(${flag})
213         if test "x$flag" != xno; then
214             PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
215         fi
216
217         LIBS="$save_LIBS"
218         CFLAGS="$save_CFLAGS"
219
220         # More AIX lossage: must compile with cc_r
221         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
222
223    # The next part tries to detect GCC inconsistency with -shared on some
224    # architectures and systems. The problem is that in certain
225    # configurations, when -shared is specified, GCC "forgets" to
226    # internally use various flags which are still necessary.
227    
228    # First, check whether caller wants us to skip -shared checks
229    # this is useful
230    AC_MSG_CHECKING([whether to check for GCC pthread/shared inconsistencies])
231    if test x"$3" = x1; then
232       AC_MSG_RESULT([no])
233    else
234       AC_MSG_RESULT([yes])
235
236       # In order not to create several levels of indentation, we test
237       # the value of "$ok" until we find out the cure or run out of
238       # ideas.
239       ok="no"
240
241       #
242       # Prepare the flags
243       #
244       save_CFLAGS="$CFLAGS"
245       save_LIBS="$LIBS"
246       save_CC="$CC"
247       # Try with the flags determined by the earlier checks.
248       #
249       # -Wl,-z,defs forces link-time symbol resolution, so that the
250       # linking checks with -shared actually have any value
251       #
252       # FIXME: -fPIC is required for -shared on many architectures,
253       # so we specify it here, but the right way would probably be to
254       # properly detect whether it is actually required.
255       CFLAGS="-shared -fPIC -Wl,-z,defs $CFLAGS $PTHREAD_CFLAGS"
256       LIBS="$PTHREAD_LIBS $LIBS"
257       CC="$PTHREAD_CC"
258
259       AC_MSG_CHECKING([whether -pthread is sufficient with -shared])
260       AC_TRY_LINK([#include <pthread.h>],
261          [pthread_t th; pthread_join(th, 0);
262          pthread_attr_init(0); pthread_cleanup_push(0, 0);
263          pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
264          [ok=yes])
265       
266       if test "x$ok" = xyes; then
267          AC_MSG_RESULT([yes])
268       else
269          AC_MSG_RESULT([no])
270       fi
271    
272       #
273       # Linux gcc on some architectures such as mips/mipsel forgets
274       # about -lpthread
275       #
276       if test x"$ok" = xno; then
277          AC_MSG_CHECKING([whether -lpthread fixes that])
278          LIBS="-lpthread $PTHREAD_LIBS $save_LIBS"
279          AC_TRY_LINK([#include <pthread.h>],
280             [pthread_t th; pthread_join(th, 0);
281             pthread_attr_init(0); pthread_cleanup_push(0, 0);
282             pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
283             [ok=yes])
284    
285          if test "x$ok" = xyes; then
286             AC_MSG_RESULT([yes])
287             PTHREAD_LIBS="-lpthread $PTHREAD_LIBS"
288          else
289             AC_MSG_RESULT([no])
290          fi
291       fi
292       #
293       # FreeBSD 4.10 gcc forgets to use -lc_r instead of -lc
294       #
295       if test x"$ok" = xno; then
296          AC_MSG_CHECKING([whether -lc_r fixes that])
297          LIBS="-lc_r $PTHREAD_LIBS $save_LIBS"
298          AC_TRY_LINK([#include <pthread.h>],
299              [pthread_t th; pthread_join(th, 0);
300               pthread_attr_init(0); pthread_cleanup_push(0, 0);
301               pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
302              [ok=yes])
303    
304          if test "x$ok" = xyes; then
305             AC_MSG_RESULT([yes])
306             PTHREAD_LIBS="-lc_r $PTHREAD_LIBS"
307          else
308             AC_MSG_RESULT([no])
309          fi
310       fi
311       if test x"$ok" = xno; then
312          # OK, we have run out of ideas
313          AC_MSG_WARN([Impossible to determine how to use pthreads with shared libraries])
314
315          # so it's not safe to assume that we may use pthreads
316          acx_pthread_ok=no
317       fi
318
319       CFLAGS="$save_CFLAGS"
320       LIBS="$save_LIBS"
321       CC="$save_CC"
322    fi
323 else
324         PTHREAD_CC="$CC"
325 fi
326
327 AC_SUBST(PTHREAD_LIBS)
328 AC_SUBST(PTHREAD_CFLAGS)
329 AC_SUBST(PTHREAD_CC)
330
331 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
332 if test x"$acx_pthread_ok" = xyes; then
333         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
334         :
335 else
336         acx_pthread_ok=no
337         $2
338 fi
339 AC_LANG_RESTORE
340 ])dnl ACX_PTHREAD