Salome HOME
Regression AnalysisFile
[modules/adao.git] / adm_local / acx_pthread.m4
1 dnl  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 dnl
3 dnl  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 dnl  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 dnl
6 dnl  This library is free software; you can redistribute it and/or
7 dnl  modify it under the terms of the GNU Lesser General Public
8 dnl  License as published by the Free Software Foundation; either
9 dnl  version 2.1 of the License.
10 dnl
11 dnl  This library is distributed in the hope that it will be useful,
12 dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 dnl  Lesser General Public License for more details.
15 dnl
16 dnl  You should have received a copy of the GNU Lesser General Public
17 dnl  License along with this library; if not, write to the Free Software
18 dnl  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 dnl
20 dnl  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 dnl
22 dnl Paul RASCLE: modification for Linux: -pthread required by boost...
23 dnl Available from the GNU Autoconf Macro Archive at:
24 dnl
25 AC_DEFUN([ACX_PTHREAD], [
26 AC_REQUIRE([AC_CANONICAL_HOST])
27 AC_LANG_SAVE
28 AC_LANG_C
29 acx_pthread_ok=no
30
31 # We used to check for pthread.h first, but this fails if pthread.h
32 # requires special compiler flags (e.g. on True64 or Sequent).
33 # It gets checked for in the link test anyway.
34
35 # First of all, check if the user has set any of the PTHREAD_LIBS,
36 # etcetera environment variables, and if threads linking works using
37 # them:
38 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
39         save_CFLAGS="$CFLAGS"
40         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
41         save_LIBS="$LIBS"
42         LIBS="$PTHREAD_LIBS $LIBS"
43         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
44         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
45         AC_MSG_RESULT($acx_pthread_ok)
46         if test x"$acx_pthread_ok" = xno; then
47                 PTHREAD_LIBS=""
48                 PTHREAD_CFLAGS=""
49         fi
50         LIBS="$save_LIBS"
51         CFLAGS="$save_CFLAGS"
52 fi
53
54 # We must check for the threads library under a number of different
55 # names; the ordering is very important because some systems
56 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
57 # libraries is broken (non-POSIX).
58
59 # Create a list of thread flags to try.  Items starting with a "-" are
60 # C compiler flags, and other items are library names, except for "none"
61 # which indicates that we try without any flags at all, and "pthread-config"
62 # which is a program returning the flags for the Pth emulation library.
63
64 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
65
66 # The ordering *is* (sometimes) important.  Some notes on the
67 # individual items follow:
68
69 # pthreads: AIX (must check this before -lpthread)
70 # none: in case threads are in libc; should be tried before -Kthread and
71 #       other compiler flags to prevent continual compiler warnings
72 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
73 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
74 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
75 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
76 # -pthreads: Solaris/gcc
77 # -mthreads: Mingw32/gcc, Lynx/gcc
78 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
79 #      doesn't hurt to check since this sometimes defines pthreads too;
80 #      also defines -D_REENTRANT)
81 # pthread: Linux, etcetera
82 # --thread-safe: KAI C++
83 # pthread-config: use pthread-config program (for GNU Pth library)
84
85 case "${host_cpu}-${host_os}" in
86         *linux*)
87
88         # On linux "none" works, but boost requires -pthread, so, try first "-pthread"
89 acx_pthread_flags="-pthread pthreads none -Kthread -kthread lthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
90         ;;
91
92         *solaris*)
93
94         # On Solaris (at least, for some versions), libc contains stubbed
95         # (non-functional) versions of the pthreads routines, so link-based
96         # tests will erroneously succeed.  (We need to link with -pthread or
97         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
98         # a function called by this macro, so we could check for that, but
99         # who knows whether they'll stub that too in a future libc.)  So,
100         # we'll just look for -pthreads and -lpthread first:
101
102         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
103         ;;
104 esac
105
106 if test x"$acx_pthread_ok" = xno; then
107 for flag in $acx_pthread_flags; do
108
109         case $flag in
110                 none)
111                 AC_MSG_CHECKING([whether pthreads work without any flags])
112                 ;;
113
114                 -*)
115                 AC_MSG_CHECKING([whether pthreads work with $flag])
116                 PTHREAD_CFLAGS="$flag"
117                 ;;
118
119                 pthread-config)
120                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
121                 if test x"$acx_pthread_config" = xno; then continue; fi
122                 PTHREAD_CFLAGS="`pthread-config --cflags`"
123                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
124                 ;;
125
126                 *)
127                 AC_MSG_CHECKING([for the pthreads library -l$flag])
128                 PTHREAD_LIBS="-l$flag"
129                 ;;
130         esac
131
132         save_LIBS="$LIBS"
133         save_CFLAGS="$CFLAGS"
134         LIBS="$PTHREAD_LIBS $LIBS"
135         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
136
137         # Check for various functions.  We must include pthread.h,
138         # since some functions may be macros.  (On the Sequent, we
139         # need a special flag -Kthread to make this header compile.)
140         # We check for pthread_join because it is in -lpthread on IRIX
141         # while pthread_create is in libc.  We check for pthread_attr_init
142         # due to DEC craziness with -lpthreads.  We check for
143         # pthread_cleanup_push because it is one of the few pthread
144         # functions on Solaris that doesn't have a non-functional libc stub.
145         # We try pthread_create on general principles.
146         AC_TRY_LINK([#include <pthread.h>],
147                     [pthread_t th; pthread_join(th, 0);
148                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
149                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
150                     [acx_pthread_ok=yes])
151
152         LIBS="$save_LIBS"
153         CFLAGS="$save_CFLAGS"
154
155         AC_MSG_RESULT($acx_pthread_ok)
156         if test "x$acx_pthread_ok" = xyes; then
157                 break;
158         fi
159
160         PTHREAD_LIBS=""
161         PTHREAD_CFLAGS=""
162 done
163 fi
164
165 # Various other checks:
166 if test "x$acx_pthread_ok" = xyes; then
167         save_LIBS="$LIBS"
168         LIBS="$PTHREAD_LIBS $LIBS"
169         save_CFLAGS="$CFLAGS"
170         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
171
172         # Detect AIX lossage: threads are created detached by default
173         # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
174         AC_MSG_CHECKING([for joinable pthread attribute])
175         AC_TRY_LINK([#include <pthread.h>],
176                     [int attr=PTHREAD_CREATE_JOINABLE;],
177                     ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
178         if test x"$ok" = xunknown; then
179                 AC_TRY_LINK([#include <pthread.h>],
180                             [int attr=PTHREAD_CREATE_UNDETACHED;],
181                             ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
182         fi
183         if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
184                 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
185                           [Define to the necessary symbol if this constant
186                            uses a non-standard name on your system.])
187         fi
188         AC_MSG_RESULT(${ok})
189         if test x"$ok" = xunknown; then
190                 AC_MSG_WARN([we do not know how to create joinable pthreads])
191         fi
192
193         AC_MSG_CHECKING([if more special flags are required for pthreads])
194         flag=no
195         case "${host_cpu}-${host_os}" in
196                 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
197                 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
198         esac
199         AC_MSG_RESULT(${flag})
200         if test "x$flag" != xno; then
201                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
202         fi
203
204         LIBS="$save_LIBS"
205         CFLAGS="$save_CFLAGS"
206
207         # More AIX lossage: must compile with cc_r
208         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
209 else
210         PTHREAD_CC="$CC"
211 fi
212
213 AC_SUBST(PTHREAD_LIBS)
214 AC_SUBST(PTHREAD_CFLAGS)
215 AC_SUBST(PTHREAD_CC)
216
217 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
218 if test x"$acx_pthread_ok" = xyes; then
219         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
220         :
221 else
222         acx_pthread_ok=no
223         $2
224 fi
225 AC_LANG_RESTORE
226 ])dnl ACX_PTHREAD