Imported Upstream version 4.4
[platform/upstream/make.git] / m4 / stdbool.m4
1 # Check for stdbool.h that conforms to C99.
2
3 dnl Copyright (C) 2002-2006, 2009-2022 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7
8 #serial 9
9
10 # Prepare for substituting <stdbool.h> if it is not supported.
11
12 AC_DEFUN([gl_STDBOOL_H],
13 [
14   AC_REQUIRE([AC_CHECK_HEADER_STDBOOL])
15   AC_REQUIRE([AC_CANONICAL_HOST])
16
17   dnl On some platforms, <stdbool.h> does not exist or does not conform to C99.
18   dnl On Solaris 10 with CC=cc CXX=CC, <stdbool.h> exists but is not usable
19   dnl in C++ mode (and no <cstdbool> exists). In this case, we use our
20   dnl replacement, also in C mode (for binary compatibility between C and C++).
21   if test "$ac_cv_header_stdbool_h" = yes; then
22     case "$host_os" in
23       solaris*)
24         if test -z "$GCC"; then
25           GL_GENERATE_STDBOOL_H=true
26         else
27           GL_GENERATE_STDBOOL_H=false
28         fi
29         ;;
30       *)
31         GL_GENERATE_STDBOOL_H=false
32         ;;
33     esac
34   else
35     GL_GENERATE_STDBOOL_H=true
36   fi
37
38   if test "$ac_cv_type__Bool" = yes; then
39     HAVE__BOOL=1
40   else
41     HAVE__BOOL=0
42   fi
43   AC_SUBST([HAVE__BOOL])
44 ])
45
46 m4_version_prereq([2.72], [], [
47
48 AC_DEFUN([AC_CHECK_HEADER_STDBOOL],
49   [AC_CHECK_TYPES([_Bool])
50    AC_CACHE_CHECK([for stdbool.h that conforms to C99 or later],
51      [ac_cv_header_stdbool_h],
52      [AC_COMPILE_IFELSE(
53         [AC_LANG_PROGRAM(
54            [[#include <stdbool.h>
55
56              /* "true" and "false" should be usable in #if expressions and
57                 integer constant expressions, and "bool" should be a valid
58                 type name.
59
60                 Although C 1999 requires bool, true, and false to be macros,
61                 C 2023 and C++ 2011 overrule that, so do not test for that.
62                 Although C 1999 requires __bool_true_false_are_defined and
63                 _Bool, C 2023 says they are obsolescent, so do not require
64                 them.  */
65
66              #if !true
67                #error "'true' is not true"
68              #endif
69              #if true != 1
70                #error "'true' is not equal to 1"
71              #endif
72              char b[true == 1 ? 1 : -1];
73              char c[true];
74
75              #if false
76                #error "'false' is not false"
77              #endif
78              #if false != 0
79                #error "'false' is not equal to 0"
80              #endif
81              char d[false == 0 ? 1 : -1];
82
83              enum { e = false, f = true, g = false * true, h = true * 256 };
84
85              char i[(bool) 0.5 == true ? 1 : -1];
86              char j[(bool) 0.0 == false ? 1 : -1];
87              char k[sizeof (bool) > 0 ? 1 : -1];
88
89              struct sb { bool s: 1; bool t; } s;
90              char l[sizeof s.t > 0 ? 1 : -1];
91
92              /* The following fails for
93                 HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]. */
94              bool m[h];
95              char n[sizeof m == h * sizeof m[0] ? 1 : -1];
96              char o[-1 - (bool) 0 < 0 ? 1 : -1];
97              /* Catch a bug in an HP-UX C compiler.  See
98                 https://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
99                 https://lists.gnu.org/r/bug-coreutils/2005-11/msg00161.html
100               */
101              bool p = true;
102              bool *pp = &p;
103            ]],
104            [[
105              bool ps = &s;
106              *pp |= p;
107              *pp |= ! p;
108
109              /* Refer to every declared value, so they cannot be
110                 discarded as unused.  */
111              return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !j + !k
112                      + !l + !m + !n + !o + !p + !pp + !ps);
113            ]])],
114         [ac_cv_header_stdbool_h=yes],
115         [ac_cv_header_stdbool_h=no])])
116 ])# AC_CHECK_HEADER_STDBOOL
117
118 ]) # m4_version_prereq 2.72