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