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