mhd_find_lib.m4 (5381B)
1 # SPDX-License-Identifier: FSFAP 2 # 3 # SYNOPSIS 4 # 5 # MHD_FIND_LIB([FUNCTION_NAME], 6 # [INCLUDES=AC_INCLUDES_DEFAULT], [CHECK_CODE], 7 # [LIBS_TO_CHECK], 8 # [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND], 9 # [VAR_TO_PREPEND_LIB=LIBS], [ADDITIONAL_LIBS]) 10 # 11 # DESCRIPTION 12 # 13 # This macro checks for presence of specific function by including 14 # specified headers and compiling and linking CHECK_CODE. 15 # This checks both the declaration and the presence in library. 16 # If declaration is not found in headers then libraries are not 17 # checked. 18 # LIBS_TO_CHECK is whitespace-separated list of libraries to check. 19 # The macro first tries to link without any library, and if it fails 20 # the libraries are checked one by one. 21 # The required library (if any) prepended to VAR_TO_PREPEND_LIB (or 22 # to the LIBS variable if VAR_TO_APPEND_LIB is not specified). 23 # By using definition from provided headers, this macro ensures that 24 # correct calling convention is used for detection. 25 # 26 # Example usage: 27 # 28 # MHD_FIND_LIB([clock_gettime], 29 # [[#include <time.h>]], 30 # [[struct timespec tp; 31 # if (0 > clock_gettime(CLOCK_REALTIME, &tp)) return 3;]], 32 # [rt], 33 # [var_use_gettime='yes'],[var_use_gettime='no']) 34 # 35 # Defined cache variable used in the check so if any test will not 36 # work correctly on some platform, a user may simply fix it by giving 37 # cache variable in configure parameters, for example: 38 # 39 # ./configure mhd_cv_find_clock_gettime=no 40 # 41 # This simplifies building from source on exotic platforms as patching 42 # of configure.ac is not required to change results of tests. 43 # 44 # LICENSE 45 # 46 # Copyright (c) 2023 Karlson2k (Evgeny Grin) <k2k@narod.ru> 47 # 48 # Copying and distribution of this file, with or without modification, are 49 # permitted in any medium without royalty provided the copyright notice 50 # and this notice are preserved. This file is offered as-is, without any 51 # warranty. 52 53 #serial 1 54 55 AC_DEFUN([MHD_FIND_LIB],[dnl 56 AC_PREREQ([2.64])dnl for AS_VAR_IF, m4_ifblank, m4_ifnblank 57 m4_newline([[# Expansion of $0 macro starts here]]) 58 AC_LANG_ASSERT([C])dnl 59 m4_ifblank(m4_translit([$1],[()],[ ]), [m4_fatal([First macro argument FUNCTION_NAME must not be empty])])dnl 60 m4_ifblank([$3], [m4_fatal([Third macro argument CHECK_CODE must not be empty])])dnl 61 m4_bmatch(m4_normalize([$1]), [\s],dnl 62 [m4_fatal([First macro argument FUNCTION_NAME must not contain whitespaces])])dnl 63 m4_if(m4_index([$3], m4_normalize(m4_translit([$1],[()],[ ]))), [-1], dnl 64 [m4_fatal([CHECK_CODE parameter (third macro argument) does not contain ']m4_normalize([$1])[' token])])dnl 65 m4_bmatch([$7], [\$], [m4_fatal([$0: Seventh macro argument VAR_TO_PREPEND_LIB must not contain '$'])])dnl 66 _MHD_FIND_LIB_BODY([$1],[$2],[$3],[$4],[$5],[$6],m4_default_nblank(_mhd_norm_expd([$7]),[LIBS]),[$8])dnl 67 ])dnl AC_DEFUN MHD_FIND_LIB 68 69 # SYNOPSIS 70 # 71 # _MHD_FIND_LIB_BODY([1_FUNCTION_NAME], 72 # [2_INCLUDES=AC_INCLUDES_DEFAULT], [3_CHECK_CODE], 73 # [4_LIBS_TO_CHECK], 74 # [5_ACTION-IF-FOUND], [6_ACTION-IF-NOT-FOUND], 75 # [7_VAR_TO_PREPEND_LIB=LIBS], [8_ADDITIONAL_LIBS]) 76 77 AC_DEFUN([_MHD_FIND_LIB_BODY],[dnl 78 AS_VAR_PUSHDEF([decl_cv_Var],[ac_cv_have_decl_]m4_bpatsubst(_mhd_norm_expd(m4_translit([$1],[()],[ ])),[[^a-zA-Z0-9]],[_]))dnl 79 AS_VAR_PUSHDEF([cv_Var], [mhd_cv_find_lib_]m4_bpatsubst(_mhd_norm_expd(m4_translit([$1],[()],[ ])),[[^a-zA-Z0-9]],[_]))dnl 80 AS_VAR_SET_IF([cv_Var],[],[AC_CHECK_DECL(_mhd_norm_expd([$1]),[],[],[$2])]) 81 AC_CACHE_CHECK([for library containing function $1], [cv_Var], 82 [ 83 AS_VAR_IF([decl_cv_Var],["yes"],dnl 84 [dnl 85 mhd_find_lib_SAVE_LIBS="$LIBS" 86 m4_if([$7],LIBS,[dnl 87 mhd_find_lib_CHECK_LIBS="_mhd_norm_expd([$8]) $LIBS" 88 ],[dnl 89 mhd_find_lib_CHECK_LIBS="[$]$7 _mhd_norm_expd([$8]) $LIBS" 90 ] 91 ) 92 # Reuse the same source file 93 AC_LANG_CONFTEST( 94 [AC_LANG_SOURCE([ 95 m4_default_nblank([$2],[AC_INCLUDES_DEFAULT]) 96 97 [int main(void) 98 { 99 100 ]$3[ 101 102 return 0; 103 } 104 ]]) 105 ] 106 ) 107 for mhd_find_lib_LIB in '' $4 108 do 109 AS_IF([test -z "${mhd_find_lib_LIB}"], 110 [LIBS="${mhd_find_lib_CHECK_LIBS}"], 111 [LIBS="-l${mhd_find_lib_LIB} ${mhd_find_lib_CHECK_LIBS}"] 112 ) 113 AC_LINK_IFELSE([], 114 [ 115 AS_IF([test -z "${mhd_find_lib_LIB}"], 116 [AS_VAR_SET([cv_Var],["none required"])], 117 [AS_VAR_SET([cv_Var],["-l${mhd_find_lib_LIB}"])] 118 ) 119 ] 120 ) 121 AS_VAR_SET_IF([cv_Var],[break]) 122 done 123 AS_UNSET([mhd_find_lib_LIB]) 124 rm -f conftest.$ac_ext 125 LIBS="${mhd_find_lib_SAVE_LIBS}" 126 AS_UNSET([mhd_find_lib_SAVE_LIBS]) 127 ] 128 ) 129 AS_VAR_SET_IF([cv_Var],[:],[AS_VAR_SET([cv_Var],["no"])]) 130 ] 131 ) 132 AS_IF([test "x${cv_Var}" != "xno"], 133 [dnl 134 AS_VAR_IF([cv_Var],["none required"],[:], 135 [ 136 AS_IF([test -z "[$]$7"],[$7="${cv_Var}"],[$7="${cv_Var} [$]$7"]) 137 ] 138 ) 139 m4_n([$5])dnl 140 ],[$6])dnl AS_VAR_SET_IF cv_Var 141 AS_VAR_POPDEF([cv_Var])dnl 142 AS_VAR_POPDEF([decl_cv_Var])dnl 143 m4_newline([[# Expansion of $0 macro ends here]]) 144 ])dnl AC_DEFUN MHD_CHECK_FUNC