source: trunk/m4/acx_pthread.m4 @ 12

Revision 12, 9.1 KB checked in by mmamonski, 13 years ago (diff)

version 1.0.7 release candidate

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