1 # static.at -- test flags for static/dynamic linking -*- Autotest -*-
3 # Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
4 # Written by Ralf Wildenhues, 2006
6 # This file is part of GNU Libtool.
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.
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.
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.
27 # - How do we test whether a library was linked statically?
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.
41 # It will not work with dlpreloading until we fix its related bug.
43 # Let's try the latter until we know better.
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?)
52 # - Check no extraneous run paths have been added.
54 # - make sure -Bstatic/-Bdynamic cannot be mixed with -all-static (TODO)
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.
62 # - TODO: test exposure for dlopened and dlpreopened modules,
63 # without and with diverse static flag combinations.
65 # - TODO: test other tags: C++ etc.
66 # (most likely the Sun compiler suite will be the only problem child).
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.
72 LDFLAGS="$LDFLAGS -no-undefined"
81 srcdir_broken=`pwd`/broken-src
82 prefix_broken=`pwd`/broken
83 libdir_broken=$prefix_broken/lib
84 bindir_broken=$prefix_broken/bin
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
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
104 *,false,?*) can_link_all_static= ;;
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
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
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
133 if test "$shlibpath_var" = PATH; then
136 test -z "$PATH" || sep=:
137 PATH=${2}/bin:${3}/bin:${1}/bin$sep$PATH
144 if test "$shlibpath_var" = PATH; then
149 # func_move_libs srcdir_to_move prefix_to_move other_prefix other_prefix
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}
159 # func_restore_libs srcdir_to_restore prefix_to_restore
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}"])
169 # make sure the program can be run.
172 # On AIX without runtimelinking, this does not make sense.
173 if $have_static; then
174 echo "## The following should succeed:"
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])
188 # make sure the program cannot be run.
189 func_test_exec_fail ()
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:"
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)])
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.
216 for withdep in no yes; do
218 echo "### libraries with dependencies: $withdep"
219 rm -rf src $prefix $prefix1 $prefix2 $prefix3
220 mkdir src $prefix $bindir $prefix1 $prefix2 $prefix3
223 ### build the libraries.
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
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
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
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
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])
268 # per-deplib static/shared flags.
269 # also try a bit redundant flags, and shuffled order (for run paths check).
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])
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
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], [], [])
317 dnl AT_CHECK([$LIBTOOL --mode=install cp m123b$EXEEXT $bindir/m123b$EXEEXT], [0], [ignore], [ignore])
320 ### Run each program once so that relinking has happened.
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`
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.
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=
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
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
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
358 AT_SETUP([ccache -all-static])
362 # poor man's ccache clone
364 -*) echo "bogus argument: $1" >&2; exit 1 ;;
371 [[int main(void) { return 0; }
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])