va_copy.m4 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # va_copy.m4 serial 1 (js-1.6.20070208)
  2. dnl ##
  3. dnl ## Check for C99 va_copy() implementation
  4. dnl ## (and provide fallback implementation if neccessary)
  5. dnl ##
  6. dnl ## configure.in:
  7. dnl ## AC_CHECK_VA_COPY
  8. dnl ## foo.c:
  9. dnl ## #include "config.h"
  10. dnl ## [...]
  11. dnl ## va_copy(d,s)
  12. dnl ##
  13. dnl ## This check is rather complex: first because we really have to
  14. dnl ## try various possible implementations in sequence and second, we
  15. dnl ## cannot define a macro in config.h with parameters directly.
  16. dnl ##
  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. dnl # test driver for va_copy() implementation
  51. m4_define(__va_copy_check, [
  52. AH_VERBATIM($1,
  53. [/* Predefined possible va_copy() implementation (id: $1) */
  54. #define __VA_COPY_USE_$1(d, s) $2])
  55. if test ".$ac_cv_va_copy" = .; then
  56. AC_TRY_RUN(__va_copy_test($2), [ac_cv_va_copy="$1"])
  57. fi
  58. ])
  59. dnl # Autoconf check for va_copy() implementation checking
  60. AC_DEFUN([AC_CHECK_VA_COPY],[
  61. dnl # provide Autoconf display check message
  62. AC_MSG_CHECKING(for va_copy() function)
  63. dnl # check for various implementations in priorized sequence
  64. AC_CACHE_VAL(ac_cv_va_copy, [
  65. ac_cv_va_copy=""
  66. dnl # 1. check for standardized C99 macro
  67. __va_copy_check(C99, [va_copy((d), (s))])
  68. dnl # 2. check for alternative/deprecated GCC macro
  69. __va_copy_check(GCM, [VA_COPY((d), (s))])
  70. dnl # 3. check for internal GCC macro (high-level define)
  71. __va_copy_check(GCH, [__va_copy((d), (s))])
  72. dnl # 4. check for internal GCC macro (built-in function)
  73. __va_copy_check(GCB, [__builtin_va_copy((d), (s))])
  74. dnl # 5. check for assignment approach (assuming va_list is a struct)
  75. __va_copy_check(ASS, [do { (d) = (s); } while (0)])
  76. dnl # 6. check for assignment approach (assuming va_list is a pointer)
  77. __va_copy_check(ASP, [do { *(d) = *(s); } while (0)])
  78. dnl # 7. check for memory copying approach (assuming va_list is a struct)
  79. __va_copy_check(CPS, [memcpy((void *)&(d), (void *)&(s)), sizeof((s))])
  80. dnl # 8. check for memory copying approach (assuming va_list is a pointer)
  81. __va_copy_check(CPP, [memcpy((void *)(d), (void *)(s)), sizeof(*(s))])
  82. if test ".$ac_cv_va_copy" = .; then
  83. AC_ERROR([no working implementation found])
  84. fi
  85. ])
  86. dnl # optionally activate the fallback implementation
  87. if test ".$ac_cv_va_copy" = ".C99"; then
  88. AC_DEFINE(HAVE_VA_COPY, 1, [Define if va_copy() macro exists (and no fallback implementation is required)])
  89. fi
  90. dnl # declare which fallback implementation to actually use
  91. AC_DEFINE_UNQUOTED([__VA_COPY_USE], [__VA_COPY_USE_$ac_cv_va_copy],
  92. [Define to id of used va_copy() implementation])
  93. dnl # provide activation hook for fallback implementation
  94. AH_VERBATIM([__VA_COPY_ACTIVATION],
  95. [/* Optional va_copy() implementation activation */
  96. #ifndef HAVE_VA_COPY
  97. #define va_copy(d, s) __VA_COPY_USE(d, s)
  98. #endif
  99. ])
  100. dnl # provide Autoconf display result message
  101. if test ".$ac_cv_va_copy" = ".C99"; then
  102. AC_MSG_RESULT([yes])
  103. else
  104. AC_MSG_RESULT([no (using fallback implementation)])
  105. fi
  106. ])