Blame acinclude.m4

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