Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / static.at
1 # static.at -- test flags for static/dynamic linking          -*- 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 ##### NOTES #####
26 #
27 # - How do we test whether a library was linked statically?
28 #   We could
29 #   - try $NM on the program to see whether it includes the symbol definitions.
30 #     disadvantage: for uninstalled programs, we may need to find out the name
31 #     of the _real_ linked-against-uninstalled executable
32 #     (.libs/prog vs .libs/lt-prog etc).
33 #   - simply remove the libraries before execution.  If the program still works,
34 #     then the library was linked statically.
35 #     -Does this work on all systems?
36 #     -No, it will fail on AIX with non-rtl-created libraries: plain `-static'
37 #     will only cause the linker not to consider `*.so' libraries, but only
38 #     `*.a'.  The latter, however, may still be shared images.  :-/
39 #     `-all-static' still works, however.
40 #   
41 #     It will not work with dlpreloading until we fix its related bug.
42 #   
43 #   Let's try the latter until we know better.
44
45 # - Test -Bstatic/-Bdynamic.  It should work with all of:
46 #   - (un)installed libtool libraries
47 #   - non-libtool libraries
48 #   - direct or pulled-in libraries
49 #   - libraries of which there are only one kind available (TODO)
50 #     (in the static case, should having only the shared one provoke failure?)
51
52 # - Check no extraneous run paths have been added.
53
54 # - make sure -Bstatic/-Bdynamic cannot be mixed with -all-static (TODO)
55
56 # - should -Bstatic/-Bdynamic be mixable with -static or -static-libtool-libs?
57 #   Semantics could be as follows:
58 #   - `-static'/`-static-libtool-libs' set the default which is the initial
59 #     value, then `-Bstatic'/`-Bdynamic' override that
60 #   - `-Bdefault' resets to the default value given by the other switches.
61
62 # - TODO: test exposure for dlopened and dlpreopened modules,
63 #   without and with diverse static flag combinations.
64
65 # - TODO: test other tags: C++ etc.
66 #   (most likely the Sun compiler suite will be the only problem child).
67
68 AT_SETUP([static linking flags for programs])
69 AT_KEYWORDS([libtool])
70 AT_KEYWORDS([interactive])dnl Some of the exec_fail test cause popups with MinGW.
71
72 LDFLAGS="$LDFLAGS -no-undefined"
73 prefix=`pwd`/inst
74 bindir=$prefix/bin
75 prefix1=`pwd`/inst1
76 prefix2=`pwd`/inst2
77 prefix3=`pwd`/inst3
78 libdir1=$prefix1/lib
79 libdir2=$prefix2/lib
80 libdir3=$prefix3/lib
81 srcdir_broken=`pwd`/broken-src
82 prefix_broken=`pwd`/broken
83 libdir_broken=$prefix_broken/lib
84 bindir_broken=$prefix_broken/bin
85
86 have_static=false
87 have_shared=false
88 per_deplib=false
89 $LIBTOOL --features | grep 'enable static libraries' >/dev/null && have_static=:
90 $LIBTOOL --features | grep 'enable shared libraries' >/dev/null && have_shared=:
91 eval `$LIBTOOL --config |
92         $EGREP '^(per_deplib_(static|dynamic)_flag|shlibpath_var|link_static_flag)='`
93 if test -n "$per_deplib_static_flag" && test -n "$per_deplib_dynamic_flag"; then
94   per_deplib=:
95 fi
96 # On GNU/Linux with --disable-static, m-all-static fails to link.
97 # What we'd like to state here is: if the user actively passed
98 # --disable-static (as opposed to: the libtool.m4 macros set
99 # enable_static=no), then they cannot expect -all-static to work.
100 # So we punt, knowing that we mangle enable_static on AIX only.
101 can_link_all_static=-all-static
102 case $host_os,$have_static,$link_static_flag in
103   aix*) ;;
104   *,false,?*) can_link_all_static= ;;
105 esac
106
107 # Create broken libraries.  They will later be moved to those
108 # directories where the respective libraries should be linked
109 # statically from.  This detects both failure to link statically
110 # and failure to omit extraneous run paths.
111 mkdir $srcdir_broken $prefix_broken $libdir_broken
112 (
113   cd $srcdir_broken
114   echo 'int this_should_not_be_linked_against() { return 0; }' > a.c
115   $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c a.c
116   for i in 1 1dep 2 2dep 3 3dep; do
117     $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba$i.la a.lo -rpath $libdir_broken
118     $LIBTOOL --mode=install cp liba$i.la $libdir_broken/liba$i.la
119   done
120 )
121
122 func_fix_path ()
123 {
124   # For w32, hardcoding cannot work, but $libdir/../bin is where binaries
125   # usually are installed.  Since we use several prefixes for testing
126   # convenience -- it allows us to replace the good libraries easily with
127   # broken ones and vice versa -- we have to set PATH to find them.
128   # Since OTOH we put broken libs of all names in the "moved" prefixes,
129   # we have to ensure that this prefix comes last: otherwise we may link
130   # against a broken library but the good one would come later in the PATH.
131   # So we let the caller of this function set the order: the "other" two
132   # come first.
133   if test "$shlibpath_var" = PATH; then
134     save_PATH=$PATH
135     sep=
136     test -z "$PATH" || sep=:
137     PATH=${2}/bin:${3}/bin:${1}/bin$sep$PATH
138   fi
139
140 }
141
142 func_restore_path ()
143 {
144   if test "$shlibpath_var" = PATH; then
145     PATH=$save_PATH
146   fi
147 }
148
149 # func_move_libs srcdir_to_move prefix_to_move other_prefix other_prefix
150 func_move_libs ()
151 {
152   LT_AT_MVDIR(["${1}"], ["${1}-moved"])
153   LT_AT_MVDIR(["${2}"], ["${2}-moved"])
154   LT_AT_MVDIR(["$srcdir_broken"], ["${1}"])
155   LT_AT_MVDIR(["$prefix_broken"], ["${2}"])
156   func_fix_path ${2} ${3} ${4}
157 }
158
159 # func_restore_libs srcdir_to_restore prefix_to_restore
160 func_restore_libs ()
161 {
162   func_restore_path
163   LT_AT_MVDIR(["${2}"], ["$prefix_broken"])
164   LT_AT_MVDIR(["${1}"], ["$srcdir_broken"])
165   LT_AT_MVDIR(["${2}-moved"], ["${2}"])
166   LT_AT_MVDIR(["${1}-moved"], ["${1}"])
167 }
168
169 # make sure the program can be run.
170 func_test_exec ()
171 {
172   # On AIX without runtimelinking, this does not make sense.
173   if $have_static; then
174     echo "## The following should succeed:"
175     for st
176     do
177       echo "# m$st"
178       LT_AT_EXEC_CHECK([./m$st])
179       # For some per-deplib flag combinations there may be no installed program,
180       # because liba2 is not yet installed.
181       if test -f "$bindir/m${st}$EXEEXT"; then
182         LT_AT_EXEC_CHECK([$bindir/m$st])
183       fi
184     done
185   fi
186 }
187
188 # make sure the program cannot be run.
189 func_test_exec_fail ()
190 {
191   # No point in testing if we're linking statically anyway.
192   # TODO: Maybe in the `else' case we could test for success?
193   if $have_shared; then
194     echo "## The following should fail:"
195     for st
196     do
197       echo "# m$st"
198       LT_AT_EXEC_CHECK([./m$st], [1], [], [ignore], [|| (exit 1)])
199       # For some per-deplib flag combinations there may be no installed program,
200       # because liba2 is not yet installed.
201       if test -f "$bindir/m${st}$EXEEXT"; then
202         LT_AT_EXEC_CHECK([$bindir/m$st], [1], [], [ignore], [|| (exit 1)])
203       fi
204     done
205   fi
206 }
207
208
209 # Try three independent libraries,
210 #   one installed libtool library,
211 #   one uninstalled libtool library,
212 #   one non-libtool library,
213 # the libtool libraries each having a dependency, or not.
214 # Try both an uninstalled and the corresponding installed program.
215
216 for withdep in no yes; do
217   echo
218   echo "### libraries with dependencies: $withdep"
219   rm -rf src $prefix $prefix1 $prefix2 $prefix3
220   mkdir src $prefix $bindir $prefix1 $prefix2 $prefix3
221   cd src
222
223   ### build the libraries.
224   for i in 1 2 3; do
225     eval ldir=\$libdir$i
226     mkdir a$i $ldir
227     cd a$i
228     case $withdep,$i in
229     no,* | yes,3)
230       echo "int a$i() { return 0; }" > a$i.c
231       $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c a$i.c
232       $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba$i.la a$i.lo -rpath $ldir
233       ;;
234     *)
235       echo "int a${i}dep() { return 0; }" > a${i}dep.c
236       $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c a${i}dep.c
237       $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba${i}dep.la a${i}dep.lo -rpath $ldir
238       echo "extern int a${i}dep(); int a$i() { return a${i}dep(); }" > a$i.c
239       $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c a$i.c
240       $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba$i.la a$i.lo -rpath $ldir ./liba${i}dep.la
241       ;;
242     esac
243     cd ..
244   done
245
246   ### install the libraries.
247   test "$withdep" = yes && $LIBTOOL --mode=install cp a1/liba1dep.la $libdir1/liba1dep.la
248   $LIBTOOL --mode=install cp a1/liba1.la $libdir1/liba1.la
249   $LIBTOOL --mode=install cp a3/liba3.la $libdir3/liba3.la
250   $LIBTOOL --mode=clean rm -f a1/liba1.la a3/liba3.la
251   test "$withdep" = yes && $LIBTOOL --mode=clean rm -f a1/liba1dep.la 
252   # simulate a non-libtool lib:
253   rm -f $libdir3/liba3.la
254
255
256   ### build the programs.
257   echo 'extern int a1(), a2(), a3();
258   int main() { return a1() + a2() + a3(); }' > m.c
259   $CC $CPPFLAGS $CFLAGS -c m.c
260
261   # global static flags.
262   for st in -static -static-libtool-libs $can_link_all_static; do
263     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS $st -o m${st}$EXEEXT m.$OBJEXT \
264         -L$libdir1 -la1 a2/liba2.la -L$libdir3 -R$libdir3 -la3],
265        [0], [ignore], [ignore])
266   done
267
268   # per-deplib static/shared flags.
269   # also try a bit redundant flags, and shuffled order (for run paths check).
270   if $per_deplib; then
271     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m1$EXEEXT m.$OBJEXT \
272               -L$libdir1 -Bstatic -la1 -Bdynamic a2/liba2.la -L$libdir3 -R$libdir3 -la3],
273              [0], [ignore], [ignore])
274     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m2$EXEEXT m.$OBJEXT \
275               -L$libdir1 -la1 -Bstatic a2/liba2.la -Bdynamic -L$libdir3 -R$libdir3 -la3],
276              [0], [ignore], [ignore])
277     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m3$EXEEXT m.$OBJEXT \
278               -L$libdir1 -la1 a2/liba2.la -L$libdir3 -Bstatic -la3 -Bdynamic],
279              [0], [ignore], [ignore])
280     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m12$EXEEXT m.$OBJEXT \
281               -L$libdir1 -Bstatic -la1 a2/liba2.la -Bdynamic -L$libdir3 -R$libdir3 -la3],
282               [0], [ignore], [ignore])
283     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m13$EXEEXT m.$OBJEXT \
284               -L$libdir1 -Bstatic -la1 -Bdynamic a2/liba2.la \
285               -L$libdir3 -Bstatic -la3 -Bdynamic],
286               [0], [ignore], [ignore])
287     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m31$EXEEXT m.$OBJEXT \
288               -L$libdir3 -Bstatic -la3 -Bdynamic a2/liba2.la \
289               -L$libdir1 -Bstatic -la1 -Bdynamic],
290               [0], [ignore], [ignore])
291     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m23$EXEEXT m.$OBJEXT \
292               -L$libdir1 -la1 -Bstatic a2/liba2.la -Bdynamic \
293               -L$libdir3 -Bstatic -la3 -Bdynamic],
294               [0], [ignore], [ignore])
295     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m123$EXEEXT m.$OBJEXT \
296               -L$libdir1 -Bstatic -la1 a2/liba2.la -L$libdir3 -la3 -Bdynamic],
297               [0], [ignore], [ignore])
298     AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m123a$EXEEXT m.$OBJEXT \
299               -L$libdir1 -Bstatic -la1 -Bdynamic -Bstatic a2/liba2.la -Bdynamic \
300               -Bstatic -L$libdir3 -la3 -Bdynamic],
301               [0], [ignore], [ignore])
302     dnl # This usually fails.  So don't do it.
303     dnl AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m123b$EXEEXT m.$OBJEXT \
304     dnl          -L$libdir1 -Bstatic -la1 a2/liba2.la -L$libdir3 -la3],
305     dnl          [0], [ignore], [ignore])
306   fi
307
308   ### install the programs.
309   # We can't install any program that links dynamically against liba2.
310   for st in -static -static-libtool-libs $can_link_all_static `$per_deplib && echo 2 12 23 123 123a`; do 
311     echo "# m$st"
312     AT_CHECK([$LIBTOOL --mode=install cp m${st}$EXEEXT $bindir/m${st}$EXEEXT], [0], [ignore], [stderr])
313     if $have_static; then
314       AT_CHECK([$EGREP 'relinking|has not been installed' stderr], [1], [], [])
315     fi
316   done
317   dnl AT_CHECK([$LIBTOOL --mode=install cp m123b$EXEEXT $bindir/m123b$EXEEXT], [0], [ignore], [ignore])
318
319
320   ### Run each program once so that relinking has happened.
321   
322   func_fix_path $prefix1 $prefix2 $prefix3
323   func_test_exec -static -static-libtool-libs -all-static `$per_deplib && echo 1 2 3 12 13 23 31 123 123a`
324   func_restore_path
325
326   # For each library:
327   # - remove the library images to catch failure to link statically/dynamically,
328   # - add false other deplibs in the paths to catch (some) wrongly added run paths.
329
330   # if -all-static does not work, do not exercise it any more.
331   all_static=-all-static
332   test -z "$link_static_flag" && all_static=
333
334   echo "### test whether installed libtool library liba2 was linked statically"
335   func_move_libs a2 $prefix2 $prefix3 $prefix1
336   func_test_exec -static -static-libtool-libs $all_static `$per_deplib && echo 2 12 23 123 123a`
337   $per_deplib && func_test_exec_fail 1 3 13 31
338   func_restore_libs a2 $prefix2
339
340   echo "### test whether uninstalled libtool library liba1 was linked statically"
341   func_move_libs a1 $prefix1 $prefix2 $prefix3
342   func_test_exec -static-libtool-libs $all_static `$per_deplib && echo 1 12 13 31 123 123a`
343   $per_deplib && func_test_exec_fail -static 2 3 23
344   func_restore_libs a1 $prefix1
345
346   echo "### test whether non-libtool library liba3 was linked statically"
347   func_move_libs a3 $prefix3 $prefix1 $prefix2
348   func_test_exec $all_static `$per_deplib && echo 3 13 23 31 123 123a`
349   func_test_exec_fail -static -static-libtool-libs `$per_deplib && echo 1 2 12`
350   func_restore_libs a3 $prefix3
351
352   cd ..
353 done
354
355 AT_CLEANUP
356
357
358 AT_SETUP([ccache -all-static])
359
360 AT_DATA([ccache],
361 [[#! /bin/sh
362 # poor man's ccache clone
363 case $1 in
364 -*) echo "bogus argument: $1" >&2; exit 1 ;;
365 esac
366 exec "$@"
367 ]])
368 chmod +x ./ccache
369
370 AT_DATA([a.c],
371 [[int main(void) { return 0; }
372 ]])
373
374 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c a.c], [], [ignore], [ignore])
375 AT_CHECK([$LIBTOOL --mode=link --tag=CC ./ccache $CC $CFLAGS $LDFLAGS -all-static a.$OBJEXT -o a$EXEEXT],
376          [], [ignore], [ignore])
377
378 AT_CLEANUP