Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / link-order2.at
1 # link-order2.at -- test link order of deplibs                -*- Autotest -*-
2 #
3 #   Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
4 #   Written by Ralf Wildenhues, 2006
5 #
6 #   This file is part of GNU Libtool.
7 #
8 # GNU Libtool is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; either version 2 of
11 # the License, or (at your option) any later version.
12 #
13 # GNU Libtool is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Libtool; see the file COPYING.  If not, a copy
20 # can be downloaded from  http://www.gnu.org/licenses/gpl.html,
21 # or obtained by writing to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 ####
24
25 # link-order2.test: make sure that depdepls are added right after
26 # the libs that pull them in: necessary at least for static linking
27 # and on systems where libraries do not link against other libraries.
28 #
29 # We test by overriding one symbol from a depdepl library.
30 # Test both a libtool library and a non-libtool library (simulated
31 # by removing the .la file, and setting $shlibpath_var so it is
32 # found at runtime).  In order to ensure that the depdepl is
33 # actually loaded in each case, we also use an unrelated symbol
34 # from it directly in the main program.  We test for failure by
35 # reversing the link order.
36 # We test both static and shared: it should work in both cases.
37 # But for the static case to work, the two symbols in the depdepl
38 # need to be in separate object files, otherwise duplicate symbol
39 # definitions can happen.
40
41 # The shared case fails on AIX without runtimelinking and on Darwin.
42 # On Darwin, using shared libs this only works without -no-undefined
43 # and only with MACOS_DEPLOYMENT_TARGET 10.2 and earlier
44 # (in that case $allow_undefined_flag contains -flat_namespace).
45
46 AT_SETUP([Link order of deplibs])
47 AT_KEYWORDS([libtool])
48 AT_KEYWORDS([interactive])dnl running 'wrong' may cause a popup window.
49
50 eval `$LIBTOOL --config | $EGREP '^(shlibpath_var|allow_undefined_flag)='`
51
52 undefined_setting=-no-undefined
53 shared_fails=no
54 case $host_os,$LDFLAGS,$allow_undefined_flag in
55 aix*,*-brtl*,*) ;;
56 aix*) shared_fails=yes ;;
57 darwin*,*,*-flat_namespace*) undefined_setting= ;;
58 darwin*,*,*) shared_fails=yes ;;
59 esac
60
61 deflibdir=`pwd`/lib
62 defbindir=`pwd`/lib
63 libdir=`pwd`/inst/lib
64 bindir=`pwd`/inst/bin
65 mkdir bin lib inst inst/bin inst/lib
66 LDFLAGS="$LDFLAGS $undefined_setting -L$deflibdir"
67
68 cat >a0.c <<\EOF
69 int a (void) { return 1; }
70 EOF
71
72 cat >a1.c <<\EOF
73 int a (void) { return 0; }
74 EOF
75
76 cat >b.c <<\EOF
77 int a (void);
78 int b (void) { return a (); }
79 EOF
80
81 cat >main.c <<\EOF
82 #include <stdlib.h>
83 extern int a (void);
84 extern int b (void);
85 int main (void)
86 {
87   return !(a () + b () == 0);
88 }
89 EOF
90
91 for file in a0 a1 b; do
92   $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c $file.c
93 done
94 $CC $CPPFLAGS $CFLAGS -c main.c
95
96 # Build an old, installed library.
97 $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS $static -o liba0.la a0.lo -rpath $deflibdir
98 $LIBTOOL --mode=install cp liba0.la $deflibdir/liba0.la
99 $LIBTOOL --mode=clean rm -f liba0.la
100
101 for type_of_depdepl in libtool non-libtool; do
102   echo "type of depdepl: $type_of_depdepl"
103   if test $type_of_depdepl = non-libtool; then
104     # Simulate a non-Libtool system library.
105     rm $deflibdir/liba1.la
106     addpath=$deflibdir
107     if test "$shlibpath_var" = PATH; then
108       addpath=$defbindir
109     fi
110     sep=
111     eval test -n \"\$$shlibpath_var\" && sep=:
112     eval $shlibpath_var='$addpath$sep$'$shlibpath_var
113     export $shlibpath_var
114   fi
115   for static in '' -static-libtool-libs; do
116     case `$LIBTOOL --features` in
117     *disable\ static\ libraries*) test "$static" = '' || continue;;
118     esac
119     test "$type_of_depdepl,$static" = "non-libtool,-static-libtool-libs" &&
120        static=-all-static
121     $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS $static -o liba1.la a1.lo -rpath $libdir
122     $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS $static -o libb.la b.lo liba1.la -rpath $libdir
123     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS $static -o main$EXEEXT main.$OBJEXT libb.la -la0],
124              [], [ignore], [ignore])
125     LT_AT_EXEC_CHECK([./main])
126     # Now test that if we reverse the link order, the program fails.
127     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS $static -o wrong$EXEEXT main.$OBJEXT -la0 libb.la],
128              [], [ignore], [ignore])
129     if test $shared_fails,$static != yes,; then
130       LT_AT_EXEC_CHECK([./wrong], [1], [], [ignore], [|| (exit 1)])
131     fi
132
133     $LIBTOOL --mode=install cp liba1.la $libdir/liba1.la
134     $LIBTOOL --mode=install cp libb.la $libdir/libb.la
135     $LIBTOOL --mode=install cp main$EXEEXT $bindir/main$EXEEXT
136     $LIBTOOL --mode=install cp wrong$EXEEXT $bindir/wrong$EXEEXT
137     $LIBTOOL --mode=clean rm -f liba1.la libb.la
138     LT_AT_EXEC_CHECK([$bindir/main])
139     if test $shared_fails,$static != yes,; then
140       LT_AT_EXEC_CHECK([$bindir/wrong], [1], [], [ignore], [|| (exit 1)])
141     fi
142   done
143 done
144
145 AT_CHECK([test $shared_fails = no || (exit 77)])
146
147 # Now the converse: if both the program and the library need libm, then
148 # it needs to be sorted last.  (TODO)
149
150 AT_CLEANUP