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