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