mhd_check_func_run.m4 (5225B)
1 # SPDX-License-Identifier: FSFAP 2 # 3 # SYNOPSIS 4 # 5 # MHD_CHECK_FUNC_RUN(FUNCTION_NAME, 6 # [INCLUDES=AC_INCLUDES_DEFAULT], CHECK_CODE 7 # COMMAND_IF_CROSS_COMPILING, 8 # [ACTION_IF_AVAILABLE], [ACTION_IF_NOT_AVAILABLE], 9 # [ADDITIONAL_LIBS]) 10 # 11 # DESCRIPTION 12 # 13 # This macro checks for presence of specific function by including 14 # specified headers, checking for declaration of the function and then 15 # compiling and linking CHECK_CODE. If all previous steps succeed, 16 # the binary is executed. 17 # The CHECK_CODE is full body of the "main" function and must include 18 # the final "return" keyword. 19 # This checks the declaration, the presence in library and the functionality. 20 # If function is available then macro HAVE_function_name (the name 21 # of the function conveted to all uppercase characters) is defined 22 # automatically. 23 # By using definition from provided headers, this macro ensures that 24 # correct calling convention is used for detection. 25 # The compilation and linking is tried even if cross-compiling. If they 26 # failed then the result is "no". 27 # The COMMAND_IF_CROSS_COMPILING commands are executed if and only if 28 # the compilation and linking succeed. These commands must set 'cacheVar' 29 # variable to "yes", "no", "assuming yes" or "assuming no". 30 # ACTION_IF_SUCCEED is executed if result is "yes" or "assuming yes". 31 # ACTION_IF_FAILED is executed if result is "no" or "assuming no". 32 # 33 # Example usage: 34 # 35 # MHD_CHECK_FUNC_RUN([memmem], 36 # [[#include <string.h>]], 37 # [[const void *ptr = memmem("aa", 2, "a", 1); 38 # if (((void*)0) == ptr) return 1; 39 # return 0;]], 40 # [cacheVar="assuming yes"], 41 # [var_use_memmem='yes'], [var_use_memmem='no']) 42 # 43 # The cache variable used in check so if any test will not work 44 # correctly on some platform, user may simply fix it by giving cache 45 # variable in configure parameters, for example: 46 # 47 # ./configure mhd_cv_works_func_memmem=no 48 # 49 # This simplifies building from source on exotic platforms as patching 50 # of configure.ac is not required to change the results of the tests. 51 # 52 # LICENSE 53 # 54 # Copyright (c) 2024 Karlson2k (Evgeny Grin) <k2k@narod.ru> 55 # 56 # Copying and distribution of this file, with or without modification, are 57 # permitted in any medium without royalty provided the copyright notice 58 # and this notice are preserved. This file is offered as-is, without any 59 # warranty. 60 61 #serial 1 62 63 dnl $1: FUNCTION_NAME 64 dnl $2: INCLUDES 65 dnl $3: CHECK_CODE 66 dnl $4: COMMAND_IF_CROSS_COMPILING 67 dnl $5: ACTION_IF_AVAILABLE 68 dnl $6: ACTION_IF_NOT_AVAILABLE 69 dnl $7: ADDITIONAL_LIBS 70 AC_DEFUN([MHD_CHECK_FUNC_RUN],[dnl 71 AC_PREREQ([2.64])dnl for AS_VAR_SET_IF, m4_ifblank, m4_ifnblank, m4_default_nblank 72 m4_newline([[# Expansion of $0 macro starts here]]) 73 AC_LANG_ASSERT([C])dnl 74 m4_ifblank(m4_translit([$1],[()],[ ]), [m4_fatal([$0: First macro argument ("FUNCTION_NAME") must not be empty])])dnl 75 m4_ifblank([$3], [m4_fatal([$0: Third macro argument ("CHECK_CODE") must not be empty])])dnl 76 m4_bmatch(m4_normalize([$1]), [\s],dnl 77 [m4_fatal([$0: First macro argument ("FUNCTION_NAME") must not contain whitespaces])])dnl 78 m4_if(m4_index([$3], m4_normalize(m4_translit([$1],[()],[ ]))), [-1], dnl 79 [m4_fatal([$0: "CHECK_CODE" macro parameter (third argument) does not contain ']m4_normalize([$1])[' token])])dnl 80 m4_if(m4_index([$3],return), [-1], dnl 81 [m4_fatal([$0: "CHECK_CODE" macro parameter (third argument) does not contain 'return' token])])dnl 82 m4_bmatch(_mhd_norm_expd([$4]),[\<]cacheVar[\>],[],dnl 83 [m4_fatal([$0: The fourth macro argument ("COMMAND_IF_CROSS_COMPILING") must assign ]dnl 84 [a value to the cache variable 'cacheVar'. The variable must be not overquoted.])])dnl 85 AS_VAR_PUSHDEF([decl_cv_Var],[ac_cv_have_decl_]m4_bpatsubst(_mhd_norm_expd(m4_translit([$1],[()],[ ])),[[^a-zA-Z0-9]],[_]))dnl 86 AS_VAR_PUSHDEF([cacheVar],[mhd_cv_works_func_]m4_bpatsubst(_mhd_norm_expd(m4_translit([$1],[()],[ ])),[[^a-zA-Z0-9]],[_]))dnl 87 AS_VAR_SET_IF([cacheVar],[],[AC_CHECK_DECL(_mhd_norm_expd([$1]),[],[cacheVar="no"],[$2])]) 88 AC_CACHE_CHECK([whether '$1' available and works],[cacheVar],dnl 89 [ 90 AC_LANG_CONFTEST([AC_LANG_SOURCE([ 91 m4_default_nblank([$2],[AC_INCLUDES_DEFAULT]) 92 93 [int main(void) 94 { 95 ]$3[ 96 } 97 ]])]) 98 m4_ifnblank([$7],[dnl 99 mhd_check_func_SAVE_LIBS="$LIBS" 100 LIBS="_mhd_norm_expd([$7]) $LIBS" 101 ])dnl m4_ifnblank 102 AS_VAR_IF([cross_compiling],["yes"], 103 [AC_LINK_IFELSE([],[ 104 $4 105 ],[cacheVar='no'])dnl AC_LINK_IFELSE 106 ],dnl 107 [AC_RUN_IFELSE([],[cacheVar='yes'],[cacheVar='no'],[[# Dummy placeholder]]) 108 ]) 109 rm -f conftest.$ac_ext 110 m4_ifnblank([$7],[dnl 111 LIBS="${mhd_check_func_SAVE_LIBS}" 112 AS_UNSET([mhd_check_func_SAVE_LIBS]) 113 ])dnl m4_ifnblank 114 ])dnl AC_CACHE_CHECK 115 m4_ifnblank([$5],[ 116 AS_IF([test "x$cacheVar" = "xyes" || test "x$cacheVar" = "xassuming yes"],[$5])dnl AS_IF 117 ])dnl m4_ifnblank 118 m4_ifnblank([$6],[ 119 AS_IF([test "x$cacheVar" = "xno" || test "x$cacheVar" = "xassuming no"],[$6])dnl AS_IF 120 ])dnl m4_ifnblank 121 m4_newline([[# Expansion of $0 macro ends here]]) 122 ])dnl AC_DEFUN MHD_CHECK_FUNC_RUN