michael@0: # This was retrieved from michael@0: # http://svn.0pointer.de/viewvc/trunk/common/acx_pthread.m4?revision=1277&root=avahi michael@0: # See also (perhaps for new versions?) michael@0: # http://svn.0pointer.de/viewvc/trunk/common/acx_pthread.m4?root=avahi michael@0: # michael@0: # We've rewritten the inconsistency check code (from avahi), to work michael@0: # more broadly. In particular, it no longer assumes ld accepts -zdefs. michael@0: # This caused a restructing of the code, but the functionality has only michael@0: # changed a little. michael@0: michael@0: dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) michael@0: dnl michael@0: dnl @summary figure out how to build C programs using POSIX threads michael@0: dnl michael@0: dnl This macro figures out how to build C programs using POSIX threads. michael@0: dnl It sets the PTHREAD_LIBS output variable to the threads library and michael@0: dnl linker flags, and the PTHREAD_CFLAGS output variable to any special michael@0: dnl C compiler flags that are needed. (The user can also force certain michael@0: dnl compiler flags/libs to be tested by setting these environment michael@0: dnl variables.) michael@0: dnl michael@0: dnl Also sets PTHREAD_CC to any special C compiler that is needed for michael@0: dnl multi-threaded programs (defaults to the value of CC otherwise). michael@0: dnl (This is necessary on AIX to use the special cc_r compiler alias.) michael@0: dnl michael@0: dnl NOTE: You are assumed to not only compile your program with these michael@0: dnl flags, but also link it with them as well. e.g. you should link michael@0: dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS michael@0: dnl $LIBS michael@0: dnl michael@0: dnl If you are only building threads programs, you may wish to use michael@0: dnl these variables in your default LIBS, CFLAGS, and CC: michael@0: dnl michael@0: dnl LIBS="$PTHREAD_LIBS $LIBS" michael@0: dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS" michael@0: dnl CC="$PTHREAD_CC" michael@0: dnl michael@0: dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute michael@0: dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to michael@0: dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX). michael@0: dnl michael@0: dnl ACTION-IF-FOUND is a list of shell commands to run if a threads michael@0: dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to michael@0: dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the michael@0: dnl default action will define HAVE_PTHREAD. michael@0: dnl michael@0: dnl Please let the authors know if this macro fails on any platform, or michael@0: dnl if you have any other suggestions or comments. This macro was based michael@0: dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with michael@0: dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros michael@0: dnl posted by Alejandro Forero Cuervo to the autoconf macro repository. michael@0: dnl We are also grateful for the helpful feedback of numerous users. michael@0: dnl michael@0: dnl @category InstalledPackages michael@0: dnl @author Steven G. Johnson michael@0: dnl @version 2006-05-29 michael@0: dnl @license GPLWithACException michael@0: dnl michael@0: dnl Checks for GCC shared/pthread inconsistency based on work by michael@0: dnl Marcin Owsiany michael@0: michael@0: michael@0: AC_DEFUN([ACX_PTHREAD], [ michael@0: AC_REQUIRE([AC_CANONICAL_HOST]) michael@0: AC_LANG_SAVE michael@0: AC_LANG_C michael@0: acx_pthread_ok=no michael@0: michael@0: # We used to check for pthread.h first, but this fails if pthread.h michael@0: # requires special compiler flags (e.g. on True64 or Sequent). michael@0: # It gets checked for in the link test anyway. michael@0: michael@0: # First of all, check if the user has set any of the PTHREAD_LIBS, michael@0: # etcetera environment variables, and if threads linking works using michael@0: # them: michael@0: if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then michael@0: save_CFLAGS="$CFLAGS" michael@0: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" michael@0: save_LIBS="$LIBS" michael@0: LIBS="$PTHREAD_LIBS $LIBS" michael@0: AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS]) michael@0: AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes) michael@0: AC_MSG_RESULT($acx_pthread_ok) michael@0: if test x"$acx_pthread_ok" = xno; then michael@0: PTHREAD_LIBS="" michael@0: PTHREAD_CFLAGS="" michael@0: fi michael@0: LIBS="$save_LIBS" michael@0: CFLAGS="$save_CFLAGS" michael@0: fi michael@0: michael@0: # We must check for the threads library under a number of different michael@0: # names; the ordering is very important because some systems michael@0: # (e.g. DEC) have both -lpthread and -lpthreads, where one of the michael@0: # libraries is broken (non-POSIX). michael@0: michael@0: # Create a list of thread flags to try. Items starting with a "-" are michael@0: # C compiler flags, and other items are library names, except for "none" michael@0: # which indicates that we try without any flags at all, and "pthread-config" michael@0: # which is a program returning the flags for the Pth emulation library. michael@0: michael@0: acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" michael@0: michael@0: # The ordering *is* (sometimes) important. Some notes on the michael@0: # individual items follow: michael@0: michael@0: # pthreads: AIX (must check this before -lpthread) michael@0: # none: in case threads are in libc; should be tried before -Kthread and michael@0: # other compiler flags to prevent continual compiler warnings michael@0: # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) michael@0: # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) michael@0: # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) michael@0: # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads) michael@0: # -pthreads: Solaris/gcc michael@0: # -mthreads: Mingw32/gcc, Lynx/gcc michael@0: # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it michael@0: # doesn't hurt to check since this sometimes defines pthreads too; michael@0: # also defines -D_REENTRANT) michael@0: # ... -mt is also the pthreads flag for HP/aCC michael@0: # pthread: Linux, etcetera michael@0: # --thread-safe: KAI C++ michael@0: # pthread-config: use pthread-config program (for GNU Pth library) michael@0: michael@0: case "${host_cpu}-${host_os}" in michael@0: *solaris*) michael@0: michael@0: # On Solaris (at least, for some versions), libc contains stubbed michael@0: # (non-functional) versions of the pthreads routines, so link-based michael@0: # tests will erroneously succeed. (We need to link with -pthreads/-mt/ michael@0: # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather michael@0: # a function called by this macro, so we could check for that, but michael@0: # who knows whether they'll stub that too in a future libc.) So, michael@0: # we'll just look for -pthreads and -lpthread first: michael@0: michael@0: acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags" michael@0: ;; michael@0: esac michael@0: michael@0: if test x"$acx_pthread_ok" = xno; then michael@0: for flag in $acx_pthread_flags; do michael@0: michael@0: case $flag in michael@0: none) michael@0: AC_MSG_CHECKING([whether pthreads work without any flags]) michael@0: ;; michael@0: michael@0: -*) michael@0: AC_MSG_CHECKING([whether pthreads work with $flag]) michael@0: PTHREAD_CFLAGS="$flag" michael@0: ;; michael@0: michael@0: pthread-config) michael@0: AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no) michael@0: if test x"$acx_pthread_config" = xno; then continue; fi michael@0: PTHREAD_CFLAGS="`pthread-config --cflags`" michael@0: PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" michael@0: ;; michael@0: michael@0: *) michael@0: AC_MSG_CHECKING([for the pthreads library -l$flag]) michael@0: PTHREAD_LIBS="-l$flag" michael@0: ;; michael@0: esac michael@0: michael@0: save_LIBS="$LIBS" michael@0: save_CFLAGS="$CFLAGS" michael@0: LIBS="$PTHREAD_LIBS $LIBS" michael@0: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" michael@0: michael@0: # Check for various functions. We must include pthread.h, michael@0: # since some functions may be macros. (On the Sequent, we michael@0: # need a special flag -Kthread to make this header compile.) michael@0: # We check for pthread_join because it is in -lpthread on IRIX michael@0: # while pthread_create is in libc. We check for pthread_attr_init michael@0: # due to DEC craziness with -lpthreads. We check for michael@0: # pthread_cleanup_push because it is one of the few pthread michael@0: # functions on Solaris that doesn't have a non-functional libc stub. michael@0: # We try pthread_create on general principles. michael@0: AC_TRY_LINK([#include ], michael@0: [pthread_t th; pthread_join(th, 0); michael@0: pthread_attr_init(0); pthread_cleanup_push(0, 0); michael@0: pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], michael@0: [acx_pthread_ok=yes]) michael@0: michael@0: LIBS="$save_LIBS" michael@0: CFLAGS="$save_CFLAGS" michael@0: michael@0: AC_MSG_RESULT($acx_pthread_ok) michael@0: if test "x$acx_pthread_ok" = xyes; then michael@0: break; michael@0: fi michael@0: michael@0: PTHREAD_LIBS="" michael@0: PTHREAD_CFLAGS="" michael@0: done michael@0: fi michael@0: michael@0: # Various other checks: michael@0: if test "x$acx_pthread_ok" = xyes; then michael@0: save_LIBS="$LIBS" michael@0: LIBS="$PTHREAD_LIBS $LIBS" michael@0: save_CFLAGS="$CFLAGS" michael@0: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" michael@0: michael@0: # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. michael@0: AC_MSG_CHECKING([for joinable pthread attribute]) michael@0: attr_name=unknown michael@0: for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do michael@0: AC_TRY_LINK([#include ], [int attr=$attr; return attr;], michael@0: [attr_name=$attr; break]) michael@0: done michael@0: AC_MSG_RESULT($attr_name) michael@0: if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then michael@0: AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name, michael@0: [Define to necessary symbol if this constant michael@0: uses a non-standard name on your system.]) michael@0: fi michael@0: michael@0: AC_MSG_CHECKING([if more special flags are required for pthreads]) michael@0: flag=no michael@0: case "${host_cpu}-${host_os}" in michael@0: *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";; michael@0: *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";; michael@0: esac michael@0: AC_MSG_RESULT(${flag}) michael@0: if test "x$flag" != xno; then michael@0: PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS" michael@0: fi michael@0: michael@0: LIBS="$save_LIBS" michael@0: CFLAGS="$save_CFLAGS" michael@0: # More AIX lossage: must compile with xlc_r or cc_r michael@0: if test x"$GCC" != xyes; then michael@0: AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC}) michael@0: else michael@0: PTHREAD_CC=$CC michael@0: fi michael@0: michael@0: # The next part tries to detect GCC inconsistency with -shared on some michael@0: # architectures and systems. The problem is that in certain michael@0: # configurations, when -shared is specified, GCC "forgets" to michael@0: # internally use various flags which are still necessary. michael@0: michael@0: # michael@0: # Prepare the flags michael@0: # michael@0: save_CFLAGS="$CFLAGS" michael@0: save_LIBS="$LIBS" michael@0: save_CC="$CC" michael@0: michael@0: # Try with the flags determined by the earlier checks. michael@0: # michael@0: # -Wl,-z,defs forces link-time symbol resolution, so that the michael@0: # linking checks with -shared actually have any value michael@0: # michael@0: # FIXME: -fPIC is required for -shared on many architectures, michael@0: # so we specify it here, but the right way would probably be to michael@0: # properly detect whether it is actually required. michael@0: CFLAGS="-shared -fPIC -Wl,-z,defs $CFLAGS $PTHREAD_CFLAGS" michael@0: LIBS="$PTHREAD_LIBS $LIBS" michael@0: CC="$PTHREAD_CC" michael@0: michael@0: # In order not to create several levels of indentation, we test michael@0: # the value of "$done" until we find the cure or run out of ideas. michael@0: done="no" michael@0: michael@0: # First, make sure the CFLAGS we added are actually accepted by our michael@0: # compiler. If not (and OS X's ld, for instance, does not accept -z), michael@0: # then we can't do this test. michael@0: if test x"$done" = xno; then michael@0: AC_MSG_CHECKING([whether to check for GCC pthread/shared inconsistencies]) michael@0: AC_TRY_LINK(,, , [done=yes]) michael@0: michael@0: if test "x$done" = xyes ; then michael@0: AC_MSG_RESULT([no]) michael@0: else michael@0: AC_MSG_RESULT([yes]) michael@0: fi michael@0: fi michael@0: michael@0: if test x"$done" = xno; then michael@0: AC_MSG_CHECKING([whether -pthread is sufficient with -shared]) michael@0: AC_TRY_LINK([#include ], michael@0: [pthread_t th; pthread_join(th, 0); michael@0: pthread_attr_init(0); pthread_cleanup_push(0, 0); michael@0: pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], michael@0: [done=yes]) michael@0: michael@0: if test "x$done" = xyes; then michael@0: AC_MSG_RESULT([yes]) michael@0: else michael@0: AC_MSG_RESULT([no]) michael@0: fi michael@0: fi michael@0: michael@0: # michael@0: # Linux gcc on some architectures such as mips/mipsel forgets michael@0: # about -lpthread michael@0: # michael@0: if test x"$done" = xno; then michael@0: AC_MSG_CHECKING([whether -lpthread fixes that]) michael@0: LIBS="-lpthread $PTHREAD_LIBS $save_LIBS" michael@0: AC_TRY_LINK([#include ], michael@0: [pthread_t th; pthread_join(th, 0); michael@0: pthread_attr_init(0); pthread_cleanup_push(0, 0); michael@0: pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], michael@0: [done=yes]) michael@0: michael@0: if test "x$done" = xyes; then michael@0: AC_MSG_RESULT([yes]) michael@0: PTHREAD_LIBS="-lpthread $PTHREAD_LIBS" michael@0: else michael@0: AC_MSG_RESULT([no]) michael@0: fi michael@0: fi michael@0: # michael@0: # FreeBSD 4.10 gcc forgets to use -lc_r instead of -lc michael@0: # michael@0: if test x"$done" = xno; then michael@0: AC_MSG_CHECKING([whether -lc_r fixes that]) michael@0: LIBS="-lc_r $PTHREAD_LIBS $save_LIBS" michael@0: AC_TRY_LINK([#include ], michael@0: [pthread_t th; pthread_join(th, 0); michael@0: pthread_attr_init(0); pthread_cleanup_push(0, 0); michael@0: pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], michael@0: [done=yes]) michael@0: michael@0: if test "x$done" = xyes; then michael@0: AC_MSG_RESULT([yes]) michael@0: PTHREAD_LIBS="-lc_r $PTHREAD_LIBS" michael@0: else michael@0: AC_MSG_RESULT([no]) michael@0: fi michael@0: fi michael@0: if test x"$done" = xno; then michael@0: # OK, we have run out of ideas michael@0: AC_MSG_WARN([Impossible to determine how to use pthreads with shared libraries]) michael@0: michael@0: # so it's not safe to assume that we may use pthreads michael@0: acx_pthread_ok=no michael@0: fi michael@0: michael@0: CFLAGS="$save_CFLAGS" michael@0: LIBS="$save_LIBS" michael@0: CC="$save_CC" michael@0: else michael@0: PTHREAD_CC="$CC" michael@0: fi michael@0: michael@0: AC_SUBST(PTHREAD_LIBS) michael@0: AC_SUBST(PTHREAD_CFLAGS) michael@0: AC_SUBST(PTHREAD_CC) michael@0: michael@0: # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: michael@0: if test x"$acx_pthread_ok" = xyes; then michael@0: ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1]) michael@0: : michael@0: else michael@0: acx_pthread_ok=no michael@0: $2 michael@0: fi michael@0: AC_LANG_RESTORE michael@0: ])dnl ACX_PTHREAD