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