attributes.m4 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 Check for a -Werror flag or equivalent. -Werror is the GCC
  90. dnl and ICC flag that tells the compiler to treat all the warnings
  91. dnl as fatal. We usually need this option to make sure that some
  92. dnl constructs (like attributes) are not simply ignored.
  93. dnl
  94. dnl Other compilers don't support -Werror per se, but they support
  95. dnl an equivalent flag:
  96. dnl - Sun Studio compiler supports -errwarn=%all
  97. AC_DEFUN([CC_CHECK_WERROR], [
  98. AC_CACHE_CHECK(
  99. [for $CC way to treat warnings as errors],
  100. [cc_cv_werror],
  101. [CC_CHECK_CFLAGS_SILENT([-Werror], [cc_cv_werror=-Werror],
  102. [CC_CHECK_CFLAGS_SILENT([-errwarn=%all], [cc_cv_werror=-errwarn=%all])])
  103. ])
  104. ])
  105. AC_DEFUN([CC_CHECK_ATTRIBUTE], [
  106. AC_REQUIRE([CC_CHECK_WERROR])
  107. AC_CACHE_CHECK([if $CC supports __attribute__(( ifelse([$2], , [$1], [$2]) ))],
  108. AS_TR_SH([cc_cv_attribute_$1]),
  109. [ac_save_CFLAGS="$CFLAGS"
  110. CFLAGS="$CFLAGS $cc_cv_werror"
  111. AC_COMPILE_IFELSE([AC_LANG_SOURCE([$3])],
  112. [eval "AS_TR_SH([cc_cv_attribute_$1])='yes'"],
  113. [eval "AS_TR_SH([cc_cv_attribute_$1])='no'"])
  114. CFLAGS="$ac_save_CFLAGS"
  115. ])
  116. AS_IF([eval test x$]AS_TR_SH([cc_cv_attribute_$1])[ = xyes],
  117. [AC_DEFINE(
  118. AS_TR_CPP([SUPPORT_ATTRIBUTE_$1]), 1,
  119. [Define this if the compiler supports __attribute__(( ifelse([$2], , [$1], [$2]) ))]
  120. )
  121. $4],
  122. [$5])
  123. ])
  124. AC_DEFUN([CC_ATTRIBUTE_CONSTRUCTOR], [
  125. CC_CHECK_ATTRIBUTE(
  126. [constructor],,
  127. [extern void foo();
  128. void __attribute__((constructor)) ctor() { foo(); }],
  129. [$1], [$2])
  130. ])
  131. AC_DEFUN([CC_ATTRIBUTE_DESTRUCTOR], [
  132. CC_CHECK_ATTRIBUTE(
  133. [destructor],,
  134. [extern void foo();
  135. void __attribute__((destructor)) dtor() { foo(); }],
  136. [$1], [$2])
  137. ])
  138. AC_DEFUN([CC_ATTRIBUTE_FORMAT], [
  139. CC_CHECK_ATTRIBUTE(
  140. [format], [format(printf, n, n)],
  141. [void __attribute__((format(printf, 1, 2))) printflike(const char *fmt, ...) { fmt = (void *)0; }],
  142. [$1], [$2])
  143. ])
  144. AC_DEFUN([CC_ATTRIBUTE_FORMAT_ARG], [
  145. CC_CHECK_ATTRIBUTE(
  146. [format_arg], [format_arg(printf)],
  147. [char *__attribute__((format_arg(1))) gettextlike(const char *fmt) { fmt = (void *)0; }],
  148. [$1], [$2])
  149. ])
  150. AC_DEFUN([CC_ATTRIBUTE_VISIBILITY], [
  151. CC_CHECK_ATTRIBUTE(
  152. [visibility_$1], [visibility("$1")],
  153. [void __attribute__((visibility("$1"))) $1_function() { }],
  154. [$2], [$3])
  155. ])
  156. AC_DEFUN([CC_ATTRIBUTE_NONNULL], [
  157. CC_CHECK_ATTRIBUTE(
  158. [nonnull], [nonnull()],
  159. [void __attribute__((nonnull())) some_function(void *foo, void *bar) { foo = (void*)0; bar = (void*)0; }],
  160. [$1], [$2])
  161. ])
  162. AC_DEFUN([CC_ATTRIBUTE_UNUSED], [
  163. CC_CHECK_ATTRIBUTE(
  164. [unused], ,
  165. [void some_function(void *foo, __attribute__((unused)) void *bar);],
  166. [$1], [$2])
  167. ])
  168. AC_DEFUN([CC_ATTRIBUTE_SENTINEL], [
  169. CC_CHECK_ATTRIBUTE(
  170. [sentinel], ,
  171. [void some_function(void *foo, ...) __attribute__((sentinel));],
  172. [$1], [$2])
  173. ])
  174. AC_DEFUN([CC_ATTRIBUTE_DEPRECATED], [
  175. CC_CHECK_ATTRIBUTE(
  176. [deprecated], ,
  177. [void some_function(void *foo, ...) __attribute__((deprecated));],
  178. [$1], [$2])
  179. ])
  180. AC_DEFUN([CC_ATTRIBUTE_ALIAS], [
  181. CC_CHECK_ATTRIBUTE(
  182. [alias], [weak, alias],
  183. [void other_function(void *foo) { }
  184. void some_function(void *foo) __attribute__((weak, alias("other_function")));],
  185. [$1], [$2])
  186. ])
  187. AC_DEFUN([CC_ATTRIBUTE_MALLOC], [
  188. CC_CHECK_ATTRIBUTE(
  189. [malloc], ,
  190. [void * __attribute__((malloc)) my_alloc(int n);],
  191. [$1], [$2])
  192. ])
  193. AC_DEFUN([CC_ATTRIBUTE_PACKED], [
  194. CC_CHECK_ATTRIBUTE(
  195. [packed], ,
  196. [struct astructure { char a; int b; long c; void *d; } __attribute__((packed));
  197. char assert@<:@(sizeof(struct astructure) == (sizeof(char)+sizeof(int)+sizeof(long)+sizeof(void*)))-1@:>@;],
  198. [$1], [$2])
  199. ])
  200. AC_DEFUN([CC_ATTRIBUTE_CONST], [
  201. CC_CHECK_ATTRIBUTE(
  202. [const], ,
  203. [int __attribute__((const)) twopow(int n) { return 1 << n; } ],
  204. [$1], [$2])
  205. ])
  206. AC_DEFUN([CC_FLAG_VISIBILITY], [
  207. AC_REQUIRE([CC_CHECK_WERROR])
  208. AC_CACHE_CHECK([if $CC supports -fvisibility=hidden],
  209. [cc_cv_flag_visibility],
  210. [cc_flag_visibility_save_CFLAGS="$CFLAGS"
  211. CFLAGS="$CFLAGS $cc_cv_werror"
  212. CC_CHECK_CFLAGS_SILENT([-fvisibility=hidden],
  213. cc_cv_flag_visibility='yes',
  214. cc_cv_flag_visibility='no')
  215. CFLAGS="$cc_flag_visibility_save_CFLAGS"])
  216. AS_IF([test "x$cc_cv_flag_visibility" = "xyes"],
  217. [AC_DEFINE([SUPPORT_FLAG_VISIBILITY], 1,
  218. [Define this if the compiler supports the -fvisibility flag])
  219. $1],
  220. [$2])
  221. ])
  222. AC_DEFUN([CC_FUNC_EXPECT], [
  223. AC_REQUIRE([CC_CHECK_WERROR])
  224. AC_CACHE_CHECK([if compiler has __builtin_expect function],
  225. [cc_cv_func_expect],
  226. [ac_save_CFLAGS="$CFLAGS"
  227. CFLAGS="$CFLAGS $cc_cv_werror"
  228. AC_COMPILE_IFELSE(
  229. [int some_function() {
  230. int a = 3;
  231. return (int)__builtin_expect(a, 3);
  232. }],
  233. [cc_cv_func_expect=yes],
  234. [cc_cv_func_expect=no])
  235. CFLAGS="$ac_save_CFLAGS"
  236. ])
  237. AS_IF([test "x$cc_cv_func_expect" = "xyes"],
  238. [AC_DEFINE([SUPPORT__BUILTIN_EXPECT], 1,
  239. [Define this if the compiler supports __builtin_expect() function])
  240. $1],
  241. [$2])
  242. ])
  243. AC_DEFUN([CC_ATTRIBUTE_ALIGNED], [
  244. AC_REQUIRE([CC_CHECK_WERROR])
  245. AC_CACHE_CHECK([highest __attribute__ ((aligned ())) supported],
  246. [cc_cv_attribute_aligned],
  247. [ac_save_CFLAGS="$CFLAGS"
  248. CFLAGS="$CFLAGS $cc_cv_werror"
  249. for cc_attribute_align_try in 64 32 16 8 4 2; do
  250. AC_COMPILE_IFELSE([
  251. int main() {
  252. static char c __attribute__ ((aligned($cc_attribute_align_try))) = 0;
  253. return c;
  254. }], [cc_cv_attribute_aligned=$cc_attribute_align_try; break])
  255. done
  256. CFLAGS="$ac_save_CFLAGS"
  257. ])
  258. if test "x$cc_cv_attribute_aligned" != "x"; then
  259. AC_DEFINE_UNQUOTED([ATTRIBUTE_ALIGNED_MAX], [$cc_cv_attribute_aligned],
  260. [Define the highest alignment supported])
  261. fi
  262. ])