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