1# inttypes.m4 serial 29
2dnl Copyright (C) 2006-2020 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl From Derek Price, Bruno Haible.
8dnl Test whether <inttypes.h> is supported or must be substituted.
9
10AC_DEFUN([gl_INTTYPES_H],
11[
12  AC_REQUIRE([gl_INTTYPES_INCOMPLETE])
13  gl_INTTYPES_PRI_SCN
14])
15
16AC_DEFUN_ONCE([gl_INTTYPES_INCOMPLETE],
17[
18  AC_REQUIRE([gl_STDINT_H])
19  AC_CHECK_HEADERS_ONCE([inttypes.h])
20
21  dnl Override <inttypes.h> always, so that the portability warnings work.
22  AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
23  gl_CHECK_NEXT_HEADERS([inttypes.h])
24
25  AC_REQUIRE([gl_MULTIARCH])
26
27  dnl Check for declarations of anything we want to poison if the
28  dnl corresponding gnulib module is not in use.
29  gl_WARN_ON_USE_PREPARE([[#include <inttypes.h>
30    ]], [imaxabs imaxdiv strtoimax strtoumax])
31
32  AC_REQUIRE([AC_C_RESTRICT])
33])
34
35# Ensure that the PRI* and SCN* macros are defined appropriately.
36AC_DEFUN([gl_INTTYPES_PRI_SCN],
37[
38  AC_REQUIRE([gt_INTTYPES_PRI])
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  gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
64    [INT32_MAX_LT_INTMAX_MAX],
65    [defined INT32_MAX && defined INTMAX_MAX],
66    [INT32_MAX < INTMAX_MAX],
67    [sizeof (int) < sizeof (long long int)])
68  if test $APPLE_UNIVERSAL_BUILD = 0; then
69    gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
70      [INT64_MAX_EQ_LONG_MAX],
71      [defined INT64_MAX],
72      [INT64_MAX == LONG_MAX],
73      [sizeof (long long int) == sizeof (long int)])
74  else
75    INT64_MAX_EQ_LONG_MAX=-1
76  fi
77  gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
78    [UINT32_MAX_LT_UINTMAX_MAX],
79    [defined UINT32_MAX && defined UINTMAX_MAX],
80    [UINT32_MAX < UINTMAX_MAX],
81    [sizeof (unsigned int) < sizeof (unsigned long long int)])
82  if test $APPLE_UNIVERSAL_BUILD = 0; then
83    gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION(
84      [UINT64_MAX_EQ_ULONG_MAX],
85      [defined UINT64_MAX],
86      [UINT64_MAX == ULONG_MAX],
87      [sizeof (unsigned long long int) == sizeof (unsigned long int)])
88  else
89    UINT64_MAX_EQ_ULONG_MAX=-1
90  fi
91])
92
93# Define the symbol $1 to be 1 if the condition is true, 0 otherwise.
94# If $2 is true, the condition is $3; otherwise if long long int is supported
95# approximate the condition with $4; otherwise, assume the condition is false.
96# The condition should work on all C99 platforms; the approximations should be
97# good enough to work on all practical pre-C99 platforms.
98# $2 is evaluated by the C preprocessor, $3 and $4 as compile-time constants.
99AC_DEFUN([gl_INTTYPES_CHECK_LONG_LONG_INT_CONDITION],
100[
101  AC_CACHE_CHECK([whether $3],
102    [gl_cv_test_$1],
103    [AC_COMPILE_IFELSE(
104       [AC_LANG_PROGRAM(
105          [[/* Work also in C++ mode.  */
106            #define __STDC_LIMIT_MACROS 1
107
108            /* Work if build is not clean.  */
109            #define _GL_JUST_INCLUDE_SYSTEM_STDINT_H
110
111            #include <limits.h>
112            #if HAVE_STDINT_H
113             #include <stdint.h>
114            #endif
115
116            #if $2
117             #define CONDITION ($3)
118            #else
119             #define CONDITION ($4)
120            #endif
121            int test[CONDITION ? 1 : -1];]])],
122       [gl_cv_test_$1=yes],
123       [gl_cv_test_$1=no])])
124  if test $gl_cv_test_$1 = yes; then
125    $1=1;
126  else
127    $1=0;
128  fi
129  AC_SUBST([$1])
130])
131
132AC_DEFUN([gl_INTTYPES_MODULE_INDICATOR],
133[
134  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
135  AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
136  gl_MODULE_INDICATOR_SET_VARIABLE([$1])
137])
138
139AC_DEFUN([gl_INTTYPES_H_DEFAULTS],
140[
141  GNULIB_IMAXABS=0;      AC_SUBST([GNULIB_IMAXABS])
142  GNULIB_IMAXDIV=0;      AC_SUBST([GNULIB_IMAXDIV])
143  GNULIB_STRTOIMAX=0;    AC_SUBST([GNULIB_STRTOIMAX])
144  GNULIB_STRTOUMAX=0;    AC_SUBST([GNULIB_STRTOUMAX])
145  dnl Assume proper GNU behavior unless another module says otherwise.
146  HAVE_DECL_IMAXABS=1;   AC_SUBST([HAVE_DECL_IMAXABS])
147  HAVE_DECL_IMAXDIV=1;   AC_SUBST([HAVE_DECL_IMAXDIV])
148  HAVE_DECL_STRTOIMAX=1; AC_SUBST([HAVE_DECL_STRTOIMAX])
149  HAVE_DECL_STRTOUMAX=1; AC_SUBST([HAVE_DECL_STRTOUMAX])
150  HAVE_IMAXDIV_T=1;      AC_SUBST([HAVE_IMAXDIV_T])
151  REPLACE_STRTOIMAX=0;   AC_SUBST([REPLACE_STRTOIMAX])
152  REPLACE_STRTOUMAX=0;   AC_SUBST([REPLACE_STRTOUMAX])
153  INT32_MAX_LT_INTMAX_MAX=1;  AC_SUBST([INT32_MAX_LT_INTMAX_MAX])
154  INT64_MAX_EQ_LONG_MAX='defined _LP64';  AC_SUBST([INT64_MAX_EQ_LONG_MAX])
155  PRI_MACROS_BROKEN=0;   AC_SUBST([PRI_MACROS_BROKEN])
156  PRIPTR_PREFIX=__PRIPTR_PREFIX;  AC_SUBST([PRIPTR_PREFIX])
157  UINT32_MAX_LT_UINTMAX_MAX=1;  AC_SUBST([UINT32_MAX_LT_UINTMAX_MAX])
158  UINT64_MAX_EQ_ULONG_MAX='defined _LP64';  AC_SUBST([UINT64_MAX_EQ_ULONG_MAX])
159])
160