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"])
74 dnl # Autoconf check for va_copy() implementation checking
75 AC_DEFUN([AC_CHECK_VA_COPY],[
76 dnl # provide Autoconf display check message
77 AC_MSG_CHECKING(for va_copy() function)
78 dnl # check for various implementations in priorized sequence
79 AC_CACHE_VAL(ac_cv_va_copy, [
81 dnl # 1. check for standardized C99 macro
82 __va_copy_check(C99, [va_copy((d), (s))])
83 dnl # 2. check for alternative/deprecated GCC macro
84 __va_copy_check(GCM, [VA_COPY((d), (s))])
85 dnl # 3. check for internal GCC macro (high-level define)
86 __va_copy_check(GCH, [__va_copy((d), (s))])
87 dnl # 4. check for internal GCC macro (built-in function)
88 __va_copy_check(GCB, [__builtin_va_copy((d), (s))])
89 dnl # 5. check for assignment approach (assuming va_list is a struct)
90 __va_copy_check(ASS, [do { (d) = (s); } while (0)])
91 dnl # 6. check for assignment approach (assuming va_list is a pointer)
92 __va_copy_check(ASP, [do { *(d) = *(s); } while (0)])
93 dnl # 7. check for memory copying approach (assuming va_list is a struct)
94 __va_copy_check(CPS, [memcpy((void *)&(d), (void *)&(s)), sizeof((s))])
95 dnl # 8. check for memory copying approach (assuming va_list is a pointer)
96 __va_copy_check(CPP, [memcpy((void *)(d), (void *)(s)), sizeof(*(s))])
97 if test ".$ac_cv_va_copy" = .; then
98 AC_ERROR([no working implementation found])
101 dnl # optionally activate the fallback implementation
102 if test ".$ac_cv_va_copy" = ".C99"; then
103 AC_DEFINE(HAVE_VA_COPY, 1, [Define if va_copy() macro exists (and no fallback implementation is required)])
105 dnl # declare which fallback implementation to actually use
106 AC_DEFINE_UNQUOTED([__VA_COPY_USE], [__VA_COPY_USE_$ac_cv_va_copy],
107 [Define to id of used va_copy() implementation])
108 dnl # provide activation hook for fallback implementation
109 AH_VERBATIM([__VA_COPY_ACTIVATION],
110 [/* Optional va_copy() implementation activation */
112 #define va_copy(d, s) __VA_COPY_USE(d, s)
115 dnl # provide Autoconf display result message
116 if test ".$ac_cv_va_copy" = ".C99"; then
119 AC_MSG_RESULT([no (using fallback implementation)])