inttypes: Move some configure check to module 'imaxabs'.
[gnulib.git] / m4 / inttypes.m4
1 # inttypes.m4 serial 20
2 dnl Copyright (C) 2006-2011 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([imaxdiv])
16   AC_CHECK_DECLS_ONCE([strtoimax])
17   AC_CHECK_DECLS_ONCE([strtoumax])
18
19   dnl Override <inttypes.h> always, so that the portability warnings work.
20   AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
21   gl_CHECK_NEXT_HEADERS([inttypes.h])
22
23   AC_REQUIRE([gl_MULTIARCH])
24
25   dnl Ensure that <stdint.h> defines the limit macros, since gnulib's
26   dnl <inttypes.h> relies on them.  This macro is only needed when a
27   dnl C++ compiler is in use; it has no effect for a C compiler.
28   dnl Also be careful to define __STDC_LIMIT_MACROS only when gnulib's
29   dnl <inttypes.h> is going to be created, and to avoid redefinition warnings
30   dnl if the __STDC_LIMIT_MACROS is already defined through the CPPFLAGS.
31   AC_DEFINE([GL_TRIGGER_STDC_LIMIT_MACROS], [1],
32     [Define to make the limit macros in <stdint.h> visible.])
33   AH_VERBATIM([__STDC_LIMIT_MACROS_ZZZ],
34 [/* Ensure that <stdint.h> defines the limit macros, since gnulib's
35    <inttypes.h> relies on them.  */
36 #if defined __cplusplus && !defined __STDC_LIMIT_MACROS && GL_TRIGGER_STDC_LIMIT_MACROS
37 # define __STDC_LIMIT_MACROS 1
38 #endif
39 ])
40
41   PRIPTR_PREFIX=
42   if test -n "$STDINT_H"; then
43     dnl Using the gnulib <stdint.h>. It always defines intptr_t to 'long'.
44     PRIPTR_PREFIX='"l"'
45   else
46     dnl Using the system's <stdint.h>.
47     for glpfx in '' l ll I64; do
48       case $glpfx in
49         '')  gltype1='int';;
50         l)   gltype1='long int';;
51         ll)  gltype1='long long int';;
52         I64) gltype1='__int64';;
53       esac
54       AC_COMPILE_IFELSE(
55         [AC_LANG_PROGRAM([[#include <stdint.h>
56            extern intptr_t foo;
57            extern $gltype1 foo;]])],
58         [PRIPTR_PREFIX='"'$glpfx'"'])
59       test -n "$PRIPTR_PREFIX" && break
60     done
61   fi
62   AC_SUBST([PRIPTR_PREFIX])
63
64   if test "$ac_cv_have_decl_imaxdiv" = yes; then
65     HAVE_DECL_IMAXDIV=1
66   else
67     HAVE_DECL_IMAXDIV=0
68   fi
69
70   if test "$ac_cv_have_decl_strtoimax" = yes; then
71     HAVE_DECL_STRTOIMAX=1
72   else
73     HAVE_DECL_STRTOIMAX=0
74   fi
75
76   if test "$ac_cv_have_decl_strtoumax" = yes; then
77     HAVE_DECL_STRTOUMAX=1
78   else
79     HAVE_DECL_STRTOUMAX=0
80   fi
81
82   gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
83     [INT32_MAX_LT_INTMAX_MAX],
84     [defined INT32_MAX && defined INTMAX_MAX],
85     [INT32_MAX < INTMAX_MAX],
86     [sizeof (int) < sizeof (long long int)])
87   if test $APPLE_UNIVERSAL_BUILD = 0; then
88     gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
89       [INT64_MAX_EQ_LONG_MAX],
90       [defined INT64_MAX],
91       [INT64_MAX == LONG_MAX],
92       [sizeof (long long int) == sizeof (long int)])
93   else
94     INT64_MAX_EQ_LONG_MAX=-1
95   fi
96   gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
97     [UINT32_MAX_LT_UINTMAX_MAX],
98     [defined UINT32_MAX && defined UINTMAX_MAX],
99     [UINT32_MAX < UINTMAX_MAX],
100     [sizeof (unsigned int) < sizeof (unsigned long long int)])
101   if test $APPLE_UNIVERSAL_BUILD = 0; then
102     gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
103       [UINT64_MAX_EQ_ULONG_MAX],
104       [defined UINT64_MAX],
105       [UINT64_MAX == ULONG_MAX],
106       [sizeof (unsigned long long int) == sizeof (unsigned long int)])
107   else
108     UINT64_MAX_EQ_ULONG_MAX=-1
109   fi
110
111   dnl Check for declarations of anything we want to poison if the
112   dnl corresponding gnulib module is not in use.
113   gl_WARN_ON_USE_PREPARE([[#include <inttypes.h>
114     ]], [imaxabs imaxdiv strtoimax strtoumax])
115 ])
116
117 # Define the symbol $1 to be 1 if the condition is true, 0 otherwise.
118 # If $2 is true, the condition is $3; otherwise if long long int is supported
119 # approximate the condition with $4; otherwise, assume the condition is false.
120 # The condition should work on all C99 platforms; the approximations should be
121 # good enough to work on all practical pre-C99 platforms.
122 # $2 is evaluated by the C preprocessor, $3 and $4 as compile-time constants.
123 AC_DEFUN([gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION],
124 [
125   AC_CACHE_CHECK([whether $3],
126     [gl_cv_test_$1],
127     [AC_COMPILE_IFELSE(
128        [AC_LANG_PROGRAM(
129           [[/* Work also in C++ mode.  */
130             #define __STDC_LIMIT_MACROS 1
131
132             /* Work if build is not clean.  */
133             #define _GL_JUST_INCLUDE_SYSTEM_STDINT_H
134
135             #include <limits.h>
136             #if HAVE_STDINT_H
137              #include <stdint.h>
138             #endif
139
140             #if $2
141              #define CONDITION ($3)
142             #elif HAVE_LONG_LONG_INT
143              #define CONDITION ($4)
144             #else
145              #define CONDITION 0
146             #endif
147             int test[CONDITION ? 1 : -1];]])],
148        [gl_cv_test_$1=yes],
149        [gl_cv_test_$1=no])])
150   if test $gl_cv_test_$1 = yes; then
151     $1=1;
152   else
153     $1=0;
154   fi
155   AC_SUBST([$1])
156 ])
157
158 AC_DEFUN([gl_INTTYPES_MODULE_INDICATOR],
159 [
160   dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
161   AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
162   gl_MODULE_INDICATOR_SET_VARIABLE([$1])
163 ])
164
165 AC_DEFUN([gl_INTTYPES_H_DEFAULTS],
166 [
167   GNULIB_IMAXABS=0;      AC_SUBST([GNULIB_IMAXABS])
168   GNULIB_IMAXDIV=0;      AC_SUBST([GNULIB_IMAXDIV])
169   GNULIB_STRTOIMAX=0;    AC_SUBST([GNULIB_STRTOIMAX])
170   GNULIB_STRTOUMAX=0;    AC_SUBST([GNULIB_STRTOUMAX])
171   dnl Assume proper GNU behavior unless another module says otherwise.
172   HAVE_DECL_IMAXABS=1;   AC_SUBST([HAVE_DECL_IMAXABS])
173   HAVE_DECL_IMAXDIV=1;   AC_SUBST([HAVE_DECL_IMAXDIV])
174   HAVE_DECL_STRTOIMAX=1; AC_SUBST([HAVE_DECL_STRTOIMAX])
175   HAVE_DECL_STRTOUMAX=1; AC_SUBST([HAVE_DECL_STRTOUMAX])
176 ])