Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
resource file of context usage project
[simgrid.git] / acmacro / va_copy.m4
1 dnl AC_CHECK_VA_COPY: check for C99 va_copy() implementation (and
2 dnl                   provide fallback implementation if neccessary)
3
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
11 dnl
12 dnl OK, you're definitely the expert on this point... :)
13
14 dnl ##
15 dnl ##  Check for C99 va_copy() implementation
16 dnl ##  (and provide fallback implementation if neccessary)
17 dnl ##
18 dnl ##  configure.in:
19 dnl ##    AC_CHECK_VA_COPY
20 dnl ##  foo.c:
21 dnl ##    #include "config.h"
22 dnl ##    [...]
23 dnl ##    va_copy(d,s)
24 dnl ##
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.
28 dnl ##
29
30 dnl #   test program for va_copy() implementation
31 changequote(<<,>>)
32 m4_define(__va_copy_test, <<[
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #define DO_VA_COPY(d, s) $1
37 void test(char *str, ...)
38 {
39     va_list ap, ap2;
40     int i;
41     va_start(ap, str);
42     DO_VA_COPY(ap2, ap);
43     for (i = 1; i <= 9; i++) {
44         int k = (int)va_arg(ap, int);
45         if (k != i)
46             abort();
47     }
48     DO_VA_COPY(ap, ap2);
49     for (i = 1; i <= 9; i++) {
50         int k = (int)va_arg(ap, int);
51         if (k != i)
52             abort();
53     }
54     va_end(ap);
55 }
56 int main(int argc, char *argv[])
57 {
58     test("test", 1, 2, 3, 4, 5, 6, 7, 8, 9);
59     exit(0);
60 }
61 ]>>)
62 changequote([,])
63
64 dnl #   test driver for va_copy() implementation
65 m4_define(__va_copy_check, [
66     AH_VERBATIM($1,
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
72     fi
73 ])
74
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, [
81     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])
100     fi
101   ])
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)])
105   fi
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 */
112 #ifndef HAVE_VA_COPY
113 #define va_copy(d, s) __VA_COPY_USE(d, s)
114 #endif
115 ])
116   dnl #   provide Autoconf display result message
117   if test ".$ac_cv_va_copy" = ".C99"; then
118       AC_MSG_RESULT([yes])
119   else
120       AC_MSG_RESULT([no (using fallback implementation)])
121   fi
122 ])
123