Allow gcc builds with -Wall -Werror.
[libtool/ericb.git] / tests / need_lib_prefix.at
blobf6cfcfb35f6c4dd98fad32c4ad674562437b08bd
1 # need-lib-prefix.at -- test libltdl functionality            -*- Autotest -*-
3 #   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
4 #   Written by Gary V. Vaughan, 2007
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.
23 ####
25 AT_SETUP([enforced lib prefix])
26 AT_KEYWORDS([libltdl libtool])
28 AT_DATA([main.c],
29 [[#include <ltdl.h>
30 #include <stdio.h>
32 typedef int fun (int);
34 static int errors = 0;
36 static void
37 complain (const char *msg)
39   const char *errmsg = lt_dlerror ();
40   fprintf (stderr, "%s", msg);
41   if (errmsg)
42     fprintf (stderr, ": %s\n", errmsg);
43   else
44     fprintf (stderr, ".\n");
45   ++errors;
48 static lt_dlhandle
49 moduleopen (const char *filename)
51   lt_dlhandle handle;
53   handle = lt_dlopen (filename);
54   if (!handle)
55     {
56       fprintf (stderr, "can't open the module %s!\n", filename);
57       complain ("error was");
58     }
60   return handle;
63 static int
64 moduletest (lt_dlhandle handle)
66   const lt_dlinfo *info = lt_dlgetinfo (handle);
67   fun *f = (fun *) lt_dlsym (handle, "f");
68   int *v = (int *) lt_dlsym (handle, "i");
70   if (!f)
71     {
72       complain ("function `f' not found");
73       return 1;
74     }
75   if (!v)
76     {
77       complain ("variable `i' not found");
78       return 1;
79     }
80   printf ("%s: %d\n", info->name, f (*v));
82   return 0;
85 int
86 main (int argc, char **argv)
88   lt_dlhandle handle;
90   LTDL_SET_PRELOADED_SYMBOLS();
92   if (lt_dlinit() != 0)
93     {
94       fprintf (stderr, "error during initialization: %s\n", lt_dlerror());
95       return 1;
96     }
98   if (lt_dlpreload_open (0, moduletest) != 0)
99     complain ("error during preloading");
101   if (lt_dlexit () != 0)
102     complain ("error during exit");
104   return (errors != 0);
108 AT_DATA([foo1.c],
109 [[#define f foo1_LTX_f
110 #define i foo1_LTX_i
111 #ifdef __cplusplus
112 extern "C" {
113 #endif
114 int f (int x) { return x / 3; }
115 int i = 7;
116 #ifdef __cplusplus
118 #endif
121 AT_DATA([foo2.c],
122 [[#define f libfoo2_LTX_f
123 #define i libfoo2_LTX_i
124 #ifdef __cplusplus
125 extern "C" {
126 #endif
127 int f (int x) { return (x * x) / 10; }
128 int i = 6;
129 #ifdef __cplusplus
131 #endif
134 LT_AT_HOST_DATA([expout],
135 [[libfoo1: 2
136 libfoo2: 3
139 : ${LTDLINCL="-I$abs_top_srcdir/libltdl"}
140 : ${LIBLTDL="$abs_builddir/../libltdl/libltdlc.la"}
142 # Skip this test when called from:
143 #    make distcheck DISTCHECK_CONFIGURE_FLAGS=--disable-ltdl-install
144 AT_CHECK([case $LIBLTDL in #(
145  */_inst/lib/*) test -f $LIBLTDL || (exit 77) ;;
146 esac], [], [ignore])
148 CPPFLAGS="$LTDLINCL $CPPFLAGS"
149 LDFLAGS="$LDFLAGS"
151 # Create our own libtool, forcing need_lib_prefix setting
152 sed 's,^\(need_lib_prefix\)=.*$,\1=unknown,' $LIBTOOL > ./libtool
153 LIBTOOL="$SHELL ./libtool"
155 # Installation directory:
156 instdir=`pwd`/_inst
158 $CC $CPPFLAGS $CFLAGS -c main.c
159 for file in foo1 foo2; do
160   $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c $file.c
161 done
163 AT_CHECK([$LIBTOOL --mode=link $CC -module -avoid-version $CFLAGS $LDFLAGS -o foo1.la foo1.lo -rpath $instdir/lib],
164          [], [ignore], [ignore])
165 AT_CHECK([$LIBTOOL --mode=link $CC -module -avoid-version $CFLAGS $LDFLAGS -o libfoo2.la foo2.lo -rpath $instdir/lib],
166          [], [ignore], [ignore])
167 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o main$EXEEXT main.$OBJEXT -dlpreopen foo1.la -dlpreopen libfoo2.la $LIBLTDL],
168           [], [ignore], [ignore])
170 LT_AT_NOINST_EXEC_CHECK([./main], [-dlopen foo1.la -dlopen libfoo2.la],
171               [], [expout], [])
173 # Install the libraries.
174 mkdir $instdir
175 mkdir $instdir/lib
176 $LIBTOOL --mode=install cp foo1.la $instdir/lib/foo1.la
177 $LIBTOOL --mode=install cp libfoo2.la $instdir/lib/libfoo2.la
179 # Install the binary
180 mkdir $instdir/bin
181 $LIBTOOL --mode=install cp main$EXEEXT $instdir/bin/main$EXEEXT
183 LT_AT_EXEC_CHECK([$instdir/bin/main], [], [expout], [])
185 AT_CLEANUP