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