michael@0: # =========================================================================== michael@0: # http://www.nongnu.org/autoconf-archive/ax_pthread.html michael@0: # =========================================================================== michael@0: # michael@0: # SYNOPSIS michael@0: # michael@0: # AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) michael@0: # michael@0: # DESCRIPTION michael@0: # michael@0: # This macro figures out how to build C programs using POSIX threads. It michael@0: # sets the PTHREAD_LIBS output variable to the threads library and linker michael@0: # flags, and the PTHREAD_CFLAGS output variable to any special C compiler michael@0: # flags that are needed. (The user can also force certain compiler michael@0: # flags/libs to be tested by setting these environment variables.) michael@0: # michael@0: # Also sets PTHREAD_CC to any special C compiler that is needed for michael@0: # multi-threaded programs (defaults to the value of CC otherwise). (This michael@0: # is necessary on AIX to use the special cc_r compiler alias.) michael@0: # michael@0: # NOTE: You are assumed to not only compile your program with these flags, michael@0: # but also link it with them as well. e.g. you should link with michael@0: # $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS michael@0: # michael@0: # If you are only building threads programs, you may wish to use these michael@0: # variables in your default LIBS, CFLAGS, and CC: michael@0: # michael@0: # LIBS="$PTHREAD_LIBS $LIBS" michael@0: # CFLAGS="$CFLAGS $PTHREAD_CFLAGS" michael@0: # CC="$PTHREAD_CC" michael@0: # michael@0: # In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant michael@0: # has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to that name michael@0: # (e.g. PTHREAD_CREATE_UNDETACHED on AIX). michael@0: # michael@0: # ACTION-IF-FOUND is a list of shell commands to run if a threads library michael@0: # is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it michael@0: # is not found. If ACTION-IF-FOUND is not specified, the default action michael@0: # will define HAVE_PTHREAD. michael@0: # michael@0: # Please let the authors know if this macro fails on any platform, or if michael@0: # you have any other suggestions or comments. This macro was based on work michael@0: # by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help michael@0: # from M. Frigo), as well as ac_pthread and hb_pthread macros posted by michael@0: # Alejandro Forero Cuervo to the autoconf macro repository. We are also michael@0: # grateful for the helpful feedback of numerous users. michael@0: # michael@0: # LICENSE michael@0: # michael@0: # Copyright (c) 2008 Steven G. Johnson michael@0: # michael@0: # This program is free software: you can redistribute it and/or modify it michael@0: # under the terms of the GNU General Public License as published by the michael@0: # Free Software Foundation, either version 3 of the License, or (at your michael@0: # option) any later version. michael@0: # michael@0: # This program is distributed in the hope that it will be useful, but michael@0: # WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General michael@0: # Public License for more details. michael@0: # michael@0: # You should have received a copy of the GNU General Public License along michael@0: # with this program. If not, see . michael@0: # michael@0: # As a special exception, the respective Autoconf Macro's copyright owner michael@0: # gives unlimited permission to copy, distribute and modify the configure michael@0: # scripts that are the output of Autoconf when processing the Macro. You michael@0: # need not follow the terms of the GNU General Public License when using michael@0: # or distributing such scripts, even though portions of the text of the michael@0: # Macro appear in them. The GNU General Public License (GPL) does govern michael@0: # all other use of the material that constitutes the Autoconf Macro. michael@0: # michael@0: # This special exception to the GPL applies to versions of the Autoconf michael@0: # Macro released by the Autoconf Archive. When you make and distribute a michael@0: # modified version of the Autoconf Macro, you may extend this special michael@0: # exception to the GPL to apply to your modified version as well. michael@0: michael@0: #serial 6 michael@0: michael@0: AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD]) michael@0: AC_DEFUN([AX_PTHREAD], [ michael@0: AC_REQUIRE([AC_CANONICAL_HOST]) michael@0: AC_LANG_SAVE michael@0: AC_LANG_C michael@0: ax_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, ax_pthread_ok=yes) michael@0: AC_MSG_RESULT($ax_pthread_ok) michael@0: if test x"$ax_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: ax_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: ax_pthread_flags="-pthreads pthread -mt -pthread $ax_pthread_flags" michael@0: ;; michael@0: michael@0: *-darwin*) michael@0: acx_pthread_flags="-pthread $acx_pthread_flags" michael@0: ;; michael@0: esac michael@0: michael@0: if test x"$ax_pthread_ok" = xno; then michael@0: for flag in $ax_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(ax_pthread_config, pthread-config, yes, no) michael@0: if test x"$ax_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: static void routine(void* a) {a=0;} michael@0: static void* start_routine(void* a) {return a;}], michael@0: [pthread_t th; pthread_attr_t attr; michael@0: pthread_join(th, 0); michael@0: pthread_attr_init(&attr); michael@0: pthread_cleanup_push(routine, 0); michael@0: pthread_create(&th,0,start_routine,0); michael@0: pthread_cleanup_pop(0); ], michael@0: [ax_pthread_ok=yes]) michael@0: michael@0: LIBS="$save_LIBS" michael@0: CFLAGS="$save_CFLAGS" michael@0: michael@0: AC_MSG_RESULT($ax_pthread_ok) michael@0: if test "x$ax_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$ax_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: 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: 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"$ax_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: ax_pthread_ok=no michael@0: $2 michael@0: fi michael@0: AC_LANG_RESTORE michael@0: ])dnl AX_PTHREAD