* m4/include_next.m4 (gl_INCLUDE_NEXT): Define and AC_SUBST
[gnulib.git] / m4 / inttypes.m4
1 # inttypes.m4 serial 9
2 dnl Copyright (C) 2006-2007 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_DECLS_ONCE([imaxabs])
15   AC_CHECK_DECLS_ONCE([imaxdiv])
16   AC_CHECK_DECLS_ONCE([strtoimax])
17   AC_CHECK_DECLS_ONCE([strtoumax])
18
19   dnl Now see if we need a substitute <inttypes.h>.
20   dnl A complete <inttypes.h> requires
21   dnl   - a complete <stdint.h>,
22   dnl   - the existence of an <inttypes.h>,
23   dnl   - that imaxabs, imaxdiv, strtoimax, strtoumax are declared,
24   dnl   - some additional tests.
25   AC_CACHE_CHECK([whether inttypes.h conforms to C99],
26     [gl_cv_header_working_inttypes_h],
27     [gl_cv_header_working_inttypes_h=no
28      if test "$gl_cv_header_working_stdint_h" = yes \
29         && test $ac_cv_header_inttypes_h = yes \
30         && test "$ac_cv_have_decl_imaxabs" = yes \
31         && test "$ac_cv_have_decl_imaxdiv" = yes \
32         && test "$ac_cv_have_decl_strtoimax" = yes \
33         && test "$ac_cv_have_decl_strtoumax" = yes; then
34        AC_COMPILE_IFELSE([
35          AC_LANG_PROGRAM([
36 #include <stddef.h>
37 #define __STDC_LIMIT_MACROS 1 /* to make it work also in C++ mode */
38 #define __STDC_CONSTANT_MACROS 1 /* to make it work also in C++ mode */
39 #define __STDC_FORMAT_MACROS 1 /* to make it work also in C++ mode */
40 #define _GL_JUST_INCLUDE_SYSTEM_INTTYPES_H /* work if build isn't clean */
41 #include <inttypes.h>
42
43 /* No need to duplicate the tests of stdint.m4; they are subsumed by
44    $gl_cv_header_working_stdint_h = yes.  */
45
46 /* Tests for macros supposed to be defined in inttypes.h.  */
47
48 const char *k = /* implicit string concatenation */
49 #ifdef INT8_MAX
50   PRId8 PRIi8
51 #endif
52 #ifdef UINT8_MAX
53   PRIo8 PRIu8 PRIx8 PRIX8
54 #endif
55 #ifdef INT16_MAX
56   PRId16 PRIi16
57 #endif
58 #ifdef UINT16_MAX
59   PRIo16 PRIu16 PRIx16 PRIX16
60 #endif
61 #ifdef INT32_MAX
62   PRId32 PRIi32
63 #endif
64 #ifdef UINT32_MAX
65   PRIo32 PRIu32 PRIx32 PRIX32
66 #endif
67 #ifdef INT64_MAX
68   PRId64 PRIi64
69 #endif
70 #ifdef UINT64_MAX
71   PRIo64 PRIu64 PRIx64 PRIX64
72 #endif
73   PRIdLEAST8 PRIiLEAST8 PRIoLEAST8 PRIuLEAST8 PRIxLEAST8 PRIXLEAST8
74   PRIdLEAST16 PRIiLEAST16 PRIoLEAST16 PRIuLEAST16 PRIxLEAST16 PRIXLEAST16
75   PRIdLEAST32 PRIiLEAST32 PRIoLEAST32 PRIuLEAST32 PRIxLEAST32 PRIXLEAST32
76   PRIdLEAST64 PRIiLEAST64
77   PRIoLEAST64 PRIuLEAST64 PRIxLEAST64 PRIXLEAST64
78   PRIdFAST8 PRIiFAST8 PRIoFAST8 PRIuFAST8 PRIxFAST8 PRIXFAST8
79   PRIdFAST16 PRIiFAST16 PRIoFAST16 PRIuFAST16 PRIxFAST16 PRIXFAST16
80   PRIdFAST32 PRIiFAST32 PRIoFAST32 PRIuFAST32 PRIxFAST32 PRIXFAST32
81   PRIdFAST64 PRIiFAST64
82   PRIoFAST64 PRIuFAST64 PRIxFAST64 PRIXFAST64
83   PRIdMAX PRIiMAX PRIoMAX PRIuMAX PRIxMAX PRIXMAX
84 #ifdef INTPTR_MAX
85   PRIdPTR PRIiPTR
86 #endif
87 #ifdef UINTPTR_MAX
88   PRIoPTR PRIuPTR PRIxPTR PRIXPTR
89 #endif
90   ;
91 const char *l = /* implicit string concatenation */
92 #ifdef INT8_MAX
93   SCNd8 SCNi8
94 #endif
95 #ifdef UINT8_MAX
96   SCNo8 SCNu8 SCNx8
97 #endif
98 #ifdef INT16_MAX
99   SCNd16 SCNi16
100 #endif
101 #ifdef UINT16_MAX
102   SCNo16 SCNu16 SCNx16
103 #endif
104 #ifdef INT32_MAX
105   SCNd32 SCNi32
106 #endif
107 #ifdef UINT32_MAX
108   SCNo32 SCNu32 SCNx32
109 #endif
110 #ifdef INT64_MAX
111   SCNd64 SCNi64
112 #endif
113 #ifdef UINT64_MAX
114   SCNo64 SCNu64 SCNx64
115 #endif
116   SCNdLEAST8 SCNiLEAST8 SCNoLEAST8 SCNuLEAST8 SCNxLEAST8
117   SCNdLEAST16 SCNiLEAST16 SCNoLEAST16 SCNuLEAST16 SCNxLEAST16
118   SCNdLEAST32 SCNiLEAST32 SCNoLEAST32 SCNuLEAST32 SCNxLEAST32
119   SCNdLEAST64 SCNiLEAST64
120   SCNoLEAST64 SCNuLEAST64 SCNxLEAST64
121   SCNdFAST8 SCNiFAST8 SCNoFAST8 SCNuFAST8 SCNxFAST8
122   SCNdFAST16 SCNiFAST16 SCNoFAST16 SCNuFAST16 SCNxFAST16
123   SCNdFAST32 SCNiFAST32 SCNoFAST32 SCNuFAST32 SCNxFAST32
124   SCNdFAST64 SCNiFAST64
125   SCNoFAST64 SCNuFAST64 SCNxFAST64
126   SCNdMAX SCNiMAX SCNoMAX SCNuMAX SCNxMAX
127 #ifdef INTPTR_MAX
128   SCNdPTR SCNiPTR
129 #endif
130 #ifdef UINTPTR_MAX
131   SCNoPTR SCNuPTR SCNxPTR
132 #endif
133   ;
134          ])],
135          [gl_cv_header_working_inttypes_h=yes])
136      fi])
137
138   dnl Override <inttypes.h> always, so that the portability warnings work.
139   if false && test $gl_cv_header_working_inttypes_h = yes; then
140     dnl Use the existing <inttypes.h>.
141     INTTYPES_H=''
142   else
143
144     AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
145     gl_CHECK_NEXT_HEADERS([inttypes.h])
146
147     PRIPTR_PREFIX=
148     if test -n "$STDINT_H"; then
149       dnl Using the gnulib <stdint.h>. It always defines intptr_t to 'long'.
150       PRIPTR_PREFIX='"l"'
151     else
152       dnl Using the system's <stdint.h>.
153       for glpfx in '' l ll I64; do
154         case $glpfx in
155           '')  gltype1='int';;
156           l)   gltype1='long int';;
157           ll)  gltype1='long long int';;
158           I64) gltype1='__int64';;
159         esac
160         AC_COMPILE_IFELSE(
161           [AC_LANG_PROGRAM([#include <stdint.h>
162              extern intptr_t foo;
163              extern $gltype1 foo;])],
164           [PRIPTR_PREFIX='"'$glpfx'"'])
165         test -n "$PRIPTR_PREFIX" && break
166       done
167     fi
168     AC_SUBST([PRIPTR_PREFIX])
169
170     if test "$ac_cv_have_decl_imaxabs" = yes; then
171       HAVE_DECL_IMAXABS=1
172     else
173       HAVE_DECL_IMAXABS=0
174     fi
175
176     if test "$ac_cv_have_decl_imaxdiv" = yes; then
177       HAVE_DECL_IMAXDIV=1
178     else
179       HAVE_DECL_IMAXDIV=0
180     fi
181
182     if test "$ac_cv_have_decl_strtoimax" = yes; then
183       HAVE_DECL_STRTOIMAX=1
184     else
185       HAVE_DECL_STRTOIMAX=0
186     fi
187
188     if test "$ac_cv_have_decl_strtoumax" = yes; then
189       HAVE_DECL_STRTOUMAX=1
190     else
191       HAVE_DECL_STRTOUMAX=0
192     fi
193
194     INTTYPES_H='inttypes.h'
195   fi
196   AC_SUBST(INTTYPES_H)
197 ])
198
199 AC_DEFUN([gl_INTTYPES_MODULE_INDICATOR],
200 [
201   dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
202   AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
203   GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1
204 ])
205
206 AC_DEFUN([gl_INTTYPES_H_DEFAULTS],
207 [
208   GNULIB_IMAXABS=0;      AC_SUBST([GNULIB_IMAXABS])
209   GNULIB_IMAXDIV=0;      AC_SUBST([GNULIB_IMAXDIV])
210   GNULIB_STRTOIMAX=0;    AC_SUBST([GNULIB_STRTOIMAX])
211   GNULIB_STRTOUMAX=0;    AC_SUBST([GNULIB_STRTOUMAX])
212   dnl Assume proper GNU behavior unless another module says otherwise.
213   HAVE_DECL_IMAXABS=1;   AC_SUBST([HAVE_DECL_IMAXABS])
214   HAVE_DECL_IMAXDIV=1;   AC_SUBST([HAVE_DECL_IMAXDIV])
215   HAVE_DECL_STRTOIMAX=1; AC_SUBST([HAVE_DECL_STRTOIMAX])
216   HAVE_DECL_STRTOUMAX=1; AC_SUBST([HAVE_DECL_STRTOUMAX])
217 ])