visibility.m4 3.4 KB

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