1 # static.at -- test flags for static/dynamic linking -*- Autotest -*-
3 # Copyright (C) 2006-2008, 2011-2019, 2021-2024 Free Software
5 # Written by Ralf Wildenhues, 2006
7 # This file is part of GNU Libtool.
9 # GNU Libtool is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; either version 2 of
12 # the License, or (at your option) any later version.
14 # GNU Libtool is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with GNU Libtool; see the file COPYING. If not, a copy
21 # can be downloaded from http://www.gnu.org/licenses/gpl.html,
22 # or obtained by writing to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 # - How do we test whether a library was linked statically?
30 # - try $NM on the program to see whether it includes the symbol definitions.
31 # disadvantage: for uninstalled programs, we may need to find out the name
32 # of the _real_ linked-against-uninstalled executable
33 # (.libs/prog vs .libs/lt-prog etc).
34 # - simply remove the libraries before execution. If the program still works,
35 # then the library was linked statically.
36 # -Does this work on all systems?
37 # -No, it will fail on AIX with non-rtl-created libraries: plain '-static'
38 # will only cause the linker not to consider '*.so' libraries, but only
39 # '*.a'. The latter, however, may still be shared images. :-/
40 # '-all-static' still works, however.
42 # It will not work with dlpreloading until we fix its related bug.
44 # Let's try the latter until we know better.
46 # - Test -Bstatic/-Bdynamic. It should work with all of:
47 # - (un)installed libtool libraries
48 # - non-libtool libraries
49 # - direct or pulled-in libraries
50 # - libraries of which there are only one kind available (TODO)
51 # (in the static case, should having only the shared one provoke failure?)
53 # - Check no extraneous run paths have been added.
55 # - make sure -Bstatic/-Bdynamic cannot be mixed with -all-static (TODO)
57 # - should -Bstatic/-Bdynamic be mixable with -static or -static-libtool-libs?
58 # Semantics could be as follows:
59 # - '-static'/'-static-libtool-libs' set the default, which is the initial
60 # value, then '-Bstatic'/'-Bdynamic' override that
61 # - '-Bdefault' resets to the default value given by the other switches.
63 # - TODO: test exposure for dlopened and dlpreopened modules,
64 # without and with diverse static flag combinations.
66 # - TODO: test other tags: C++ etc.
67 # (most likely the Sun compiler suite will be the only problem child).
69 AT_SETUP([static linking flags for programs])
70 AT_KEYWORDS([libtool])
71 AT_KEYWORDS([interactive])dnl Some of the exec_fail test cause popups with MinGW.
73 LDFLAGS="$LDFLAGS -no-undefined"
82 srcdir_broken=`pwd`/broken-src
83 prefix_broken=`pwd`/broken
84 libdir_broken=$prefix_broken/lib
85 bindir_broken=$prefix_broken/bin
90 $LIBTOOL --features | $GREP 'enable static libraries' >/dev/null && have_static=:
91 $LIBTOOL --features | $GREP 'enable shared libraries' >/dev/null && have_shared=:
92 eval `$LIBTOOL --config |
93 $EGREP '^(per_deplib_(static|dynamic)_flag|shlibpath_var|link_static_flag)='`
94 if test -n "$per_deplib_static_flag" && test -n "$per_deplib_dynamic_flag"; then
97 # On GNU/Linux with --disable-static, m-all-static fails to link.
98 # What we'd like to state here is: if the user actively passed
99 # --disable-static (as opposed to: the libtool.m4 macros set
100 # enable_static=no), then they cannot expect -all-static to work.
101 # So we punt, knowing that we mangle enable_static on AIX only.
102 can_link_all_static=-all-static
103 case $host_os,$have_static,$link_static_flag in
105 *,false,?*) can_link_all_static= ;;
108 # Create broken libraries. They will later be moved to those
109 # directories where the respective libraries should be linked
110 # statically from. This detects both failure to link statically
111 # and failure to omit extraneous run paths.
112 mkdir $srcdir_broken $prefix_broken $libdir_broken
115 echo 'int this_should_not_be_linked_against() { return 0; }' > a.c
116 $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c a.c
117 for i in 1 1dep 2 2dep 3 3dep; do
118 $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba$i.la a.lo -rpath $libdir_broken
119 $LIBTOOL --mode=install cp liba$i.la $libdir_broken/liba$i.la
125 # For w32, hardcoding cannot work, but $libdir/../bin is where binaries
126 # usually are installed. Since we use several prefixes for testing
127 # convenience -- it allows us to replace the good libraries easily with
128 # broken ones and vice versa -- we have to set PATH to find them.
129 # Since OTOH we put broken libs of all names in the "moved" prefixes,
130 # we have to ensure that this prefix comes last: otherwise we may link
131 # against a broken library but the good one would come later in the PATH.
132 # So we let the caller of this function set the order: the "other" two
134 if test PATH = "$shlibpath_var"; then
137 test -z "$PATH" || sep=:
138 PATH=$2/bin:$3/bin:$1/bin$sep$PATH
145 test PATH = "$shlibpath_var" && PATH=$save_PATH
148 # func_move_libs srcdir_to_move prefix_to_move other_prefix other_prefix
151 LT_AT_MVDIR(["$1"], ["$1-moved"])
152 LT_AT_MVDIR(["$2"], ["$2-moved"])
153 LT_AT_MVDIR(["$srcdir_broken"], ["$1"])
154 LT_AT_MVDIR(["$prefix_broken"], ["$2"])
155 func_fix_path "$2" "$3" "$4"
158 # func_restore_libs srcdir_to_restore prefix_to_restore
162 LT_AT_MVDIR(["$2"], ["$prefix_broken"])
163 LT_AT_MVDIR(["$1"], ["$srcdir_broken"])
164 LT_AT_MVDIR(["$2-moved"], ["$2"])
165 LT_AT_MVDIR(["$1-moved"], ["$1"])
168 # make sure the program can be run.
171 # On AIX without runtimelinking, this does not make sense.
172 if $have_static; then
173 echo "## The following should succeed:"
177 LT_AT_EXEC_CHECK([./m$st])
178 # For some per-deplib flag combinations there may be no installed program,
179 # because liba2 is not yet installed.
180 if test -f "$bindir/m$st$EXEEXT"; then
181 LT_AT_EXEC_CHECK([$bindir/m$st])
187 # make sure the program cannot be run.
188 func_test_exec_fail ()
190 # No point in testing if we're linking statically anyway.
191 # TODO: Maybe in the 'else' case we could test for success?
192 if $have_shared; then
193 echo "## The following should fail:"
197 LT_AT_EXEC_CHECK([./m$st], [1], [], [ignore], [|| (exit 1)])
198 # For some per-deplib flag combinations there may be no installed program,
199 # because liba2 is not yet installed.
200 if test -f "$bindir/m$st$EXEEXT"; then
201 LT_AT_EXEC_CHECK([$bindir/m$st], [1], [], [ignore], [|| (exit 1)])
208 # Try three independent libraries,
209 # one installed libtool library,
210 # one uninstalled libtool library,
211 # one non-libtool library,
212 # the libtool libraries each having a dependency, or not.
213 # Try both an uninstalled and the corresponding installed program.
215 for withdep in no yes; do
217 echo "### libraries with dependencies: $withdep"
218 rm -rf src $prefix $prefix1 $prefix2 $prefix3
219 mkdir src $prefix $bindir $prefix1 $prefix2 $prefix3
222 ### build the libraries.
229 echo "int a$i() { return 0; }" > a$i.c
230 $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c a$i.c
231 $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba$i.la a$i.lo -rpath $ldir
234 echo "int a${i}dep() { return 0; }" > a${i}dep.c
235 $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c a${i}dep.c
236 $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba${i}dep.la a${i}dep.lo -rpath $ldir
237 echo "extern int a${i}dep(); int a$i() { return a${i}dep(); }" > a$i.c
238 $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c a$i.c
239 $LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba$i.la a$i.lo -rpath $ldir ./liba${i}dep.la
245 ### install the libraries.
246 test yes = "$withdep" && $LIBTOOL --mode=install cp a1/liba1dep.la $libdir1/liba1dep.la
247 $LIBTOOL --mode=install cp a1/liba1.la $libdir1/liba1.la
248 $LIBTOOL --mode=install cp a3/liba3.la $libdir3/liba3.la
249 $LIBTOOL --mode=clean rm -f a1/liba1.la a3/liba3.la
250 test yes = "$withdep" && $LIBTOOL --mode=clean rm -f a1/liba1dep.la
251 # simulate a non-libtool lib:
252 rm -f $libdir3/liba3.la
255 ### build the programs.
256 echo 'extern int a1(), a2(), a3();
257 int main() { return a1() + a2() + a3(); }' > m.c
258 $CC $CPPFLAGS $CFLAGS -c m.c
260 # global static flags.
261 for st in -static -static-libtool-libs $can_link_all_static; do
262 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS $st -o m$st$EXEEXT m.$OBJEXT \
263 -L$libdir1 -la1 a2/liba2.la -L$libdir3 -R$libdir3 -la3],
264 [0], [ignore], [ignore])
267 # per-deplib static/shared flags.
268 # also try a bit redundant flags, and shuffled order (for run paths check).
270 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m1$EXEEXT m.$OBJEXT \
271 -L$libdir1 -Bstatic -la1 -Bdynamic a2/liba2.la -L$libdir3 -R$libdir3 -la3],
272 [0], [ignore], [ignore])
273 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m2$EXEEXT m.$OBJEXT \
274 -L$libdir1 -la1 -Bstatic a2/liba2.la -Bdynamic -L$libdir3 -R$libdir3 -la3],
275 [0], [ignore], [ignore])
276 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m3$EXEEXT m.$OBJEXT \
277 -L$libdir1 -la1 a2/liba2.la -L$libdir3 -Bstatic -la3 -Bdynamic],
278 [0], [ignore], [ignore])
279 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m12$EXEEXT m.$OBJEXT \
280 -L$libdir1 -Bstatic -la1 a2/liba2.la -Bdynamic -L$libdir3 -R$libdir3 -la3],
281 [0], [ignore], [ignore])
282 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m13$EXEEXT m.$OBJEXT \
283 -L$libdir1 -Bstatic -la1 -Bdynamic a2/liba2.la \
284 -L$libdir3 -Bstatic -la3 -Bdynamic],
285 [0], [ignore], [ignore])
286 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m31$EXEEXT m.$OBJEXT \
287 -L$libdir3 -Bstatic -la3 -Bdynamic a2/liba2.la \
288 -L$libdir1 -Bstatic -la1 -Bdynamic],
289 [0], [ignore], [ignore])
290 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m23$EXEEXT m.$OBJEXT \
291 -L$libdir1 -la1 -Bstatic a2/liba2.la -Bdynamic \
292 -L$libdir3 -Bstatic -la3 -Bdynamic],
293 [0], [ignore], [ignore])
294 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m123$EXEEXT m.$OBJEXT \
295 -L$libdir1 -Bstatic -la1 a2/liba2.la -L$libdir3 -la3 -Bdynamic],
296 [0], [ignore], [ignore])
297 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m123a$EXEEXT m.$OBJEXT \
298 -L$libdir1 -Bstatic -la1 -Bdynamic -Bstatic a2/liba2.la -Bdynamic \
299 -Bstatic -L$libdir3 -la3 -Bdynamic],
300 [0], [ignore], [ignore])
301 dnl # This usually fails. So don't do it.
302 dnl AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o m123b$EXEEXT m.$OBJEXT \
303 dnl -L$libdir1 -Bstatic -la1 a2/liba2.la -L$libdir3 -la3],
304 dnl [0], [ignore], [ignore])
307 ### install the programs.
308 # We can't install any program that links dynamically against liba2.
309 for st in -static -static-libtool-libs $can_link_all_static `$per_deplib && echo 2 12 23 123 123a`; do
311 AT_CHECK([$LIBTOOL --mode=install cp m$st$EXEEXT $bindir/m$st$EXEEXT], [0], [ignore], [stderr])
312 if $have_static; then
313 AT_CHECK([$EGREP 'relinking|has not been installed' stderr], [1], [], [])
316 dnl AT_CHECK([$LIBTOOL --mode=install cp m123b$EXEEXT $bindir/m123b$EXEEXT], [0], [ignore], [ignore])
319 ### Run each program once so that relinking has happened.
321 func_fix_path $prefix1 $prefix2 $prefix3
322 func_test_exec -static -static-libtool-libs -all-static `$per_deplib && echo 1 2 3 12 13 23 31 123 123a`
326 # - remove the library images to catch failure to link statically/dynamically,
327 # - add false other deplibs in the paths to catch (some) wrongly added run paths.
329 # if '-all-static' does not work, do not exercise it any more.
330 all_static=-all-static
331 test -z "$link_static_flag" && all_static=
333 echo "### test whether installed libtool library liba2 was linked statically"
334 func_move_libs a2 $prefix2 $prefix3 $prefix1
335 func_test_exec -static -static-libtool-libs $all_static `$per_deplib && echo 2 12 23 123 123a`
336 $per_deplib && func_test_exec_fail 1 3 13 31
337 func_restore_libs a2 $prefix2
339 echo "### test whether uninstalled libtool library liba1 was linked statically"
340 func_move_libs a1 $prefix1 $prefix2 $prefix3
341 func_test_exec -static-libtool-libs $all_static `$per_deplib && echo 1 12 13 31 123 123a`
342 $per_deplib && func_test_exec_fail -static 2 3 23
343 func_restore_libs a1 $prefix1
345 echo "### test whether non-libtool library liba3 was linked statically"
346 func_move_libs a3 $prefix3 $prefix1 $prefix2
347 func_test_exec $all_static `$per_deplib && echo 3 13 23 31 123 123a`
348 # no '-static-libtool-libs' flag below, because some hosts such as
349 # Cray prefer static libs by default.
350 # and doesn't exercise anything not already tested above:
351 func_test_exec_fail -static `$per_deplib && echo 1 2 12`
352 func_restore_libs a3 $prefix3
360 AT_SETUP([ccache -all-static])
364 # poor man's ccache clone
366 -*) echo "bogus argument: $1" >&2; exit 1 ;;
373 [[int main(void) { return 0; }
376 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c a.c], [], [ignore], [ignore])
377 AT_CHECK([$LIBTOOL --mode=link --tag=CC ./ccache $CC $CFLAGS $LDFLAGS -all-static a.$OBJEXT -o a$EXEEXT],
378 [], [ignore], [ignore])