mhd_check_cc_cflag.m4 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # SYNOPSIS
  2. #
  3. # MHD_CHECK_CC_CFLAG([FLAG-TO-TEST], [VARIABLE-TO-PREPEND-CFLAGS],
  4. # [ACTION-IF-SUPPORTED], [ACTION-IF-NOT-SUPPORTED])
  5. #
  6. # DESCRIPTION
  7. #
  8. # This macro checks whether the specific compiler flag is supported.
  9. # The check is performing by prepending FLAG-TO-TEST to CFLAGS, then
  10. # prepending value of VARIABLE-TO-PREPEND-CFLAGS (if any) to CFLAGS, and
  11. # then performing compile and link test. If test succeed without warnings,
  12. # then the flag is considered to be supported. Otherwise, if compile and link
  13. # without test flag can be done without any warning, the flag is considered
  14. # to be unsuppoted.
  15. #
  16. # Example usage:
  17. #
  18. # MHD_CHECK_CC_CFLAG([-Wshadow], [additional_CFLAGS],
  19. # [additional_CFLAGS="${additional_CFLAGS} -Wshadow"])
  20. #
  21. # Defined cache variable used in check so if any test will not work
  22. # correctly on some platform, user may simply fix it by giving cache
  23. # variable in configure parameters, for example:
  24. #
  25. # ./configure mhd_cv_cc_fl_supp__Wshadow=no
  26. #
  27. # This simplify building from source on exotic platforms as patching
  28. # of configure.ac is not required to change results of tests.
  29. #
  30. # LICENSE
  31. #
  32. # Copyright (c) 2022 Karlson2k (Evgeny Grin) <k2k@narod.ru>
  33. #
  34. # Copying and distribution of this file, with or without modification, are
  35. # permitted in any medium without royalty provided the copyright notice
  36. # and this notice are preserved. This file is offered as-is, without any
  37. # warranty.
  38. #serial 2
  39. AC_DEFUN([MHD_CHECK_CC_CFLAG],[dnl
  40. _MHD_CHECK_CC_XFLAG([$1],[$2],[$3],[$4],[[CFLAGS]])dnl
  41. ])
  42. # SYNOPSIS
  43. #
  44. # _MHD_CHECK_CC_XFLAG([FLAG-TO-TEST], [VARIABLE-TO-PREPEND-CFLAGS],
  45. # [ACTION-IF-SUPPORTED], [ACTION-IF-NOT-SUPPORTED],
  46. # [CFLAGS|LDFLAGS])
  47. #
  48. AC_DEFUN([_MHD_CHECK_CC_XFLAG],[dnl
  49. AC_PREREQ([2.64])dnl for AS_VAR_IF, m4_ifnblank
  50. AC_LANG_ASSERT([C])dnl
  51. AC_REQUIRE([AC_PROG_CC])dnl
  52. m4_ifblank([$1], [m4_fatal([$0: First macro argument must not be empty])])dnl
  53. m4_bmatch(m4_normalize([$1]), [\s],dnl
  54. [m4_fatal([$0: First macro argument must not contain whitespaces])])dnl
  55. m4_bmatch(_mhd_norm_expd([$2]), [\s],dnl
  56. [m4_fatal([$0: Second macro argument must not contain whitespaces])])dnl
  57. m4_bmatch([$1], [\$], [m4_fatal([$0: First macro argument must not contain '$'])])dnl
  58. m4_bmatch([$2], [\$], [m4_fatal([$0: Second macro argument must not contain '$'])])dnl
  59. AC_REQUIRE([MHD_FIND_CC_XFLAG_WARNPARAMS])dnl sets 'mhd_xFLAGS_params_warn' variable
  60. m4_bmatch(_mhd_norm_expd([$5]), [^\(CFLAGS\|LDFLAGS\)$],[],dnl
  61. [m4_fatal([$0: Fifth macro argument must be either 'CFLAGS' or 'LDFLAGS; ']_mhd_norm_expd([$5])[' is not supported])])dnl
  62. _MHD_CHECK_CC_XFLAG_BODY([$1],[$2],[$3],[$4],[mhd_xFLAGS_params_warn],[$5])dnl
  63. ])
  64. # SYNOPSIS
  65. #
  66. # _MHD_CHECK_CC_XFLAG_BODY([FLAG-TO-TEST], [VARIABLE-TO-PREPEND-CFLAGS],
  67. # [ACTION-IF-SUPPORTED], [ACTION-IF-NOT-SUPPORTED],
  68. # [VARIABLE-TO-ENABLE-WARNS], [CFLAGS|LDFLAGS])
  69. #
  70. AC_DEFUN([_MHD_CHECK_CC_XFLAG_BODY],[dnl
  71. AC_LANG_ASSERT([C])dnl
  72. m4_ifblank([$1], [m4_fatal([$0: First macro argument must not be empty])])dnl
  73. m4_bmatch(_mhd_norm_expd([$1]), [\s],dnl
  74. [m4_fatal([$0: First macro argument must not contain whitespaces])])dnl
  75. m4_bmatch(_mhd_norm_expd([$2]), [\s],dnl
  76. [m4_fatal([$0: Second macro argument must not contain whitespaces])])dnl
  77. m4_bmatch([$1], [\$], [m4_fatal([$0: First macro argument must not contain '$'])])dnl
  78. m4_bmatch([$2], [\$], [m4_fatal([$0: Second macro argument must not contain '$'])])dnl
  79. m4_ifblank([$5], [m4_fatal([$0: Fifth macro argument must not be empty])])dnl
  80. m4_bmatch([$5], [\$], [m4_fatal([$0: Fifth macro argument must not contain '$'])])dnl
  81. m4_bmatch(_mhd_norm_expd([$6]), [^\(CFLAGS\|LDFLAGS\)$],[],dnl
  82. [m4_fatal([$0: Sixth macro argument must be either 'CFLAGS' or 'LDFLAGS; ']_mhd_norm_expd([$6])[' is not supported])])dnl
  83. m4_pushdef([XFLAGS],_mhd_norm_expd([$6]))dnl
  84. dnl Keep uppercase letters to avoid clashes for parameters like -fPIE and -fpie
  85. AS_VAR_PUSHDEF([cv_Var],[mhd_cv_cc_]m4_tolower(m4_substr(_mhd_norm_expd([$6]),0,1))[fl_supp_]m4_bpatsubst(_mhd_norm_expd([$1]),[[^a-zA-Z0-9]],[_]))dnl
  86. AC_CACHE_CHECK([whether $[]CC supports _mhd_norm_expd([$1]) flag], cv_Var,
  87. [dnl
  88. AS_VAR_PUSHDEF([save_xFLAGS_Var], [mhd_check_cc_flag_save_]XFLAGS)dnl
  89. AS_VAR_SET([save_xFLAGS_Var],["${XFLAGS}"])
  90. m4_ifnblank([$2],[dnl
  91. m4_if(_mhd_norm_expd([$2]),[XFLAGS],
  92. [XFLAGS="${save_xFLAGS_Var} _mhd_norm_expd([$1]) $[]{_mhd_norm_expd([$5])}"],
  93. [XFLAGS="$[]{_mhd_norm_expd([$2])} _mhd_norm_expd([$1]) ${save_xFLAGS_Var} $[]{_mhd_norm_expd([$5])}"])
  94. ],[dnl
  95. XFLAGS="_mhd_norm_expd([$1]) $[]XFLAGS $[]{_mhd_norm_expd([$5])}"
  96. ])dnl
  97. mhd_check_cc_flag_save_c_werror_flag="$ac_c_werror_flag"
  98. ac_c_werror_flag=yes
  99. [#] Reuse the same source for all the checks here
  100. AC_LANG_CONFTEST([AC_LANG_SOURCE([[
  101. int main(void)
  102. {
  103. return 0;
  104. }
  105. ]])])
  106. AC_LINK_IFELSE([],
  107. [AS_VAR_SET([cv_Var],["yes"])],
  108. [ [#] Compile and link failed with test flag added
  109. m4_ifnblank([$2],[dnl
  110. m4_if(_mhd_norm_expd([$2]),[XFLAGS],
  111. [XFLAGS="${save_xFLAGS_Var} $[]{_mhd_norm_expd([$5])}"],
  112. [XFLAGS="$[]{_mhd_norm_expd([$2])} ${save_xFLAGS_Var} $[]{_mhd_norm_expd([$5])}"])
  113. ],[dnl
  114. XFLAGS="${save_xFLAGS_Var} $[]{_mhd_norm_expd([$5])}"
  115. ])dnl
  116. AC_LINK_IFELSE([],
  117. [AS_VAR_SET([cv_Var],["no"])],
  118. [ [#] Compile and link failed with test flag removed as well
  119. m4_ifnblank([$2],[dnl
  120. m4_if(_mhd_norm_expd([$2]),[XFLAGS],
  121. [XFLAGS="${save_xFLAGS_Var} _mhd_norm_expd([$1])"],
  122. [XFLAGS="$[]{_mhd_norm_expd([$2])} _mhd_norm_expd([$1]) ${save_xFLAGS_Var}"])
  123. ],[dnl
  124. XFLAGS="_mhd_norm_expd([$1]) ${save_xFLAGS_Var}"
  125. ])dnl
  126. ac_c_werror_flag="$mhd_check_cc_flag_save_c_werror_flag"
  127. AC_LINK_IFELSE([],
  128. [AS_VAR_SET([cv_Var],["yes"])],
  129. [AS_VAR_SET([cv_Var],["no"])],
  130. )
  131. ]
  132. )
  133. ]
  134. )
  135. ac_c_werror_flag="$mhd_check_cc_flag_save_c_werror_flag"
  136. AS_VAR_SET([XFLAGS],["${save_xFLAGS_Var}"])
  137. AS_UNSET(save_xFLAGS_Var)
  138. AS_VAR_POPDEF([save_xFLAGS_Var])dnl
  139. ]
  140. )
  141. m4_ifnblank([$3$4],[dnl
  142. AS_VAR_IF([cv_Var], ["yes"], [$3], m4_default_nblank([$4]))
  143. ])dnl
  144. AS_VAR_POPDEF([cv_Var])dnl
  145. m4_popdef([XFLAGS])dnl
  146. ])
  147. #
  148. # SYNOPSIS
  149. #
  150. # MHD_FIND_CC_XFLAG_WARNPARAMS()
  151. #
  152. AC_DEFUN([MHD_FIND_CC_XFLAG_WARNPARAMS],[dnl
  153. AC_LANG_ASSERT([C])dnl
  154. AC_REQUIRE([MHD_FIND_CC_CFLAG_WWARN])dnl
  155. mhd_xFLAGS_params_warn=''
  156. _MHD_CHECK_CC_XFLAG_BODY([-Wunused-command-line-argument],[],
  157. [
  158. MHD_APPEND_FLAG_TO_VAR([mhd_xFLAGS_params_warn],[-Wunused-command-line-argument])
  159. ],[],[mhd_cv_cc_flag_Wwarn],[[CFLAGS]]
  160. )
  161. _MHD_CHECK_CC_XFLAG_BODY([-Wignored-optimization-argument],[],
  162. [
  163. MHD_APPEND_FLAG_TO_VAR([mhd_xFLAGS_params_warn],[-Wignored-optimization-argument])
  164. ],[],[mhd_cv_cc_flag_Wwarn],[[CFLAGS]]
  165. )
  166. _MHD_CHECK_CC_XFLAG_BODY([-Winvalid-command-line-argument],[],
  167. [
  168. MHD_APPEND_FLAG_TO_VAR([mhd_xFLAGS_params_warn],[-Winvalid-command-line-argument])
  169. ],[],[mhd_cv_cc_flag_Wwarn],[[CFLAGS]]
  170. )
  171. _MHD_CHECK_CC_XFLAG_BODY([-Wunknown-argument],[],
  172. [
  173. MHD_APPEND_FLAG_TO_VAR([mhd_xFLAGS_params_warn],[-Wunknown-argument])
  174. ],[],[mhd_cv_cc_flag_Wwarn],[[CFLAGS]]
  175. )
  176. MHD_PREPEND_FLAG_TO_VAR([mhd_xFLAGS_params_warn],[$mhd_cv_cc_flag_Wwarn])
  177. ])
  178. #
  179. # SYNOPSIS
  180. #
  181. # MHD_FIND_CC_CFLAG_WWARN()
  182. #
  183. AC_DEFUN([MHD_FIND_CC_CFLAG_WWARN],[dnl
  184. AC_PREREQ([2.64])dnl for AS_VAR_IF, m4_ifnblank
  185. AC_REQUIRE([AC_PROG_CC])dnl
  186. AC_LANG_ASSERT([C])dnl
  187. AC_MSG_CHECKING([for $[]CC flag to warn on unknown -W parameters])
  188. AC_CACHE_VAL([mhd_cv_cc_flag_Wwarn],
  189. [
  190. mhd_check_cc_flagwarn_save_c_werror_flag="$ac_c_werror_flag"
  191. ac_c_werror_flag=yes
  192. mhd_find_cc_Wwarn_save_CFLAGS="$CFLAGS"
  193. AS_UNSET([mhd_cv_cc_flag_Wwarn])
  194. for mhd_cv_cc_flag_Wwarn in '' '-Wunknown-warning-option' '-Werror=unknown-warning-option' ; do
  195. AS_VAR_IF([mhd_cv_cc_flag_Wwarn], ["unknown"], [break])
  196. [#] Reuse the same source for all the checks here
  197. AC_LANG_CONFTEST([AC_LANG_SOURCE([[
  198. int main(void)
  199. {
  200. return 0;
  201. }
  202. ]])])
  203. CFLAGS="-Wmhd-noexist-flag $mhd_find_cc_Wwarn_save_CFLAGS $mhd_cv_cc_flag_Wwarn"
  204. AC_LINK_IFELSE([],
  205. [],
  206. [ [#] Compile and link failed if test flag and non-existing flag added
  207. CFLAGS="$mhd_find_cc_Wwarn_save_CFLAGS $mhd_cv_cc_flag_Wwarn"
  208. AC_LINK_IFELSE([],
  209. [ [#] Compile and link succeed if only test flag added
  210. break
  211. ]
  212. )
  213. ]
  214. )
  215. done
  216. CFLAGS="$mhd_find_cc_Wwarn_save_CFLAGS"
  217. AS_VAR_IF([mhd_cv_cc_flag_Wwarn], ["unknown"],
  218. [
  219. _AS_ECHO_LOG([No suitable flags detected. Check whether default flags are correct.])
  220. AC_LINK_IFELSE([AC_LANG_SOURCE([[
  221. int main(void)
  222. {
  223. return 0;
  224. }
  225. ]])],
  226. [:],
  227. [ [#] Compile and link fails (or warns) with default flags
  228. AC_MSG_WARN([Compiler warns (of fails) with default flags!])
  229. AC_MSG_WARN([Check whether compiler and compiler flags are correct.])
  230. ]
  231. )
  232. ]
  233. )
  234. CFLAGS="$mhd_find_cc_Wwarn_save_CFLAGS"
  235. AS_UNSET([mhd_find_cc_Wwarn_save_CFLAGS])
  236. ac_c_werror_flag="$mhd_check_cc_flagwarn_save_c_werror_flag"
  237. AS_UNSET([mhd_check_cc_flagwarn_save_c_werror_flag])
  238. ]
  239. )
  240. AS_IF([test -z "$mhd_cv_cc_flag_Wwarn"],
  241. [AC_MSG_RESULT([none needed])],
  242. [AC_MSG_RESULT([$mhd_cv_cc_flag_Wwarn])])
  243. AS_VAR_IF([mhd_cv_cc_flag_Wwarn], ["unknown"],
  244. [AC_MSG_WARN([Unable to find compiler flags to warn on unsupported -W options. Final compiler options may be suboptimal.])])
  245. ])