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