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