Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / bindir.at
1 # bindir.at -  Test the -bindir option
2 #
3 #   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 #   Written by Dave Korn, 2009
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 #  In this testcase, and in the chunk of code that makes use
27 # of $bindir in ltmain.m4sh, we would very much have liked to
28 # automatically decide which systems require dynamically-loaded
29 # libraries to be installed to a directory in $PATH according
30 # to the libtool properties that tell us that "the system provides
31 # no way to hard-code library paths into executables, and also
32 # has no $shlibpath_var independent of the PATH variable", in
33 # Ralf's words.  But it turns out this is not possible, as:-
34 #
35 #> * Dave Korn wrote on Fri, Aug 14, 2009 at 04:30:27AM CEST:
36 #>>> Ralf Wildenhues wrote:
37 #>>>
38 #>>>>> But in this particular case, I would argue that either you look at
39 #>>>>> the libtool variables shlibpath_var and hardcode_action for "PATH"
40 #>>>>> and "unsupported".
41 #>>>
42 #>>> On Cygwin, $hardcode_action = "immediate" in the generated libtool
43 #>>> script. Did you perhaps mean $hardcode_shlibpath_var, which is indeed
44 #>>> "unsupported"?
45 #>
46 #> Oh brother, yes, I forgot about those bogus hardcode_libdir_flag_spec
47 #> settings.  They fool _LT_LINKER_HARDCODE_LIBPATH.  I don't remember whether
48 #> they were needed in order to not break some semantics in ltmain (they
49 #> probably were).
50 #>
51 #> Anyway, $hardcode_action = "immediate" is definitely wrong, but fixing that
52 #> now is also way out of scope of this patch.  So I guess we need to stick to
53 #> host matching in the code, and work out a separate fix for the setting of
54 #> $hardcode_libdir_flag_spec.
55 #
56 # So alas we punt for now, and just hardcode the relevant OSs that require
57 # this functionality.  That's Cygwin, MinGW and CeGCC for now; see the case
58 # statement in libtool.m4sh around where the 'tdlname' variable is set.
59
60 ####
61 # First a simple test that we can build and run an executable with a couple of
62 # tiny libraries.
63
64 AT_SETUP([bindir basic lib test])
65
66 bindirneeded=:
67 case "$host_os" in
68   cygwin*|mingw*|cegcc*)
69     ;;
70   *)
71     bindirneeded=false
72     ;;
73 esac
74
75 ####
76 # These routines save the PATH before a test and restore it after,
77 # prepending a chosen directory to the path on the platforms where
78 # -bindir is needed after saving.
79 #
80
81 func_save_and_prepend_path ()
82 {
83   save_PATH=$PATH
84   if $bindirneeded ; then
85     PATH=$1:$PATH
86   fi
87   export PATH
88 }
89
90 func_restore_path ()
91 {
92   PATH=$save_PATH
93   export PATH
94 }
95
96 AT_DATA([foo.c],[[
97 int x=11;
98 ]])
99
100 AT_DATA([baz.c],[[
101 extern int x;
102 int baz (void);
103 int baz (void) { return x;}
104 ]])
105
106 AT_DATA([bar.c],[[
107 extern int baz (void);
108 int y=3;
109 int bar (void);
110 int bar (void) { return y + baz ();}
111 ]])
112
113 AT_DATA([main.c],[[
114 #include <stdlib.h>
115 extern int baz (void);
116 extern int bar (void);
117 int main() {
118 if (baz () + bar () - 25) abort ();
119 return 0;
120 }
121 ]])
122
123
124 curdir=`pwd`
125 eval "`$LIBTOOL --config | grep '^objdir='`"
126
127 AT_CHECK([$LIBTOOL --mode=compile --tag=CC $CC -c -o foo.lo $CPPFLAGS $CFLAGS foo.c],[0],[ignore],[ignore])
128 AT_CHECK([$LIBTOOL --mode=compile --tag=CC $CC -c -o baz.lo $CPPFLAGS $CFLAGS baz.c],[0],[ignore],[ignore])
129 AT_CHECK([$LIBTOOL --mode=link --tag=CC $CC -no-undefined -o libfoo.la $CPPFLAGS $CFLAGS $LDFLAGS foo.lo baz.lo -rpath $curdir/$objdir],[0],[ignore],[ignore])
130
131 AT_CHECK([$LIBTOOL --mode=compile --tag=CC $CC -c -o bar.lo $CPPFLAGS $CFLAGS bar.c],[0],[ignore],[ignore])
132 AT_CHECK([$LIBTOOL --mode=link --tag=CC $CC -no-undefined -o libbar.la $CPPFLAGS $CFLAGS $LDFLAGS bar.lo libfoo.la -rpath $curdir/$objdir],[0],[ignore],[ignore])
133
134 AT_CHECK([$LIBTOOL --mode=compile --tag=CC $CC -c -o main.lo $CPPFLAGS $CFLAGS main.c],[0],[ignore],[ignore])
135 AT_CHECK([$LIBTOOL --mode=link --tag=CC $CC -o main$EXEEXT $CPPFLAGS $CFLAGS $LDFLAGS main.lo libbar.la libfoo.la],[0],[ignore],[ignore])
136
137 # Check both static and shared versions run.  We don't install them
138 # here, that will be covered by the later tests; we've rpath'd things
139 # so that they can all be run in situ.
140
141 LT_AT_NOINST_EXEC_CHECK([./main])
142
143 # Ensure libraries can be found on PATH, if we are on one
144 # of the affected platforms, before testing the shared version.
145
146 func_save_and_prepend_path $curdir/$objdir
147 $bindirneeded && {
148   LT_AT_NOINST_EXEC_CHECK([$objdir/main])
149 }
150
151 #  In fact, prepending the PATH as above is superfluous on the windows
152 # platforms that this feature is primarily aimed at, as the DLL search
153 # path always includes the directory from which the app was launched.
154 # To make sure it still works even when not side-by-side, we'll install
155 # the main executable and execute it from there while the PATH still
156 # points to the shared libs in the .libs subdir.  On other platforms,
157 # the rpaths we set at link time will guarantee it runs from the bindir.
158
159 mkdir $curdir/bin
160 AT_CHECK([$LIBTOOL --mode=install $lt_INSTALL main$EXEEXT $curdir/bin/main$EXEEXT], [], [ignore], [ignore])
161 LT_AT_EXEC_CHECK([$curdir/bin/main$EXEEXT], [0], [ignore], [ignore], [])
162 func_restore_path
163
164 AT_CLEANUP
165
166 ####
167 # This is the main testcase.  For a variety of bindir and libdir
168 # settings, it verifies that all the files get installed exactly
169 # where we want them to go, and that they can be executed once
170 # installed.
171 #
172
173 AT_SETUP([bindir install tests])
174
175 bindirneeded=:
176 case "$host_os" in
177   cygwin*|mingw*|cegcc*)
178     ;;
179   *)
180     bindirneeded=false
181     ;;
182 esac
183
184 eval "`$LIBTOOL --config | grep '^build_libtool_libs='`"
185 AT_CHECK([test "$build_libtool_libs" = yes || exit 77])
186
187 ####
188 # These routines save the PATH before a test and restore it after,
189 # prepending a chosen directory to the path on the platforms where
190 # -bindir is needed after saving.
191 #
192
193 func_save_and_prepend_path ()
194 {
195   save_PATH=$PATH
196   if $bindirneeded ; then
197     PATH=$1:$PATH
198   fi
199   export PATH
200 }
201
202 func_restore_path ()
203 {
204   PATH=$save_PATH
205   export PATH
206 }
207
208 AT_DATA([foo.c],[[
209 int x=11;
210 ]])
211
212 AT_DATA([baz.c],[[
213 extern int x;
214 int baz (void);
215 int baz (void) { return x;}
216 ]])
217
218 AT_DATA([bar.c],[[
219 extern int baz (void);
220 int y=3;
221 int bar (void);
222 int bar (void) { return y + baz ();}
223 ]])
224
225 AT_DATA([main.c],[[
226 #include <stdlib.h>
227 extern int baz (void);
228 extern int bar (void);
229 int main() {
230 if (baz () + bar () - 25) abort ();
231 return 0;
232 }
233 ]])
234
235 # We only need to compile once, but we'll need to relink for each different value
236 # of libdir in order to set the rpath, and we'll install for each combination of
237 # libdir and bindir.
238
239 AT_CHECK([$LIBTOOL --mode=compile --tag=CC $CC -c -o foo.lo $CPPFLAGS $CFLAGS foo.c],[0],[ignore],[ignore])
240 AT_CHECK([$LIBTOOL --mode=compile --tag=CC $CC -c -o baz.lo $CPPFLAGS $CFLAGS baz.c],[0],[ignore],[ignore])
241 AT_CHECK([$LIBTOOL --mode=compile --tag=CC $CC -c -o bar.lo $CPPFLAGS $CFLAGS bar.c],[0],[ignore],[ignore])
242 AT_CHECK([$LIBTOOL --mode=compile --tag=CC $CC -c -o main.lo $CPPFLAGS $CFLAGS main.c],[0],[ignore],[ignore])
243
244 # Now try installing the libs.  There are the following cases:
245 # No -bindir
246 # -bindir below lib install dir
247 # -bindir is lib install dir
248 # -bindir beside lib install dir
249 # -bindir above lib dir
250 # -bindir above and beside lib dir
251 # -bindir in entirely unrelated prefix.
252
253 curdir=`pwd`
254 for libdir in \
255         $curdir/usr/lib/gcc/i686-pc-cygwin/4.5.0 \
256         $curdir/usr/lib/gcc/../gcc/.//i686-pc-cygwin/4.5.0/../../././//. \
257         $curdir/usr/lib/ \
258         $curdir/usr/lib \
259         $curdir/baz \
260         $curdir/baz/lib/ ;
261 do
262
263   # Do a basic install with no -bindir option for reference.  We use the sbin/
264   # dir for the main exe to avoid the potential "this only works because it's
265   # side-by-side with the libs" default DLL search path problem mentioned above.
266   rm -rf $libdir $curdir/bin $curdir/sbin $curdir/baz $curdir/usr
267   AS_MKDIR_P($libdir)
268   AS_MKDIR_P($curdir/sbin)
269   AT_CHECK([$LIBTOOL --mode=link --tag=CC $CC -no-undefined -o libfoo.la $CPPFLAGS $CFLAGS $LDFLAGS foo.lo bar.lo baz.lo -rpath $libdir],[0],[ignore],[ignore])
270   AT_CHECK([$LIBTOOL --mode=link --tag=CC $CC -o main$EXEEXT $CPPFLAGS $CFLAGS $LDFLAGS main.lo libfoo.la -rpath $libdir],[0],[ignore],[ignore])
271   AT_CHECK([$LIBTOOL --mode=install $lt_INSTALL libfoo.la $libdir], [], [ignore], [ignore])
272   AT_CHECK([$LIBTOOL --mode=install $lt_INSTALL main$EXEEXT $curdir/sbin/main$EXEEXT], [], [ignore], [ignore])
273
274   # And ensure it went where we expect.  Could be looking for any of
275   # 'cygfoo-0.dll', 'libfoo-0.dll', 'foo-0.dll', or 'libfoo.so.0'.  We'll
276   # simplify this check by taking advantage of the fact that if it's a DLL,
277   # it has to go in bindir, so we'll not check for both forms in libdir.
278   if $bindirneeded; then
279     AT_CHECK([test -f $libdir/../bin/???foo-0.dll || ls $libdir/../bin/*foo*0*], [], [ignore], [ignore])
280   else
281     AT_CHECK([ls $libdir/*foo*], [], [ignore], [ignore])
282   fi
283
284   # And that it can be executed.
285   extrapath=
286   $bindirneeded && extrapath=$libdir/../bin
287   func_save_and_prepend_path $extrapath
288   LT_AT_EXEC_CHECK([$curdir/sbin/main$EXEEXT], [0], [ignore], [ignore], [])
289   func_restore_path
290
291   for bindir in \
292         $curdir/usr/lib/gcc/i686-pc-cygwin/4.5.0/bin/ \
293         $curdir/usr/lib/gcc/i686-pc-cygwin/4.5.0/bin \
294         $curdir/usr/lib/gcc/i686-pc-cygwin/bin \
295         $curdir/usr/lib/bin \
296         $curdir/usr/bin/ \
297         $curdir/usr/bin \
298         /tmp/foo/bar ;
299   do
300
301     # Clear any old stuff out before we install.  Because bindir
302     # may be in /tmp, we have to take care to create it securely
303     # and not to delete and recreate it if we do.
304     rm -rf $libdir $curdir/bin $curdir/sbin $curdir/baz $curdir/usr
305
306     tmp=
307     case $bindir in
308       /tmp*)
309         # Create a temporary directory $tmp in $TMPDIR (default /tmp).
310         # Use mktemp if possible; otherwise fall back on mkdir,
311         # with $RANDOM to make collisions less likely.
312         : ${TMPDIR=/tmp}
313         {
314           tmp=`
315             (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
316           ` &&
317           test -n "$tmp" && test -d "$tmp"
318         } || {
319           tmp=$TMPDIR/foo$$-$RANDOM
320           (umask 077 && mkdir "$tmp")
321         } || AT_CHECK([exit 77])
322         bindir=$tmp/bar
323       ;;
324       *)
325         # Clear any old stuff out before we install.
326         rm -rf $bindir
327         AS_MKDIR_P($bindir)
328       ;;
329     esac
330
331     # Relink with new rpaths.
332     AT_CHECK([$LIBTOOL --mode=link --tag=CC $CC -no-undefined -bindir $bindir -o libfoo.la $CPPFLAGS $CFLAGS $LDFLAGS foo.lo bar.lo baz.lo -rpath $libdir],[0],[ignore],[ignore])
333     AT_CHECK([$LIBTOOL --mode=link --tag=CC $CC -o main$EXEEXT $CPPFLAGS $CFLAGS $LDFLAGS main.lo libfoo.la],[0],[ignore],[ignore])
334
335     # Recreate directories (bindir already done) and install.
336     AS_MKDIR_P($libdir)
337     AS_MKDIR_P($curdir/sbin)
338     AT_CHECK([$LIBTOOL --mode=install $lt_INSTALL libfoo.la $libdir], [], [ignore], [ignore])
339     AT_CHECK([$LIBTOOL --mode=install $lt_INSTALL main$EXEEXT $curdir/sbin/main$EXEEXT], [], [ignore], [ignore])
340
341     # Ensure it went to bindir rather than default dir this time.
342     if $bindirneeded; then
343       AT_CHECK([test -f $bindir/???foo-0.dll || ls $bindir/*foo*0*], [], [ignore], [ignore])
344     else
345       AT_CHECK([ls $libdir/*foo*], [], [ignore], [ignore])
346     fi
347
348     # And that it can be executed.
349     extrapath=
350     $bindirneeded && extrapath=$bindir
351     func_save_and_prepend_path $extrapath
352     LT_AT_EXEC_CHECK([$curdir/sbin/main$EXEEXT], [0], [ignore], [ignore], [])
353     func_restore_path
354
355     # Clean up if we made a temp dir.  Subdirs under our testdir get rm'd
356     # and recreated at the top of the loop.  Securely created subdirs under
357     # /tmp get created precisely once and rm'd when we're done with them.
358     if test ! -z "$tmp" ; then
359       rm -rf "$tmp"
360     fi
361
362   done
363 done
364
365 AT_CLEANUP