1dnl @synopsis GCC_HEADER_STDINT [( HEADER-TO-GENERATE [, HEADERS-TO-CHECK])]
2dnl
3dnl the "ISO C9X: 7.18 Integer types <stdint.h>" section requires the
4dnl existence of an include file <stdint.h> that defines a set of
5dnl typedefs, especially uint8_t,int32_t,uintptr_t.
6dnl Many older installations will not provide this file, but some will
7dnl have the very same definitions in <inttypes.h>. In other enviroments
8dnl we can use the inet-types in <sys/types.h> which would define the
9dnl typedefs int8_t and u_int8_t respectivly.
10dnl
11dnl This macros will create a local "_stdint.h" or the headerfile given as
12dnl an argument. In many cases that file will pick the definition from a
13dnl "#include <stdint.h>" or "#include <inttypes.h>" statement, while
14dnl in other environments it will provide the set of basic 'stdint's defined:
15dnl int8_t,uint8_t,int16_t,uint16_t,int32_t,uint32_t,intptr_t,uintptr_t
16dnl int_least32_t.. int_fast32_t.. intmax_t
17dnl which may or may not rely on the definitions of other files.
18dnl
19dnl Sometimes the stdint.h or inttypes.h headers conflict with sys/types.h,
20dnl so we test the headers together with sys/types.h and always include it
21dnl into the generated header (to match the tests with the generated file).
22dnl Hopefully this is not a big annoyance.
23dnl
24dnl If your installed header files require the stdint-types you will want to
25dnl create an installable file mylib-int.h that all your other installable
26dnl header may include. So, for a library package named "mylib", just use
27dnl      GCC_HEADER_STDINT(mylib-int.h)
28dnl in configure.in and install that header file in Makefile.am along with
29dnl the other headers (mylib.h).  The mylib-specific headers can simply
30dnl use "#include <mylib-int.h>" to obtain the stdint-types.
31dnl
32dnl Remember, if the system already had a valid <stdint.h>, the generated
33dnl file will include it directly. No need for fuzzy HAVE_STDINT_H things...
34dnl
35dnl @author  Guido Draheim <guidod@gmx.de>, Paolo Bonzini <bonzini@gnu.org>
36
37AC_DEFUN([GCC_HEADER_STDINT],
38[m4_define(_GCC_STDINT_H, m4_ifval($1, $1, _stdint.h))
39
40inttype_headers=`echo inttypes.h sys/inttypes.h $2 | sed -e 's/,/ /g'`
41
42acx_cv_header_stdint=stddef.h
43acx_cv_header_stdint_kind="(already complete)"
44for i in stdint.h $inttype_headers; do
45  unset ac_cv_type_uintptr_t
46  unset ac_cv_type_uintmax_t
47  unset ac_cv_type_int_least32_t
48  unset ac_cv_type_int_fast32_t
49  unset ac_cv_type_uint64_t
50  _AS_ECHO_N([looking for a compliant stdint.h in $i, ])
51  AC_CHECK_TYPE(uintmax_t,[acx_cv_header_stdint=$i],continue,[#include <sys/types.h>
52#include <$i>])
53  AC_CHECK_TYPE(uintptr_t,,[acx_cv_header_stdint_kind="(mostly complete)"], [#include <sys/types.h>
54#include <$i>])
55  AC_CHECK_TYPE(int_least32_t,,[acx_cv_header_stdint_kind="(mostly complete)"], [#include <sys/types.h>
56#include <$i>])
57  AC_CHECK_TYPE(int_fast32_t,,[acx_cv_header_stdint_kind="(mostly complete)"], [#include <sys/types.h>
58#include <$i>])
59  AC_CHECK_TYPE(uint64_t,,[acx_cv_header_stdint_kind="(lacks uint64_t)"], [#include <sys/types.h>
60#include <$i>])
61  break
62done
63if test "$acx_cv_header_stdint" = stddef.h; then
64  acx_cv_header_stdint_kind="(lacks uintmax_t)"
65  for i in stdint.h $inttype_headers; do
66    unset ac_cv_type_uintptr_t
67    unset ac_cv_type_uint32_t
68    unset ac_cv_type_uint64_t
69    _AS_ECHO_N([looking for an incomplete stdint.h in $i, ])
70    AC_CHECK_TYPE(uint32_t,[acx_cv_header_stdint=$i],continue,[#include <sys/types.h>
71#include <$i>])
72    AC_CHECK_TYPE(uint64_t,,,[#include <sys/types.h>
73#include <$i>])
74    AC_CHECK_TYPE(uintptr_t,,,[#include <sys/types.h>
75#include <$i>])
76    break
77  done
78fi
79if test "$acx_cv_header_stdint" = stddef.h; then
80  acx_cv_header_stdint_kind="(u_intXX_t style)"
81  for i in sys/types.h $inttype_headers; do
82    unset ac_cv_type_u_int32_t
83    unset ac_cv_type_u_int64_t
84    _AS_ECHO_N([looking for u_intXX_t types in $i, ])
85    AC_CHECK_TYPE(u_int32_t,[acx_cv_header_stdint=$i],continue,[#include <sys/types.h>
86#include <$i>])
87    AC_CHECK_TYPE(u_int64_t,,,[#include <sys/types.h>
88#include <$i>])
89    break
90  done
91fi
92if test "$acx_cv_header_stdint" = stddef.h; then
93  acx_cv_header_stdint_kind="(using manual detection)"
94fi
95
96test -z "$ac_cv_type_uintptr_t" && ac_cv_type_uintptr_t=no
97test -z "$ac_cv_type_uint64_t" && ac_cv_type_uint64_t=no
98test -z "$ac_cv_type_u_int64_t" && ac_cv_type_u_int64_t=no
99test -z "$ac_cv_type_int_least32_t" && ac_cv_type_int_least32_t=no
100test -z "$ac_cv_type_int_fast32_t" && ac_cv_type_int_fast32_t=no
101
102# ----------------- Summarize what we found so far
103
104AC_MSG_CHECKING([what to include in _GCC_STDINT_H])
105
106case `AS_BASENAME(_GCC_STDINT_H)` in
107  stdint.h) AC_MSG_WARN([are you sure you want it there?]) ;;
108  inttypes.h) AC_MSG_WARN([are you sure you want it there?]) ;;
109  *) ;;
110esac
111
112AC_MSG_RESULT($acx_cv_header_stdint $acx_cv_header_stdint_kind)
113
114# ----------------- done included file, check C basic types --------
115
116# Lacking an uintptr_t?  Test size of void *
117case "$acx_cv_header_stdint:$ac_cv_type_uintptr_t" in
118  stddef.h:* | *:no) AC_CHECK_SIZEOF(void *) ;;
119esac
120
121# Lacking an uint64_t?  Test size of long
122case "$acx_cv_header_stdint:$ac_cv_type_uint64_t:$ac_cv_type_u_int64_t" in
123  stddef.h:*:* | *:no:no) AC_CHECK_SIZEOF(long) ;;
124esac
125
126if test $acx_cv_header_stdint = stddef.h; then
127  # Lacking a good header?  Test size of everything and deduce all types.
128  AC_CHECK_SIZEOF(int)
129  AC_CHECK_SIZEOF(short)
130  AC_CHECK_SIZEOF(char)
131
132  AC_MSG_CHECKING(for type equivalent to int8_t)
133  case "$ac_cv_sizeof_char" in
134    1) acx_cv_type_int8_t=char ;;
135    *) AC_MSG_ERROR(no 8-bit type, please report a bug)
136  esac
137  AC_MSG_RESULT($acx_cv_type_int8_t)
138
139  AC_MSG_CHECKING(for type equivalent to int16_t)
140  case "$ac_cv_sizeof_int:$ac_cv_sizeof_short" in
141    2:*) acx_cv_type_int16_t=int ;;
142    *:2) acx_cv_type_int16_t=short ;;
143    *) AC_MSG_ERROR(no 16-bit type, please report a bug)
144  esac
145  AC_MSG_RESULT($acx_cv_type_int16_t)
146
147  AC_MSG_CHECKING(for type equivalent to int32_t)
148  case "$ac_cv_sizeof_int:$ac_cv_sizeof_long" in
149    4:*) acx_cv_type_int32_t=int ;;
150    *:4) acx_cv_type_int32_t=long ;;
151    *) AC_MSG_ERROR(no 32-bit type, please report a bug)
152  esac
153  AC_MSG_RESULT($acx_cv_type_int32_t)
154fi
155
156# These tests are here to make the output prettier
157
158if test "$ac_cv_type_uint64_t" != yes && test "$ac_cv_type_u_int64_t" != yes; then
159  case "$ac_cv_sizeof_long" in
160    8) acx_cv_type_int64_t=long ;;
161  esac
162  AC_MSG_CHECKING(for type equivalent to int64_t)
163  AC_MSG_RESULT(${acx_cv_type_int64_t-'using preprocessor symbols'})
164fi
165
166# Now we can use the above types
167
168if test "$ac_cv_type_uintptr_t" != yes; then
169  AC_MSG_CHECKING(for type equivalent to intptr_t)
170  case $ac_cv_sizeof_void_p in
171    2) acx_cv_type_intptr_t=int16_t ;;
172    4) acx_cv_type_intptr_t=int32_t ;;
173    8) acx_cv_type_intptr_t=int64_t ;;
174    *) AC_MSG_ERROR(no equivalent for intptr_t, please report a bug)
175  esac
176  AC_MSG_RESULT($acx_cv_type_intptr_t)
177fi
178
179# ----------------- done all checks, emit header -------------
180AC_CONFIG_COMMANDS(_GCC_STDINT_H, [
181if test "$GCC" = yes; then
182  echo "/* generated for " `$CC --version | sed 1q` "*/" > tmp-stdint.h
183else
184  echo "/* generated for $CC */" > tmp-stdint.h
185fi
186
187sed 's/^ *//' >> tmp-stdint.h <<EOF
188
189  #ifndef GCC_GENERATED_STDINT_H
190  #define GCC_GENERATED_STDINT_H 1
191
192  #include <sys/types.h>
193EOF
194
195if test "$acx_cv_header_stdint" != stdint.h; then
196  echo "#include <stddef.h>" >> tmp-stdint.h
197fi
198if test "$acx_cv_header_stdint" != stddef.h; then
199  echo "#include <$acx_cv_header_stdint>" >> tmp-stdint.h
200fi
201
202sed 's/^ *//' >> tmp-stdint.h <<EOF
203  /* glibc uses these symbols as guards to prevent redefinitions.  */
204  #ifdef __int8_t_defined
205  #define _INT8_T
206  #define _INT16_T
207  #define _INT32_T
208  #endif
209  #ifdef __uint32_t_defined
210  #define _UINT32_T
211  #endif
212
213EOF
214
215# ----------------- done header, emit basic int types -------------
216if test "$acx_cv_header_stdint" = stddef.h; then
217  sed 's/^ *//' >> tmp-stdint.h <<EOF
218
219    #ifndef _UINT8_T
220    #define _UINT8_T
221    typedef unsigned $acx_cv_type_int8_t uint8_t;
222    #endif
223
224    #ifndef _UINT16_T
225    #define _UINT16_T
226    typedef unsigned $acx_cv_type_int16_t uint16_t;
227    #endif
228
229    #ifndef _UINT32_T
230    #define _UINT32_T
231    typedef unsigned $acx_cv_type_int32_t uint32_t;
232    #endif
233
234    #ifndef _INT8_T
235    #define _INT8_T
236    typedef $acx_cv_type_int8_t int8_t;
237    #endif
238
239    #ifndef _INT16_T
240    #define _INT16_T
241    typedef $acx_cv_type_int16_t int16_t;
242    #endif
243
244    #ifndef _INT32_T
245    #define _INT32_T
246    typedef $acx_cv_type_int32_t int32_t;
247    #endif
248EOF
249elif test "$ac_cv_type_u_int32_t" = yes; then
250  sed 's/^ *//' >> tmp-stdint.h <<EOF
251
252    /* int8_t int16_t int32_t defined by inet code, we do the u_intXX types */
253    #ifndef _INT8_T
254    #define _INT8_T
255    #endif
256    #ifndef _INT16_T
257    #define _INT16_T
258    #endif
259    #ifndef _INT32_T
260    #define _INT32_T
261    #endif
262
263    #ifndef _UINT8_T
264    #define _UINT8_T
265    typedef u_int8_t uint8_t;
266    #endif
267
268    #ifndef _UINT16_T
269    #define _UINT16_T
270    typedef u_int16_t uint16_t;
271    #endif
272
273    #ifndef _UINT32_T
274    #define _UINT32_T
275    typedef u_int32_t uint32_t;
276    #endif
277EOF
278else
279  sed 's/^ *//' >> tmp-stdint.h <<EOF
280
281    /* Some systems have guard macros to prevent redefinitions, define them.  */
282    #ifndef _INT8_T
283    #define _INT8_T
284    #endif
285    #ifndef _INT16_T
286    #define _INT16_T
287    #endif
288    #ifndef _INT32_T
289    #define _INT32_T
290    #endif
291    #ifndef _UINT8_T
292    #define _UINT8_T
293    #endif
294    #ifndef _UINT16_T
295    #define _UINT16_T
296    #endif
297    #ifndef _UINT32_T
298    #define _UINT32_T
299    #endif
300EOF
301fi
302
303# ------------- done basic int types, emit int64_t types ------------
304if test "$ac_cv_type_uint64_t" = yes; then
305  sed 's/^ *//' >> tmp-stdint.h <<EOF
306
307    /* system headers have good uint64_t and int64_t */
308    #ifndef _INT64_T
309    #define _INT64_T
310    #endif
311    #ifndef _UINT64_T
312    #define _UINT64_T
313    #endif
314EOF
315elif test "$ac_cv_type_u_int64_t" = yes; then
316  sed 's/^ *//' >> tmp-stdint.h <<EOF
317
318    /* system headers have an u_int64_t (and int64_t) */
319    #ifndef _INT64_T
320    #define _INT64_T
321    #endif
322    #ifndef _UINT64_T
323    #define _UINT64_T
324    typedef u_int64_t uint64_t;
325    #endif
326EOF
327elif test -n "$acx_cv_type_int64_t"; then
328  sed 's/^ *//' >> tmp-stdint.h <<EOF
329
330    /* architecture has a 64-bit type, $acx_cv_type_int64_t */
331    #ifndef _INT64_T
332    #define _INT64_T
333    typedef $acx_cv_type_int64_t int64_t;
334    #endif
335    #ifndef _UINT64_T
336    #define _UINT64_T
337    typedef unsigned $acx_cv_type_int64_t uint64_t;
338    #endif
339EOF
340else
341  sed 's/^ *//' >> tmp-stdint.h <<EOF
342
343    /* some common heuristics for int64_t, using compiler-specific tests */
344    #if defined __STDC_VERSION__ && (__STDC_VERSION__-0) >= 199901L
345    #ifndef _INT64_T
346    #define _INT64_T
347    typedef long long int64_t;
348    #endif
349    #ifndef _UINT64_T
350    #define _UINT64_T
351    typedef unsigned long long uint64_t;
352    #endif
353
354    #elif defined __GNUC__ && defined (__STDC__) && __STDC__-0
355    /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
356       does not implement __extension__.  But that compiler doesn't define
357       __GNUC_MINOR__.  */
358    # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
359    # define __extension__
360    # endif
361
362    # ifndef _INT64_T
363    # define _INT64_T
364    __extension__ typedef long long int64_t;
365    # endif
366    # ifndef _UINT64_T
367    # define _UINT64_T
368    __extension__ typedef unsigned long long uint64_t;
369    # endif
370
371    #elif !defined __STRICT_ANSI__
372    # if defined _MSC_VER || defined __WATCOMC__ || defined __BORLANDC__
373
374    #  ifndef _INT64_T
375    #  define _INT64_T
376    typedef __int64 int64_t;
377    #  endif
378    #  ifndef _UINT64_T
379    #  define _UINT64_T
380    typedef unsigned __int64 uint64_t;
381    #  endif
382    # endif /* compiler */
383
384    #endif /* ANSI version */
385EOF
386fi
387
388# ------------- done int64_t types, emit intptr types ------------
389if test "$ac_cv_type_uintptr_t" != yes; then
390  sed 's/^ *//' >> tmp-stdint.h <<EOF
391
392    /* Define intptr_t based on sizeof(void*) = $ac_cv_sizeof_void_p */
393    typedef u$acx_cv_type_intptr_t uintptr_t;
394    typedef $acx_cv_type_intptr_t  intptr_t;
395EOF
396fi
397
398# ------------- done intptr types, emit int_least types ------------
399if test "$ac_cv_type_int_least32_t" != yes; then
400  sed 's/^ *//' >> tmp-stdint.h <<EOF
401
402    /* Define int_least types */
403    typedef int8_t     int_least8_t;
404    typedef int16_t    int_least16_t;
405    typedef int32_t    int_least32_t;
406    #ifdef _INT64_T
407    typedef int64_t    int_least64_t;
408    #endif
409
410    typedef uint8_t    uint_least8_t;
411    typedef uint16_t   uint_least16_t;
412    typedef uint32_t   uint_least32_t;
413    #ifdef _UINT64_T
414    typedef uint64_t   uint_least64_t;
415    #endif
416EOF
417fi
418
419# ------------- done intptr types, emit int_fast types ------------
420if test "$ac_cv_type_int_fast32_t" != yes; then
421  dnl NOTE: The following code assumes that sizeof (int) > 1.
422  dnl Fix when strange machines are reported.
423  sed 's/^ *//' >> tmp-stdint.h <<EOF
424
425    /* Define int_fast types.  short is often slow */
426    typedef int8_t       int_fast8_t;
427    typedef int          int_fast16_t;
428    typedef int32_t      int_fast32_t;
429    #ifdef _INT64_T
430    typedef int64_t      int_fast64_t;
431    #endif
432
433    typedef uint8_t      uint_fast8_t;
434    typedef unsigned int uint_fast16_t;
435    typedef uint32_t     uint_fast32_t;
436    #ifdef _UINT64_T
437    typedef uint64_t     uint_fast64_t;
438    #endif
439EOF
440fi
441
442if test "$ac_cv_type_uintmax_t" != yes; then
443  sed 's/^ *//' >> tmp-stdint.h <<EOF
444
445    /* Define intmax based on what we found */
446    #ifdef _INT64_T
447    typedef int64_t       intmax_t;
448    #else
449    typedef long          intmax_t;
450    #endif
451    #ifdef _UINT64_T
452    typedef uint64_t      uintmax_t;
453    #else
454    typedef unsigned long uintmax_t;
455    #endif
456EOF
457fi
458
459sed 's/^ *//' >> tmp-stdint.h <<EOF
460
461  #endif /* GCC_GENERATED_STDINT_H */
462EOF
463
464if test -r ]_GCC_STDINT_H[ && cmp -s tmp-stdint.h ]_GCC_STDINT_H[; then
465  rm -f tmp-stdint.h
466else
467  mv -f tmp-stdint.h ]_GCC_STDINT_H[
468fi
469
470], [
471GCC="$GCC"
472CC="$CC"
473acx_cv_header_stdint="$acx_cv_header_stdint"
474acx_cv_type_int8_t="$acx_cv_type_int8_t"
475acx_cv_type_int16_t="$acx_cv_type_int16_t"
476acx_cv_type_int32_t="$acx_cv_type_int32_t"
477acx_cv_type_int64_t="$acx_cv_type_int64_t"
478acx_cv_type_intptr_t="$acx_cv_type_intptr_t"
479ac_cv_type_uintmax_t="$ac_cv_type_uintmax_t"
480ac_cv_type_uintptr_t="$ac_cv_type_uintptr_t"
481ac_cv_type_uint64_t="$ac_cv_type_uint64_t"
482ac_cv_type_u_int64_t="$ac_cv_type_u_int64_t"
483ac_cv_type_u_int32_t="$ac_cv_type_u_int32_t"
484ac_cv_type_int_least32_t="$ac_cv_type_int_least32_t"
485ac_cv_type_int_fast32_t="$ac_cv_type_int_fast32_t"
486ac_cv_sizeof_void_p="$ac_cv_sizeof_void_p"
487])
488
489])
490