attributes.m4 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. dnl Macros to check the presence of generic (non-typed) symbols.
  2. dnl Copyright (c) 2006-2007 Diego Pettenò <flameeyes@gmail.com>
  3. dnl Copyright (c) 2006-2007 xine project
  4. dnl
  5. dnl This program is free software; you can redistribute it and/or modify
  6. dnl it under the terms of the GNU General Public License as published by
  7. dnl the Free Software Foundation; either version 2, or (at your option)
  8. dnl any later version.
  9. dnl
  10. dnl This program is distributed in the hope that it will be useful,
  11. dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. dnl GNU General Public License for more details.
  14. dnl
  15. dnl You should have received a copy of the GNU General Public License
  16. dnl along with this program; if not, write to the Free Software
  17. dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. dnl 02110-1301, USA.
  19. dnl
  20. dnl As a special exception, the copyright owners of the
  21. dnl macro gives unlimited permission to copy, distribute and modify the
  22. dnl configure scripts that are the output of Autoconf when processing the
  23. dnl Macro. You need not follow the terms of the GNU General Public
  24. dnl License when using or distributing such scripts, even though portions
  25. dnl of the text of the Macro appear in them. The GNU General Public
  26. dnl License (GPL) does govern all other use of the material that
  27. dnl constitutes the Autoconf Macro.
  28. dnl
  29. dnl This special exception to the GPL applies to versions of the
  30. dnl Autoconf Macro released by this project. When you make and
  31. dnl distribute a modified version of the Autoconf Macro, you may extend
  32. dnl this special exception to the GPL to apply to your modified version as
  33. dnl well.
  34. dnl Check if the flag is supported by compiler
  35. dnl CC_CHECK_CFLAGS_SILENT([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
  36. AC_DEFUN([CC_CHECK_CFLAGS_SILENT], [
  37. AC_CACHE_VAL(AS_TR_SH([cc_cv_cflags_$1]),
  38. [ac_save_CFLAGS="$CFLAGS"
  39. CFLAGS="$CFLAGS $1"
  40. AC_LINK_IFELSE([AC_LANG_SOURCE([int main() { return 0; }])],
  41. [eval "AS_TR_SH([cc_cv_cflags_$1])='yes'"],
  42. [eval "AS_TR_SH([cc_cv_cflags_$1])='no'"])
  43. CFLAGS="$ac_save_CFLAGS"
  44. ])
  45. AS_IF([eval test x$]AS_TR_SH([cc_cv_cflags_$1])[ = xyes],
  46. [$2], [$3])
  47. ])
  48. dnl Check if the flag is supported by compiler (cacheable)
  49. dnl CC_CHECK_CFLAGS([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
  50. AC_DEFUN([CC_CHECK_CFLAGS], [
  51. AC_CACHE_CHECK([if $CC supports $1 flag],
  52. AS_TR_SH([cc_cv_cflags_$1]),
  53. CC_CHECK_CFLAGS_SILENT([$1]) dnl Don't execute actions here!
  54. )
  55. AS_IF([eval test x$]AS_TR_SH([cc_cv_cflags_$1])[ = xyes],
  56. [$2], [$3])
  57. ])
  58. dnl CC_CHECK_CFLAG_APPEND(FLAG, [action-if-found], [action-if-not-found])
  59. dnl Check for CFLAG and appends them to CFLAGS if supported
  60. AC_DEFUN([CC_CHECK_CFLAG_APPEND], [
  61. AC_CACHE_CHECK([if $CC supports $1 flag],
  62. AS_TR_SH([cc_cv_cflags_$1]),
  63. CC_CHECK_CFLAGS_SILENT([$1]) dnl Don't execute actions here!
  64. )
  65. AS_IF([eval test x$]AS_TR_SH([cc_cv_cflags_$1])[ = xyes],
  66. [CFLAGS="$CFLAGS $1"; $2], [$3])
  67. ])
  68. dnl CC_CHECK_CFLAGS_APPEND([FLAG1 FLAG2], [action-if-found], [action-if-not])
  69. AC_DEFUN([CC_CHECK_CFLAGS_APPEND], [
  70. for flag in $1; do
  71. CC_CHECK_CFLAG_APPEND($flag, [$2], [$3])
  72. done
  73. ])
  74. dnl Check if the flag is supported by linker (cacheable)
  75. dnl CC_CHECK_LDFLAGS([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
  76. AC_DEFUN([CC_CHECK_LDFLAGS], [
  77. AC_CACHE_CHECK([if $CC supports $1 flag],
  78. AS_TR_SH([cc_cv_ldflags_$1]),
  79. [ac_save_LDFLAGS="$LDFLAGS"
  80. LDFLAGS="$LDFLAGS $1"
  81. AC_LINK_IFELSE([AC_LANG_SOURCE([int main() { return 1; }])],
  82. [eval "AS_TR_SH([cc_cv_ldflags_$1])='yes'"],
  83. [eval "AS_TR_SH([cc_cv_ldflags_$1])="])
  84. LDFLAGS="$ac_save_LDFLAGS"
  85. ])
  86. AS_IF([eval test x$]AS_TR_SH([cc_cv_ldflags_$1])[ = xyes],
  87. [$2], [$3])
  88. ])
  89. dnl define the LDFLAGS_NOUNDEFINED variable with the correct value for
  90. dnl the current linker to avoid undefined references in a shared object.
  91. AC_DEFUN([CC_NOUNDEFINED], [
  92. dnl We check $host for which systems to enable this for.
  93. AC_REQUIRE([AC_CANONICAL_HOST])
  94. case $host in
  95. dnl FreeBSD (et al.) does not complete linking for shared objects when pthreads
  96. dnl are requested, as different implementations are present; to avoid problems
  97. dnl use -Wl,-z,defs only for those platform not behaving this way.
  98. *-freebsd* | *-openbsd*) ;;
  99. *)
  100. dnl First of all check for the --no-undefined variant of GNU ld. This allows
  101. dnl for a much more readable commandline, so that people can understand what
  102. dnl it does without going to look for what the heck -z defs does.
  103. for possible_flags in "-Wl,--no-undefined" "-Wl,-z,defs"; do
  104. CC_CHECK_LDFLAGS([$possible_flags], [LDFLAGS_NOUNDEFINED="$possible_flags"])
  105. break
  106. done
  107. ;;
  108. esac
  109. AC_SUBST([LDFLAGS_NOUNDEFINED])
  110. ])
  111. dnl Check for a -Werror flag or equivalent. -Werror is the GCC
  112. dnl and ICC flag that tells the compiler to treat all the warnings
  113. dnl as fatal. We usually need this option to make sure that some
  114. dnl constructs (like attributes) are not simply ignored.
  115. dnl
  116. dnl Other compilers don't support -Werror per se, but they support
  117. dnl an equivalent flag:
  118. dnl - Sun Studio compiler supports -errwarn=%all
  119. AC_DEFUN([CC_CHECK_WERROR], [
  120. AC_CACHE_CHECK(
  121. [for $CC way to treat warnings as errors],
  122. [cc_cv_werror],
  123. [CC_CHECK_CFLAGS_SILENT([-Werror], [cc_cv_werror=-Werror],
  124. [CC_CHECK_CFLAGS_SILENT([-errwarn=%all], [cc_cv_werror=-errwarn=%all])])
  125. ])
  126. ])
  127. AC_DEFUN([CC_CHECK_ATTRIBUTE], [
  128. AC_REQUIRE([CC_CHECK_WERROR])
  129. AC_CACHE_CHECK([if $CC supports __attribute__(( ifelse([$2], , [$1], [$2]) ))],
  130. AS_TR_SH([cc_cv_attribute_$1]),
  131. [ac_save_CFLAGS="$CFLAGS"
  132. CFLAGS="$CFLAGS $cc_cv_werror"
  133. AC_COMPILE_IFELSE([AC_LANG_SOURCE([$3])],
  134. [eval "AS_TR_SH([cc_cv_attribute_$1])='yes'"],
  135. [eval "AS_TR_SH([cc_cv_attribute_$1])='no'"])
  136. CFLAGS="$ac_save_CFLAGS"
  137. ])
  138. AS_IF([eval test x$]AS_TR_SH([cc_cv_attribute_$1])[ = xyes],
  139. [AC_DEFINE(
  140. AS_TR_CPP([SUPPORT_ATTRIBUTE_$1]), 1,
  141. [Define this if the compiler supports __attribute__(( ifelse([$2], , [$1], [$2]) ))]
  142. )
  143. $4],
  144. [$5])
  145. ])
  146. AC_DEFUN([CC_ATTRIBUTE_CONSTRUCTOR], [
  147. CC_CHECK_ATTRIBUTE(
  148. [constructor],,
  149. [extern void foo();
  150. void __attribute__((constructor)) ctor() { foo(); }],
  151. [$1], [$2])
  152. ])
  153. AC_DEFUN([CC_ATTRIBUTE_DESTRUCTOR], [
  154. CC_CHECK_ATTRIBUTE(
  155. [destructor],,
  156. [extern void foo();
  157. void __attribute__((destructor)) dtor() { foo(); }],
  158. [$1], [$2])
  159. ])
  160. AC_DEFUN([CC_ATTRIBUTE_FORMAT], [
  161. CC_CHECK_ATTRIBUTE(
  162. [format], [format(printf, n, n)],
  163. [void __attribute__((format(printf, 1, 2))) printflike(const char *fmt, ...) { fmt = (void *)0; }],
  164. [$1], [$2])
  165. ])
  166. AC_DEFUN([CC_ATTRIBUTE_FORMAT_ARG], [
  167. CC_CHECK_ATTRIBUTE(
  168. [format_arg], [format_arg(printf)],
  169. [char *__attribute__((format_arg(1))) gettextlike(const char *fmt) { fmt = (void *)0; }],
  170. [$1], [$2])
  171. ])
  172. AC_DEFUN([CC_ATTRIBUTE_VISIBILITY], [
  173. CC_CHECK_ATTRIBUTE(
  174. [visibility_$1], [visibility("$1")],
  175. [void __attribute__((visibility("$1"))) $1_function() { }],
  176. [$2], [$3])
  177. ])
  178. AC_DEFUN([CC_ATTRIBUTE_NONNULL], [
  179. CC_CHECK_ATTRIBUTE(
  180. [nonnull], [nonnull()],
  181. [void __attribute__((nonnull())) some_function(void *foo, void *bar) { foo = (void*)0; bar = (void*)0; }],
  182. [$1], [$2])
  183. ])
  184. AC_DEFUN([CC_ATTRIBUTE_UNUSED], [
  185. CC_CHECK_ATTRIBUTE(
  186. [unused], ,
  187. [void some_function(void *foo, __attribute__((unused)) void *bar);],
  188. [$1], [$2])
  189. ])
  190. AC_DEFUN([CC_ATTRIBUTE_SENTINEL], [
  191. CC_CHECK_ATTRIBUTE(
  192. [sentinel], ,
  193. [void some_function(void *foo, ...) __attribute__((sentinel));],
  194. [$1], [$2])
  195. ])
  196. AC_DEFUN([CC_ATTRIBUTE_DEPRECATED], [
  197. CC_CHECK_ATTRIBUTE(
  198. [deprecated], ,
  199. [void some_function(void *foo, ...) __attribute__((deprecated));],
  200. [$1], [$2])
  201. ])
  202. AC_DEFUN([CC_ATTRIBUTE_ALIAS], [
  203. CC_CHECK_ATTRIBUTE(
  204. [alias], [weak, alias],
  205. [void other_function(void *foo) { }
  206. void some_function(void *foo) __attribute__((weak, alias("other_function")));],
  207. [$1], [$2])
  208. ])
  209. AC_DEFUN([CC_ATTRIBUTE_MALLOC], [
  210. CC_CHECK_ATTRIBUTE(
  211. [malloc], ,
  212. [void * __attribute__((malloc)) my_alloc(int n);],
  213. [$1], [$2])
  214. ])
  215. AC_DEFUN([CC_ATTRIBUTE_PACKED], [
  216. CC_CHECK_ATTRIBUTE(
  217. [packed], ,
  218. [struct astructure { char a; int b; long c; void *d; } __attribute__((packed));
  219. char assert@<:@(sizeof(struct astructure) == (sizeof(char)+sizeof(int)+sizeof(long)+sizeof(void*)))-1@:>@;],
  220. [$1], [$2])
  221. ])
  222. AC_DEFUN([CC_ATTRIBUTE_CONST], [
  223. CC_CHECK_ATTRIBUTE(
  224. [const], ,
  225. [int __attribute__((const)) twopow(int n) { return 1 << n; } ],
  226. [$1], [$2])
  227. ])
  228. AC_DEFUN([CC_FLAG_VISIBILITY], [
  229. AC_REQUIRE([CC_CHECK_WERROR])
  230. AC_CACHE_CHECK([if $CC supports -fvisibility=hidden],
  231. [cc_cv_flag_visibility],
  232. [cc_flag_visibility_save_CFLAGS="$CFLAGS"
  233. CFLAGS="$CFLAGS $cc_cv_werror"
  234. CC_CHECK_CFLAGS_SILENT([-fvisibility=hidden],
  235. cc_cv_flag_visibility='yes',
  236. cc_cv_flag_visibility='no')
  237. CFLAGS="$cc_flag_visibility_save_CFLAGS"])
  238. AS_IF([test "x$cc_cv_flag_visibility" = "xyes"],
  239. [AC_DEFINE([SUPPORT_FLAG_VISIBILITY], 1,
  240. [Define this if the compiler supports the -fvisibility flag])
  241. $1],
  242. [$2])
  243. ])
  244. AC_DEFUN([CC_FUNC_EXPECT], [
  245. AC_REQUIRE([CC_CHECK_WERROR])
  246. AC_CACHE_CHECK([if compiler has __builtin_expect function],
  247. [cc_cv_func_expect],
  248. [ac_save_CFLAGS="$CFLAGS"
  249. CFLAGS="$CFLAGS $cc_cv_werror"
  250. AC_COMPILE_IFELSE([AC_LANG_SOURCE(
  251. [int some_function() {
  252. int a = 3;
  253. return (int)__builtin_expect(a, 3);
  254. }])],
  255. [cc_cv_func_expect=yes],
  256. [cc_cv_func_expect=no])
  257. CFLAGS="$ac_save_CFLAGS"
  258. ])
  259. AS_IF([test "x$cc_cv_func_expect" = "xyes"],
  260. [AC_DEFINE([SUPPORT__BUILTIN_EXPECT], 1,
  261. [Define this if the compiler supports __builtin_expect() function])
  262. $1],
  263. [$2])
  264. ])
  265. AC_DEFUN([CC_ATTRIBUTE_ALIGNED], [
  266. AC_REQUIRE([CC_CHECK_WERROR])
  267. AC_CACHE_CHECK([highest __attribute__ ((aligned ())) supported],
  268. [cc_cv_attribute_aligned],
  269. [ac_save_CFLAGS="$CFLAGS"
  270. CFLAGS="$CFLAGS $cc_cv_werror"
  271. for cc_attribute_align_try in 64 32 16 8 4 2; do
  272. AC_COMPILE_IFELSE([AC_LANG_SOURCE([
  273. int main() {
  274. static char c __attribute__ ((aligned($cc_attribute_align_try))) = 0;
  275. return c;
  276. }])], [cc_cv_attribute_aligned=$cc_attribute_align_try; break])
  277. done
  278. CFLAGS="$ac_save_CFLAGS"
  279. ])
  280. if test "x$cc_cv_attribute_aligned" != "x"; then
  281. AC_DEFINE_UNQUOTED([ATTRIBUTE_ALIGNED_MAX], [$cc_cv_attribute_aligned],
  282. [Define the highest alignment supported])
  283. fi
  284. ])