[backfire] backport r27776
[openwrt-10.03/.git] / tools / missing-macros / src / m4 / va_copy.m4
1 # va_copy.m4 serial 1 (js-1.6.20070208)
2
3 dnl ##
4 dnl ##  Check for C99 va_copy() implementation
5 dnl ##  (and provide fallback implementation if neccessary)
6 dnl ##
7 dnl ##  configure.in:
8 dnl ##    AC_CHECK_VA_COPY
9 dnl ##  foo.c:
10 dnl ##    #include "config.h"
11 dnl ##    [...]
12 dnl ##    va_copy(d,s)
13 dnl ##
14 dnl ##  This check is rather complex: first because we really have to
15 dnl ##  try various possible implementations in sequence and second, we
16 dnl ##  cannot define a macro in config.h with parameters directly.
17 dnl ##
18
19 dnl #   test program for va_copy() implementation
20 changequote(<<,>>)
21 m4_define(__va_copy_test, <<[
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #define DO_VA_COPY(d, s) $1
26 void test(char *str, ...)
27 {
28     va_list ap, ap2;
29     int i;
30     va_start(ap, str);
31     DO_VA_COPY(ap2, ap);
32     for (i = 1; i <= 9; i++) {
33         int k = (int)va_arg(ap, int);
34         if (k != i)
35             abort();
36     }
37     DO_VA_COPY(ap, ap2);
38     for (i = 1; i <= 9; i++) {
39         int k = (int)va_arg(ap, int);
40         if (k != i)
41             abort();
42     }
43     va_end(ap);
44 }
45 int main(int argc, char *argv[])
46 {
47     test("test", 1, 2, 3, 4, 5, 6, 7, 8, 9);
48     exit(0);
49 }
50 ]>>)
51 changequote([,])
52
53 dnl #   test driver for va_copy() implementation
54 m4_define(__va_copy_check, [
55     AH_VERBATIM($1,
56 [/* Predefined possible va_copy() implementation (id: $1) */
57 #define __VA_COPY_USE_$1(d, s) $2])
58     if test ".$ac_cv_va_copy" = .; then
59         AC_TRY_RUN(__va_copy_test($2), [ac_cv_va_copy="$1"])
60     fi
61 ])
62
63 dnl #   Autoconf check for va_copy() implementation checking
64 AC_DEFUN([AC_CHECK_VA_COPY],[
65   dnl #   provide Autoconf display check message
66   AC_MSG_CHECKING(for va_copy() function)
67   dnl #   check for various implementations in priorized sequence   
68   AC_CACHE_VAL(ac_cv_va_copy, [
69     ac_cv_va_copy=""
70     dnl #   1. check for standardized C99 macro
71     __va_copy_check(C99, [va_copy((d), (s))])
72     dnl #   2. check for alternative/deprecated GCC macro
73     __va_copy_check(GCM, [VA_COPY((d), (s))])
74     dnl #   3. check for internal GCC macro (high-level define)
75     __va_copy_check(GCH, [__va_copy((d), (s))])
76     dnl #   4. check for internal GCC macro (built-in function)
77     __va_copy_check(GCB, [__builtin_va_copy((d), (s))])
78     dnl #   5. check for assignment approach (assuming va_list is a struct)
79     __va_copy_check(ASS, [do { (d) = (s); } while (0)])
80     dnl #   6. check for assignment approach (assuming va_list is a pointer)
81     __va_copy_check(ASP, [do { *(d) = *(s); } while (0)])
82     dnl #   7. check for memory copying approach (assuming va_list is a struct)
83     __va_copy_check(CPS, [memcpy((void *)&(d), (void *)&(s)), sizeof((s))])
84     dnl #   8. check for memory copying approach (assuming va_list is a pointer)
85     __va_copy_check(CPP, [memcpy((void *)(d), (void *)(s)), sizeof(*(s))])
86     if test ".$ac_cv_va_copy" = .; then
87         AC_ERROR([no working implementation found])
88     fi
89   ])
90   dnl #   optionally activate the fallback implementation
91   if test ".$ac_cv_va_copy" = ".C99"; then
92       AC_DEFINE(HAVE_VA_COPY, 1, [Define if va_copy() macro exists (and no fallback implementation is required)])
93   fi
94   dnl #   declare which fallback implementation to actually use
95   AC_DEFINE_UNQUOTED([__VA_COPY_USE], [__VA_COPY_USE_$ac_cv_va_copy],
96       [Define to id of used va_copy() implementation])
97   dnl #   provide activation hook for fallback implementation
98   AH_VERBATIM([__VA_COPY_ACTIVATION],
99 [/* Optional va_copy() implementation activation */
100 #ifndef HAVE_VA_COPY
101 #define va_copy(d, s) __VA_COPY_USE(d, s)
102 #endif
103 ])
104   dnl #   provide Autoconf display result message
105   if test ".$ac_cv_va_copy" = ".C99"; then
106       AC_MSG_RESULT([yes])
107   else
108       AC_MSG_RESULT([no (using fallback implementation)])
109   fi
110 ])
111