source: Xerces/m4/acx_pthread.m4 @ 835

Last change on this file since 835 was 721, checked in by lindanl, 9 years ago

Add the original Xerces code (version 3.1.1)

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