Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / link-order.at
1 # link-order.at -- ensure that library linking order matches  -*- Autotest -*-
2 #
3 #   Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
4 #   Written by Alexandre Oliva & Ralf Wildenhues, 2005
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
26 AT_SETUP([Link order test])
27 AT_KEYWORDS([libtool])
28 eval `$LIBTOOL --config | $EGREP '^(hardcode_direct|hardcode_direct_absolute|shrext_cmds)='`
29 LDFLAGS="$LDFLAGS -no-undefined"
30
31 module=no
32 eval shared_ext=\"$shrext_cmds\"
33
34 prefix_old=`pwd`/old
35 prefix_new=`pwd`/new
36 mkdir src $prefix_old $prefix_new $prefix_old/lib $prefix_new/lib
37
38 AT_DATA(src/c.c,
39 [[int c = 1;
40 ]])
41
42 $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c src/c.c -o src/c.lo
43 $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o src/libcee.la src/c.lo -rpath $prefix_old/lib
44 $LIBTOOL --mode=install cp src/libcee.la $prefix_old/lib/libcee.la
45
46 for i in old new; do
47   rm -rf src
48   mkdir src
49
50   cat >src/a_$i.c <<EOF
51 /* w32 fun, MSVC needs to dllimport when using a shared library, so use
52  * DLL_EXPORT to discriminate as that happens to coinside in this case.
53  * gnu has auto import.
54  */
55 #if defined _MSC_VER && defined DLL_EXPORT
56 #  define LIBCEE_SCOPE __declspec(dllimport)
57 #else
58 #  define LIBCEE_SCOPE extern
59 #endif
60 LIBCEE_SCOPE int c;
61 extern int b_$i();
62 int a_$i() { return c + b_$i(); }
63 EOF
64
65   cat >src/b_$i.c <<EOF
66 /* w32 fun, MSVC needs to dllimport when using a shared library, so use
67  * DLL_EXPORT to discriminate as that happens to coinside in this case.
68  * gnu has auto import.
69  */
70 #if defined _MSC_VER && defined DLL_EXPORT
71 #  define LIBCEE_SCOPE __declspec(dllimport)
72 #else
73 #  define LIBCEE_SCOPE extern
74 #endif
75 LIBCEE_SCOPE int c;
76 int b_$i() { return 1 + c; }
77 EOF
78
79   prefix=`eval echo \\$prefix_$i`
80   $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c src/a_$i.c -o src/a.lo
81   $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c src/b_$i.c -o src/b.lo
82   $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o src/libb.la src/b.lo \
83       -L$prefix_old/lib -lcee -rpath $prefix/lib
84   $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o src/liba.la src/a.lo \
85       src/libb.la -L$prefix_old/lib -lcee -rpath $prefix/lib
86   $LIBTOOL --mode=install cp src/libb.la $prefix/lib/libb.la
87   AT_CHECK([$LIBTOOL --mode=install cp src/liba.la $prefix/lib/liba.la],
88         [0], [stdout], [stderr])
89 done
90
91 $LIBTOOL --mode=clean rm -f src/libb.la
92 $LIBTOOL --mode=clean rm -f src/liba.la
93
94 case $host_os in
95 aix* | interix*) ;;  # These systems have different path syntax
96 *)
97   # Do not error if we do not relink (e.g. static-only systems)
98   case $hardcode_direct$hardcode_direct_absolute in
99   yesno)
100     AT_CHECK([if $EGREP relinking stderr; then
101          $EGREP " .*\/new\/lib/libb$shared_ext .*\/old\/lib/libcee$shared_ext" stdout
102        else :; fi], [0], [ignore], [], [echo "wrong link order"])
103     ;;
104   *)
105     AT_CHECK([if $EGREP relinking stderr; then
106          $EGREP " -L.*\/new\/lib -lb -L.*\/old\/lib -lcee" stdout
107        else :; fi], [0], [ignore], [], [echo "wrong link order"])
108     ;;
109   esac
110   ;;
111 esac
112
113 for i in old new; do
114   cat >src/main_$i.c <<EOF
115 extern int a_$i();
116 int main(void) { return a_$i() != 3; }
117 EOF
118
119   prefix=`eval echo \\$prefix_$i`
120   $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c src/main_$i.c -o src/main_$i.lo
121   AT_CHECK($LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o src/main_$i$EXEEXT src/main_$i.lo -L$prefix/lib -la,
122            [0], [ignore], [ignore])
123   LT_AT_EXEC_CHECK([src/main_$i])
124 done
125
126 AT_CLEANUP