]> git.cworth.org Git - tar/blob - m4/inttypes.m4
Imported Upstream version 1.24
[tar] / m4 / inttypes.m4
1 # inttypes.m4 serial 18
2 dnl Copyright (C) 2006-2010 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl From Derek Price, Bruno Haible.
8 dnl Test whether <inttypes.h> is supported or must be substituted.
9
10 AC_DEFUN([gl_INTTYPES_H],
11 [
12   AC_REQUIRE([gl_STDINT_H])
13   AC_REQUIRE([gt_INTTYPES_PRI])
14   AC_CHECK_HEADERS_ONCE([inttypes.h])
15   AC_CHECK_DECLS_ONCE([imaxabs])
16   AC_CHECK_DECLS_ONCE([imaxdiv])
17   AC_CHECK_DECLS_ONCE([strtoimax])
18   AC_CHECK_DECLS_ONCE([strtoumax])
19
20   dnl Now see if we need a substitute <inttypes.h>.
21   dnl A complete <inttypes.h> requires
22   dnl   - a complete <stdint.h>,
23   dnl   - the existence of an <inttypes.h>,
24   dnl   - that imaxabs, imaxdiv, strtoimax, strtoumax are declared,
25   dnl   - some additional tests.
26   AC_CACHE_CHECK([whether inttypes.h conforms to C99],
27     [gl_cv_header_working_inttypes_h],
28     [gl_cv_header_working_inttypes_h=no
29      if test "$gl_cv_header_working_stdint_h" = yes \
30         && test $ac_cv_header_inttypes_h = yes \
31         && test "$ac_cv_have_decl_imaxabs" = yes \
32         && test "$ac_cv_have_decl_imaxdiv" = yes \
33         && test "$ac_cv_have_decl_strtoimax" = yes \
34         && test "$ac_cv_have_decl_strtoumax" = yes; then
35        AC_COMPILE_IFELSE([
36          AC_LANG_PROGRAM([[
37 #include <stddef.h>
38 #define __STDC_LIMIT_MACROS 1 /* to make it work also in C++ mode */
39 #define __STDC_CONSTANT_MACROS 1 /* to make it work also in C++ mode */
40 #define __STDC_FORMAT_MACROS 1 /* to make it work also in C++ mode */
41 #define _GL_JUST_INCLUDE_SYSTEM_INTTYPES_H /* work if build isn't clean */
42 #include <inttypes.h>
43
44 /* No need to duplicate the tests of stdint.m4; they are subsumed by
45    $gl_cv_header_working_stdint_h = yes.  */
46
47 /* Tests for macros supposed to be defined in inttypes.h.  */
48
49 const char *k = /* implicit string concatenation */
50 #ifdef INT8_MAX
51   PRId8 PRIi8
52 #endif
53 #ifdef UINT8_MAX
54   PRIo8 PRIu8 PRIx8 PRIX8
55 #endif
56 #ifdef INT16_MAX
57   PRId16 PRIi16
58 #endif
59 #ifdef UINT16_MAX
60   PRIo16 PRIu16 PRIx16 PRIX16
61 #endif
62 #ifdef INT32_MAX
63   PRId32 PRIi32
64 #endif
65 #ifdef UINT32_MAX
66   PRIo32 PRIu32 PRIx32 PRIX32
67 #endif
68 #ifdef INT64_MAX
69   PRId64 PRIi64
70 #endif
71 #ifdef UINT64_MAX
72   PRIo64 PRIu64 PRIx64 PRIX64
73 #endif
74   PRIdLEAST8 PRIiLEAST8 PRIoLEAST8 PRIuLEAST8 PRIxLEAST8 PRIXLEAST8
75   PRIdLEAST16 PRIiLEAST16 PRIoLEAST16 PRIuLEAST16 PRIxLEAST16 PRIXLEAST16
76   PRIdLEAST32 PRIiLEAST32 PRIoLEAST32 PRIuLEAST32 PRIxLEAST32 PRIXLEAST32
77   PRIdLEAST64 PRIiLEAST64
78   PRIoLEAST64 PRIuLEAST64 PRIxLEAST64 PRIXLEAST64
79   PRIdFAST8 PRIiFAST8 PRIoFAST8 PRIuFAST8 PRIxFAST8 PRIXFAST8
80   PRIdFAST16 PRIiFAST16 PRIoFAST16 PRIuFAST16 PRIxFAST16 PRIXFAST16
81   PRIdFAST32 PRIiFAST32 PRIoFAST32 PRIuFAST32 PRIxFAST32 PRIXFAST32
82   PRIdFAST64 PRIiFAST64
83   PRIoFAST64 PRIuFAST64 PRIxFAST64 PRIXFAST64
84   PRIdMAX PRIiMAX PRIoMAX PRIuMAX PRIxMAX PRIXMAX
85 #ifdef INTPTR_MAX
86   PRIdPTR PRIiPTR
87 #endif
88 #ifdef UINTPTR_MAX
89   PRIoPTR PRIuPTR PRIxPTR PRIXPTR
90 #endif
91   ;
92 const char *l = /* implicit string concatenation */
93 #ifdef INT8_MAX
94   SCNd8 SCNi8
95 #endif
96 #ifdef UINT8_MAX
97   SCNo8 SCNu8 SCNx8
98 #endif
99 #ifdef INT16_MAX
100   SCNd16 SCNi16
101 #endif
102 #ifdef UINT16_MAX
103   SCNo16 SCNu16 SCNx16
104 #endif
105 #ifdef INT32_MAX
106   SCNd32 SCNi32
107 #endif
108 #ifdef UINT32_MAX
109   SCNo32 SCNu32 SCNx32
110 #endif
111 #ifdef INT64_MAX
112   SCNd64 SCNi64
113 #endif
114 #ifdef UINT64_MAX
115   SCNo64 SCNu64 SCNx64
116 #endif
117   SCNdLEAST8 SCNiLEAST8 SCNoLEAST8 SCNuLEAST8 SCNxLEAST8
118   SCNdLEAST16 SCNiLEAST16 SCNoLEAST16 SCNuLEAST16 SCNxLEAST16
119   SCNdLEAST32 SCNiLEAST32 SCNoLEAST32 SCNuLEAST32 SCNxLEAST32
120   SCNdLEAST64 SCNiLEAST64
121   SCNoLEAST64 SCNuLEAST64 SCNxLEAST64
122   SCNdFAST8 SCNiFAST8 SCNoFAST8 SCNuFAST8 SCNxFAST8
123   SCNdFAST16 SCNiFAST16 SCNoFAST16 SCNuFAST16 SCNxFAST16
124   SCNdFAST32 SCNiFAST32 SCNoFAST32 SCNuFAST32 SCNxFAST32
125   SCNdFAST64 SCNiFAST64
126   SCNoFAST64 SCNuFAST64 SCNxFAST64
127   SCNdMAX SCNiMAX SCNoMAX SCNuMAX SCNxMAX
128 #ifdef INTPTR_MAX
129   SCNdPTR SCNiPTR
130 #endif
131 #ifdef UINTPTR_MAX
132   SCNoPTR SCNuPTR SCNxPTR
133 #endif
134   ;
135          ]])],
136          [gl_cv_header_working_inttypes_h=yes])
137      fi])
138
139   dnl Override <inttypes.h> always, so that the portability warnings work.
140   AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
141   gl_CHECK_NEXT_HEADERS([inttypes.h])
142
143   AC_REQUIRE([gl_MULTIARCH])
144
145   dnl Ensure that <stdint.h> defines the limit macros, since gnulib's
146   dnl <inttypes.h> relies on them.  This macro is only needed when a
147   dnl C++ compiler is in use; it has no effect for a C compiler.
148   dnl Also be careful to define __STDC_LIMIT_MACROS only when gnulib's
149   dnl <inttypes.h> is going to be created, and to avoid redefinition warnings
150   dnl if the __STDC_LIMIT_MACROS is already defined through the CPPFLAGS.
151   AC_DEFINE([GL_TRIGGER_STDC_LIMIT_MACROS], [1],
152     [Define to make the limit macros in <stdint.h> visible.])
153   AH_VERBATIM([__STDC_LIMIT_MACROS_ZZZ],
154 [/* Ensure that <stdint.h> defines the limit macros, since gnulib's
155    <inttypes.h> relies on them.  */
156 #if defined __cplusplus && !defined __STDC_LIMIT_MACROS && GL_TRIGGER_STDC_LIMIT_MACROS
157 # define __STDC_LIMIT_MACROS 1
158 #endif
159 ])
160
161   PRIPTR_PREFIX=
162   if test -n "$STDINT_H"; then
163     dnl Using the gnulib <stdint.h>. It always defines intptr_t to 'long'.
164     PRIPTR_PREFIX='"l"'
165   else
166     dnl Using the system's <stdint.h>.
167     for glpfx in '' l ll I64; do
168       case $glpfx in
169         '')  gltype1='int';;
170         l)   gltype1='long int';;
171         ll)  gltype1='long long int';;
172         I64) gltype1='__int64';;
173       esac
174       AC_COMPILE_IFELSE(
175         [AC_LANG_PROGRAM([[#include <stdint.h>
176            extern intptr_t foo;
177            extern $gltype1 foo;]])],
178         [PRIPTR_PREFIX='"'$glpfx'"'])
179       test -n "$PRIPTR_PREFIX" && break
180     done
181   fi
182   AC_SUBST([PRIPTR_PREFIX])
183
184   if test "$ac_cv_have_decl_imaxabs" = yes; then
185     HAVE_DECL_IMAXABS=1
186   else
187     HAVE_DECL_IMAXABS=0
188   fi
189
190   if test "$ac_cv_have_decl_imaxdiv" = yes; then
191     HAVE_DECL_IMAXDIV=1
192   else
193     HAVE_DECL_IMAXDIV=0
194   fi
195
196   if test "$ac_cv_have_decl_strtoimax" = yes; then
197     HAVE_DECL_STRTOIMAX=1
198   else
199     HAVE_DECL_STRTOIMAX=0
200   fi
201
202   if test "$ac_cv_have_decl_strtoumax" = yes; then
203     HAVE_DECL_STRTOUMAX=1
204   else
205     HAVE_DECL_STRTOUMAX=0
206   fi
207
208   gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
209     [INT32_MAX_LT_INTMAX_MAX],
210     [defined INT32_MAX && defined INTMAX_MAX],
211     [INT32_MAX < INTMAX_MAX],
212     [sizeof (int) < sizeof (long long int)])
213   if test $APPLE_UNIVERSAL_BUILD = 0; then
214     gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
215       [INT64_MAX_EQ_LONG_MAX],
216       [defined INT64_MAX],
217       [INT64_MAX == LONG_MAX],
218       [sizeof (long long int) == sizeof (long int)])
219   else
220     INT64_MAX_EQ_LONG_MAX=-1
221   fi
222   gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
223     [UINT32_MAX_LT_UINTMAX_MAX],
224     [defined UINT32_MAX && defined UINTMAX_MAX],
225     [UINT32_MAX < UINTMAX_MAX],
226     [sizeof (unsigned int) < sizeof (unsigned long long int)])
227   if test $APPLE_UNIVERSAL_BUILD = 0; then
228     gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
229       [UINT64_MAX_EQ_ULONG_MAX],
230       [defined UINT64_MAX],
231       [UINT64_MAX == ULONG_MAX],
232       [sizeof (unsigned long long int) == sizeof (unsigned long int)])
233   else
234     UINT64_MAX_EQ_ULONG_MAX=-1
235   fi
236
237   dnl Check for declarations of anything we want to poison if the
238   dnl corresponding gnulib module is not in use.
239   gl_WARN_ON_USE_PREPARE([[#include <inttypes.h>
240     ]], [imaxabs imaxdiv strtoimax strtoumax])
241 ])
242
243 # Define the symbol $1 to be 1 if the condition is true, 0 otherwise.
244 # If $2 is true, the condition is $3; otherwise if long long int is supported
245 # approximate the condition with $4; otherwise, assume the condition is false.
246 # The condition should work on all C99 platforms; the approximations should be
247 # good enough to work on all practical pre-C99 platforms.
248 # $2 is evaluated by the C preprocessor, $3 and $4 as compile-time constants.
249 AC_DEFUN([gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION],
250 [
251   AC_CACHE_CHECK([whether $3],
252     [gl_cv_test_$1],
253     [AC_COMPILE_IFELSE(
254        [AC_LANG_PROGRAM(
255           [[/* Work also in C++ mode.  */
256             #define __STDC_LIMIT_MACROS 1
257
258             /* Work if build is not clean.  */
259             #define _GL_JUST_INCLUDE_SYSTEM_STDINT_H
260
261             #include <limits.h>
262             #if HAVE_STDINT_H
263              #include <stdint.h>
264             #endif
265
266             #if $2
267              #define CONDITION ($3)
268             #elif HAVE_LONG_LONG_INT
269              #define CONDITION ($4)
270             #else
271              #define CONDITION 0
272             #endif
273             int test[CONDITION ? 1 : -1];]])],
274        [gl_cv_test_$1=yes],
275        [gl_cv_test_$1=no])])
276   if test $gl_cv_test_$1 = yes; then
277     $1=1;
278   else
279     $1=0;
280   fi
281   AC_SUBST([$1])
282 ])
283
284 AC_DEFUN([gl_INTTYPES_MODULE_INDICATOR],
285 [
286   dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
287   AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
288   gl_MODULE_INDICATOR_SET_VARIABLE([$1])
289 ])
290
291 AC_DEFUN([gl_INTTYPES_H_DEFAULTS],
292 [
293   GNULIB_IMAXABS=0;      AC_SUBST([GNULIB_IMAXABS])
294   GNULIB_IMAXDIV=0;      AC_SUBST([GNULIB_IMAXDIV])
295   GNULIB_STRTOIMAX=0;    AC_SUBST([GNULIB_STRTOIMAX])
296   GNULIB_STRTOUMAX=0;    AC_SUBST([GNULIB_STRTOUMAX])
297   dnl Assume proper GNU behavior unless another module says otherwise.
298   HAVE_DECL_IMAXABS=1;   AC_SUBST([HAVE_DECL_IMAXABS])
299   HAVE_DECL_IMAXDIV=1;   AC_SUBST([HAVE_DECL_IMAXDIV])
300   HAVE_DECL_STRTOIMAX=1; AC_SUBST([HAVE_DECL_STRTOIMAX])
301   HAVE_DECL_STRTOUMAX=1; AC_SUBST([HAVE_DECL_STRTOUMAX])
302 ])