libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

mhd_bool.m4 (5465B)


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