1 dnl AC_CHECK_VA_COPY: check for C99 va_copy() implementation (and
2 dnl provide fallback implementation if neccessary)
4 dnl This code is stolen from the OSSP ex autoconf macros by
5 dnl Ralf S. Engelschall.
6 dnl # ``"Reuse an expert's code" is the right
7 dnl # advice for most people. But it's a useless
8 dnl # advice for the experts writing the code
9 dnl # in the first place.'
10 dnl # -- Dan J. Bernstein
12 dnl OK, you're definitely the expert on this point... :)
15 dnl ## Check for C99 va_copy() implementation
16 dnl ## (and provide fallback implementation if neccessary)
19 dnl ## AC_CHECK_VA_COPY
21 dnl ## #include "config.h"
25 dnl ## This check is rather complex: first because we really have to
26 dnl ## try various possible implementations in sequence and second, we
27 dnl ## cannot define a macro in config.h with parameters directly.
30 dnl # test program for va_copy() implementation
32 m4_define(__va_copy_test, <<[
36 #define DO_VA_COPY(d, s) $1
37 void test(char *str, ...)
43 for (i = 1; i <= 9; i++) {
44 int k = (int)va_arg(ap, int);
49 for (i = 1; i <= 9; i++) {
50 int k = (int)va_arg(ap, int);
56 int main(int argc, char *argv[])
58 test("test", 1, 2, 3, 4, 5, 6, 7, 8, 9);
64 dnl # test driver for va_copy() implementation
65 m4_define(__va_copy_check, [
67 [/* Predefined possible va_copy() implementation (id: $1) */
68 #define __VA_COPY_USE_$1(d, s) $2])
69 if test ".$ac_cv_va_copy" = .; then
70 AC_TRY_RUN(__va_copy_test($2), [ac_cv_va_copy="$1"],,[ac_cv_va_copy="$1"])
71 dnl Let's be optimistic and use C99 version when cross-compiling
75 dnl # Autoconf check for va_copy() implementation checking
76 AC_DEFUN([AC_CHECK_VA_COPY],[
77 dnl # provide Autoconf display check message
78 AC_MSG_CHECKING(for va_copy() function)
79 dnl # check for various implementations in priorized sequence
80 AC_CACHE_VAL(ac_cv_va_copy, [
82 dnl # 1. check for standardized C99 macro
83 __va_copy_check(C99, [va_copy((d), (s))])
84 dnl # 2. check for alternative/deprecated GCC macro
85 __va_copy_check(GCM, [VA_COPY((d), (s))])
86 dnl # 3. check for internal GCC macro (high-level define)
87 __va_copy_check(GCH, [__va_copy((d), (s))])
88 dnl # 4. check for internal GCC macro (built-in function)
89 __va_copy_check(GCB, [__builtin_va_copy((d), (s))])
90 dnl # 5. check for assignment approach (assuming va_list is a struct)
91 __va_copy_check(ASS, [do { (d) = (s); } while (0)])
92 dnl # 6. check for assignment approach (assuming va_list is a pointer)
93 __va_copy_check(ASP, [do { *(d) = *(s); } while (0)])
94 dnl # 7. check for memory copying approach (assuming va_list is a struct)
95 __va_copy_check(CPS, [memcpy((void *)&(d), (void *)&(s)), sizeof((s))])
96 dnl # 8. check for memory copying approach (assuming va_list is a pointer)
97 __va_copy_check(CPP, [memcpy((void *)(d), (void *)(s)), sizeof(*(s))])
98 if test ".$ac_cv_va_copy" = .; then
99 AC_ERROR([no working implementation found])
102 dnl # optionally activate the fallback implementation
103 if test ".$ac_cv_va_copy" = ".C99"; then
104 AC_DEFINE(HAVE_VA_COPY, 1, [Define if va_copy() macro exists (and no fallback implementation is required)])
106 dnl # declare which fallback implementation to actually use
107 AC_DEFINE_UNQUOTED([__VA_COPY_USE], [__VA_COPY_USE_$ac_cv_va_copy],
108 [Define to id of used va_copy() implementation])
109 dnl # provide activation hook for fallback implementation
110 AH_VERBATIM([__VA_COPY_ACTIVATION],
111 [/* Optional va_copy() implementation activation */
113 #define va_copy(d, s) __VA_COPY_USE(d, s)
116 dnl # provide Autoconf display result message
117 if test ".$ac_cv_va_copy" = ".C99"; then
120 AC_MSG_RESULT([no (using fallback implementation)])