mhd_find_lib.m4 5.2 KB

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