mhd_bool.m4 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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) <k2k@narod.ru>
  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. #serial 2
  33. AC_DEFUN([MHD_BOOL],[dnl
  34. AC_PREREQ([2.64])dnl for AS_VAR_IF
  35. m4_newline([[# Expansion of macro $0 starts here]])
  36. AC_LANG_ASSERT([C])dnl
  37. AC_REQUIRE([AC_PROG_CC])dnl
  38. 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
  39. AH_TEMPLATE([[HAVE_BUILTIN_TYPE_BOOL]], [Define to 1 i][f you have the boolean type that takes only 'true' and 'false'.])dnl
  40. AH_TEMPLATE([[bool]], [Define to the name of the type which is used as a replacemnt f][or boolean type.])dnl
  41. [mhd_bool_test_code='
  42. static bool get_false(void)
  43. {
  44. static bool test_arr[sizeof(bool)*2 - 1] = {false};
  45. return test_arr[0];
  46. }
  47. int main(void)
  48. {
  49. static bool var1 = true;
  50. static bool var2 = false;
  51. static int int_arr[2 - 3 * ((int) (false))] = {(int) true, (int) false};
  52. static bool bool_arr[2 - 3 * ((int) (!true))] = {false, true};
  53. i][f(!var1 || var2 || !int_arr[0] || int_arr[1] || bool_arr[0] ||
  54. !bool_arr[1] || get_false() || 0 == sizeof(bool) || false || !true)
  55. return 5;
  56. return 0;
  57. }
  58. ']
  59. AC_CACHE_CHECK([[whether "bool", "true" and "false" work natively]],[[mhd_cv_bool_native]],
  60. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[${mhd_bool_test_code}]])],[mhd_cv_bool_native="yes"],
  61. [mhd_cv_bool_native="no"])dnl AC_COMPILE_IFELSE
  62. ])
  63. AS_VAR_IF([mhd_cv_bool_native],["yes"],[
  64. AC_DEFINE([[HAVE_BUILTIN_TYPE_BOOL]],[1])],[dnl mhd_cv_bool_native "yes"
  65. AC_CACHE_CHECK([[whether <stdbool.h> is present and defines proper "bool", "true" and "false"]],[[mhd_cv_bool_stdbool]],
  66. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
  67. #include <stdbool.h>
  68. ${mhd_bool_test_code}
  69. ]])],[mhd_cv_bool_stdbool="yes"],[mhd_cv_bool_stdbool="no"])dnl AC_COMPILE_IFELSE
  70. ])
  71. dnl end of AC_CACHE_CHECK
  72. AS_VAR_IF([mhd_cv_bool_stdbool],["yes"],[
  73. AC_DEFINE([[HAVE_STDBOOL_H]],[1])
  74. AC_DEFINE([[HAVE_BUILTIN_TYPE_BOOL]],[1])],[dnl mhd_cv_bool_stdbool "yes"
  75. # Need 'bool', 'false' and 'true'.
  76. AC_CHECK_TYPE([bool],[AC_DEFINE([[HAVE_BUILTIN_TYPE_BOOL]], [[1]])],[dnl AC_CHECK_TYPE bool
  77. AC_CHECK_TYPE([_Bool],[AC_DEFINE([[HAVE_BUILTIN_TYPE_BOOL]], [[1]])
  78. AC_DEFINE([[bool]], [[_Bool]])],[AC_DEFINE([[bool]], [[int]])
  79. ],[[]])dnl AC_CHECK_TYPE _Bool
  80. ],[[]])dnl AC_CHECK_TYPE bool
  81. # Have 'bool'. Need 'false' and 'true'.
  82. AC_CACHE_CHECK([[whether "false" keyword available and works]],[[mhd_cv_keyword_false_works]],
  83. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
  84. int main(void)
  85. {
  86. static bool var1 = false || false;
  87. static bool var2 = false;
  88. static bool bool_arr[2 - 3 * ((int) (false))] = {false, !false};
  89. i][f(var1 || var2 || bool_arr[0] || !bool_arr[1] || false)
  90. return 5;
  91. return 0;
  92. }
  93. ]])], [[mhd_cv_keyword_false_works='yes']], [[mhd_cv_keyword_false_works='no']])])
  94. dnl end of AC_CACHE_CHECK
  95. AS_VAR_IF([[mhd_cv_keyword_false_works]], [["no"]],[dnl
  96. 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.])])
  97. dnl AS_VAR_IF mhd_cv_keyword_false_works
  98. # Have 'bool' and 'false'. Need 'true'.
  99. AC_CACHE_CHECK([[whether "true" keyword available and works]],[[mhd_cv_keyword_true_works]],
  100. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
  101. int main(void)
  102. {
  103. static bool var1 = true && true;
  104. static bool var2 = true;
  105. static bool bool_arr[2 - 3 * ((int) (!true))] = {true, !true};
  106. i][f(!var1 || !var2 || !bool_arr[0] || bool_arr[1] || !true)
  107. return 5;
  108. return 0;
  109. }
  110. ]])], [[mhd_cv_keyword_true_works='yes']], [[mhd_cv_keyword_true_works='no']])])
  111. dnl end of AC_CACHE_CHECK
  112. AS_VAR_IF([[mhd_cv_keyword_true_works]], [["no"]],[dnl
  113. 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.])])
  114. dnl AS_VAR_IF mhd_cv_keyword_false_works
  115. # Have 'bool', 'false' and 'true'.
  116. AC_CACHE_CHECK([[whether the defined "bool", "true" and "false" can work together]],[[mhd_cv_bool_true_false_work]],
  117. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[${mhd_bool_test_code}]])],[mhd_cv_bool_true_false_work="yes"],
  118. [mhd_cv_bool_true_false_work="no"])dnl AC_COMPILE_IFELSE
  119. ])
  120. dnl end of AC_CACHE_CHECK
  121. AS_VAR_IF([[mhd_cv_bool_true_false_work]], [["yes"]],[[:]],[dnl
  122. AC_MSG_FAILURE([cannot find suitable replacements for "bool", "true" and "false"])
  123. ])
  124. dnl end of AS_VAR_IF mhd_cv_bool_true_false_work "yes"
  125. ])
  126. dnl end of AS_VAR_IF mhd_cv_bool_stdbool "yes"
  127. ])
  128. dnl end of AS_VAR_IF mhd_cv_bool_native "yes"
  129. AS_UNSET([mhd_bool_test_code])
  130. m4_newline([[# Expansion of macro $0 ends here]])
  131. ])dnl AC_DEFUN MHD_BOOL