1dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
2dnl
3dnl @summary figure out how to build C programs using POSIX threads
4dnl
5dnl This macro figures out how to build C programs using POSIX threads.
6dnl It sets the PTHREAD_LIBS output variable to the threads library and
7dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
8dnl C compiler flags that are needed. (The user can also force certain
9dnl compiler flags/libs to be tested by setting these environment
10dnl variables.)
11dnl
12dnl Also sets PTHREAD_CC to any special C compiler that is needed for
13dnl multi-threaded programs (defaults to the value of CC otherwise).
14dnl (This is necessary on AIX to use the special cc_r compiler alias.)
15dnl
16dnl NOTE: You are assumed to not only compile your program with these
17dnl flags, but also link it with them as well. e.g. you should link
18dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
19dnl $LIBS
20dnl
21dnl If you are only building threads programs, you may wish to use
22dnl these variables in your default LIBS, CFLAGS, and CC:
23dnl
24dnl        LIBS="$PTHREAD_LIBS $LIBS"
25dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
26dnl        CC="$PTHREAD_CC"
27dnl
28dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
29dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
30dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
31dnl
32dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
33dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
34dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
35dnl default action will define HAVE_PTHREAD.
36dnl
37dnl Please let the authors know if this macro fails on any platform, or
38dnl if you have any other suggestions or comments. This macro was based
39dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
40dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
41dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
42dnl We are also grateful for the helpful feedback of numerous users.
43dnl
44dnl @category InstalledPackages
45dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
46dnl @version 2006-05-29
47dnl @license GPLWithACException
48dnl
49dnl Checks for GCC shared/pthread inconsistency based on work by
50dnl Marcin Owsiany <marcin@owsiany.pl>
51
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
92acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
93
94# The ordering *is* (sometimes) important.  Some notes on the
95# individual items follow:
96
97# pthreads: AIX (must check this before -lpthread)
98# none: in case threads are in libc; should be tried before -Kthread and
99#       other compiler flags to prevent continual compiler warnings
100# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
101# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
102# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
103# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
104# -pthreads: Solaris/gcc
105# -mthreads: Mingw32/gcc, Lynx/gcc
106# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
107#      doesn't hurt to check since this sometimes defines pthreads too;
108#      also defines -D_REENTRANT)
109#      ... -mt is also the pthreads flag for HP/aCC
110# pthread: Linux, etcetera
111# --thread-safe: KAI C++
112# pthread-config: use pthread-config program (for GNU Pth library)
113
114case "${host_cpu}-${host_os}" in
115        *solaris*)
116
117        # On Solaris (at least, for some versions), libc contains stubbed
118        # (non-functional) versions of the pthreads routines, so link-based
119        # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
120        # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
121        # a function called by this macro, so we could check for that, but
122        # who knows whether they'll stub that too in a future libc.)  So,
123        # we'll just look for -pthreads and -lpthread first:
124
125        acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
126        ;;
127esac
128
129if test x"$acx_pthread_ok" = xno; then
130for flag in $acx_pthread_flags; do
131
132        case $flag in
133                none)
134                AC_MSG_CHECKING([whether pthreads work without any flags])
135                ;;
136
137                -*)
138                AC_MSG_CHECKING([whether pthreads work with $flag])
139                PTHREAD_CFLAGS="$flag"
140                ;;
141
142		pthread-config)
143		AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
144		if test x"$acx_pthread_config" = xno; then continue; fi
145		PTHREAD_CFLAGS="`pthread-config --cflags`"
146		PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
147		;;
148
149                *)
150                AC_MSG_CHECKING([for the pthreads library -l$flag])
151                PTHREAD_LIBS="-l$flag"
152                ;;
153        esac
154
155        save_LIBS="$LIBS"
156        save_CFLAGS="$CFLAGS"
157        LIBS="$PTHREAD_LIBS $LIBS"
158        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
159
160        # Check for various functions.  We must include pthread.h,
161        # since some functions may be macros.  (On the Sequent, we
162        # need a special flag -Kthread to make this header compile.)
163        # We check for pthread_join because it is in -lpthread on IRIX
164        # while pthread_create is in libc.  We check for pthread_attr_init
165        # due to DEC craziness with -lpthreads.  We check for
166        # pthread_cleanup_push because it is one of the few pthread
167        # functions on Solaris that doesn't have a non-functional libc stub.
168        # We try pthread_create on general principles.
169        AC_TRY_LINK([#include <pthread.h>],
170                    [pthread_t th; pthread_join(th, 0);
171                     pthread_attr_init(0); pthread_cleanup_push(0, 0);
172                     pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
173                    [acx_pthread_ok=yes])
174
175        LIBS="$save_LIBS"
176        CFLAGS="$save_CFLAGS"
177
178        AC_MSG_RESULT($acx_pthread_ok)
179        if test "x$acx_pthread_ok" = xyes; then
180                break;
181        fi
182
183        PTHREAD_LIBS=""
184        PTHREAD_CFLAGS=""
185done
186fi
187
188# Various other checks:
189if test "x$acx_pthread_ok" = xyes; then
190        save_LIBS="$LIBS"
191        LIBS="$PTHREAD_LIBS $LIBS"
192        save_CFLAGS="$CFLAGS"
193        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
194
195        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
196	AC_MSG_CHECKING([for joinable pthread attribute])
197	attr_name=unknown
198	for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
199	    AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
200                        [attr_name=$attr; break])
201	done
202        AC_MSG_RESULT($attr_name)
203        if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
204            AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
205                               [Define to necessary symbol if this constant
206                                uses a non-standard name on your system.])
207        fi
208
209        AC_MSG_CHECKING([if more special flags are required for pthreads])
210        flag=no
211        case "${host_cpu}-${host_os}" in
212            *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
213            *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
214        esac
215        AC_MSG_RESULT(${flag})
216        if test "x$flag" != xno; then
217            PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
218        fi
219
220        LIBS="$save_LIBS"
221        CFLAGS="$save_CFLAGS"
222        # More AIX lossage: must compile with xlc_r or cc_r
223	if test x"$GCC" != xyes; then
224          AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
225        else
226          PTHREAD_CC=$CC
227	fi
228
229   # The next part tries to detect GCC inconsistency with -shared on some
230   # architectures and systems. The problem is that in certain
231   # configurations, when -shared is specified, GCC "forgets" to
232   # internally use various flags which are still necessary.
233
234   AC_MSG_CHECKING([whether to check for GCC pthread/shared inconsistencies])
235   check_inconsistencies=yes
236   case "${host_cpu}-${host_os}" in
237     *-darwin*) check_inconsistencies=no ;;
238   esac
239   if test x"$GCC" != xyes -o "x$check_inconsistencies" != xyes ; then
240      AC_MSG_RESULT([no])
241   else
242      AC_MSG_RESULT([yes])
243
244      # In order not to create several levels of indentation, we test
245      # the value of "$ok" until we find out the cure or run out of
246      # ideas.
247      ok="no"
248
249      #
250      # Prepare the flags
251      #
252      save_CFLAGS="$CFLAGS"
253      save_LIBS="$LIBS"
254      save_CC="$CC"
255      # Try with the flags determined by the earlier checks.
256      #
257      # -Wl,-z,defs forces link-time symbol resolution, so that the
258      # linking checks with -shared actually have any value
259      #
260      # FIXME: -fPIC is required for -shared on many architectures,
261      # so we specify it here, but the right way would probably be to
262      # properly detect whether it is actually required.
263      CFLAGS="-shared -fPIC -Wl,-z,defs $CFLAGS $PTHREAD_CFLAGS"
264      LIBS="$PTHREAD_LIBS $LIBS"
265      CC="$PTHREAD_CC"
266
267      AC_MSG_CHECKING([whether -pthread is sufficient with -shared])
268      AC_TRY_LINK([#include <pthread.h>],
269         [pthread_t th; pthread_join(th, 0);
270         pthread_attr_init(0); pthread_cleanup_push(0, 0);
271         pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
272         [ok=yes])
273
274      if test "x$ok" = xyes; then
275         AC_MSG_RESULT([yes])
276      else
277         AC_MSG_RESULT([no])
278      fi
279
280      #
281      # Linux gcc on some architectures such as mips/mipsel forgets
282      # about -lpthread
283      #
284      if test x"$ok" = xno; then
285         AC_MSG_CHECKING([whether -lpthread fixes that])
286         LIBS="-lpthread $PTHREAD_LIBS $save_LIBS"
287         AC_TRY_LINK([#include <pthread.h>],
288            [pthread_t th; pthread_join(th, 0);
289            pthread_attr_init(0); pthread_cleanup_push(0, 0);
290            pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
291            [ok=yes])
292
293         if test "x$ok" = xyes; then
294            AC_MSG_RESULT([yes])
295            PTHREAD_LIBS="-lpthread $PTHREAD_LIBS"
296         else
297            AC_MSG_RESULT([no])
298         fi
299      fi
300      #
301      # FreeBSD 4.10 gcc forgets to use -lc_r instead of -lc
302      #
303      if test x"$ok" = xno; then
304         AC_MSG_CHECKING([whether -lc_r fixes that])
305         LIBS="-lc_r $PTHREAD_LIBS $save_LIBS"
306         AC_TRY_LINK([#include <pthread.h>],
307             [pthread_t th; pthread_join(th, 0);
308              pthread_attr_init(0); pthread_cleanup_push(0, 0);
309              pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
310             [ok=yes])
311
312         if test "x$ok" = xyes; then
313            AC_MSG_RESULT([yes])
314            PTHREAD_LIBS="-lc_r $PTHREAD_LIBS"
315         else
316            AC_MSG_RESULT([no])
317         fi
318      fi
319      if test x"$ok" = xno; then
320         # OK, we have run out of ideas
321         AC_MSG_WARN([Impossible to determine how to use pthreads with shared libraries])
322
323         # so it's not safe to assume that we may use pthreads
324         acx_pthread_ok=no
325      fi
326
327      CFLAGS="$save_CFLAGS"
328      LIBS="$save_LIBS"
329      CC="$save_CC"
330   fi
331else
332        PTHREAD_CC="$CC"
333fi
334
335AC_SUBST(PTHREAD_LIBS)
336AC_SUBST(PTHREAD_CFLAGS)
337AC_SUBST(PTHREAD_CC)
338
339# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
340if test x"$acx_pthread_ok" = xyes; then
341        ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
342        :
343else
344        acx_pthread_ok=no
345        $2
346fi
347AC_LANG_RESTORE
348])dnl ACX_PTHREAD
349AC_DEFUN([AC_CHECK_DEFINE],[
350AS_VAR_PUSHDEF([ac_var],[ac_cv_defined_$1_$2])dnl
351AC_CACHE_CHECK([for $1 in $2], ac_var,
352AC_TRY_COMPILE([#include <$2>],[
353  #ifdef $1
354  int ok;
355  #else
356  choke me
357  #endif
358],AS_VAR_SET(ac_var, yes),AS_VAR_SET(ac_var, no)))
359AS_IF([test AS_VAR_GET(ac_var) != "no"], [$3], [$4])dnl
360AS_VAR_POPDEF([ac_var])dnl
361])
362
363AC_DEFUN([ACX_LIBWRAP], [
364LIBWRAP_LIBS=
365saved_LIBS="$LIBS"
366LIBS="$LIBS -lwrap"
367AC_MSG_CHECKING([for tcpwrap library and headers])
368AC_LINK_IFELSE(
369AC_LANG_PROGRAM(
370[#include <tcpd.h>
371#include <syslog.h>
372int allow_severity = LOG_INFO;
373int deny_severity = LOG_WARNING;],
374[struct request_info *req;
375return hosts_access (req);]),
376[AC_DEFINE(HAVE_LIBWRAP, [], [Have tcpwrap?])
377LIBWRAP_LIBS="-lwrap"
378AC_MSG_RESULT(yes)],
379[AC_MSG_RESULT(no)])
380LIBS="$saved_LIBS"
381])
382
383AC_DEFUN([ACX_LIRC], [
384LIRC_CFLAGS=
385LIRC_LIBS=
386AC_CHECK_HEADER(lirc/lirc_client.h,[AC_CHECK_LIB(lirc_client,lirc_init,[HAVE_LIRC=1
387LIRC_LIBS=-llirc_client],HAVE_LIRC=0)],HAVE_LIRC=0)
388])
389