xref: /aosp_15_r20/external/webp/m4/ax_pthread.m4 (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li# ===========================================================================
2*b2055c35SXin Li#        http://www.gnu.org/software/autoconf-archive/ax_pthread.html
3*b2055c35SXin Li# ===========================================================================
4*b2055c35SXin Li#
5*b2055c35SXin Li# SYNOPSIS
6*b2055c35SXin Li#
7*b2055c35SXin Li#   AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
8*b2055c35SXin Li#
9*b2055c35SXin Li# DESCRIPTION
10*b2055c35SXin Li#
11*b2055c35SXin Li#   This macro figures out how to build C programs using POSIX threads. It
12*b2055c35SXin Li#   sets the PTHREAD_LIBS output variable to the threads library and linker
13*b2055c35SXin Li#   flags, and the PTHREAD_CFLAGS output variable to any special C compiler
14*b2055c35SXin Li#   flags that are needed. (The user can also force certain compiler
15*b2055c35SXin Li#   flags/libs to be tested by setting these environment variables.)
16*b2055c35SXin Li#
17*b2055c35SXin Li#   Also sets PTHREAD_CC to any special C compiler that is needed for
18*b2055c35SXin Li#   multi-threaded programs (defaults to the value of CC otherwise). (This
19*b2055c35SXin Li#   is necessary on AIX to use the special cc_r compiler alias.)
20*b2055c35SXin Li#
21*b2055c35SXin Li#   NOTE: You are assumed to not only compile your program with these flags,
22*b2055c35SXin Li#   but also link it with them as well. e.g. you should link with
23*b2055c35SXin Li#   $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
24*b2055c35SXin Li#
25*b2055c35SXin Li#   If you are only building threads programs, you may wish to use these
26*b2055c35SXin Li#   variables in your default LIBS, CFLAGS, and CC:
27*b2055c35SXin Li#
28*b2055c35SXin Li#     LIBS="$PTHREAD_LIBS $LIBS"
29*b2055c35SXin Li#     CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
30*b2055c35SXin Li#     CC="$PTHREAD_CC"
31*b2055c35SXin Li#
32*b2055c35SXin Li#   In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
33*b2055c35SXin Li#   has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to that name
34*b2055c35SXin Li#   (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
35*b2055c35SXin Li#
36*b2055c35SXin Li#   Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the
37*b2055c35SXin Li#   PTHREAD_PRIO_INHERIT symbol is defined when compiling with
38*b2055c35SXin Li#   PTHREAD_CFLAGS.
39*b2055c35SXin Li#
40*b2055c35SXin Li#   ACTION-IF-FOUND is a list of shell commands to run if a threads library
41*b2055c35SXin Li#   is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it
42*b2055c35SXin Li#   is not found. If ACTION-IF-FOUND is not specified, the default action
43*b2055c35SXin Li#   will define HAVE_PTHREAD.
44*b2055c35SXin Li#
45*b2055c35SXin Li#   Please let the authors know if this macro fails on any platform, or if
46*b2055c35SXin Li#   you have any other suggestions or comments. This macro was based on work
47*b2055c35SXin Li#   by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help
48*b2055c35SXin Li#   from M. Frigo), as well as ac_pthread and hb_pthread macros posted by
49*b2055c35SXin Li#   Alejandro Forero Cuervo to the autoconf macro repository. We are also
50*b2055c35SXin Li#   grateful for the helpful feedback of numerous users.
51*b2055c35SXin Li#
52*b2055c35SXin Li#   Updated for Autoconf 2.68 by Daniel Richard G.
53*b2055c35SXin Li#
54*b2055c35SXin Li# LICENSE
55*b2055c35SXin Li#
56*b2055c35SXin Li#   Copyright (c) 2008 Steven G. Johnson <[email protected]>
57*b2055c35SXin Li#   Copyright (c) 2011 Daniel Richard G. <[email protected]>
58*b2055c35SXin Li#
59*b2055c35SXin Li#   This program is free software: you can redistribute it and/or modify it
60*b2055c35SXin Li#   under the terms of the GNU General Public License as published by the
61*b2055c35SXin Li#   Free Software Foundation, either version 3 of the License, or (at your
62*b2055c35SXin Li#   option) any later version.
63*b2055c35SXin Li#
64*b2055c35SXin Li#   This program is distributed in the hope that it will be useful, but
65*b2055c35SXin Li#   WITHOUT ANY WARRANTY; without even the implied warranty of
66*b2055c35SXin Li#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
67*b2055c35SXin Li#   Public License for more details.
68*b2055c35SXin Li#
69*b2055c35SXin Li#   You should have received a copy of the GNU General Public License along
70*b2055c35SXin Li#   with this program. If not, see <http://www.gnu.org/licenses/>.
71*b2055c35SXin Li#
72*b2055c35SXin Li#   As a special exception, the respective Autoconf Macro's copyright owner
73*b2055c35SXin Li#   gives unlimited permission to copy, distribute and modify the configure
74*b2055c35SXin Li#   scripts that are the output of Autoconf when processing the Macro. You
75*b2055c35SXin Li#   need not follow the terms of the GNU General Public License when using
76*b2055c35SXin Li#   or distributing such scripts, even though portions of the text of the
77*b2055c35SXin Li#   Macro appear in them. The GNU General Public License (GPL) does govern
78*b2055c35SXin Li#   all other use of the material that constitutes the Autoconf Macro.
79*b2055c35SXin Li#
80*b2055c35SXin Li#   This special exception to the GPL applies to versions of the Autoconf
81*b2055c35SXin Li#   Macro released by the Autoconf Archive. When you make and distribute a
82*b2055c35SXin Li#   modified version of the Autoconf Macro, you may extend this special
83*b2055c35SXin Li#   exception to the GPL to apply to your modified version as well.
84*b2055c35SXin Li
85*b2055c35SXin Li#serial 21
86*b2055c35SXin Li
87*b2055c35SXin LiAU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
88*b2055c35SXin LiAC_DEFUN([AX_PTHREAD], [
89*b2055c35SXin LiAC_REQUIRE([AC_CANONICAL_HOST])
90*b2055c35SXin LiAC_LANG_PUSH([C])
91*b2055c35SXin Liax_pthread_ok=no
92*b2055c35SXin Li
93*b2055c35SXin Li# We used to check for pthread.h first, but this fails if pthread.h
94*b2055c35SXin Li# requires special compiler flags (e.g. on True64 or Sequent).
95*b2055c35SXin Li# It gets checked for in the link test anyway.
96*b2055c35SXin Li
97*b2055c35SXin Li# First of all, check if the user has set any of the PTHREAD_LIBS,
98*b2055c35SXin Li# etcetera environment variables, and if threads linking works using
99*b2055c35SXin Li# them:
100*b2055c35SXin Liif test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
101*b2055c35SXin Li        save_CFLAGS="$CFLAGS"
102*b2055c35SXin Li        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
103*b2055c35SXin Li        save_LIBS="$LIBS"
104*b2055c35SXin Li        LIBS="$PTHREAD_LIBS $LIBS"
105*b2055c35SXin Li        AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
106*b2055c35SXin Li        AC_TRY_LINK_FUNC([pthread_join], [ax_pthread_ok=yes])
107*b2055c35SXin Li        AC_MSG_RESULT([$ax_pthread_ok])
108*b2055c35SXin Li        if test x"$ax_pthread_ok" = xno; then
109*b2055c35SXin Li                PTHREAD_LIBS=""
110*b2055c35SXin Li                PTHREAD_CFLAGS=""
111*b2055c35SXin Li        fi
112*b2055c35SXin Li        LIBS="$save_LIBS"
113*b2055c35SXin Li        CFLAGS="$save_CFLAGS"
114*b2055c35SXin Lifi
115*b2055c35SXin Li
116*b2055c35SXin Li# We must check for the threads library under a number of different
117*b2055c35SXin Li# names; the ordering is very important because some systems
118*b2055c35SXin Li# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
119*b2055c35SXin Li# libraries is broken (non-POSIX).
120*b2055c35SXin Li
121*b2055c35SXin Li# Create a list of thread flags to try.  Items starting with a "-" are
122*b2055c35SXin Li# C compiler flags, and other items are library names, except for "none"
123*b2055c35SXin Li# which indicates that we try without any flags at all, and "pthread-config"
124*b2055c35SXin Li# which is a program returning the flags for the Pth emulation library.
125*b2055c35SXin Li
126*b2055c35SXin Liax_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
127*b2055c35SXin Li
128*b2055c35SXin Li# The ordering *is* (sometimes) important.  Some notes on the
129*b2055c35SXin Li# individual items follow:
130*b2055c35SXin Li
131*b2055c35SXin Li# pthreads: AIX (must check this before -lpthread)
132*b2055c35SXin Li# none: in case threads are in libc; should be tried before -Kthread and
133*b2055c35SXin Li#       other compiler flags to prevent continual compiler warnings
134*b2055c35SXin Li# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
135*b2055c35SXin Li# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
136*b2055c35SXin Li# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
137*b2055c35SXin Li# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
138*b2055c35SXin Li# -pthreads: Solaris/gcc
139*b2055c35SXin Li# -mthreads: Mingw32/gcc, Lynx/gcc
140*b2055c35SXin Li# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
141*b2055c35SXin Li#      doesn't hurt to check since this sometimes defines pthreads too;
142*b2055c35SXin Li#      also defines -D_REENTRANT)
143*b2055c35SXin Li#      ... -mt is also the pthreads flag for HP/aCC
144*b2055c35SXin Li# pthread: Linux, etcetera
145*b2055c35SXin Li# --thread-safe: KAI C++
146*b2055c35SXin Li# pthread-config: use pthread-config program (for GNU Pth library)
147*b2055c35SXin Li
148*b2055c35SXin Licase ${host_os} in
149*b2055c35SXin Li        solaris*)
150*b2055c35SXin Li
151*b2055c35SXin Li        # On Solaris (at least, for some versions), libc contains stubbed
152*b2055c35SXin Li        # (non-functional) versions of the pthreads routines, so link-based
153*b2055c35SXin Li        # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
154*b2055c35SXin Li        # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
155*b2055c35SXin Li        # a function called by this macro, so we could check for that, but
156*b2055c35SXin Li        # who knows whether they'll stub that too in a future libc.)  So,
157*b2055c35SXin Li        # we'll just look for -pthreads and -lpthread first:
158*b2055c35SXin Li
159*b2055c35SXin Li        ax_pthread_flags="-pthreads pthread -mt -pthread $ax_pthread_flags"
160*b2055c35SXin Li        ;;
161*b2055c35SXin Li
162*b2055c35SXin Li        darwin*)
163*b2055c35SXin Li        ax_pthread_flags="-pthread $ax_pthread_flags"
164*b2055c35SXin Li        ;;
165*b2055c35SXin Liesac
166*b2055c35SXin Li
167*b2055c35SXin Li# Clang doesn't consider unrecognized options an error unless we specify
168*b2055c35SXin Li# -Werror. We throw in some extra Clang-specific options to ensure that
169*b2055c35SXin Li# this doesn't happen for GCC, which also accepts -Werror.
170*b2055c35SXin Li
171*b2055c35SXin LiAC_MSG_CHECKING([if compiler needs -Werror to reject unknown flags])
172*b2055c35SXin Lisave_CFLAGS="$CFLAGS"
173*b2055c35SXin Liax_pthread_extra_flags="-Werror"
174*b2055c35SXin LiCFLAGS="$CFLAGS $ax_pthread_extra_flags -Wunknown-warning-option -Wsizeof-array-argument"
175*b2055c35SXin LiAC_COMPILE_IFELSE([AC_LANG_PROGRAM([int foo(void);],[foo()])],
176*b2055c35SXin Li                  [AC_MSG_RESULT([yes])],
177*b2055c35SXin Li                  [ax_pthread_extra_flags=
178*b2055c35SXin Li                   AC_MSG_RESULT([no])])
179*b2055c35SXin LiCFLAGS="$save_CFLAGS"
180*b2055c35SXin Li
181*b2055c35SXin Liif test x"$ax_pthread_ok" = xno; then
182*b2055c35SXin Lifor flag in $ax_pthread_flags; do
183*b2055c35SXin Li
184*b2055c35SXin Li        case $flag in
185*b2055c35SXin Li                none)
186*b2055c35SXin Li                AC_MSG_CHECKING([whether pthreads work without any flags])
187*b2055c35SXin Li                ;;
188*b2055c35SXin Li
189*b2055c35SXin Li                -*)
190*b2055c35SXin Li                AC_MSG_CHECKING([whether pthreads work with $flag])
191*b2055c35SXin Li                PTHREAD_CFLAGS="$flag"
192*b2055c35SXin Li                ;;
193*b2055c35SXin Li
194*b2055c35SXin Li                pthread-config)
195*b2055c35SXin Li                AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no])
196*b2055c35SXin Li                if test x"$ax_pthread_config" = xno; then continue; fi
197*b2055c35SXin Li                PTHREAD_CFLAGS="`pthread-config --cflags`"
198*b2055c35SXin Li                PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
199*b2055c35SXin Li                ;;
200*b2055c35SXin Li
201*b2055c35SXin Li                *)
202*b2055c35SXin Li                AC_MSG_CHECKING([for the pthreads library -l$flag])
203*b2055c35SXin Li                PTHREAD_LIBS="-l$flag"
204*b2055c35SXin Li                ;;
205*b2055c35SXin Li        esac
206*b2055c35SXin Li
207*b2055c35SXin Li        save_LIBS="$LIBS"
208*b2055c35SXin Li        save_CFLAGS="$CFLAGS"
209*b2055c35SXin Li        LIBS="$PTHREAD_LIBS $LIBS"
210*b2055c35SXin Li        CFLAGS="$CFLAGS $PTHREAD_CFLAGS $ax_pthread_extra_flags"
211*b2055c35SXin Li
212*b2055c35SXin Li        # Check for various functions.  We must include pthread.h,
213*b2055c35SXin Li        # since some functions may be macros.  (On the Sequent, we
214*b2055c35SXin Li        # need a special flag -Kthread to make this header compile.)
215*b2055c35SXin Li        # We check for pthread_join because it is in -lpthread on IRIX
216*b2055c35SXin Li        # while pthread_create is in libc.  We check for pthread_attr_init
217*b2055c35SXin Li        # due to DEC craziness with -lpthreads.  We check for
218*b2055c35SXin Li        # pthread_cleanup_push because it is one of the few pthread
219*b2055c35SXin Li        # functions on Solaris that doesn't have a non-functional libc stub.
220*b2055c35SXin Li        # We try pthread_create on general principles.
221*b2055c35SXin Li        AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>
222*b2055c35SXin Li                        static void routine(void *a) { a = 0; }
223*b2055c35SXin Li                        static void *start_routine(void *a) { return a; }],
224*b2055c35SXin Li                       [pthread_t th; pthread_attr_t attr;
225*b2055c35SXin Li                        pthread_create(&th, 0, start_routine, 0);
226*b2055c35SXin Li                        pthread_join(th, 0);
227*b2055c35SXin Li                        pthread_attr_init(&attr);
228*b2055c35SXin Li                        pthread_cleanup_push(routine, 0);
229*b2055c35SXin Li                        pthread_cleanup_pop(0) /* ; */])],
230*b2055c35SXin Li                [ax_pthread_ok=yes],
231*b2055c35SXin Li                [])
232*b2055c35SXin Li
233*b2055c35SXin Li        LIBS="$save_LIBS"
234*b2055c35SXin Li        CFLAGS="$save_CFLAGS"
235*b2055c35SXin Li
236*b2055c35SXin Li        AC_MSG_RESULT([$ax_pthread_ok])
237*b2055c35SXin Li        if test "x$ax_pthread_ok" = xyes; then
238*b2055c35SXin Li                break;
239*b2055c35SXin Li        fi
240*b2055c35SXin Li
241*b2055c35SXin Li        PTHREAD_LIBS=""
242*b2055c35SXin Li        PTHREAD_CFLAGS=""
243*b2055c35SXin Lidone
244*b2055c35SXin Lifi
245*b2055c35SXin Li
246*b2055c35SXin Li# Various other checks:
247*b2055c35SXin Liif test "x$ax_pthread_ok" = xyes; then
248*b2055c35SXin Li        save_LIBS="$LIBS"
249*b2055c35SXin Li        LIBS="$PTHREAD_LIBS $LIBS"
250*b2055c35SXin Li        save_CFLAGS="$CFLAGS"
251*b2055c35SXin Li        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
252*b2055c35SXin Li
253*b2055c35SXin Li        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
254*b2055c35SXin Li        AC_MSG_CHECKING([for joinable pthread attribute])
255*b2055c35SXin Li        attr_name=unknown
256*b2055c35SXin Li        for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
257*b2055c35SXin Li            AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],
258*b2055c35SXin Li                           [int attr = $attr; return attr /* ; */])],
259*b2055c35SXin Li                [attr_name=$attr; break],
260*b2055c35SXin Li                [])
261*b2055c35SXin Li        done
262*b2055c35SXin Li        AC_MSG_RESULT([$attr_name])
263*b2055c35SXin Li        if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
264*b2055c35SXin Li            AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE], [$attr_name],
265*b2055c35SXin Li                               [Define to necessary symbol if this constant
266*b2055c35SXin Li                                uses a non-standard name on your system.])
267*b2055c35SXin Li        fi
268*b2055c35SXin Li
269*b2055c35SXin Li        AC_MSG_CHECKING([if more special flags are required for pthreads])
270*b2055c35SXin Li        flag=no
271*b2055c35SXin Li        case ${host_os} in
272*b2055c35SXin Li            aix* | freebsd* | darwin*) flag="-D_THREAD_SAFE";;
273*b2055c35SXin Li            osf* | hpux*) flag="-D_REENTRANT";;
274*b2055c35SXin Li            solaris*)
275*b2055c35SXin Li            if test "$GCC" = "yes"; then
276*b2055c35SXin Li                flag="-D_REENTRANT"
277*b2055c35SXin Li            else
278*b2055c35SXin Li                # TODO: What about Clang on Solaris?
279*b2055c35SXin Li                flag="-mt -D_REENTRANT"
280*b2055c35SXin Li            fi
281*b2055c35SXin Li            ;;
282*b2055c35SXin Li        esac
283*b2055c35SXin Li        AC_MSG_RESULT([$flag])
284*b2055c35SXin Li        if test "x$flag" != xno; then
285*b2055c35SXin Li            PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
286*b2055c35SXin Li        fi
287*b2055c35SXin Li
288*b2055c35SXin Li        AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
289*b2055c35SXin Li            [ax_cv_PTHREAD_PRIO_INHERIT], [
290*b2055c35SXin Li                AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],
291*b2055c35SXin Li                                                [[int i = PTHREAD_PRIO_INHERIT;]])],
292*b2055c35SXin Li                    [ax_cv_PTHREAD_PRIO_INHERIT=yes],
293*b2055c35SXin Li                    [ax_cv_PTHREAD_PRIO_INHERIT=no])
294*b2055c35SXin Li            ])
295*b2055c35SXin Li        AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes"],
296*b2055c35SXin Li            [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.])])
297*b2055c35SXin Li
298*b2055c35SXin Li        LIBS="$save_LIBS"
299*b2055c35SXin Li        CFLAGS="$save_CFLAGS"
300*b2055c35SXin Li
301*b2055c35SXin Li        # More AIX lossage: compile with *_r variant
302*b2055c35SXin Li        if test "x$GCC" != xyes; then
303*b2055c35SXin Li            case $host_os in
304*b2055c35SXin Li                aix*)
305*b2055c35SXin Li                AS_CASE(["x/$CC"],
306*b2055c35SXin Li                  [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6],
307*b2055c35SXin Li                  [#handle absolute path differently from PATH based program lookup
308*b2055c35SXin Li                   AS_CASE(["x$CC"],
309*b2055c35SXin Li                     [x/*],
310*b2055c35SXin Li                     [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])],
311*b2055c35SXin Li                     [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])])
312*b2055c35SXin Li                ;;
313*b2055c35SXin Li            esac
314*b2055c35SXin Li        fi
315*b2055c35SXin Lifi
316*b2055c35SXin Li
317*b2055c35SXin Litest -n "$PTHREAD_CC" || PTHREAD_CC="$CC"
318*b2055c35SXin Li
319*b2055c35SXin LiAC_SUBST([PTHREAD_LIBS])
320*b2055c35SXin LiAC_SUBST([PTHREAD_CFLAGS])
321*b2055c35SXin LiAC_SUBST([PTHREAD_CC])
322*b2055c35SXin Li
323*b2055c35SXin Li# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
324*b2055c35SXin Liif test x"$ax_pthread_ok" = xyes; then
325*b2055c35SXin Li        ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1])
326*b2055c35SXin Li        :
327*b2055c35SXin Lielse
328*b2055c35SXin Li        ax_pthread_ok=no
329*b2055c35SXin Li        $2
330*b2055c35SXin Lifi
331*b2055c35SXin LiAC_LANG_POP
332*b2055c35SXin Li])dnl AX_PTHREAD
333