media/webrtc/trunk/testing/gtest/m4/acx_pthread.m4

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

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

mercurial