1dnl ##
2dnl ##  OSSP cfg - Configuration Parsing
3dnl ##  Copyright (c) 2002-2006 Ralf S. Engelschall <rse@engelschall.com>
4dnl ##  Copyright (c) 2002-2006 The OSSP Project <http://www.ossp.org/>
5dnl ##
6dnl ##  This file is part of OSSP cfg, a configuration parsing library which
7dnl ##  can be found at http://www.ossp.org/pkg/lib/cfg/.
8dnl ##
9dnl ##  Permission to use, copy, modify, and distribute this software for
10dnl ##  any purpose with or without fee is hereby granted, provided that
11dnl ##  the above copyright notice and this permission notice appear in all
12dnl ##  copies.
13dnl ##
14dnl ##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
15dnl ##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16dnl ##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17dnl ##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
18dnl ##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19dnl ##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20dnl ##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
21dnl ##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22dnl ##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23dnl ##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24dnl ##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25dnl ##  SUCH DAMAGE.
26dnl ##
27dnl ##  aclocal.m4: GNU Autoconf local macro definitions
28dnl ##
29
30dnl ##
31dnl ##  Check whether compiler option works
32dnl ##
33dnl ##  configure.in:
34dnl ##    AC_COMPILER_OPTION(<name>, <display>, <option>,
35dnl ##                       <action-success>, <action-failure>)
36dnl ##
37
38AC_DEFUN(AC_COMPILER_OPTION,[dnl
39AC_MSG_CHECKING(whether compiler option(s) $2 work)
40AC_CACHE_VAL(ac_cv_compiler_option_$1,[
41SAVE_CFLAGS="$CFLAGS"
42CFLAGS="$CFLAGS $3"
43AC_TRY_COMPILE([],[], ac_cv_compiler_option_$1=yes, ac_cv_compiler_option_$1=no)
44CFLAGS="$SAVE_CFLAGS"
45])dnl
46if test ".$ac_cv_compiler_option_$1" = .yes; then
47    ifelse([$4], , :, [$4])
48else
49    ifelse([$5], , :, [$5])
50fi
51AC_MSG_RESULT([$ac_cv_compiler_option_$1])
52])dnl
53
54dnl ##
55dnl ##  Debugging Support
56dnl ##
57dnl ##  configure.in:
58dnl ##    AC_CHECK_DEBUGGING
59dnl ##
60
61AC_DEFUN(AC_CHECK_DEBUGGING,[dnl
62AC_ARG_ENABLE(debug,dnl
63[  --enable-debug          build for debugging (default=no)],
64[dnl
65if test ".$ac_cv_prog_gcc" = ".yes"; then
66    case "$CFLAGS" in
67        *-O* ) ;;
68           * ) CFLAGS="$CFLAGS -O2" ;;
69    esac
70    case "$CFLAGS" in
71        *-g* ) ;;
72           * ) CFLAGS="$CFLAGS -g" ;;
73    esac
74    case "$CFLAGS" in
75        *-pipe* ) ;;
76              * ) AC_COMPILER_OPTION(pipe, -pipe, -pipe, CFLAGS="$CFLAGS -pipe") ;;
77    esac
78    AC_COMPILER_OPTION(defdbg, -DDEBUG, -DDEBUG, CFLAGS="$CFLAGS -DDEBUG")
79    CFLAGS="$CFLAGS -pedantic"
80    CFLAGS="$CFLAGS -Wall"
81    WMORE=""
82    WMORE="$WMORE -Wbad-function-cast"
83    WMORE="$WMORE -Wcast-align"
84    WMORE="$WMORE -Winline"
85    WMORE="$WMORE -Wmissing-declarations"
86    WMORE="$WMORE -Wmissing-prototypes"
87    WMORE="$WMORE -Wmultichar"
88    WMORE="$WMORE -Wnested-externs"
89    WMORE="$WMORE -Wno-system-headers"
90    WMORE="$WMORE -Wpointer-arith"
91    WMORE="$WMORE -Wredundant-decls"
92    WMORE="$WMORE -Wshadow"
93    WMORE="$WMORE -Wstrict-prototypes"
94    AC_COMPILER_OPTION(wmore, -W<xxx>, $WMORE, CFLAGS="$CFLAGS $WMORE")
95else
96    case "$CFLAGS" in
97        *-g* ) ;;
98           * ) CFLAGS="$CFLAGS -g" ;;
99    esac
100fi
101msg="enabled"
102],[
103if test ".$ac_cv_prog_gcc" = ".yes"; then
104case "$CFLAGS" in
105    *-pipe* ) ;;
106          * ) AC_COMPILER_OPTION(pipe, -pipe, -pipe, CFLAGS="$CFLAGS -pipe") ;;
107esac
108fi
109case "$CFLAGS" in
110    *-g* ) CFLAGS=`echo "$CFLAGS" |\
111                   sed -e 's/ -g / /g' -e 's/ -g$//' -e 's/^-g //g' -e 's/^-g$//'` ;;
112esac
113case "$CXXFLAGS" in
114    *-g* ) CXXFLAGS=`echo "$CXXFLAGS" |\
115                     sed -e 's/ -g / /g' -e 's/ -g$//' -e 's/^-g //g' -e 's/^-g$//'` ;;
116esac
117msg=disabled
118])dnl
119AC_MSG_CHECKING(for compilation debug mode)
120AC_MSG_RESULT([$msg])
121if test ".$msg" = .enabled; then
122    enable_shared=no
123fi
124])
125
126dnl ##
127dnl ##  Check for an external/extension library.
128dnl ##  - is aware of <libname>-config style scripts
129dnl ##  - searches under standard paths include, lib, etc.
130dnl ##  - searches under subareas like .libs, etc.
131dnl ##
132dnl ##  configure.in:
133dnl ##      AC_CHECK_EXTLIB(<realname>, <libname>, <func>, <header>,
134dnl ##                      [<success-action> [, <fail-action>]])
135dnl ##  Makefile.in:
136dnl ##      CFLAGS  = @CFLAGS@
137dnl ##      LDFLAGS = @LDFLAGS@
138dnl ##      LIBS    = @LIBS@
139dnl ##  shell:
140dnl ##      $ ./configure --with-<libname>[=DIR]
141dnl ##
142
143AC_DEFUN(AC_CHECK_EXTLIB,[dnl
144AC_ARG_WITH($2, [dnl
145[  --with-]m4_substr([$2[[=DIR]]                     ], 0, 19)[build with external $1 library (default=no)]], [dnl
146    if test ".$with_$2" = .yes; then
147        #   via config script in PATH
148        $2_version=`($2-config --version) 2>/dev/null`
149        if test ".$$2_version" != .; then
150            CPPFLAGS="$CPPFLAGS `$2-config --cflags`"
151            CFLAGS="$CFLAGS `$2-config --cflags`"
152            LDFLAGS="$LDFLAGS `$2-config --ldflags`"
153        fi
154    else
155        if test -d "$with_$2"; then
156            found=0
157            #   via config script
158            for dir in $with_$2/bin $with_$2; do
159                if test -f "$dir/$2-config" && test ! -f "$dir/$2-config.in"; then
160                    $2_version=`($dir/$2-config --version) 2>/dev/null`
161                    if test ".$$2_version" != .; then
162                        CPPFLAGS="$CPPFLAGS `$dir/$2-config --cflags`"
163                        CFLAGS="$CFLAGS `$dir/$2-config --cflags`"
164                        LDFLAGS="$LDFLAGS `$dir/$2-config --ldflags`"
165                        found=1
166                        break
167                    fi
168                fi
169            done
170            #   in standard sub-areas
171            if test ".$found" = .0; then
172                for dir in $with_$2/include/$2 $with_$2/include $with_$2; do
173                    if test -f "$dir/$4"; then
174                        CPPFLAGS="$CPPFLAGS -I$dir"
175                        CFLAGS="$CFLAGS -I$dir"
176                        found=1
177                        break
178                    fi
179                done
180                for dir in $with_$2/lib/$2 $with_$2/lib $with_$2; do
181                    if test -f "$dir/lib$2.la" && test -d "$dir/.libs"; then
182                        LDFLAGS="$LDFLAGS -L$dir -L$dir/.libs"
183                        found=1
184                        break
185                    elif test -f "$dir/lib$2.a" || test -f "$dir/lib$2.so"; then
186                        LDFLAGS="$LDFLAGS -L$dir"
187                        found=1
188                        break
189                    fi
190                done
191            fi
192            #   in any sub-area
193            if test ".$found" = .0; then
194changequote(, )dnl
195                for file in x `find $with_$2 -name "$4" -type f -print`; do
196                    test .$file = .x && continue
197                    dir=`echo $file | sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;'`
198                    CPPFLAGS="$CPPFLAGS -I$dir"
199                    CFLAGS="$CFLAGS -I$dir"
200                done
201                for file in x `find $with_$2 -name "lib$2.[aso]" -type f -print`; do
202                    test .$file = .x && continue
203                    dir=`echo $file | sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;'`
204                    LDFLAGS="$LDFLAGS -L$dir"
205                done
206changequote([, ])dnl
207            fi
208        fi
209    fi
210    AC_HAVE_HEADERS($4)
211    AC_CHECK_LIB($2, $3)
212    with_$2=yes
213    ac_var="ac_cv_header_`echo $4 | sed 'y%./+-%__p_%'`"
214    eval "ac_val=\$$ac_var"
215    if test ".$ac_val" != .yes; then
216        with_$2=no
217    fi
218    if test ".$ac_cv_lib_$2_$3" != .yes; then
219        with_$2=no
220    fi
221    if test ".$with_$2" = .no; then
222        AC_ERROR([Unable to find $1 library])
223    fi
224    ], [dnl
225if test ".$with_$2" = .; then
226    with_$2=no
227fi
228    ])dnl
229AC_MSG_CHECKING(whether to build against external $1 library)
230if test ".$with_$2" = .yes; then
231    ifelse([$5], , :, [$5])
232else
233    ifelse([$6], , :, [$6])
234fi
235AC_MSG_RESULT([$with_$2])
236])dnl
237
238dnl ##
239dnl ##  Check for GNU Bison and GNU Flex
240dnl ##
241dnl ##  configure.in:
242dnl ##      AC_CHECK_BISON(BISON, 1.30, [1.3[0-9]|1.[4-9]])
243dnl ##      AC_CHECK_FLEX(FLEX, 2.5.6, [2.5.[6-9]|2.5.1[0-9]|2.[6-9].*])
244dnl ##
245dnl ##  Makefile.in:
246dnl ##      BISON = @BISON@
247dnl ##      FLEX  = @FLEX@
248dnl ##
249
250AC_DEFUN(AC_CHECK_BISON,[dnl
251if test ".$enable_maintainer" = .yes; then
252    bison_version=""
253    for prog in bison bison-beta bison-alpha bison-snap; do
254        AC_PATH_PROG($1, $prog, NA)
255        if test ".$$1" != .NA; then
256            bison_version=`($$1 --version | head -1 | sed -e 's;^[[^0-9]]*\([[0-9]][[0-9.]]*\).*;\1;') 2>/dev/null`
257            case "$bison_version" in
258                $3 ) break ;;
259                * ) $1="NA"; unset ac_cv_path_$1 ;;
260            esac
261        else
262            unset ac_cv_path_$1
263        fi
264    done
265    if test ".$$1" = .NA; then
266        if test ".$bison_version" != .; then
267            AC_ERROR([found GNU Bison version $bison_version; require version >= $2])
268        else
269            AC_ERROR([require GNU Bison version >= $2])
270        fi
271    fi
272else
273    dnl # disable the use of the tools, but still allow manual override
274    test ".$$1" = . && $1=true
275    AC_PATH_PROG($1, bison, true)
276fi
277])
278
279AC_DEFUN(AC_CHECK_FLEX,[dnl
280if test ".$enable_maintainer" = .yes; then
281    flex_version=""
282    for prog in flex flex-beta flex-alpha flex-snap; do
283        AC_PATH_PROG($1, $prog, NA)
284        if test ".$$1" != .NA; then
285            flex_version=`($$1 --version | head -1 | sed -e 's;^[[^0-9]]*\([[0-9]][[0-9.]]*\).*;\1;') 2>/dev/null`
286            case "$flex_version" in
287                $3 ) break ;;
288                * ) $1="NA"; unset ac_cv_path_$1 ;;
289            esac
290        else
291            unset ac_cv_path_$1
292        fi
293    done
294    if test ".$$1" = .NA; then
295        if test ".$flex_version" != .; then
296            AC_ERROR([found GNU Flex version $flex_version; require version >= $2])
297        else
298            AC_ERROR([require GNU Flex version >= $2])
299        fi
300    fi
301else
302    test ".$$1" = . && $1=true
303    AC_PATH_PROG($1, flex, true)
304fi
305])
306
307dnl ##
308dnl ##  Check for C99 va_copy() implementation
309dnl ##  (and provide fallback implementation if neccessary)
310dnl ##
311dnl ##  configure.in:
312dnl ##    AC_CHECK_VA_COPY
313dnl ##  foo.c:
314dnl ##    #include "config.h"
315dnl ##    [...]
316dnl ##    va_copy(d,s)
317dnl ##
318dnl ##  This check is rather complex: first because we really have to
319dnl ##  try various possible implementations in sequence and second, we
320dnl ##  cannot define a macro in config.h with parameters directly.
321dnl ##
322
323dnl #   test program for va_copy() implementation
324changequote(<<,>>)
325m4_define(__va_copy_test, <<[
326#include <stdlib.h>
327#include <stdarg.h>
328#include <string.h>
329#define DO_VA_COPY(d, s) $1
330void test(char *str, ...)
331{
332    va_list ap, ap2;
333    int i;
334    va_start(ap, str);
335    DO_VA_COPY(ap2, ap);
336    for (i = 1; i <= 9; i++) {
337        int k = (int)va_arg(ap, int);
338        if (k != i)
339            abort();
340    }
341    DO_VA_COPY(ap, ap2);
342    for (i = 1; i <= 9; i++) {
343        int k = (int)va_arg(ap, int);
344        if (k != i)
345            abort();
346    }
347    va_end(ap);
348}
349int main(int argc, char *argv[])
350{
351    test("test", 1, 2, 3, 4, 5, 6, 7, 8, 9);
352    exit(0);
353}
354]>>)
355changequote([,])
356
357dnl #   test driver for va_copy() implementation
358m4_define(__va_copy_check, [
359    AH_VERBATIM($1,
360[/* Predefined possible va_copy() implementation (id: $1) */
361#define __VA_COPY_USE_$1(d, s) $2])
362    if test ".$ac_cv_va_copy" = .; then
363        AC_TRY_RUN(__va_copy_test($2), [ac_cv_va_copy="$1"])
364    fi
365])
366
367dnl #   Autoconf check for va_copy() implementation checking
368AC_DEFUN(AC_CHECK_VA_COPY,[
369  dnl #   provide Autoconf display check message
370  AC_MSG_CHECKING(for va_copy() function)
371  dnl #   check for various implementations in priorized sequence
372  AC_CACHE_VAL(ac_cv_va_copy, [
373    ac_cv_va_copy=""
374    dnl #   1. check for standardized C99 macro
375    __va_copy_check(C99, [va_copy((d), (s))])
376    dnl #   2. check for alternative/deprecated GCC macro
377    __va_copy_check(GCM, [VA_COPY((d), (s))])
378    dnl #   3. check for internal GCC macro (high-level define)
379    __va_copy_check(GCH, [__va_copy((d), (s))])
380    dnl #   4. check for internal GCC macro (built-in function)
381    __va_copy_check(GCB, [__builtin_va_copy((d), (s))])
382    dnl #   5. check for assignment approach (assuming va_list is a struct)
383    __va_copy_check(ASS, [do { (d) = (s); } while (0)])
384    dnl #   6. check for assignment approach (assuming va_list is a pointer)
385    __va_copy_check(ASP, [do { *(d) = *(s); } while (0)])
386    dnl #   7. check for memory copying approach (assuming va_list is a struct)
387    __va_copy_check(CPS, [memcpy((void *)&(d), (void *)&(s)), sizeof((s))])
388    dnl #   8. check for memory copying approach (assuming va_list is a pointer)
389    __va_copy_check(CPP, [memcpy((void *)(d), (void *)(s)), sizeof(*(s))])
390    if test ".$ac_cv_va_copy" = .; then
391        AC_ERROR([no working implementation found])
392    fi
393  ])
394  dnl #   optionally activate the fallback implementation
395  if test ".$ac_cv_va_copy" = ".C99"; then
396      AC_DEFINE(HAVE_VA_COPY, 1, [Define if va_copy() macro exists (and no fallback implementation is required)])
397  fi
398  dnl #   declare which fallback implementation to actually use
399  AC_DEFINE_UNQUOTED([__VA_COPY_USE], [__VA_COPY_USE_$ac_cv_va_copy],
400      [Define to id of used va_copy() implementation])
401  dnl #   provide activation hook for fallback implementation
402  AH_VERBATIM([__VA_COPY_ACTIVATION],
403[/* Optional va_copy() implementation activation */
404#ifndef HAVE_VA_COPY
405#define va_copy(d, s) __VA_COPY_USE(d, s)
406#endif
407])
408  dnl #   provide Autoconf display result message
409  if test ".$ac_cv_va_copy" = ".C99"; then
410      AC_MSG_RESULT([yes])
411  else
412      AC_MSG_RESULT([no (using fallback implementation)])
413  fi
414])
415
416