1dnl ##
2dnl ##  GNU Pth - The GNU Portable Threads
3dnl ##  Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
4dnl ##
5dnl ##  This file is part of GNU Pth, a non-preemptive thread scheduling
6dnl ##  library which can be found at http://www.gnu.org/software/pth/.
7dnl ##
8dnl ##  This library is free software; you can redistribute it and/or
9dnl ##  modify it under the terms of the GNU Lesser General Public
10dnl ##  License as published by the Free Software Foundation; either
11dnl ##  version 2.1 of the License, or (at your option) any later version.
12dnl ##
13dnl ##  This library is distributed in the hope that it will be useful,
14dnl ##  but WITHOUT ANY WARRANTY; without even the implied warranty of
15dnl ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16dnl ##  Lesser General Public License for more details.
17dnl ##
18dnl ##  You should have received a copy of the GNU Lesser General Public
19dnl ##  License along with this library; if not, write to the Free Software
20dnl ##  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21dnl ##  USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
22dnl ##
23dnl ##  aclocal.m4: Pth Autoconf macros
24dnl ##
25                        dnl # ``"Reuse an expert's code" is the right
26                        dnl #   advice for most people. But it's a useless
27                        dnl #   advice for the experts writing the code
28                        dnl #   in the first place.'
29                        dnl #               -- Dan J. Bernstein
30
31dnl ##
32dnl ##  Display Configuration Headers
33dnl ##
34dnl ##  configure.ac:
35dnl ##    AC_MSG_PART(<text>)
36dnl ##
37
38m4_define(AC_MSG_PART,[dnl
39if test ".$enable_subdir" != .yes; then
40    AC_MSG_RESULT()
41    AC_MSG_RESULT(${TB}$1:${TN})
42fi
43])dnl
44
45dnl ##
46dnl ##  Display a message under --verbose
47dnl ##
48dnl ##  configure.ac:
49dnl ##    AC_MSG_VERBOSE(<text>)
50dnl ##
51
52m4_define(AC_MSG_VERBOSE,[dnl
53if test ".$verbose" = .yes; then
54    AC_MSG_RESULT([  $1])
55fi
56])
57
58dnl ##
59dnl ##  Do not display message for a command
60dnl ##
61dnl ##  configure.ac:
62dnl ##    AC_MSG_SILENT(...)
63dnl ##
64
65m4_define(AC_FD_TMP, 9)
66m4_define(AC_MSG_SILENT,[dnl
67exec AC_FD_TMP>&AC_FD_MSG AC_FD_MSG>/dev/null
68$1
69exec AC_FD_MSG>&AC_FD_TMP AC_FD_TMP>&-
70])
71
72dnl ##
73dnl ##  Perform something only once
74dnl ##
75dnl ##  configure.ac:
76dnl ##    AC_ONCE(<action>)
77dnl ##
78
79m4_define(AC_ONCE,[
80ifelse(ac_once_$1, already_done, ,[
81    m4_define(ac_once_$1, already_done)
82    $2
83])dnl
84])
85
86dnl ##
87dnl ##  Support for $(S)
88dnl ##
89dnl ##  configure.ac:
90dnl ##    AC_SRCDIR_PREFIX(<varname>)
91dnl ##
92
93AC_DEFUN(AC_SRCDIR_PREFIX,[
94ac_prog=[$]0
95changequote(, )dnl
96ac_srcdir=`echo $ac_prog | sed -e 's%/[^/][^/]*$%%' -e 's%\([^/]\)/*$%\1%'`
97changequote([, ])dnl
98if test ".$ac_srcdir" = ".$ac_prog"; then
99    ac_srcdir=""
100elif test "x$ac_srcdir" = "x."; then
101    ac_srcdir=""
102else
103    if test ".$CFLAGS" = .; then
104        CFLAGS="-I$ac_srcdir"
105    else
106        CFLAGS="$CFLAGS -I$ac_srcdir"
107    fi
108    ac_srcdir="$ac_srcdir/"
109fi
110$1="$ac_srcdir"
111AC_SUBST($1)
112])dnl
113
114dnl ##
115dnl ##  Support for --enable-subdir (for use with pth.m4)
116dnl ##
117dnl ##  configure.ac:
118dnl ##    AC_ENABLESUBDIR
119dnl ##
120
121AC_DEFUN(AC_ENABLESUBDIR,[
122AC_ARG_ENABLE(subdir,dnl
123[  --enable-subdir         enable local building as subdirectory (default=no)],[dnl
124],[dnl
125enable_subdir=no
126])dnl
127if test ".$enable_subdir" = .yes; then
128    enable_batch=yes
129    enable_shared=no
130fi
131])dnl
132
133dnl ##
134dnl ##  Support for Configuration Headers
135dnl ##
136dnl ##  configure.ac:
137dnl ##    AC_HEADLINE(<short-name>, <long-name>,
138dnl ##                <vers-var>, <vers-file>,
139dnl ##                <copyright>)
140dnl ##
141
142AC_DEFUN(AC_HEADLINE,[dnl
143#   configuration header
144if test ".`echo dummy [$]@ | grep enable-subdir`" != .; then
145    enable_subdir=yes
146fi
147if test ".`echo dummy [$]@ | grep help`" = .; then
148    #   bootstrapping shtool
149    ac_prog=[$]0
150changequote(, )dnl
151    ac_srcdir=`echo $ac_prog | sed -e 's%/[^/][^/]*$%%' -e 's%\([^/]\)/*$%\1%'`
152changequote([, ])dnl
153    test ".$ac_srcdir" = ".$ac_prog" && ac_srcdir=.
154    ac_shtool="${CONFIG_SHELL-/bin/sh} $ac_srcdir/shtool"
155
156    #   find out terminal sequences
157    if test ".$enable_subdir" != .yes; then
158        TB=`$ac_shtool echo -n -e %B 2>/dev/null`
159        TN=`$ac_shtool echo -n -e %b 2>/dev/null`
160    else
161        TB=''
162        TN=''
163    fi
164
165    #   find out package version
166    $3_STR="`$ac_shtool version -lc -dlong $ac_srcdir/$4`"
167    AC_SUBST($3_STR)
168
169    #   friendly header ;)
170    if test ".$enable_subdir" != .yes; then
171        echo "Configuring ${TB}$1${TN} ($2), Version ${TB}${$3_STR}${TN}"
172        echo "$5"
173    fi
174
175    #   additionally find out hex version
176    $3_HEX="`$ac_shtool version -lc -dhex $ac_srcdir/$4`"
177    AC_SUBST($3_HEX)
178fi
179])dnl
180
181dnl ##
182dnl ##  Support for Platform IDs
183dnl ##
184dnl ##  configure.ac:
185dnl ##    AC_PLATFORM(<variable>)
186dnl ##
187
188AC_DEFUN(AC_PLATFORM,[
189if test ".$host" != .; then
190    $1="$host"
191else
192    $1=`${CONFIG_SHELL-/bin/sh} $srcdir/config.guess`
193fi
194$1=`${CONFIG_SHELL-/bin/sh} $srcdir/config.sub $$1` || exit 1
195AC_SUBST($1)
196if test ".$enable_subdir" != .yes; then
197    echo "Platform: ${TB}${$1}${TN}"
198fi
199])dnl
200
201dnl ##
202dnl ##  Support for config.param files
203dnl ##
204dnl ##  configure.ac:
205dnl ##    AC_CONFIG_PARAM(<file>)
206dnl ##
207
208AC_DEFUN(AC_CONFIG_PARAM,[
209AC_DIVERT_PUSH(-1)
210AC_ARG_WITH(param,[  --with-param=ID[[,ID,..]] load parameters from $1])
211AC_DIVERT_POP()
212AC_DIVERT_PUSH(NOTICE)
213ac_prev=""
214ac_param=""
215if test -f $1; then
216    ac_param="$1:common"
217fi
218for ac_option
219do
220    if test ".$ac_prev" != .; then
221        eval "$ac_prev=\$ac_option"
222        ac_prev=""
223        continue
224    fi
225    case "$ac_option" in
226        -*=*) ac_optarg=`echo "$ac_option" | sed 's/[[-_a-zA-Z0-9]]*=//'` ;;
227           *) ac_optarg="" ;;
228    esac
229    case "$ac_option" in
230        --with-param=* )
231            case $ac_optarg in
232                *:* )
233                    ac_from=`echo $ac_optarg | sed -e 's/:.*//'`
234                    ac_what=`echo $ac_optarg | sed -e 's/.*://'`
235                    ;;
236                * )
237                    ac_from="$1"
238                    ac_what="$ac_optarg"
239                    ;;
240            esac
241            if test ".$ac_param" = .; then
242                ac_param="$ac_from:$ac_what"
243            else
244                ac_param="$ac_param,$ac_from:$ac_what"
245            fi
246            ;;
247    esac
248done
249if test ".$ac_param" != .; then
250    # echo "loading parameters"
251    OIFS="$IFS"
252    IFS=","
253    pconf="/tmp/autoconf.$$"
254    echo "ac_options=''" >$pconf
255    ac_from="$1"
256    for ac_section in $ac_param; do
257        changequote(, )
258        case $ac_section in
259            *:* )
260                ac_from=`echo "$ac_section" | sed -e 's/:.*//'`
261                ac_section=`echo "$ac_section" | sed -e 's/.*://'`
262                ;;
263        esac
264        (echo ''; cat $ac_from; echo '') |\
265        sed -e "1,/[ 	]*[ 	]*${ac_section}[ 	]*{[ 	]*/d" \
266            -e '/[ 	]*}[ 	]*/,$d' \
267            -e ':join' -e '/\\[ 	]*$/N' -e 's/\\[ 	]*\n[ 	]*//' -e 'tjoin' \
268            -e 's/^[ 	]*//g' \
269            -e 's/^\([^-].*=.*\) IF \(.*\)$/if \2; then \1; fi/' \
270            -e 's/^\(--.*=.*\) IF \(.*\)$/if \2; then ac_options="$ac_options \1"; fi/' \
271            -e 's/^\(--.*\) IF \(.*\)$/if \2; then ac_options="$ac_options \1"; fi/' \
272            -e 's/^\(--.*=.*\)$/ac_options="$ac_options \1"/' \
273            -e 's/^\(--.*\)$/ac_options="$ac_options \1"/' \
274            >>$pconf
275        changequote([, ])
276    done
277    IFS="$OIFS"
278    . $pconf
279    rm -f $pconf >/dev/null 2>&1
280    if test ".[$]*" = .; then
281        set -- $ac_options
282    else
283        set -- "[$]@" $ac_options
284    fi
285fi
286AC_DIVERT_POP()
287])dnl
288
289dnl ##
290dnl ##  Check whether compiler option works
291dnl ##
292dnl ##  configure.ac:
293dnl ##    AC_COMPILER_OPTION(<name>, <display>, <option>,
294dnl ##                       <action-success>, <action-failure>)
295dnl ##
296
297AC_DEFUN(AC_COMPILER_OPTION,[dnl
298AC_MSG_CHECKING(for compiler option $2)
299AC_CACHE_VAL(ac_cv_compiler_option_$1,[
300cat >conftest.$ac_ext <<EOF
301int main() { return 0; }
302EOF
303${CC-cc} -c $CFLAGS $CPPFLAGS $3 conftest.$ac_ext 1>conftest.out 2>conftest.err
304if test $? -ne 0 -o -s conftest.err; then
305     ac_cv_compiler_option_$1=no
306else
307     ac_cv_compiler_option_$1=yes
308fi
309rm -f conftest.$ac_ext conftest.out conftest.err
310])dnl
311if test ".$ac_cv_compiler_option_$1" = .yes; then
312    ifelse([$4], , :, [$4])
313else
314    ifelse([$5], , :, [$5])
315fi
316AC_MSG_RESULT([$ac_cv_compiler_option_$1])
317])dnl
318
319dnl ##
320dnl ##  Check whether `long long' and `long double' type exists
321dnl ##
322dnl ##  configure.ac:
323dnl ##    AC_CHECK_LONGLONG
324dnl ##    AC_CHECK_LONGDOUBLE
325dnl ##
326
327AC_DEFUN(AC_CHECK_LONGLONG,[dnl
328AC_MSG_CHECKING(for built-in type long long)
329 AC_CACHE_VAL(ac_cv_type_longlong,[dnl
330  AC_TRY_LINK([
331#include <sys/types.h>
332  ],[
333    long long X = 2, Y = 1, Z;
334    Z = X / Y;
335  ],
336    ac_cv_type_longlong=yes,
337    ac_cv_type_longlong=no
338  )dnl
339 ])dnl
340AC_MSG_RESULT([$ac_cv_type_longlong])
341if test ".$ac_cv_type_longlong" = .yes; then
342    AC_DEFINE(HAVE_LONGLONG, 1, [define if "long long" type exists])
343fi
344])dnl
345
346AC_DEFUN(AC_CHECK_LONGDOUBLE,[dnl
347AC_MSG_CHECKING(for built-in type long double)
348 AC_CACHE_VAL(ac_cv_type_longdouble,[dnl
349  AC_TRY_LINK([
350#include <sys/types.h>
351  ],[
352    long double X = 2, Y = 1, Z;
353    Z = X / Y;
354  ],
355    ac_cv_type_longdouble=yes,
356    ac_cv_type_longdouble=no
357  )dnl
358 ])dnl
359AC_MSG_RESULT([$ac_cv_type_longdouble])
360if test ".$ac_cv_type_longdouble" = .yes; then
361    AC_DEFINE(HAVE_LONGDOUBLE, 1, [define if "long double" type exists])
362fi
363])dnl
364
365dnl ##
366dnl ##  Debugging Support
367dnl ##
368dnl ##  configure.ac:
369dnl ##    AC_CHECK_DEBUGGING
370dnl ##
371
372AC_DEFUN(AC_CHECK_DEBUGGING,[dnl
373AC_ARG_ENABLE(debug,dnl
374[  --enable-debug          build for debugging (default=no)],
375[dnl
376if test ".$ac_cv_prog_gcc" = ".yes"; then
377    case "$CFLAGS" in
378        *-O* ) ;;
379           * ) CFLAGS="$CFLAGS -O2" ;;
380    esac
381    case "$CFLAGS" in
382        *-g* ) ;;
383           * ) CFLAGS="$CFLAGS -g" ;;
384    esac
385    case "$CFLAGS" in
386        *-pipe* ) ;;
387              * ) AC_COMPILER_OPTION(pipe, -pipe, -pipe, CFLAGS="$CFLAGS -pipe") ;;
388    esac
389    case $PLATFORM in
390        *-*-freebsd*|*-*-solaris* ) CFLAGS="$CFLAGS -pedantic" ;;
391    esac
392    CFLAGS="$CFLAGS -Wall"
393    WMORE="-Wshadow -Wpointer-arith -Wcast-align -Winline"
394    WMORE="$WMORE -Wmissing-prototypes -Wmissing-declarations -Wnested-externs"
395    AC_COMPILER_OPTION(wmore, -W<xxx>, $WMORE, CFLAGS="$CFLAGS $WMORE")
396    AC_COMPILER_OPTION(wnolonglong, -Wno-long-long, -Wno-long-long, CFLAGS="$CFLAGS -Wno-long-long")
397    AC_COMPILER_OPTION(fnostrictaliasing, -fno-strict-aliasing, -fno-strict-aliasing, CFLAGS="$CFLAGS -fno-strict-aliasing")
398else
399    case "$CFLAGS" in
400        *-g* ) ;;
401           * ) CFLAGS="$CFLAGS -g" ;;
402    esac
403fi
404msg="enabled"
405AC_DEFINE(PTH_DEBUG, 1, [define to enable Pth debugging])
406],[
407if test ".$ac_cv_prog_gcc" = ".yes"; then
408case "$CFLAGS" in
409    *-pipe* ) ;;
410          * ) AC_COMPILER_OPTION(pipe, -pipe, -pipe, CFLAGS="$CFLAGS -pipe") ;;
411esac
412fi
413case "$CFLAGS" in
414    *-g* ) CFLAGS=`echo "$CFLAGS" |\
415                   sed -e 's/ -g / /g' -e 's/ -g$//' -e 's/^-g //g' -e 's/^-g$//'` ;;
416esac
417case "$CXXFLAGS" in
418    *-g* ) CXXFLAGS=`echo "$CXXFLAGS" |\
419                     sed -e 's/ -g / /g' -e 's/ -g$//' -e 's/^-g //g' -e 's/^-g$//'` ;;
420esac
421msg="disabled"
422])dnl
423AC_MSG_CHECKING(for compilation debug mode)
424AC_MSG_RESULT([$msg])
425if test ".$msg" = .enabled; then
426    enable_shared=no
427fi
428])
429
430dnl ##
431dnl ##  Profiling Support
432dnl ##
433dnl ##  configure.ac:
434dnl ##    AC_CHECK_PROFILING
435dnl ##
436
437AC_DEFUN(AC_CHECK_PROFILING,[dnl
438AC_MSG_CHECKING(for compilation profile mode)
439AC_ARG_ENABLE(profile,dnl
440[  --enable-profile        build for profiling (default=no)],
441[dnl
442if test ".$ac_cv_prog_gcc" = ".no"; then
443    AC_MSG_ERROR([profiling requires gcc and gprof])
444fi
445CFLAGS=`echo "$CFLAGS" | sed -e 's/-O2//g'`
446CFLAGS="$CFLAGS -O0 -pg"
447LDFLAGS="$LDFLAGS -pg"
448msg="enabled"
449],[
450msg="disabled"
451])dnl
452AC_MSG_RESULT([$msg])
453if test ".$msg" = .enabled; then
454    enable_shared=no
455fi
456])
457
458dnl ##
459dnl ##  Build Parameters
460dnl ##
461dnl ##  configure.ac:
462dnl ##    AC_CHECK_BUILDPARAM
463dnl ##
464
465AC_DEFUN(AC_CHECK_BUILDPARAM,[dnl
466dnl #   determine build mode
467AC_MSG_CHECKING(whether to activate batch build mode)
468AC_ARG_ENABLE(batch,dnl
469[  --enable-batch          enable batch build mode (default=no)],[dnl
470],[dnl
471enable_batch=no
472])dnl
473if test ".$silent" = .yes; then
474    enable_batch=yes
475fi
476AC_MSG_RESULT([$enable_batch])
477BATCH="$enable_batch"
478AC_SUBST(BATCH)
479dnl #   determine build targets
480TARGET_ALL='$(TARGET_PREQ) $(TARGET_LIBS)'
481AC_MSG_CHECKING(whether to activate maintainer build targets)
482AC_ARG_ENABLE(maintainer,dnl
483[  --enable-maintainer     enable maintainer build targets (default=no)],[dnl
484],[dnl
485enable_maintainer=no
486])dnl
487AC_MSG_RESULT([$enable_maintainer])
488if test ".$enable_maintainer" = .yes; then
489    TARGET_ALL="$TARGET_ALL \$(TARGET_MANS)"
490fi
491AC_MSG_CHECKING(whether to activate test build targets)
492AC_ARG_ENABLE(tests,dnl
493[  --enable-tests          enable test build targets (default=yes)],[dnl
494],[dnl
495enable_tests=yes
496])dnl
497AC_MSG_RESULT([$enable_tests])
498if test ".$enable_tests" = .yes; then
499    TARGET_ALL="$TARGET_ALL \$(TARGET_TEST)"
500fi
501AC_SUBST(TARGET_ALL)
502])
503
504dnl ##
505dnl ##  Optimization Support
506dnl ##
507dnl ##  configure.ac:
508dnl ##    AC_CHECK_OPTIMIZE
509dnl ##
510
511AC_DEFUN(AC_CHECK_OPTIMIZE,[dnl
512AC_ARG_ENABLE(optimize,dnl
513[  --enable-optimize       build with optimization (default=no)],
514[dnl
515if test ".$ac_cv_prog_gcc" = ".yes"; then
516    #  compiler is gcc
517    case "$CFLAGS" in
518        *-O* ) ;;
519        * ) CFLAGS="$CFLAGS -O2" ;;
520    esac
521    case "$CFLAGS" in
522        *-pipe* ) ;;
523        * ) AC_COMPILER_OPTION(pipe, -pipe, -pipe, CFLAGS="$CFLAGS -pipe") ;;
524    esac
525    OPT_CFLAGS='-funroll-loops -fstrength-reduce -fomit-frame-pointer -ffast-math'
526    AC_COMPILER_OPTION(optimize_std, [-f<xxx> for optimizations], $OPT_CFLAGS, CFLAGS="$CFLAGS $OPT_CFLAGS")
527    case $PLATFORM in
528        i?86*-*-*|?86*-*-* )
529            OPT_CFLAGS='-malign-functions=4 -malign-jumps=4 -malign-loops=4'
530            AC_COMPILER_OPTION(optimize_x86, [-f<xxx> for Intel x86 CPU], $OPT_CFLAGS, CFLAGS="$CFLAGS $OPT_CFLAGS")
531            ;;
532    esac
533else
534    #  compiler is NOT gcc
535    case "$CFLAGS" in
536        *-O* ) ;;
537           * ) CFLAGS="$CFLAGS -O" ;;
538    esac
539    case $PLATFORM in
540        *-*-solaris* )
541            AC_COMPILER_OPTION(fast, -fast, -fast, CFLAGS="$CFLAGS -fast")
542            ;;
543    esac
544fi
545msg="enabled"
546],[
547msg="disabled"
548])dnl
549AC_MSG_CHECKING(for compilation optimization mode)
550AC_MSG_RESULT([$msg])
551])
552
553dnl ##
554dnl ##  Check for a pre-processor define in a header
555dnl ##
556dnl ##  configure.ac:
557dnl ##    AC_CHECK_DEFINE(<define>, <header>)
558dnl ##  acconfig.h:
559dnl ##    #undef HAVE_<define>
560dnl ##
561
562AC_DEFUN(AC_CHECK_DEFINE,[dnl
563AC_MSG_CHECKING(for define $1 in $2)
564AC_CACHE_VAL(ac_cv_define_$1,
565[AC_EGREP_CPP([YES_IS_DEFINED], [
566#include <$2>
567#ifdef $1
568YES_IS_DEFINED
569#endif
570], ac_cv_define_$1=yes, ac_cv_define_$1=no)])dnl
571AC_MSG_RESULT($ac_cv_define_$1)
572if test $ac_cv_define_$1 = yes; then
573    AC_DEFINE(HAVE_[]translit($1, [a-z], [A-Z]), 1,
574              [define if pre-processor define $1 exists in header $2])
575fi
576])
577
578dnl ##
579dnl ##  Check for an ANSI C typedef in a header
580dnl ##
581dnl ##  configure.ac:
582dnl ##    AC_CHECK_TYPEDEF(<typedef>, <header>)
583dnl ##  acconfig.h:
584dnl ##    #undef HAVE_<typedef>
585dnl ##
586
587AC_DEFUN(AC_CHECK_TYPEDEF,[dnl
588AC_REQUIRE([AC_HEADER_STDC])dnl
589AC_MSG_CHECKING(for typedef $1)
590AC_CACHE_VAL(ac_cv_typedef_$1,
591[AC_EGREP_CPP(dnl
592changequote(<<,>>)dnl
593<<(^|[^a-zA-Z_0-9])$1[^a-zA-Z_0-9]>>dnl
594changequote([,]), [
595#include <$2>
596], ac_cv_typedef_$1=yes, ac_cv_typedef_$1=no)])dnl
597AC_MSG_RESULT($ac_cv_typedef_$1)
598if test $ac_cv_typedef_$1 = yes; then
599    AC_DEFINE(HAVE_[]translit($1, [a-z], [A-Z]), 1,
600              [define if typedef $1 exists in header $2])
601fi
602])
603
604dnl ##
605dnl ##  Check for an ANSI C struct attribute in a structure defined in a header
606dnl ##
607dnl ##  configure.ac:
608dnl ##    AC_CHECK_STRUCTATTR(<attr>, <struct>, <header>)
609dnl ##  acconfig.h:
610dnl ##    #undef HAVE_<attr>
611dnl ##
612
613AC_DEFUN(AC_CHECK_STRUCTATTR,[dnl
614AC_REQUIRE([AC_HEADER_STDC])dnl
615AC_MSG_CHECKING(for attribute $1 in struct $2 from $3)
616AC_CACHE_VAL(ac_cv_structattr_$1,[dnl
617AC_TRY_LINK([
618#include <sys/types.h>
619#include <$3>
620],[
621struct $2 *sp1;
622struct $2 *sp2;
623sp1->$1 = sp2->$1;
624], ac_cv_structattr_$1=yes, ac_cv_structattr_$1=no)])dnl
625AC_MSG_RESULT($ac_cv_structattr_$1)
626if test $ac_cv_structattr_$1 = yes; then
627    AC_DEFINE(HAVE_[]translit($1, [a-z], [A-Z]), 1,
628              [define if attribute $1 exists in struct $2 from header $3])
629fi
630])
631
632dnl ##
633dnl ##  Check for argument type of a function
634dnl ##
635dnl ##  configure.ac:
636dnl ##    AC_CHECK_ARGTYPE(<header> [...], <func>, <arg-number>,
637dnl ##                     <max-arg-number>, <action-with-${ac_type}>)
638dnl ##
639
640AC_DEFUN(AC_CHECK_ARGTYPE,[dnl
641AC_REQUIRE_CPP()dnl
642AC_MSG_CHECKING([for type of argument $3 for $2()])
643AC_CACHE_VAL([ac_cv_argtype_$2$3],[
644cat >conftest.$ac_ext <<EOF
645[#]line __oline__ "configure"
646#include "confdefs.h"
647EOF
648for ifile in $1; do
649    echo "#include <$ifile>" >>conftest.$ac_ext
650done
651gpat=''
652spat=''
653i=1
654changequote(, )dnl
655while test $i -le $4; do
656    gpat="$gpat[^,]*"
657    if test $i -eq $3; then
658        spat="$spat\\([^,]*\\)"
659    else
660        spat="$spat[^,]*"
661    fi
662    if test $i -lt $4; then
663        gpat="$gpat,"
664        spat="$spat,"
665    fi
666    i=`expr $i + 1`
667done
668changequote([, ])dnl
669(eval "$ac_cpp conftest.$ac_ext") 2>&AC_FD_CC |\
670changequote(, )dnl
671sed -e ':join' \
672    -e '/,[ 	]*$/N' \
673    -e 's/,[ 	]*\n[ 	]*/, /' \
674    -e 'tjoin' |\
675egrep "[^a-zA-Z0-9_]$2[ 	]*\\($gpat\\)" | head -1 |\
676sed -e "s/.*[^a-zA-Z0-9_]$2[ 	]*($spat).*/\\1/" \
677    -e 's/(\*[a-zA-Z_][a-zA-Z_0-9]*)/(*)/' \
678    -e 's/^[ 	]*//' -e 's/[ 	]*$//' \
679    -e 's/^/arg:/' \
680    -e 's/^arg:\([^ 	]*\)$/type:\1/' \
681    -e 's/^arg:\(.*_t\)*$/type:\1/' \
682    -e 's/^arg:\(.*\*\)$/type:\1/' \
683    -e 's/^arg:\(.*[ 	]\*\)[_a-zA-Z][_a-zA-Z0-9]*$/type:\1/' \
684    -e 's/^arg:\(.*[ 	]char\)$/type:\1/' \
685    -e 's/^arg:\(.*[ 	]short\)$/type:\1/' \
686    -e 's/^arg:\(.*[ 	]int\)$/type:\1/' \
687    -e 's/^arg:\(.*[ 	]long\)$/type:\1/' \
688    -e 's/^arg:\(.*[ 	]float\)$/type:\1/' \
689    -e 's/^arg:\(.*[ 	]double\)$/type:\1/' \
690    -e 's/^arg:\(.*[ 	]unsigned\)$/type:\1/' \
691    -e 's/^arg:\(.*[ 	]signed\)$/type:\1/' \
692    -e 's/^arg:\(.*struct[ 	][_a-zA-Z][_a-zA-Z0-9]*\)$/type:\1/' \
693    -e 's/^arg:\(.*\)[ 	]_[_a-zA-Z0-9]*$/type:\1/' \
694    -e 's/^arg:\(.*\)[ 	]\([^ 	]*\)$/type:\1/' \
695    -e 's/^type://' >conftest.output
696ac_cv_argtype_$2$3=`cat conftest.output`
697changequote([, ])dnl
698rm -f conftest*
699])
700AC_MSG_RESULT([$ac_cv_argtype_$2$3])
701ac_type="$ac_cv_argtype_$2$3"
702[$5]
703])
704
705dnl ##
706dnl ##  Decision Hierachy
707dnl ##
708
709m4_define(AC_IFALLYES,[dnl
710ac_rc=yes
711for ac_spec in $1; do
712    ac_type=`echo "$ac_spec" | sed -e 's/:.*$//'`
713    ac_item=`echo "$ac_spec" | sed -e 's/^.*://'`
714    case $ac_type in
715        header [)]
716            ac_item=`echo "$ac_item" | sed 'y%./+-%__p_%'`
717            ac_var="ac_cv_header_$ac_item"
718            ;;
719        file [)]
720            ac_item=`echo "$ac_item" | sed 'y%./+-%__p_%'`
721            ac_var="ac_cv_file_$ac_item"
722            ;;
723        func    [)] ac_var="ac_cv_func_$ac_item"   ;;
724        lib     [)] ac_var="ac_cv_lib_$ac_item"    ;;
725        define  [)] ac_var="ac_cv_define_$ac_item" ;;
726        typedef [)] ac_var="ac_cv_typedef_$ac_item" ;;
727        custom  [)] ac_var="$ac_item" ;;
728    esac
729    eval "ac_val=\$$ac_var"
730    if test ".$ac_val" != .yes; then
731        ac_rc=no
732        break
733    fi
734done
735if test ".$ac_rc" = .yes; then
736    :
737    $2
738else
739    :
740    $3
741fi
742])
743
744m4_define(AC_BEGIN_DECISION,[dnl
745ac_decision_item='$1'
746ac_decision_msg='FAILED'
747ac_decision=''
748])
749
750m4_define(AC_DECIDE,[dnl
751ac_decision='$1'
752ac_decision_msg='$2'
753ac_decision_$1=yes
754ac_decision_$1_msg='$2'
755])
756
757m4_define(AC_DECISION_OVERRIDE,[dnl
758    ac_decision=''
759    for ac_item in $1; do
760         eval "ac_decision_this=\$ac_decision_${ac_item}"
761         if test ".$ac_decision_this" = .yes; then
762             ac_decision=$ac_item
763             eval "ac_decision_msg=\$ac_decision_${ac_item}_msg"
764         fi
765    done
766])
767
768m4_define(AC_DECISION_FORCE,[dnl
769ac_decision="$1"
770eval "ac_decision_msg=\"\$ac_decision_${ac_decision}_msg\""
771])
772
773m4_define(AC_END_DECISION,[dnl
774if test ".$ac_decision" = .; then
775    echo "[$]0:Error: decision on $ac_decision_item failed." 1>&2
776    echo "[$]0:Hint: see config.log for more details!" 1>&2
777    exit 1
778else
779    if test ".$ac_decision_msg" = .; then
780        ac_decision_msg="$ac_decision"
781    fi
782    AC_MSG_RESULT([decision on $ac_decision_item... ${TB}$ac_decision_msg${TN}])
783fi
784])
785
786dnl ##
787dnl ##  Check for existance of a file
788dnl ##
789dnl ##  configure.ac:
790dnl ##    AC_TEST_FILE(<file>, <success-action>, <failure-action>)
791dnl ##
792
793AC_DEFUN(AC_TEST_FILE, [
794ac_safe=`echo "$1" | sed 'y%./+-%__p_%'`
795AC_MSG_CHECKING([for $1])
796AC_CACHE_VAL(ac_cv_file_$ac_safe, [
797if test -r $1; then
798    eval "ac_cv_file_$ac_safe=yes"
799else
800    eval "ac_cv_file_$ac_safe=no"
801fi
802])dnl
803if eval "test \"`echo '$ac_cv_file_'$ac_safe`\" = yes"; then
804    AC_MSG_RESULT(yes)
805    ifelse([$2], , :, [$2])
806else
807    AC_MSG_RESULT(no)
808    ifelse([$3], , :, [$3])
809fi
810])
811
812dnl ##
813dnl ##  Check for socket/network size type
814dnl ##
815dnl ##  configure.ac:
816dnl ##    AC_CHECK_SOCKLENTYPE(<action-with-${ac_type}>)
817dnl ##
818
819dnl #   Background:
820dnl #   this exists because of shortsightedness on the POSIX committee.
821dnl #   BSD systems used "int *" as the parameter to accept(2),
822dnl #   getsockname(2), getpeername(2) et al. Consequently many Unix
823dnl #   flavors took an "int *" for that parameter. The POSIX committee
824dnl #   decided that "int" was just too generic and had to be replaced
825dnl #   with "size_t" almost everywhere. There's no problem with that
826dnl #   when you're passing by value. But when you're passing by
827dnl #   reference (as it is the case for accept(2) and friends) this
828dnl #   creates a gross source incompatibility with existing programs.
829dnl #   On 32-bit architectures it creates only a warning. On 64-bit
830dnl #   architectures it creates broken code -- because "int *" is a
831dnl #   pointer to a 64-bit quantity and "size_t *" is usually a pointer
832dnl #   to a 32-bit quantity. Some Unix flavors adopted "size_t *" for
833dnl #   the sake of POSIX compliance. Others ignored it because it was
834dnl #   such a broken interface. Chaos ensued. POSIX finally woke up
835dnl #   and decided that it was wrong and created a new type socklen_t.
836dnl #   The only useful value for socklen_t is "int", and that's how
837dnl #   everyone who has a clue implements it. It is almost always the
838dnl #   case that this type should be defined to be an "int", unless the
839dnl #   system being compiled for was created in the window of POSIX
840dnl #   madness.
841
842AC_DEFUN(AC_CHECK_SOCKLENTYPE,[dnl
843AC_CHECK_TYPEDEF(socklen_t, sys/socket.h)
844AC_CHECK_ARGTYPE(sys/types.h sys/socket.h, accept, 3, 3, [:])
845AC_MSG_CHECKING(for fallback socklen_t)
846AC_CACHE_VAL(ac_cv_check_socklentype, [
847if test ".$ac_cv_typedef_socklen_t" = .yes; then
848    ac_cv_check_socklentype='socklen_t'
849elif test ".$ac_type" != .; then
850    ac_cv_check_socklentype=`echo "$ac_type" | sed -e 's/[ 	]*\*$//'`
851else
852    ac_cv_check_socklentype='int'
853fi
854])
855AC_MSG_RESULT([$ac_cv_check_socklentype])
856ac_type="$ac_cv_check_socklentype"
857ifelse([$1], , :, [$1])
858])
859
860dnl ##
861dnl ##  Check for filedescriptor number type
862dnl ##
863dnl ##  configure.ac:
864dnl ##    AC_CHECK_NFDSTYPE(<action-with-${ac_type}>)
865dnl ##
866
867AC_DEFUN(AC_CHECK_NFDSTYPE,[dnl
868AC_CHECK_TYPEDEF(nfds_t, poll.h)
869AC_CHECK_ARGTYPE(sys/types.h poll.h, poll, 2, 3, [:])
870AC_MSG_CHECKING(for fallback nfds_t)
871AC_CACHE_VAL(ac_cv_check_nfdstype, [
872if test ".$ac_cv_typedef_nfds_t" = .yes; then
873    ac_cv_check_nfdstype='nfds_t'
874elif test ".$ac_type" != .; then
875    ac_cv_check_nfdstype=`echo "$ac_type" | sed -e 's/[ 	]*\*$//'`
876else
877    ac_cv_check_nfdstype='unsigned int'
878fi
879])
880AC_MSG_RESULT([$ac_cv_check_nfdstype])
881ac_type="$ac_cv_check_nfdstype"
882ifelse([$1], , :, [$1])
883])
884
885dnl ##
886dnl ##  Check for direction of stack growth
887dnl ##
888dnl ##  configure.ac:
889dnl ##    AC_CHECK_STACKGROWTH(<define>)
890dnl ##  acconfig.h:
891dnl ##    #undef <define>
892dnl ##  source.c:
893dnl ##    #include "config.h"
894dnl ##    #if <define> < 0
895dnl ##        ...stack grow down...
896dnl ##    #else
897dnl ##        ...stack grow up...
898dnl ##    #endif
899dnl ##
900
901AC_DEFUN(AC_CHECK_STACKGROWTH,[dnl
902AC_MSG_CHECKING(for direction of stack growth)
903AC_CACHE_VAL(ac_cv_check_stackgrowth, [
904cross_compiling=no
905AC_TRY_RUN(
906changequote(<<, >>)dnl
907<<
908#include <stdio.h>
909#include <stdlib.h>
910static int iterate = 10;
911static int growsdown(int *x)
912{
913    auto int y;
914    y = (x > &y);
915    if (--iterate > 0)
916        y = growsdown(&y);
917    if (y != (x > &y))
918        exit(1);
919    return y;
920}
921int main(int argc, char *argv[])
922{
923    FILE *f;
924    auto int x;
925    if ((f = fopen("conftestval", "w")) == NULL)
926        exit(1);
927    fprintf(f, "%s\n", growsdown(&x) ? "down" : "up");;
928    fclose(f);
929    exit(0);
930}
931>>
932changequote([, ])dnl
933,
934ac_cv_check_stackgrowth=`cat conftestval`,
935ac_cv_check_stackgrowth=down,
936ac_cv_check_stackgrowth=down
937)dnl
938])dnl
939AC_MSG_RESULT([$ac_cv_check_stackgrowth])
940if test ".$ac_cv_check_stackgrowth" = ".down"; then
941    val="-1"
942else
943    val="+1"
944fi
945AC_DEFINE_UNQUOTED($1, $val, [define for stack growth])
946])
947
948dnl ##
949dnl ##  Check whether and how a POSIX compliant sigsetjmp(3) can be achieved
950dnl ##
951dnl ##  configure.ac:
952dnl ##    AC_CHECK_SJLJ(<success-action>, <failure-action>, <type-var>)
953dnl ##
954
955AC_DEFUN(AC_CHECK_SJLJ,[dnl
956AC_MSG_CHECKING(for signal-mask aware setjmp(3)/longjmp(3))
957AC_CACHE_VAL(ac_cv_check_sjlj, [
958AC_IFALLYES(func:setjmp func:longjmp, ac_cv_check_sjlj=sjlje, ac_cv_check_sjlj=none)
959cross_compiling=no
960for testtype in ssjlj sjlj usjlj; do
961OCFLAGS="$CFLAGS"
962CFLAGS="$CFLAGS -DTEST_${testtype}"
963AC_TRY_RUN(
964changequote(<<, >>)dnl
965<<
966#if defined(TEST_ssjlj)
967#define __JMP_BUF          sigjmp_buf
968#define __SETJMP(buf)      sigsetjmp(buf,1)
969#define __LONGJMP(buf,val) siglongjmp(buf,val)
970#elif defined(TEST_sjlj)
971#define __JMP_BUF          jmp_buf
972#define __SETJMP(buf)      setjmp(buf)
973#define __LONGJMP(buf,val) longjmp(buf,val)
974#elif defined(TEST_usjlj)
975#define __JMP_BUF          jmp_buf
976#define __SETJMP(buf)      _setjmp(buf)
977#define __LONGJMP(buf,val) _longjmp(buf,val)
978#endif
979
980#include <stdio.h>
981#include <sys/types.h>
982#include <signal.h>
983#include <setjmp.h>
984#include <unistd.h>
985
986static __JMP_BUF jb;
987
988static void sighandler(int sig)
989{
990    sigset_t sigs;
991
992    /* get signal mask */
993    sigprocmask(SIG_SETMASK, NULL, &sigs);
994
995    /* make sure USR1 is still blocked */
996    if (!sigismember(&sigs, SIGUSR1))
997        exit(1);
998
999    /* block USR2 for us */
1000    sigaddset(&sigs, SIGUSR2);
1001    sigprocmask(SIG_SETMASK, &sigs, NULL);
1002
1003    /* jump back to main */
1004    __LONGJMP(jb, 1);
1005    exit(1);
1006}
1007
1008int main(int argc, char *argv[])
1009{
1010    FILE *fp;
1011    sigset_t sigs;
1012    struct sigaction sa;
1013
1014    /* the default is that it fails */
1015    if ((fp = fopen("conftestval", "w")) == NULL)
1016        exit(1);
1017    fprintf(fp, "failed\n");
1018    fclose(fp);
1019
1020    /* block USR1 and unblock USR2 signal */
1021    sigprocmask(SIG_SETMASK, NULL, &sigs);
1022    sigaddset(&sigs, SIGUSR1);
1023    sigdelset(&sigs, SIGUSR2);
1024    sigprocmask(SIG_SETMASK, &sigs, NULL);
1025
1026    /* set jump buffer */
1027    if (__SETJMP(jb) == 0) {
1028
1029        /* install signal handler for USR1 */
1030        memset((void *)&sa, 0, sizeof(struct sigaction));
1031        sigemptyset(&sa.sa_mask);
1032        sa.sa_handler = sighandler;
1033        sa.sa_flags = 0;
1034        sigaction(SIGUSR1, &sa, NULL);
1035
1036        /* send USR1 signal (which is still blocked) */
1037        kill(getpid(), SIGUSR1);
1038
1039        /* unblock USR1 and wait for it */
1040        sigprocmask(SIG_SETMASK, NULL, &sigs);
1041        sigdelset(&sigs, SIGUSR1);
1042        sigsuspend(&sigs);
1043        exit(1);
1044    }
1045
1046    /* get signal mask again */
1047    sigprocmask(SIG_SETMASK, NULL, &sigs);
1048
1049    /* make sure USR2 is again unblocked */
1050    if (sigismember(&sigs, SIGUSR2))
1051        exit(1);
1052
1053    /* Fine... */
1054    if ((fp = fopen("conftestval", "w")) == NULL)
1055        exit(1);
1056    fprintf(fp, "ok\n");
1057    fclose(fp);
1058    exit(0);
1059}
1060>>
1061changequote([, ]),
1062rc=`cat conftestval`,
1063rc=failed,
1064rc=failed
1065)
1066CFLAGS="$OCFLAGS"
1067if test ".$rc" = .ok; then
1068    ac_cv_check_sjlj=$testtype
1069    break
1070fi
1071done
1072case $PLATFORM in
1073    *-*-linux* )
1074        braindead=no
1075        case "x`uname -r`" in
1076changequote(, )dnl
1077            x2.[23456789]* ) ;;
1078changequote([, ])
1079            * ) braindead=yes ;;
1080        esac
1081        case `grep __GLIBC_MINOR /usr/include/features.h | grep '#define' |\
1082              awk '{ printf("%s", [$]3 >= 1 ? "yes" : "no"); }'` in
1083            yes ) ;;
1084            * ) braindead=yes ;;
1085        esac
1086        case $braindead in
1087            yes ) ac_cv_check_sjlj=sjljlx ;;
1088            no  ) ac_cv_check_sjlj=ssjlj  ;;
1089        esac
1090        ;;
1091    *86-*-gnu0* )
1092        #   GNU/Hurd is similar to linux 2.0 in that it has
1093        #   non-functional stubs for sigstack and sigaltstack.
1094        ac_cv_check_sjlj=sjljlx
1095        ;;
1096    *-*-isc* )
1097        ac_cv_check_sjlj=sjljisc
1098        ;;
1099    *-*-interix )
1100        #   Interix is a POSIX sub-system on Windows-NT which
1101        #   can use the Interactive UNIX dispatching code.
1102        ac_cv_check_sjlj=sjljisc
1103        ;;
1104    *-*-cygwin* )
1105        ac_cv_check_sjlj=sjljw32
1106        ;;
1107esac
1108])dnl
1109$3="$ac_cv_check_sjlj"
1110if test ".$ac_cv_check_sjlj" != .none; then
1111    AC_MSG_RESULT([yes: $ac_cv_check_sjlj])
1112    ifelse([$1], , :, [$1])
1113else
1114    AC_MSG_RESULT([no])
1115    ifelse([$2], , :, [$2])
1116fi
1117])dnl
1118
1119dnl ##
1120dnl ##  Check for number of signals (NSIG)
1121dnl ##
1122dnl ##  configure.ac:
1123dnl ##    AC_CHECK_NSIG(<define>)
1124dnl ##  acconfig.h:
1125dnl ##    #undef <define>
1126dnl ##  source.c:
1127dnl ##    #include "config.h"
1128dnl ##    ...<define>...
1129
1130AC_DEFUN(AC_CHECK_NSIG,[dnl
1131AC_MSG_CHECKING(for number of signals)
1132cross_compiling=no
1133AC_TRY_RUN(
1134changequote(<<, >>)dnl
1135<<
1136#include <stdio.h>
1137#include <sys/types.h>
1138#include <signal.h>
1139
1140int main(int argc, char *argv[])
1141{
1142    FILE *fp;
1143    int nsig;
1144
1145#if defined(NSIG)
1146    nsig = NSIG;
1147#elif defined(_NSIG)
1148    nsig = _NSIG;
1149#else
1150    nsig = (sizeof(sigset_t)*8);
1151    if (nsig < 32)
1152        nsig = 32;
1153#endif
1154    if ((fp = fopen("conftestval", "w")) == NULL)
1155        exit(1);
1156    fprintf(fp, "%d\n", nsig);
1157    fclose(fp);
1158    exit(0);
1159}
1160>>
1161changequote([, ])dnl
1162,
1163nsig=`cat conftestval`,
1164nsig=32,
1165nsig=32
1166)
1167AC_MSG_RESULT([$nsig])
1168AC_DEFINE_UNQUOTED($1, $nsig, [define for number of signals])
1169])
1170
1171dnl ##
1172dnl ##  Check for FD_SETSIZE
1173dnl ##
1174dnl ##  configure.in:
1175dnl ##    AC_CHECK_FDSETSIZE(<varname>)
1176
1177AC_DEFUN(AC_CHECK_FDSETSIZE,[dnl
1178dnl #   1. determine default value
1179AC_MSG_CHECKING(for default value of FD_SETSIZE)
1180cross_compiling=no
1181AC_TRY_RUN(
1182changequote(<<, >>)dnl
1183<<
1184#include <stdio.h>
1185#include <sys/types.h>
1186#include <unistd.h>
1187int main(int argc, char *argv[])
1188{
1189    FILE *fp;
1190    int ac_fdsetsize;
1191#if defined(FD_SETSIZE)
1192    ac_fdsetsize = FD_SETSIZE;
1193#else
1194    ac_fdsetsize = (sizeof(fd_set)*8);
1195#endif
1196    if ((fp = fopen("conftestval", "w")) == NULL)
1197        exit(1);
1198    fprintf(fp, "%d\n", ac_fdsetsize);
1199    fclose(fp);
1200    exit(0);
1201}
1202>>
1203changequote([, ])dnl
1204,
1205ac_fdsetsize=`cat conftestval`,
1206ac_fdsetsize=1024,
1207ac_fdsetsize=1024
1208)
1209AC_MSG_RESULT([$ac_fdsetsize])
1210$1="$ac_fdsetsize"
1211AC_SUBST($1)
1212
1213dnl #   2. allow user to force a particular value
1214AC_ARG_WITH(fdsetsize,dnl
1215[  --with-fdsetsize=NUM    set FD_SETSIZE while building GNU Pth],[
1216case $withval in
1217    [[1-9]][[0-9]]|[[1-9]][[0-9]][[0-9]]|[[1-9]][[0-9]][[0-9]][[0-9]]) $1=$withval ;;
1218    * ) AC_ERROR([invalid FD_SETSIZE specified -- allowed: 10-9999]) ;;
1219esac
1220dnl # we cannot use the usual
1221dnl # AC_DEFINE_UNQUOTED(FD_SETSIZE, $$1, [Define to the maximum size of fd_set])
1222dnl # here, because FD_SETSIZE has to be defined already before any other
1223dnl # system headers are included. Hence we have to use CPPFLAGS.
1224CPPFLAGS="$CPPFLAGS -DFD_SETSIZE=$$1"
1225AC_MSG_CHECKING([for overridden FD_SETSIZE value])
1226AC_MSG_RESULT([$$1])
1227])
1228
1229])
1230
1231dnl ##
1232dnl ##  Check for an external/extension library.
1233dnl ##  - is aware of <libname>-config style scripts
1234dnl ##  - searches under standard paths include, lib, etc.
1235dnl ##  - searches under subareas like .libs, etc.
1236dnl ##
1237dnl ##  configure.ac:
1238dnl ##      AC_CHECK_EXTLIB(<realname>, <libname>, <func>, <header>,
1239dnl ##                      [<success-action> [, <fail-action>]])
1240dnl ##  Makefile.in:
1241dnl ##      CFLAGS  = @CFLAGS@
1242dnl ##      LDFLAGS = @LDFLAGS@
1243dnl ##      LIBS    = @LIBS@
1244dnl ##  shell:
1245dnl ##      $ ./configure --with-<libname>[=DIR]
1246dnl ##
1247
1248AC_DEFUN(AC_CHECK_EXTLIB,[dnl
1249AC_ARG_WITH($2, [dnl
1250[  --with-]m4_substr([$2[[=DIR]]                     ], 0, 19)[build with external $1 library (default=no)]], [dnl
1251    if test ".$with_$2" = .yes; then
1252        #   via config script in PATH
1253        $2_version=`($2-config --version) 2>/dev/null`
1254        if test ".$$2_version" != .; then
1255            CPPFLAGS="$CPPFLAGS `$2-config --cflags`"
1256            CFLAGS="$CFLAGS `$2-config --cflags`"
1257            LDFLAGS="$LDFLAGS `$2-config --ldflags`"
1258        fi
1259    else
1260        if test -d "$with_$2"; then
1261            found=0
1262            #   via config script
1263            for dir in $with_$2/bin $with_$2; do
1264                if test -f "$dir/$2-config" && test ! -f "$dir/$2-config.in"; then
1265                    $2_version=`($dir/$2-config --version) 2>/dev/null`
1266                    if test ".$$2_version" != .; then
1267                        CPPFLAGS="$CPPFLAGS `$dir/$2-config --cflags`"
1268                        CFLAGS="$CFLAGS `$dir/$2-config --cflags`"
1269                        LDFLAGS="$LDFLAGS `$dir/$2-config --ldflags`"
1270                        found=1
1271                        break
1272                    fi
1273                fi
1274            done
1275            #   in standard sub-areas
1276            if test ".$found" = .0; then
1277                for dir in $with_$2/include/$2 $with_$2/include $with_$2; do
1278                    if test -f "$dir/$4"; then
1279                        CPPFLAGS="$CPPFLAGS -I$dir"
1280                        CFLAGS="$CFLAGS -I$dir"
1281                        found=1
1282                        break
1283                    fi
1284                done
1285                for dir in $with_$2/lib/$2 $with_$2/lib $with_$2; do
1286                    if test -f "$dir/lib$2.la" && test -d "$dir/.libs"; then
1287                        LDFLAGS="$LDFLAGS -L$dir -L$dir/.libs"
1288                        found=1
1289                        break
1290                    elif test -f "$dir/lib$2.a" || test -f "$dir/lib$2.so"; then
1291                        LDFLAGS="$LDFLAGS -L$dir"
1292                        found=1
1293                        break
1294                    fi
1295                done
1296            fi
1297            #   in any sub-area
1298            if test ".$found" = .0; then
1299changequote(, )dnl
1300                for file in x `find $with_$2 -name "$4" -type f -print`; do
1301                    test .$file = .x && continue
1302                    dir=`echo $file | sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;'`
1303                    CPPFLAGS="$CPPFLAGS -I$dir"
1304                    CFLAGS="$CFLAGS -I$dir"
1305                done
1306                for file in x `find $with_$2 -name "lib$2.[aso]" -type f -print`; do
1307                    test .$file = .x && continue
1308                    dir=`echo $file | sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;'`
1309                    LDFLAGS="$LDFLAGS -L$dir"
1310                done
1311changequote([, ])dnl
1312            fi
1313        fi
1314    fi
1315    AC_HAVE_HEADERS($4)
1316    AC_CHECK_LIB($2, $3)
1317    with_$2=yes
1318    ac_var="ac_cv_header_`echo $4 | sed 'y%./+-%__p_%'`"
1319    eval "ac_val=\$$ac_var"
1320    if test ".$ac_val" != .yes; then
1321        with_$2=no
1322    fi
1323    if test ".$ac_cv_lib_$2_$3" != .yes; then
1324        with_$2=no
1325    fi
1326    if test ".$with_$2" = .no; then
1327        AC_ERROR([Unable to find $1 library])
1328    fi
1329    ], [dnl
1330if test ".$with_$2" = .; then
1331    with_$2=no
1332fi
1333    ])dnl
1334AC_MSG_CHECKING(whether to build against external $1 library)
1335if test ".$with_$2" = .yes; then
1336    ifelse([$5], , :, [$5])
1337else
1338    ifelse([$6], , :, [$6])
1339fi
1340AC_MSG_RESULT([$with_$2])
1341])dnl
1342
1343dnl ##
1344dnl ##  Check whether SVR4/SUSv2 makecontext(2), swapcontext(2) and
1345dnl ##  friends can be used for user-space context switching
1346dnl ##
1347dnl ##  configure.ac:
1348dnl ##     AC_CHECK_MCSC(<success-action>, <failure-action>)
1349dnl ##
1350
1351AC_DEFUN(AC_CHECK_MCSC, [
1352AC_MSG_CHECKING(for usable SVR4/SUSv2 makecontext(2)/swapcontext(2))
1353AC_CACHE_VAL(ac_cv_check_mcsc, [
1354AC_TRY_RUN([
1355
1356#include <stdio.h>
1357#include <stdlib.h>
1358#include <ucontext.h>
1359
1360ucontext_t uc_child;
1361ucontext_t uc_main;
1362
1363void child(void *arg)
1364{
1365    if (arg != (void *)12345)
1366        exit(1);
1367    if (swapcontext(&uc_child, &uc_main) != 0)
1368        exit(1);
1369}
1370
1371int main(int argc, char *argv[])
1372{
1373    FILE *fp;
1374    void *stack;
1375
1376    /* the default is that it fails */
1377    if ((fp = fopen("conftestval", "w")) == NULL)
1378        exit(1);
1379    fprintf(fp, "no\n");
1380    fclose(fp);
1381
1382    /* configure a child user-space context */
1383    if ((stack = malloc(64*1024)) == NULL)
1384        exit(1);
1385    if (getcontext(&uc_child) != 0)
1386        exit(1);
1387    uc_child.uc_link = NULL;
1388    uc_child.uc_stack.ss_sp = (char *)stack+(32*1024);
1389    uc_child.uc_stack.ss_size = 32*1024;
1390    uc_child.uc_stack.ss_flags = 0;
1391    makecontext(&uc_child, child, 2, (void *)12345);
1392
1393    /* switch into the user context */
1394    if (swapcontext(&uc_main, &uc_child) != 0)
1395        exit(1);
1396
1397    /* Fine, child came home */
1398    if ((fp = fopen("conftestval", "w")) == NULL)
1399        exit(1);
1400    fprintf(fp, "yes\n");
1401    fclose(fp);
1402
1403    /* die successfully */
1404    exit(0);
1405}
1406],
1407ac_cv_check_mcsc=`cat conftestval`,
1408ac_cv_check_mcsc=no,
1409ac_cv_check_mcsc=no
1410)dnl
1411])dnl
1412AC_MSG_RESULT([$ac_cv_check_mcsc])
1413if test ".$ac_cv_check_mcsc" = .yes; then
1414    ifelse([$1], , :, [$1])
1415else
1416    ifelse([$2], , :, [$2])
1417fi
1418])dnl
1419
1420dnl ##
1421dnl ##  Check how stacks have to be setup for the functions
1422dnl ##  sigstack(2), sigaltstack(2) and makecontext(2).
1423dnl ##
1424dnl ##  configure.ac:
1425dnl ##    AC_CHECK_STACKSETUP(sigstack|sigaltstack|makecontext, <macro-addr>, <macro-size>)
1426dnl ##  acconfig.h:
1427dnl ##    #undef HAVE_{SIGSTACK|SIGALTSTACK|MAKECONTEXT}
1428dnl ##    #undef HAVE_STACK_T
1429dnl ##  header.h.in:
1430dnl ##    @<macro-addr>@
1431dnl ##    @<macro-size>@
1432dnl ##  source.c:
1433dnl ##    #include "header.h"
1434dnl ##    xxx.sp_ss   = <macro-addr>(skaddr, sksize);
1435dnl ##    xxx.sp_size = <macro-size>(skaddr, sksize);
1436dnl ##
1437
1438AC_DEFUN(AC_CHECK_STACKSETUP,[dnl
1439dnl #   check for consistent usage
1440ifelse($1,[sigstack],,[
1441ifelse($1,[sigaltstack],,[
1442ifelse($1,[makecontext],,[
1443errprint(__file__:__line__: [AC_CHECK_STACKSETUP: only sigstack, sigaltstack and makecontext supported
1444])])])])
1445dnl #   we require the C compiler and the standard headers
1446AC_REQUIRE([AC_HEADER_STDC])dnl
1447dnl #   we at least require the function to check
1448AC_CHECK_FUNCS($1)
1449dnl #   sigaltstack on some platforms uses stack_t instead of struct sigaltstack
1450ifelse($1, sigaltstack, [
1451    AC_ONCE(stacksetup_stack_t, [
1452        AC_CHECK_TYPEDEF(stack_t, signal.h)
1453    ])
1454])
1455dnl #   display processing header
1456AC_MSG_CHECKING(for stack setup via $1)
1457dnl #   but cache the whole results
1458AC_CACHE_VAL(ac_cv_stacksetup_$1,[
1459if test ".$ac_cv_func_$1" = .no; then
1460    dnl #   no need to check anything when function is already missing
1461    ac_cv_stacksetup_$1="N.A.:/*N.A.*/,/*N.A.*/"
1462else
1463    dnl #   setup compile environment
1464    OCFLAGS="$CFLAGS"
1465    CFLAGS="$CFLAGS -DTEST_$1"
1466    cross_compiling=no
1467    dnl #   compile and run the test program
1468    AC_TRY_RUN([
1469#include <stdio.h>
1470#include <stdlib.h>
1471#include <string.h>
1472#if defined(TEST_sigstack) || defined(TEST_sigaltstack)
1473#include <sys/types.h>
1474#include <signal.h>
1475#include <unistd.h>
1476#endif
1477#if defined(TEST_makecontext)
1478#include <ucontext.h>
1479#endif
1480union alltypes {
1481    long   l;
1482    double d;
1483    void  *vp;
1484    void (*fp)(void);
1485    char  *cp;
1486};
1487static volatile char *handler_addr = (char *)0xDEAD;
1488#if defined(TEST_sigstack) || defined(TEST_sigaltstack)
1489static volatile int handler_done = 0;
1490void handler(int sig)
1491{
1492    char garbage[1024];
1493    int i;
1494    auto int dummy;
1495    for (i = 0; i < 1024; i++)
1496        garbage[i] = 'X';
1497    handler_addr = (char *)&dummy;
1498    handler_done = 1;
1499    return;
1500}
1501#endif
1502#if defined(TEST_makecontext)
1503static ucontext_t uc_handler;
1504static ucontext_t uc_main;
1505void handler(void)
1506{
1507    char garbage[1024];
1508    int i;
1509    auto int dummy;
1510    for (i = 0; i < 1024; i++)
1511        garbage[i] = 'X';
1512    handler_addr = (char *)&dummy;
1513    swapcontext(&uc_handler, &uc_main);
1514    return;
1515}
1516#endif
1517int main(int argc, char *argv[])
1518{
1519    FILE *f;
1520    char *skaddr;
1521    char *skbuf;
1522    int sksize;
1523    char result[1024];
1524    int i;
1525    sksize = 32768;
1526    skbuf = (char *)malloc(sksize*2+2*sizeof(union alltypes));
1527    if (skbuf == NULL)
1528        exit(1);
1529    for (i = 0; i < sksize*2+2*sizeof(union alltypes); i++)
1530        skbuf[i] = 'A';
1531    skaddr = skbuf+sizeof(union alltypes);
1532#if defined(TEST_sigstack) || defined(TEST_sigaltstack)
1533    {
1534        struct sigaction sa;
1535#if defined(TEST_sigstack)
1536        struct sigstack ss;
1537#elif defined(TEST_sigaltstack) && defined(HAVE_STACK_T)
1538        stack_t ss;
1539#else
1540        struct sigaltstack ss;
1541#endif
1542#if defined(TEST_sigstack)
1543        ss.ss_sp      = (void *)(skaddr + sksize);
1544        ss.ss_onstack = 0;
1545        if (sigstack(&ss, NULL) < 0)
1546            exit(1);
1547#elif defined(TEST_sigaltstack)
1548        ss.ss_sp    = (void *)(skaddr + sksize);
1549        ss.ss_size  = sksize;
1550        ss.ss_flags = 0;
1551        if (sigaltstack(&ss, NULL) < 0)
1552            exit(1);
1553#endif
1554        memset((void *)&sa, 0, sizeof(struct sigaction));
1555        sa.sa_handler = handler;
1556        sa.sa_flags = SA_ONSTACK;
1557        sigemptyset(&sa.sa_mask);
1558        sigaction(SIGUSR1, &sa, NULL);
1559        kill(getpid(), SIGUSR1);
1560        while (!handler_done)
1561            /*nop*/;
1562    }
1563#endif
1564#if defined(TEST_makecontext)
1565    {
1566        if (getcontext(&uc_handler) != 0)
1567            exit(1);
1568        uc_handler.uc_link = NULL;
1569        uc_handler.uc_stack.ss_sp    = (void *)(skaddr + sksize);
1570        uc_handler.uc_stack.ss_size  = sksize;
1571        uc_handler.uc_stack.ss_flags = 0;
1572        makecontext(&uc_handler, handler, 1);
1573        swapcontext(&uc_main, &uc_handler);
1574    }
1575#endif
1576    if (handler_addr == (char *)0xDEAD)
1577        exit(1);
1578    if (handler_addr < skaddr+sksize) {
1579        /* stack was placed into lower area */
1580        if (*(skaddr+sksize) != 'A')
1581             sprintf(result, "(skaddr)+(sksize)-%d,(sksize)-%d",
1582                     sizeof(union alltypes), sizeof(union alltypes));
1583        else
1584             strcpy(result, "(skaddr)+(sksize),(sksize)");
1585    }
1586    else {
1587        /* stack was placed into higher area */
1588        if (*(skaddr+sksize*2) != 'A')
1589            sprintf(result, "(skaddr),(sksize)-%d", sizeof(union alltypes));
1590        else
1591            strcpy(result, "(skaddr),(sksize)");
1592    }
1593    if ((f = fopen("conftestval", "w")) == NULL)
1594        exit(1);
1595    fprintf(f, "%s\n", result);
1596    fclose(f);
1597    exit(0);
1598}
1599],[
1600dnl #   test successully passed
1601ac_cv_stacksetup_$1=`cat conftestval`
1602ac_cv_stacksetup_$1="ok:$ac_cv_stacksetup_$1"
1603],[
1604dnl #   test failed
1605ac_cv_stacksetup_$1='guessed:(skaddr),(sksize)'
1606],[
1607dnl #   cross-platform => failed
1608ac_cv_stacksetup_$1='guessed:(skaddr),(sksize)'
1609])
1610dnl #   restore original compile environment
1611CFLAGS="$OCFLAGS"
1612])dnl
1613fi
1614dnl #   extract result ingredients of single cached result value
1615type=`echo $ac_cv_stacksetup_$1 | sed -e 's;:.*$;;'`
1616addr=`echo $ac_cv_stacksetup_$1 | sed -e 's;^.*:;;' -e 's;,.*$;;'`
1617size=`echo $ac_cv_stacksetup_$1 | sed -e 's;^.*:;;' -e 's;^.*,;;'`
1618dnl #   export result ingredients
1619$2="#define $2(skaddr,sksize) ($addr)"
1620$3="#define $3(skaddr,sksize) ($size)"
1621AC_SUBST($2)dnl
1622AC_SUBST($3)dnl
1623dnl #   display result indicator
1624AC_MSG_RESULT([$type])
1625dnl #   display results in detail
1626AC_MSG_VERBOSE([$]$2)
1627AC_MSG_VERBOSE([$]$3)
1628])
1629
1630