maint: update copyright
[gnulib.git] / m4 / lib-ignore.m4
1 # If possible, ignore libraries that are not depended on.
2
3 dnl Copyright (C) 2006, 2009-2014 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
8 dnl Written by Paul Eggert.
9
10 # gl_IGNORE_UNUSED_LIBRARIES
11 # --------------------------
12 # Determines the option to be passed to the C/C++/Fortran compiler, so that it
13 # omits unused libraries.
14 # Example (on Solaris):
15 # $ cc foo.c -lnsl; ldd ./a.out
16 #         libnsl.so.1 =>   /lib/libnsl.so.1
17 #         libc.so.1 =>     /lib/libc.so.1
18 #         libmp.so.2 =>    /lib/libmp.so.2
19 #         libmd.so.1 =>    /lib/libmd.so.1
20 #         libscf.so.1 =>   /lib/libscf.so.1
21 #         libdoor.so.1 =>  /lib/libdoor.so.1
22 #         libuutil.so.1 =>         /lib/libuutil.so.1
23 #         libgen.so.1 =>   /lib/libgen.so.1
24 #         libm.so.2 =>     /lib/libm.so.2
25 # $ cc foo.c -lnsl -Wl,-z,ignore; ldd ./a.out
26 #         libc.so.1 =>     /lib/libc.so.1
27 #         libm.so.2 =>     /lib/libm.so.2
28 #
29 # Note that the option works only for the C compiler, not for the C++
30 # compiler:
31 # - Sun C likes '-Wl,-z,ignore'.
32 #   '-Qoption ld -z,ignore' is not accepted.
33 #   '-z ignore' is accepted but has no effect.
34 # - Sun C++ and Sun Fortran like '-Qoption ld -z,ignore'.
35 #   '-Wl,-z,ignore' is not accepted.
36 #   '-z ignore' is accepted but has no effect.
37 #
38 # Sets and substitutes a variable that depends on the current language:
39 # - IGNORE_UNUSED_LIBRARIES_CFLAGS    for C
40 # - IGNORE_UNUSED_LIBRARIES_CXXFLAGS  for C++
41 # - IGNORE_UNUSED_LIBRARIES_FFLAGS    for Fortran
42 #
43 # Note that the option works only for direct invocation of the compiler, not
44 # through libtool: When libtool is used to create a shared library, it will
45 # honor and translate '-Wl,-z,ignore' to '-Qoption ld -z -Qoption ld ignore'
46 # if needed, but it will drop a '-Qoption ld -z,ignore' on the command line.
47 #
48 AC_DEFUN([gl_IGNORE_UNUSED_LIBRARIES],
49 [
50   AC_CACHE_CHECK([for []_AC_LANG[] compiler flag to ignore unused libraries],
51     [gl_cv_prog_[]_AC_LANG_ABBREV[]_ignore_unused_libraries],
52     [gl_cv_prog_[]_AC_LANG_ABBREV[]_ignore_unused_libraries=none
53      gl_saved_ldflags=$LDFLAGS
54      gl_saved_libs=$LIBS
55      # Link with -lm to detect binutils 2.16 bug with --as-needed; see
56      # <http://lists.gnu.org/archive/html/bug-gnulib/2006-06/msg00131.html>.
57      LIBS="$LIBS -lm"
58      # Use long option sequences like '-z ignore' to test for the feature,
59      # to forestall problems with linkers that have -z, -i, -g, -n, etc. flags.
60      # GCC + binutils likes '-Wl,--as-needed'.
61      # GCC + Solaris ld likes '-Wl,-z,ignore'.
62      # Sun C likes '-Wl,-z,ignore'. '-z ignore' is accepted but has no effect.
63      # Don't try bare '--as-needed'; nothing likes it and the HP-UX 11.11
64      # native cc issues annoying warnings and then ignores it,
65      # which would cause us to incorrectly conclude that it worked.
66      for gl_flags in _gl_IGNORE_UNUSED_LIBRARIES_OPTIONS
67      do
68        LDFLAGS="$gl_flags $LDFLAGS"
69        AC_LINK_IFELSE([AC_LANG_PROGRAM()],
70          [gl_cv_prog_[]_AC_LANG_ABBREV[]_ignore_unused_libraries=$gl_flags])
71        LDFLAGS=$gl_saved_ldflags
72        test "$gl_cv_prog_[]_AC_LANG_ABBREV[]_ignore_unused_libraries" != none &&
73          break
74      done
75      LIBS=$gl_saved_libs
76     ])
77   IGNORE_UNUSED_LIBRARIES_[]_AC_LANG_PREFIX[]FLAGS=
78   if test "$gl_cv_prog_[]_AC_LANG_ABBREV[]_ignore_unused_libraries" != none; then
79     IGNORE_UNUSED_LIBRARIES_[]_AC_LANG_PREFIX[]FLAGS="$gl_cv_prog_[]_AC_LANG_ABBREV[]_ignore_unused_libraries"
80   fi
81   AC_SUBST([IGNORE_UNUSED_LIBRARIES_]_AC_LANG_PREFIX[FLAGS])
82 ])
83
84 # _gl_IGNORE_UNUSED_LIBRARIES_OPTIONS
85 # -----------------------------------
86 # Expands to the language dependent options to be tried.
87 AC_DEFUN([_gl_IGNORE_UNUSED_LIBRARIES_OPTIONS],
88 [_AC_LANG_DISPATCH([$0], _AC_LANG, $@)])
89
90 # _gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(C)
91 # --------------------------------------
92 m4_define([_gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(C)],
93 [ '-Wl,--as-needed' \
94   '-Wl,-z,ignore' \
95   '-z ignore'
96 ])
97
98 # _gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(C++)
99 # ----------------------------------------
100 m4_define([_gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(C++)],
101 [ '-Wl,--as-needed' \
102   '-Qoption ld -z,ignore' \
103   '-Wl,-z,ignore' \
104   '-z ignore'
105 ])
106
107 # _gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(Fortran 77)
108 # -----------------------------------------------
109 m4_copy([_gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(C++)],
110         [_gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(Fortran 77)])
111
112 # _gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(Fortran)
113 # --------------------------------------------
114 m4_copy([_gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(Fortran 77)],
115         [_gl_IGNORE_UNUSED_LIBRARIES_OPTIONS(Fortran)])