]> granicus.if.org Git - postgresql/blob - config/acx_pthread.m4
Be more aggressive about adding flags to thread compiles. The configure
[postgresql] / config / acx_pthread.m4
1 dnl PGSGL:  When updating, comment out port-specific part below;
2 dnl see the comment below with the word "PostgreSQL".
3 dnl
4 dnl Available from the GNU Autoconf Macro Archive at:
5 dnl http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html
6 dnl
7 AC_DEFUN([ACX_PTHREAD], [
8 AC_REQUIRE([AC_CANONICAL_HOST])
9 AC_LANG_SAVE
10 AC_LANG_C
11 acx_pthread_ok=no
12
13 # We used to check for pthread.h first, but this fails if pthread.h
14 # requires special compiler flags (e.g. on True64 or Sequent).
15 # It gets checked for in the link test anyway.
16
17 # First of all, check if the user has set any of the PTHREAD_LIBS,
18 # etcetera environment variables, and if threads linking works using
19 # them:
20 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
21         save_CFLAGS="$CFLAGS"
22         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
23         save_LIBS="$LIBS"
24         LIBS="$PTHREAD_LIBS $LIBS"
25         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
26         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
27         AC_MSG_RESULT($acx_pthread_ok)
28         if test x"$acx_pthread_ok" = xno; then
29                 PTHREAD_LIBS=""
30                 PTHREAD_CFLAGS=""
31         fi
32         LIBS="$save_LIBS"
33         CFLAGS="$save_CFLAGS"
34 fi
35
36 # We must check for the threads library under a number of different
37 # names; the ordering is very important because some systems
38 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
39 # libraries is broken (non-POSIX).
40
41 # Create a list of thread flags to try.  Items starting with a "-" are
42 # C compiler flags, and other items are library names, except for "none"
43 # which indicates that we try without any flags at all, and "pthread-config"
44 # which is a program returning the flags for the Pth emulation library.
45
46 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
47
48 # The ordering *is* (sometimes) important.  Some notes on the
49 # individual items follow:
50
51 # pthreads: AIX (must check this before -lpthread)
52 # none: in case threads are in libc; should be tried before -Kthread and
53 #       other compiler flags to prevent continual compiler warnings
54 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
55 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
56 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
57 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
58 # -pthreads: Solaris/gcc
59 # -mthreads: Mingw32/gcc, Lynx/gcc
60 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
61 #      doesn't hurt to check since this sometimes defines pthreads too;
62 #      also defines -D_REENTRANT)
63 # pthread: Linux, etcetera
64 # --thread-safe: KAI C++
65 # pthread-config: use pthread-config program (for GNU Pth library)
66
67 case "${host_cpu}-${host_os}" in
68         *solaris*)
69
70         # On Solaris (at least, for some versions), libc contains stubbed
71         # (non-functional) versions of the pthreads routines, so link-based
72         # tests will erroneously succeed.  (We need to link with -pthread or
73         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
74         # a function called by this macro, so we could check for that, but
75         # who knows whether they'll stub that too in a future libc.)  So,
76         # we'll just look for -pthreads and -lpthread first:
77
78         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
79         ;;
80 esac
81
82 if test x"$acx_pthread_ok" = xno; then
83 for flag in $acx_pthread_flags; do
84
85         case $flag in
86                 none)
87                 AC_MSG_CHECKING([whether pthreads work without any flags])
88                 ;;
89
90                 -*)
91                 AC_MSG_CHECKING([whether pthreads work with $flag])
92                 tryPTHREAD_CFLAGS="$flag"
93                 ;;
94
95                 pthread-config)
96                 # skip this if we already have flags defined, for PostgreSQL
97                 if test x"$PTHREAD_CFLAGS" != x -o x"$PTHREAD_LIBS" != x; then continue; fi
98                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
99                 if test x"$acx_pthread_config" = xno; then continue; fi
100                 tryPTHREAD_CFLAGS="`pthread-config --cflags`"
101                 tryPTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
102                 fi
103                 ;;
104
105                 *)
106                 AC_MSG_CHECKING([for the pthreads library -l$flag])
107                 tryPTHREAD_LIBS="-l$flag"
108                 ;;
109         esac
110
111         save_LIBS="$LIBS"
112         save_CFLAGS="$CFLAGS"
113         LIBS="$tryPTHREAD_LIBS $PTHREAD_LIBS $LIBS"
114         CFLAGS="$CFLAGS $PTHREAD_CFLAGS $tryPTHREAD_CFLAGS"
115
116         # Check for various functions.  We must include pthread.h,
117         # since some functions may be macros.  (On the Sequent, we
118         # need a special flag -Kthread to make this header compile.)
119         # We check for pthread_join because it is in -lpthread on IRIX
120         # while pthread_create is in libc.  We check for pthread_attr_init
121         # due to DEC craziness with -lpthreads.  We check for
122         # pthread_cleanup_push because it is one of the few pthread
123         # functions on Solaris that doesn't have a non-functional libc stub.
124         # We try pthread_create on general principles.
125         AC_TRY_LINK([#include <pthread.h>],
126                     [pthread_t th; pthread_join(th, 0);
127                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
128                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
129                     [acx_pthread_ok=yes])
130
131         LIBS="$save_LIBS"
132         CFLAGS="$save_CFLAGS"
133
134         AC_MSG_RESULT($acx_pthread_ok)
135         if test "x$acx_pthread_ok" = xyes; then
136             # we continue with more flags because Linux needs -lpthread
137             # for libpq builds on PostgreSQL.  The test above only
138             # tests for building binaries, not shared libraries.
139             PTHREAD_LIBS=" $tryPTHREAD_LIBS $PTHREAD_LIBS"
140             PTHREAD_CFLAGS="$PTHREAD_CFLAGS $tryPTHREAD_CFLAGS"
141         fi
142
143 done
144 fi
145
146 # Various other checks:
147 if test "x$acx_pthread_ok" = xyes; then
148         save_LIBS="$LIBS"
149         LIBS="$PTHREAD_LIBS $LIBS"
150         save_CFLAGS="$CFLAGS"
151         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
152
153         # Detect AIX lossage: threads are created detached by default
154         # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
155         AC_MSG_CHECKING([for joinable pthread attribute])
156         AC_TRY_LINK([#include <pthread.h>],
157                     [int attr=PTHREAD_CREATE_JOINABLE;],
158                     ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
159         if test x"$ok" = xunknown; then
160                 AC_TRY_LINK([#include <pthread.h>],
161                             [int attr=PTHREAD_CREATE_UNDETACHED;],
162                             ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
163         fi
164         if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
165                 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
166                           [Define to the necessary symbol if this constant
167                            uses a non-standard name on your system.])
168         fi
169         AC_MSG_RESULT(${ok})
170         if test x"$ok" = xunknown; then
171                 AC_MSG_WARN([we do not know how to create joinable pthreads])
172         fi
173
174         AC_MSG_CHECKING([if more special flags are required for pthreads])
175         flag=no
176 # We always add these in PostgreSQL
177 #       case "${host_cpu}-${host_os}" in
178 #               *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
179 #               *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
180 #       esac
181         AC_MSG_RESULT(${flag})
182         if test "x$flag" != xno; then
183                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
184         fi
185
186         LIBS="$save_LIBS"
187         CFLAGS="$save_CFLAGS"
188
189         # More AIX lossage: must compile with cc_r
190         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
191 else
192         PTHREAD_CC="$CC"
193 fi
194
195 AC_SUBST(PTHREAD_LIBS)
196 AC_SUBST(PTHREAD_CFLAGS)
197 AC_SUBST(PTHREAD_CC)
198
199 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
200 if test x"$acx_pthread_ok" = xyes; then
201         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
202         :
203 else
204         acx_pthread_ok=no
205         $2
206 fi
207 AC_LANG_RESTORE
208 ])dnl ACX_PTHREAD