public software.sextractor

[/] [trunk/] [acx_pthread.m4] - Blame information for rev 234

Go to most recent revision | Details | Compare with Previous | View Log

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