summaryrefslogtreecommitdiffstats
path: root/m4/ax_pthread.m4
diff options
context:
space:
mode:
Diffstat (limited to 'm4/ax_pthread.m4')
-rw-r--r--m4/ax_pthread.m4255
1 files changed, 146 insertions, 109 deletions
diff --git a/m4/ax_pthread.m4 b/m4/ax_pthread.m4
index 4c4051e..9f35d13 100644
--- a/m4/ax_pthread.m4
+++ b/m4/ax_pthread.m4
@@ -1,5 +1,5 @@
1# =========================================================================== 1# ===========================================================================
2# http://www.gnu.org/software/autoconf-archive/ax_pthread.html 2# https://www.gnu.org/software/autoconf-archive/ax_pthread.html
3# =========================================================================== 3# ===========================================================================
4# 4#
5# SYNOPSIS 5# SYNOPSIS
@@ -14,20 +14,24 @@
14# flags that are needed. (The user can also force certain compiler 14# flags that are needed. (The user can also force certain compiler
15# flags/libs to be tested by setting these environment variables.) 15# flags/libs to be tested by setting these environment variables.)
16# 16#
17# Also sets PTHREAD_CC to any special C compiler that is needed for 17# Also sets PTHREAD_CC and PTHREAD_CXX to any special C compiler that is
18# multi-threaded programs (defaults to the value of CC otherwise). (This 18# needed for multi-threaded programs (defaults to the value of CC
19# is necessary on AIX to use the special cc_r compiler alias.) 19# respectively CXX otherwise). (This is necessary on e.g. AIX to use the
20# special cc_r/CC_r compiler alias.)
20# 21#
21# NOTE: You are assumed to not only compile your program with these flags, 22# NOTE: You are assumed to not only compile your program with these flags,
22# but also to link with them as well. For example, you might link with 23# but also to link with them as well. For example, you might link with
23# $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS 24# $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
25# $PTHREAD_CXX $CXXFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
24# 26#
25# If you are only building threaded programs, you may wish to use these 27# If you are only building threaded programs, you may wish to use these
26# variables in your default LIBS, CFLAGS, and CC: 28# variables in your default LIBS, CFLAGS, and CC:
27# 29#
28# LIBS="$PTHREAD_LIBS $LIBS" 30# LIBS="$PTHREAD_LIBS $LIBS"
29# CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 31# CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
32# CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
30# CC="$PTHREAD_CC" 33# CC="$PTHREAD_CC"
34# CXX="$PTHREAD_CXX"
31# 35#
32# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant 36# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
33# has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to 37# has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to
@@ -55,6 +59,7 @@
55# 59#
56# Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu> 60# Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
57# Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG> 61# Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG>
62# Copyright (c) 2019 Marc Stevens <marc.stevens@cwi.nl>
58# 63#
59# This program is free software: you can redistribute it and/or modify it 64# This program is free software: you can redistribute it and/or modify it
60# under the terms of the GNU General Public License as published by the 65# under the terms of the GNU General Public License as published by the
@@ -67,7 +72,7 @@
67# Public License for more details. 72# Public License for more details.
68# 73#
69# You should have received a copy of the GNU General Public License along 74# You should have received a copy of the GNU General Public License along
70# with this program. If not, see <http://www.gnu.org/licenses/>. 75# with this program. If not, see <https://www.gnu.org/licenses/>.
71# 76#
72# As a special exception, the respective Autoconf Macro's copyright owner 77# As a special exception, the respective Autoconf Macro's copyright owner
73# gives unlimited permission to copy, distribute and modify the configure 78# gives unlimited permission to copy, distribute and modify the configure
@@ -82,7 +87,7 @@
82# modified version of the Autoconf Macro, you may extend this special 87# modified version of the Autoconf Macro, you may extend this special
83# exception to the GPL to apply to your modified version as well. 88# exception to the GPL to apply to your modified version as well.
84 89
85#serial 23 90#serial 31
86 91
87AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD]) 92AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
88AC_DEFUN([AX_PTHREAD], [ 93AC_DEFUN([AX_PTHREAD], [
@@ -104,6 +109,7 @@ if test "x$PTHREAD_CFLAGS$PTHREAD_LIBS" != "x"; then
104 ax_pthread_save_CFLAGS="$CFLAGS" 109 ax_pthread_save_CFLAGS="$CFLAGS"
105 ax_pthread_save_LIBS="$LIBS" 110 ax_pthread_save_LIBS="$LIBS"
106 AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"]) 111 AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"])
112 AS_IF([test "x$PTHREAD_CXX" != "x"], [CXX="$PTHREAD_CXX"])
107 CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 113 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
108 LIBS="$PTHREAD_LIBS $LIBS" 114 LIBS="$PTHREAD_LIBS $LIBS"
109 AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS]) 115 AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS])
@@ -123,10 +129,12 @@ fi
123# (e.g. DEC) have both -lpthread and -lpthreads, where one of the 129# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
124# libraries is broken (non-POSIX). 130# libraries is broken (non-POSIX).
125 131
126# Create a list of thread flags to try. Items starting with a "-" are 132# Create a list of thread flags to try. Items with a "," contain both
127# C compiler flags, and other items are library names, except for "none" 133# C compiler flags (before ",") and linker flags (after ","). Other items
128# which indicates that we try without any flags at all, and "pthread-config" 134# starting with a "-" are C compiler flags, and remaining items are
129# which is a program returning the flags for the Pth emulation library. 135# library names, except for "none" which indicates that we try without
136# any flags at all, and "pthread-config" which is a program returning
137# the flags for the Pth emulation library.
130 138
131ax_pthread_flags="pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" 139ax_pthread_flags="pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
132 140
@@ -194,14 +202,47 @@ case $host_os in
194 # that too in a future libc.) So we'll check first for the 202 # that too in a future libc.) So we'll check first for the
195 # standard Solaris way of linking pthreads (-mt -lpthread). 203 # standard Solaris way of linking pthreads (-mt -lpthread).
196 204
197 ax_pthread_flags="-mt,pthread pthread $ax_pthread_flags" 205 ax_pthread_flags="-mt,-lpthread pthread $ax_pthread_flags"
198 ;; 206 ;;
199esac 207esac
200 208
209# Are we compiling with Clang?
210
211AC_CACHE_CHECK([whether $CC is Clang],
212 [ax_cv_PTHREAD_CLANG],
213 [ax_cv_PTHREAD_CLANG=no
214 # Note that Autoconf sets GCC=yes for Clang as well as GCC
215 if test "x$GCC" = "xyes"; then
216 AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG],
217 [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */
218# if defined(__clang__) && defined(__llvm__)
219 AX_PTHREAD_CC_IS_CLANG
220# endif
221 ],
222 [ax_cv_PTHREAD_CLANG=yes])
223 fi
224 ])
225ax_pthread_clang="$ax_cv_PTHREAD_CLANG"
226
227
201# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC) 228# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC)
202 229
230# Note that for GCC and Clang -pthread generally implies -lpthread,
231# except when -nostdlib is passed.
232# This is problematic using libtool to build C++ shared libraries with pthread:
233# [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25460
234# [2] https://bugzilla.redhat.com/show_bug.cgi?id=661333
235# [3] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=468555
236# To solve this, first try -pthread together with -lpthread for GCC
237
203AS_IF([test "x$GCC" = "xyes"], 238AS_IF([test "x$GCC" = "xyes"],
204 [ax_pthread_flags="-pthread -pthreads $ax_pthread_flags"]) 239 [ax_pthread_flags="-pthread,-lpthread -pthread -pthreads $ax_pthread_flags"])
240
241# Clang takes -pthread (never supported any other flag), but we'll try with -lpthread first
242
243AS_IF([test "x$ax_pthread_clang" = "xyes"],
244 [ax_pthread_flags="-pthread,-lpthread -pthread"])
245
205 246
206# The presence of a feature test macro requesting re-entrant function 247# The presence of a feature test macro requesting re-entrant function
207# definitions is, on some systems, a strong hint that pthreads support is 248# definitions is, on some systems, a strong hint that pthreads support is
@@ -224,25 +265,86 @@ AS_IF([test "x$ax_pthread_check_macro" = "x--"],
224 [ax_pthread_check_cond=0], 265 [ax_pthread_check_cond=0],
225 [ax_pthread_check_cond="!defined($ax_pthread_check_macro)"]) 266 [ax_pthread_check_cond="!defined($ax_pthread_check_macro)"])
226 267
227# Are we compiling with Clang?
228 268
229AC_CACHE_CHECK([whether $CC is Clang], 269if test "x$ax_pthread_ok" = "xno"; then
230 [ax_cv_PTHREAD_CLANG], 270for ax_pthread_try_flag in $ax_pthread_flags; do
231 [ax_cv_PTHREAD_CLANG=no 271
232 # Note that Autoconf sets GCC=yes for Clang as well as GCC 272 case $ax_pthread_try_flag in
233 if test "x$GCC" = "xyes"; then 273 none)
234 AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG], 274 AC_MSG_CHECKING([whether pthreads work without any flags])
235 [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */ 275 ;;
236# if defined(__clang__) && defined(__llvm__) 276
237 AX_PTHREAD_CC_IS_CLANG 277 *,*)
238# endif 278 PTHREAD_CFLAGS=`echo $ax_pthread_try_flag | sed "s/^\(.*\),\(.*\)$/\1/"`
239 ], 279 PTHREAD_LIBS=`echo $ax_pthread_try_flag | sed "s/^\(.*\),\(.*\)$/\2/"`
240 [ax_cv_PTHREAD_CLANG=yes]) 280 AC_MSG_CHECKING([whether pthreads work with "$PTHREAD_CFLAGS" and "$PTHREAD_LIBS"])
241 fi 281 ;;
242 ]) 282
243ax_pthread_clang="$ax_cv_PTHREAD_CLANG" 283 -*)
284 AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag])
285 PTHREAD_CFLAGS="$ax_pthread_try_flag"
286 ;;
287
288 pthread-config)
289 AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no])
290 AS_IF([test "x$ax_pthread_config" = "xno"], [continue])
291 PTHREAD_CFLAGS="`pthread-config --cflags`"
292 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
293 ;;
294
295 *)
296 AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag])
297 PTHREAD_LIBS="-l$ax_pthread_try_flag"
298 ;;
299 esac
300
301 ax_pthread_save_CFLAGS="$CFLAGS"
302 ax_pthread_save_LIBS="$LIBS"
303 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
304 LIBS="$PTHREAD_LIBS $LIBS"
305
306 # Check for various functions. We must include pthread.h,
307 # since some functions may be macros. (On the Sequent, we
308 # need a special flag -Kthread to make this header compile.)
309 # We check for pthread_join because it is in -lpthread on IRIX
310 # while pthread_create is in libc. We check for pthread_attr_init
311 # due to DEC craziness with -lpthreads. We check for
312 # pthread_cleanup_push because it is one of the few pthread
313 # functions on Solaris that doesn't have a non-functional libc stub.
314 # We try pthread_create on general principles.
315
316 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>
317# if $ax_pthread_check_cond
318# error "$ax_pthread_check_macro must be defined"
319# endif
320 static void *some_global = NULL;
321 static void routine(void *a)
322 {
323 /* To avoid any unused-parameter or
324 unused-but-set-parameter warning. */
325 some_global = a;
326 }
327 static void *start_routine(void *a) { return a; }],
328 [pthread_t th; pthread_attr_t attr;
329 pthread_create(&th, 0, start_routine, 0);
330 pthread_join(th, 0);
331 pthread_attr_init(&attr);
332 pthread_cleanup_push(routine, 0);
333 pthread_cleanup_pop(0) /* ; */])],
334 [ax_pthread_ok=yes],
335 [])
336
337 CFLAGS="$ax_pthread_save_CFLAGS"
338 LIBS="$ax_pthread_save_LIBS"
339
340 AC_MSG_RESULT([$ax_pthread_ok])
341 AS_IF([test "x$ax_pthread_ok" = "xyes"], [break])
342
343 PTHREAD_LIBS=""
344 PTHREAD_CFLAGS=""
345done
346fi
244 347
245ax_pthread_clang_warning=no
246 348
247# Clang needs special handling, because older versions handle the -pthread 349# Clang needs special handling, because older versions handle the -pthread
248# option in a rather... idiosyncratic way 350# option in a rather... idiosyncratic way
@@ -261,11 +363,6 @@ if test "x$ax_pthread_clang" = "xyes"; then
261 # -pthread does define _REENTRANT, and while the Darwin headers 363 # -pthread does define _REENTRANT, and while the Darwin headers
262 # ignore this macro, third-party headers might not.) 364 # ignore this macro, third-party headers might not.)
263 365
264 PTHREAD_CFLAGS="-pthread"
265 PTHREAD_LIBS=
266
267 ax_pthread_ok=yes
268
269 # However, older versions of Clang make a point of warning the user 366 # However, older versions of Clang make a point of warning the user
270 # that, in an invocation where only linking and no compilation is 367 # that, in an invocation where only linking and no compilation is
271 # taking place, the -pthread option has no effect ("argument unused 368 # taking place, the -pthread option has no effect ("argument unused
@@ -294,7 +391,7 @@ if test "x$ax_pthread_clang" = "xyes"; then
294 # step 391 # step
295 ax_pthread_save_ac_link="$ac_link" 392 ax_pthread_save_ac_link="$ac_link"
296 ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g' 393 ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g'
297 ax_pthread_link_step=`$as_echo "$ac_link" | sed "$ax_pthread_sed"` 394 ax_pthread_link_step=`AS_ECHO(["$ac_link"]) | sed "$ax_pthread_sed"`
298 ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)" 395 ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)"
299 ax_pthread_save_CFLAGS="$CFLAGS" 396 ax_pthread_save_CFLAGS="$CFLAGS"
300 for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do 397 for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do
@@ -320,78 +417,7 @@ if test "x$ax_pthread_clang" = "xyes"; then
320 417
321fi # $ax_pthread_clang = yes 418fi # $ax_pthread_clang = yes
322 419
323if test "x$ax_pthread_ok" = "xno"; then
324for ax_pthread_try_flag in $ax_pthread_flags; do
325
326 case $ax_pthread_try_flag in
327 none)
328 AC_MSG_CHECKING([whether pthreads work without any flags])
329 ;;
330
331 -mt,pthread)
332 AC_MSG_CHECKING([whether pthreads work with -mt -lpthread])
333 PTHREAD_CFLAGS="-mt"
334 PTHREAD_LIBS="-lpthread"
335 ;;
336
337 -*)
338 AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag])
339 PTHREAD_CFLAGS="$ax_pthread_try_flag"
340 ;;
341 420
342 pthread-config)
343 AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no])
344 AS_IF([test "x$ax_pthread_config" = "xno"], [continue])
345 PTHREAD_CFLAGS="`pthread-config --cflags`"
346 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
347 ;;
348
349 *)
350 AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag])
351 PTHREAD_LIBS="-l$ax_pthread_try_flag"
352 ;;
353 esac
354
355 ax_pthread_save_CFLAGS="$CFLAGS"
356 ax_pthread_save_LIBS="$LIBS"
357 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
358 LIBS="$PTHREAD_LIBS $LIBS"
359
360 # Check for various functions. We must include pthread.h,
361 # since some functions may be macros. (On the Sequent, we
362 # need a special flag -Kthread to make this header compile.)
363 # We check for pthread_join because it is in -lpthread on IRIX
364 # while pthread_create is in libc. We check for pthread_attr_init
365 # due to DEC craziness with -lpthreads. We check for
366 # pthread_cleanup_push because it is one of the few pthread
367 # functions on Solaris that doesn't have a non-functional libc stub.
368 # We try pthread_create on general principles.
369
370 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>
371# if $ax_pthread_check_cond
372# error "$ax_pthread_check_macro must be defined"
373# endif
374 static void routine(void *a) { a = 0; }
375 static void *start_routine(void *a) { return a; }],
376 [pthread_t th; pthread_attr_t attr;
377 pthread_create(&th, 0, start_routine, 0);
378 pthread_join(th, 0);
379 pthread_attr_init(&attr);
380 pthread_cleanup_push(routine, 0);
381 pthread_cleanup_pop(0) /* ; */])],
382 [ax_pthread_ok=yes],
383 [])
384
385 CFLAGS="$ax_pthread_save_CFLAGS"
386 LIBS="$ax_pthread_save_LIBS"
387
388 AC_MSG_RESULT([$ax_pthread_ok])
389 AS_IF([test "x$ax_pthread_ok" = "xyes"], [break])
390
391 PTHREAD_LIBS=""
392 PTHREAD_CFLAGS=""
393done
394fi
395 421
396# Various other checks: 422# Various other checks:
397if test "x$ax_pthread_ok" = "xyes"; then 423if test "x$ax_pthread_ok" = "xyes"; then
@@ -438,7 +464,8 @@ if test "x$ax_pthread_ok" = "xyes"; then
438 AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT], 464 AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
439 [ax_cv_PTHREAD_PRIO_INHERIT], 465 [ax_cv_PTHREAD_PRIO_INHERIT],
440 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]], 466 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],
441 [[int i = PTHREAD_PRIO_INHERIT;]])], 467 [[int i = PTHREAD_PRIO_INHERIT;
468 return i;]])],
442 [ax_cv_PTHREAD_PRIO_INHERIT=yes], 469 [ax_cv_PTHREAD_PRIO_INHERIT=yes],
443 [ax_cv_PTHREAD_PRIO_INHERIT=no]) 470 [ax_cv_PTHREAD_PRIO_INHERIT=no])
444 ]) 471 ])
@@ -460,18 +487,28 @@ if test "x$ax_pthread_ok" = "xyes"; then
460 [#handle absolute path differently from PATH based program lookup 487 [#handle absolute path differently from PATH based program lookup
461 AS_CASE(["x$CC"], 488 AS_CASE(["x$CC"],
462 [x/*], 489 [x/*],
463 [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])], 490 [
464 [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])]) 491 AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])
492 AS_IF([test "x${CXX}" != "x"], [AS_IF([AS_EXECUTABLE_P([${CXX}_r])],[PTHREAD_CXX="${CXX}_r"])])
493 ],
494 [
495 AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])
496 AS_IF([test "x${CXX}" != "x"], [AC_CHECK_PROGS([PTHREAD_CXX],[${CXX}_r],[$CXX])])
497 ]
498 )
499 ])
465 ;; 500 ;;
466 esac 501 esac
467 fi 502 fi
468fi 503fi
469 504
470test -n "$PTHREAD_CC" || PTHREAD_CC="$CC" 505test -n "$PTHREAD_CC" || PTHREAD_CC="$CC"
506test -n "$PTHREAD_CXX" || PTHREAD_CXX="$CXX"
471 507
472AC_SUBST([PTHREAD_LIBS]) 508AC_SUBST([PTHREAD_LIBS])
473AC_SUBST([PTHREAD_CFLAGS]) 509AC_SUBST([PTHREAD_CFLAGS])
474AC_SUBST([PTHREAD_CC]) 510AC_SUBST([PTHREAD_CC])
511AC_SUBST([PTHREAD_CXX])
475 512
476# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: 513# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
477if test "x$ax_pthread_ok" = "xyes"; then 514if test "x$ax_pthread_ok" = "xyes"; then