Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement gras_datadesc_copy. Was actually easier than expected, that's a really...
[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"])
71     fi
72 ])
73
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, [
80     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])
99     fi
100   ])
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)])
104   fi
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 */
111 #ifndef HAVE_VA_COPY
112 #define va_copy(d, s) __VA_COPY_USE(d, s)
113 #endif
114 ])
115   dnl #   provide Autoconf display result message
116   if test ".$ac_cv_va_copy" = ".C99"; then
117       AC_MSG_RESULT([yes])
118   else
119       AC_MSG_RESULT([no (using fallback implementation)])
120   fi
121 ])
122