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