Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / need_lib_prefix.at
1 # need-lib-prefix.at -- test libltdl functionality            -*- Autotest -*-
2 #
3 #   Copyright (C) 2007, 2008, 2010 Free Software Foundation, Inc.
4 #   Written by Gary V. Vaughan, 2007
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 AT_SETUP([enforced lib prefix])
26 AT_KEYWORDS([libltdl libtool])
27
28 # Originally written to simulate a failure visible on systems
29 # which need a library prefix like 'lib', such as BeOS.  Currently
30 # the prefix is hardcoded as 'lib', so systems that require a
31 # different prefix skips the test.
32
33 AT_DATA([main.c],
34 [[#include <ltdl.h>
35 #include <stdio.h>
36
37 typedef int fun (int);
38
39 static int errors = 0;
40
41 static void
42 complain (const char *msg)
43 {
44   const char *errmsg = lt_dlerror ();
45   fprintf (stderr, "%s", msg);
46   if (errmsg)
47     fprintf (stderr, ": %s\n", errmsg);
48   else
49     fprintf (stderr, ".\n");
50   ++errors;
51 }
52
53 static lt_dlhandle
54 moduleopen (const char *filename)
55 {
56   lt_dlhandle handle;
57
58   handle = lt_dlopen (filename);
59   if (!handle)
60     {
61       fprintf (stderr, "can't open the module %s!\n", filename);
62       complain ("error was");
63     }
64
65   return handle;
66 }
67
68 static int
69 moduletest (lt_dlhandle handle)
70 {
71   const lt_dlinfo *info = lt_dlgetinfo (handle);
72   fun *f = (fun *) lt_dlsym (handle, "f");
73   int *v = (int *) lt_dlsym (handle, "i");
74
75   if (!f)
76     {
77       complain ("function `f' not found");
78       return 1;
79     }
80   if (!v)
81     {
82       complain ("variable `i' not found");
83       return 1;
84     }
85   printf ("%s: %d\n", info->name, f (*v));
86
87   return 0;
88 }
89
90 int
91 main (int argc, char **argv)
92 {
93   lt_dlhandle handle;
94
95   LTDL_SET_PRELOADED_SYMBOLS();
96
97   if (lt_dlinit() != 0)
98     {
99       fprintf (stderr, "error during initialization: %s\n", lt_dlerror());
100       return 1;
101     }
102
103   if (lt_dlpreload_open (0, moduletest) != 0)
104     complain ("error during preloading");
105
106   if (lt_dlexit () != 0)
107     complain ("error during exit");
108
109   return (errors != 0);
110 }
111 ]])
112
113 AT_DATA([foo1.c],
114 [[#define f foo1_LTX_f
115 #define i foo1_LTX_i
116 #ifdef __cplusplus
117 extern "C" {
118 #endif
119 int f (int x) { return x / 3; }
120 int i = 7;
121 #ifdef __cplusplus
122 }
123 #endif
124 ]])
125
126 AT_DATA([foo2.c],
127 [[#define f libfoo2_LTX_f
128 #define i libfoo2_LTX_i
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
132 int f (int x) { return (x * x) / 10; }
133 int i = 6;
134 #ifdef __cplusplus
135 }
136 #endif
137 ]])
138
139 LT_AT_HOST_DATA([expout],
140 [[libfoo1: 2
141 libfoo2: 3
142 ]])
143
144 : ${LTDLINCL="-I$abs_top_srcdir/libltdl"}
145 : ${LIBLTDL="$abs_builddir/../libltdl/libltdlc.la"}
146
147 # Skip this test when called from:
148 #    make distcheck DISTCHECK_CONFIGURE_FLAGS=--disable-ltdl-install
149 AT_CHECK([case $LIBLTDL in #(
150  */_inst/lib/*) test -f $LIBLTDL || (exit 77) ;;
151 esac], [], [ignore])
152
153 CPPFLAGS="$LTDLINCL $CPPFLAGS"
154 LDFLAGS="$LDFLAGS"
155
156 # Skip this test when libraries are not normally prefixed with lib.
157 # E.g., for MSVC and OS/2.
158 eval "`$LIBTOOL --config | $EGREP '^(libname_spec)='`"
159 name=
160 eval libname=\"$libname_spec\"
161 AT_CHECK([test "$libname" = lib || exit 77])
162
163 # Create our own libtool, forcing need_lib_prefix setting
164 sed 's,^\(need_lib_prefix\)=.*$,\1=unknown,' $LIBTOOL > ./libtool
165 LIBTOOL="$SHELL ./libtool"
166
167 # Installation directory:
168 instdir=`pwd`/_inst
169
170 $CC $CPPFLAGS $CFLAGS -c main.c
171 for file in foo1 foo2; do
172   $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c $file.c
173 done
174
175 AT_CHECK([$LIBTOOL --mode=link $CC -module -avoid-version $CFLAGS $LDFLAGS -o foo1.la foo1.lo -rpath $instdir/lib],
176          [], [ignore], [ignore])
177 AT_CHECK([$LIBTOOL --mode=link $CC -module -avoid-version $CFLAGS $LDFLAGS -o libfoo2.la foo2.lo -rpath $instdir/lib],
178          [], [ignore], [ignore])
179 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o main$EXEEXT main.$OBJEXT -dlpreopen foo1.la -dlpreopen libfoo2.la $LIBLTDL],
180           [], [ignore], [ignore])
181
182 LT_AT_NOINST_EXEC_CHECK([./main], [-dlopen foo1.la -dlopen libfoo2.la],
183               [], [expout], [])
184
185 # Install the libraries.
186 mkdir $instdir
187 mkdir $instdir/lib
188 $LIBTOOL --mode=install cp foo1.la $instdir/lib/foo1.la
189 $LIBTOOL --mode=install cp libfoo2.la $instdir/lib/libfoo2.la
190
191 # Install the binary
192 mkdir $instdir/bin
193 $LIBTOOL --mode=install cp main$EXEEXT $instdir/bin/main$EXEEXT
194
195 LT_AT_EXEC_CHECK([$instdir/bin/main], [], [expout], [])
196
197 AT_CLEANUP