3 dnl Copyright (C) 2005, 2008, 2010-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
11 dnl Tests whether the compiler supports the command-line option
12 dnl -fvisibility=hidden and the function and variable attributes
13 dnl __attribute__((__visibility__("hidden"))) and
14 dnl __attribute__((__visibility__("default"))).
15 dnl Does *not* test for __visibility__("protected") - which has tricky
16 dnl semantics (see the 'vismain' test in glibc) and does not exist e.g. on
18 dnl Does *not* test for __visibility__("internal") - which has processor
19 dnl dependent semantics.
20 dnl Does *not* test for #pragma GCC visibility push(hidden) - which is
21 dnl "really only recommended for legacy code".
22 dnl Set the variable CFLAG_VISIBILITY.
23 dnl Defines and sets the variable HAVE_VISIBILITY.
25 AC_DEFUN([gl_VISIBILITY],
27 AC_REQUIRE([AC_PROG_CC])
30 if test -n "$GCC"; then
31 dnl First, check whether -Werror can be added to the command line, or
32 dnl whether it leads to an error because of some other option that the
33 dnl user has put into $CC $CFLAGS $CPPFLAGS.
34 AC_CACHE_CHECK([whether the -Werror option is usable],
35 [gl_cv_cc_vis_werror],
36 [gl_saved_CFLAGS="$CFLAGS"
37 CFLAGS="$CFLAGS -Werror"
39 [AC_LANG_PROGRAM([[]], [[]])],
40 [gl_cv_cc_vis_werror=yes],
41 [gl_cv_cc_vis_werror=no])
42 CFLAGS="$gl_saved_CFLAGS"
44 dnl Now check whether visibility declarations are supported.
45 AC_CACHE_CHECK([for simple visibility declarations],
46 [gl_cv_cc_visibility],
47 [gl_saved_CFLAGS="$CFLAGS"
48 CFLAGS="$CFLAGS -fvisibility=hidden"
49 dnl We use the option -Werror and a function dummyfunc, because on some
50 dnl platforms (Cygwin 1.7) the use of -fvisibility triggers a warning
51 dnl "visibility attribute not supported in this configuration; ignored"
52 dnl at the first function definition in every compilation unit, and we
53 dnl don't want to use the option in this case.
54 if test $gl_cv_cc_vis_werror = yes; then
55 CFLAGS="$CFLAGS -Werror"
59 [[extern __attribute__((__visibility__("hidden"))) int hiddenvar;
60 extern __attribute__((__visibility__("default"))) int exportedvar;
61 extern __attribute__((__visibility__("hidden"))) int hiddenfunc (void);
62 extern __attribute__((__visibility__("default"))) int exportedfunc (void);
63 void dummyfunc (void);
66 int hiddenfunc (void) { return 51; }
67 int exportedfunc (void) { return 1225736919; }
68 void dummyfunc (void) {}
71 [gl_cv_cc_visibility=yes],
72 [gl_cv_cc_visibility=no])
73 CFLAGS="$gl_saved_CFLAGS"
75 if test $gl_cv_cc_visibility = yes; then
76 CFLAG_VISIBILITY="-fvisibility=hidden"
80 AC_SUBST([CFLAG_VISIBILITY])
81 AC_SUBST([HAVE_VISIBILITY])
82 AC_DEFINE_UNQUOTED([HAVE_VISIBILITY], [$HAVE_VISIBILITY],
83 [Define to 1 or 0, depending whether the compiler supports simple visibility declarations.])