1#! /bin/sh
2#
3# link.sh -- try linking Vim with different sets of libraries, finding the
4# minimal set for fastest startup.  The problem is that configure adds a few
5# libraries when they exist, but this doesn't mean they are needed for Vim.
6#
7#      Author: Bram Moolenaar
8# Last change: 2006 Sep 26
9#     License: Public domain
10#
11# Warning: This fails miserably if the linker doesn't return an error code!
12#
13# Otherwise this script is fail-safe, falling back to the original full link
14# command if anything fails.
15
16echo "$LINK " >link.cmd
17exit_value=0
18
19#
20# If auto/link.sed already exists, use it.  We assume a previous run of
21# link.sh has found the correct set of libraries.
22#
23if test -f auto/link.sed; then
24  echo "link.sh: The file 'auto/link.sed' exists, which is going to be used now."
25  echo "link.sh: If linking fails, try deleting the auto/link.sed file."
26  echo "link.sh: If this fails too, try creating an empty auto/link.sed file."
27else
28
29# If linking works with the full link command, try removing some libraries,
30# that are known not to be needed on at least one system.
31# Remove auto/pathdef.c if there is a new link command and compile it again.
32# There is a loop to remove libraries that appear several times.
33#
34# Notes:
35# - Can't remove Xext; It links fine but will give an error when running gvim
36#   with Motif.
37# - Don't remove the last -lm: On HP-UX Vim links OK but crashes when the GTK
38#   GUI is started, because the "floor" symbol could not be resolved.
39#
40  cat link.cmd
41  if sh link.cmd; then
42    touch auto/link.sed
43    cp link.cmd linkit.sh
44    for libname in SM ICE nsl dnet dnet_stub inet socket dir elf iconv Xt Xmu Xp Xpm X11 Xdmcp x w perl dl pthread thread readline m crypt attr; do
45      cont=yes
46      while test -n "$cont"; do
47        if grep "l$libname " linkit.sh >/dev/null; then
48          if test ! -f link1.sed; then
49            echo "link.sh: OK, linking works, let's try omitting a few libraries."
50            echo "link.sh: See auto/link.log for details."
51            rm -f auto/link.log
52          fi
53          echo "s/-l$libname  *//" >link1.sed
54          sed -f auto/link.sed <link.cmd >linkit2.sh
55          sed -f link1.sed <linkit2.sh >linkit.sh
56          # keep the last -lm
57          if test $libname != "m" || grep "lm " linkit.sh >/dev/null; then
58            echo "link.sh: Trying to omit the $libname library..."
59            cat linkit.sh >>auto/link.log
60            # Redirect this link output, it may contain error messages which
61            # should be ignored.
62            if sh linkit.sh >>auto/link.log 2>&1; then
63              echo "link.sh: Vim doesn't need the $libname library!"
64              cat link1.sed >>auto/link.sed
65              rm -f auto/pathdef.c
66            else
67              echo "link.sh: Vim DOES need the $libname library."
68              cont=
69              cp link.cmd linkit.sh
70            fi
71          else
72            cont=
73            cp link.cmd linkit.sh
74          fi
75        else
76          cont=
77          cp link.cmd linkit.sh
78        fi
79      done
80    done
81    if test ! -f auto/pathdef.c; then
82      $MAKE objects/pathdef.o
83    fi
84    if test ! -f link1.sed; then
85      echo "link.sh: Linked fine, no libraries can be omitted"
86      touch link3.sed
87    fi
88  else
89    exit_value=$?
90  fi
91fi
92
93#
94# Now do the real linking.
95#
96if test -s auto/link.sed; then
97  echo "link.sh: Using auto/link.sed file to omit a few libraries"
98  sed -f auto/link.sed <link.cmd >linkit.sh
99  cat linkit.sh
100  if sh linkit.sh; then
101    exit_value=0
102    echo "link.sh: Linked fine with a few libraries omitted"
103  else
104    exit_value=$?
105    echo "link.sh: Linking failed, making auto/link.sed empty and trying again"
106    mv -f auto/link.sed link2.sed
107    touch auto/link.sed
108    rm -f auto/pathdef.c
109    $MAKE objects/pathdef.o
110  fi
111fi
112if test -f auto/link.sed -a ! -s auto/link.sed -a ! -f link3.sed; then
113  echo "link.sh: Using unmodified link command"
114  cat link.cmd
115  if sh link.cmd; then
116    exit_value=0
117    echo "link.sh: Linked OK"
118  else
119    exit_value=$?
120    if test -f link2.sed; then
121      echo "link.sh: Linking doesn't work at all, removing auto/link.sed"
122      rm -f auto/link.sed
123    fi
124  fi
125fi
126
127#
128# cleanup
129#
130rm -f link.cmd linkit.sh link1.sed link2.sed link3.sed linkit2.sh
131
132#
133# return an error code if something went wrong
134#
135exit $exit_value
136
137# vim:set sw=2 et:
138