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