| 1 |
# SYNOPSIS |
| 2 |
# |
| 3 |
# MHD_BOOL |
| 4 |
# |
| 5 |
# DESCRIPTION |
| 6 |
# |
| 7 |
# This macro checks whether the boolean keywords "bool", "true", "false" |
| 8 |
# are supported by compiler. If they are not supported, suitable replacement |
| 9 |
# macros are defined. |
| 10 |
# |
| 11 |
# Example usage: |
| 12 |
# |
| 13 |
# MHD_BOOL |
| 14 |
# |
| 15 |
# The cache variables are used in the checks so if any test will not work |
| 16 |
# correctly on some platform, user may simply fix it by giving cache |
| 17 |
# variable in configure parameters, for example: |
| 18 |
# |
| 19 |
# ./configure mhd_cv_bool_native=no |
| 20 |
# |
| 21 |
# This simplify building from source on exotic platforms as patching |
| 22 |
# of configure.ac is not required to change results of tests. |
| 23 |
# |
| 24 |
# LICENSE |
| 25 |
# |
| 26 |
# Copyright (c) 2024 Karlson2k (Evgeny Grin) <[email protected]> |
| 27 |
# |
| 28 |
# Copying and distribution of this file, with or without modification, are |
| 29 |
# permitted in any medium without royalty provided the copyright notice |
| 30 |
# and this notice are preserved. This file is offered as-is, without any |
| 31 |
# warranty. |
| 32 |
|
| 33 |
#serial 2 |
| 34 |
|
| 35 |
AC_DEFUN([MHD_BOOL],[dnl |
| 36 |
AC_PREREQ([2.64])dnl for AS_VAR_IF |
| 37 |
m4_newline([[# Expansion of macro $0 starts here]]) |
| 38 |
AC_LANG_ASSERT([C])dnl |
| 39 |
AC_REQUIRE([AC_PROG_CC])dnl |
| 40 |
AH_TEMPLATE([[HAVE_STDBOOL_H]], [Define to 1 i][f you have the <stdbool.h> header file and <stdbool.h> is required to use 'bool' type.])dnl |
| 41 |
AH_TEMPLATE([[HAVE_BUILTIN_TYPE_BOOL]], [Define to 1 i][f you have the boolean type that takes only 'true' and 'false'.])dnl |
| 42 |
AH_TEMPLATE([[bool]], [Define to the name of the type which is used as a replacemnt f][or boolean type.])dnl |
| 43 |
[mhd_bool_test_code=' |
| 44 |
static bool get_false(void) |
| 45 |
{ |
| 46 |
static bool test_arr[sizeof(bool)*2 - 1] = {false}; |
| 47 |
return test_arr[0]; |
| 48 |
} |
| 49 |
|
| 50 |
int main(void) |
| 51 |
{ |
| 52 |
static bool var1 = true; |
| 53 |
static bool var2 = false; |
| 54 |
static int int_arr[2 - 3 * ((int) (false))] = {(int) true, (int) false}; |
| 55 |
static bool bool_arr[2 - 3 * ((int) (!true))] = {false, true}; |
| 56 |
i][f(!var1 || var2 || !int_arr[0] || int_arr[1] || bool_arr[0] || |
| 57 |
!bool_arr[1] || get_false() || 0 == sizeof(bool) || false || !true) |
| 58 |
return 5; |
| 59 |
return 0; |
| 60 |
} |
| 61 |
'] |
| 62 |
AC_CACHE_CHECK([[whether "bool", "true" and "false" work natively]],[[mhd_cv_bool_native]], |
| 63 |
[AC_COMPILE_IFELSE([AC_LANG_SOURCE([[${mhd_bool_test_code}]])],[mhd_cv_bool_native="yes"], |
| 64 |
[mhd_cv_bool_native="no"])dnl AC_COMPILE_IFELSE |
| 65 |
]) |
| 66 |
AS_VAR_IF([mhd_cv_bool_native],["yes"],[ |
| 67 |
AC_DEFINE([[HAVE_BUILTIN_TYPE_BOOL]],[1])],[dnl mhd_cv_bool_native "yes" |
| 68 |
AC_CACHE_CHECK([[whether <stdbool.h> is present and defines proper "bool", "true" and "false"]],[[mhd_cv_bool_stdbool]], |
| 69 |
[AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ |
| 70 |
#include <stdbool.h> |
| 71 |
|
| 72 |
${mhd_bool_test_code} |
| 73 |
]])],[mhd_cv_bool_stdbool="yes"],[mhd_cv_bool_stdbool="no"])dnl AC_COMPILE_IFELSE |
| 74 |
]) |
| 75 |
dnl end of AC_CACHE_CHECK |
| 76 |
AS_VAR_IF([mhd_cv_bool_stdbool],["yes"],[ |
| 77 |
AC_DEFINE([[HAVE_STDBOOL_H]],[1]) |
| 78 |
AC_DEFINE([[HAVE_BUILTIN_TYPE_BOOL]],[1])],[dnl mhd_cv_bool_stdbool "yes" |
| 79 |
# Need 'bool', 'false' and 'true'. |
| 80 |
AC_CHECK_TYPE([bool],[AC_DEFINE([[HAVE_BUILTIN_TYPE_BOOL]], [[1]])],[dnl AC_CHECK_TYPE bool |
| 81 |
AC_CHECK_TYPE([_Bool],[AC_DEFINE([[HAVE_BUILTIN_TYPE_BOOL]], [[1]]) |
| 82 |
AC_DEFINE([[bool]], [[_Bool]])],[AC_DEFINE([[bool]], [[int]]) |
| 83 |
],[[]])dnl AC_CHECK_TYPE _Bool |
| 84 |
],[[]])dnl AC_CHECK_TYPE bool |
| 85 |
# Have 'bool'. Need 'false' and 'true'. |
| 86 |
AC_CACHE_CHECK([[whether "false" keyword available and works]],[[mhd_cv_keyword_false_works]], |
| 87 |
[AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ |
| 88 |
int main(void) |
| 89 |
{ |
| 90 |
static bool var1 = false || false; |
| 91 |
static bool var2 = false; |
| 92 |
static bool bool_arr[2 - 3 * ((int) (false))] = {false, !false}; |
| 93 |
i][f(var1 || var2 || bool_arr[0] || !bool_arr[1] || false) |
| 94 |
return 5; |
| 95 |
return 0; |
| 96 |
} |
| 97 |
]])], [[mhd_cv_keyword_false_works='yes']], [[mhd_cv_keyword_false_works='no']])]) |
| 98 |
dnl end of AC_CACHE_CHECK |
| 99 |
AS_VAR_IF([[mhd_cv_keyword_false_works]], [["no"]],[dnl |
| 100 |
AC_DEFINE([[false]],[[(0)]], [Define to value interpreted by compiler as boolean "false", i][f "false" is not a keyword and not defined by headers.])]) |
| 101 |
dnl AS_VAR_IF mhd_cv_keyword_false_works |
| 102 |
# Have 'bool' and 'false'. Need 'true'. |
| 103 |
AC_CACHE_CHECK([[whether "true" keyword available and works]],[[mhd_cv_keyword_true_works]], |
| 104 |
[AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ |
| 105 |
int main(void) |
| 106 |
{ |
| 107 |
static bool var1 = true && true; |
| 108 |
static bool var2 = true; |
| 109 |
static bool bool_arr[2 - 3 * ((int) (!true))] = {true, !true}; |
| 110 |
i][f(!var1 || !var2 || !bool_arr[0] || bool_arr[1] || !true) |
| 111 |
return 5; |
| 112 |
return 0; |
| 113 |
} |
| 114 |
]])], [[mhd_cv_keyword_true_works='yes']], [[mhd_cv_keyword_true_works='no']])]) |
| 115 |
dnl end of AC_CACHE_CHECK |
| 116 |
AS_VAR_IF([[mhd_cv_keyword_true_works]], [["no"]],[dnl |
| 117 |
AC_DEFINE([[true]],[[(!false)]], [Define to value interpreted by compiler as boolean "true", i][f "true" is not a keyword and not defined by headers.])]) |
| 118 |
dnl AS_VAR_IF mhd_cv_keyword_false_works |
| 119 |
# Have 'bool', 'false' and 'true'. |
| 120 |
AC_CACHE_CHECK([[whether the defined "bool", "true" and "false" can work together]],[[mhd_cv_bool_true_false_work]], |
| 121 |
[AC_COMPILE_IFELSE([AC_LANG_SOURCE([[${mhd_bool_test_code}]])],[mhd_cv_bool_true_false_work="yes"], |
| 122 |
[mhd_cv_bool_true_false_work="no"])dnl AC_COMPILE_IFELSE |
| 123 |
]) |
| 124 |
dnl end of AC_CACHE_CHECK |
| 125 |
AS_VAR_IF([[mhd_cv_bool_true_false_work]], [["yes"]],[[:]],[dnl |
| 126 |
AC_MSG_FAILURE([cannot find suitable replacements for "bool", "true" and "false"]) |
| 127 |
]) |
| 128 |
dnl end of AS_VAR_IF mhd_cv_bool_true_false_work "yes" |
| 129 |
]) |
| 130 |
dnl end of AS_VAR_IF mhd_cv_bool_stdbool "yes" |
| 131 |
]) |
| 132 |
dnl end of AS_VAR_IF mhd_cv_bool_native "yes" |
| 133 |
AS_UNSET([mhd_bool_test_code]) |
| 134 |
m4_newline([[# Expansion of macro $0 ends here]]) |
| 135 |
])dnl AC_DEFUN MHD_BOOL |