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