Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / lt_dlexit.at
1 # lt_dlexit.at -- test libltdl functionality                  -*- Autotest -*-
2 #
3 #   Copyright (C) 2007, 2008, 2010 Free Software Foundation, Inc.
4 #   Written by Dave Brolley & Ralf Wildenhues, 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 # Try to keep the new interfaces of HEAD separate from those of
26 # branch-1-5 to facilitate testing of older releases.
27
28 AT_BANNER([Libltdl functionality.])
29
30 AT_SETUP([lt_dlexit unloading libs])
31 AT_KEYWORDS([libltdl])
32
33 # Test for
34 # http://lists.gnu.org/archive/html/bug-libtool/2007-01/msg00014.html
35 # http://lists.gnu.org/archive/html/bug-libtool/2008-03/msg00013.html
36
37 AT_DATA([main.c],
38 [[#include <ltdl.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 typedef int (*pfun_T) (int);
43 typedef int *pvar_T;
44
45 /* lt_dlopen wrapper */
46 static lt_dlhandle
47 xdlopen (const char *filename)
48 {
49   lt_dlhandle handle = lt_dlopen (filename);
50   if (!handle) {
51     fprintf (stderr, "can't open the module %s!\n", filename);
52     fprintf (stderr, "error was: %s\n", lt_dlerror());
53   }
54   return handle;
55 }
56
57 /* lt_dlsym wrapper: try one function and one variable */
58 static int
59 xdlsymtest (lt_dlhandle handle, const char *func, const char *var)
60 {
61   pfun_T pf = (pfun_T) lt_dlsym (handle, func);
62   pvar_T pv = (pvar_T) lt_dlsym (handle, var);
63   if (pf == NULL) {
64     fprintf (stderr, "function `%s' not found\n", func);
65     return 1;
66   }
67   if (pv == NULL) {
68     fprintf (stderr, "variable `%s' not found\n", var);
69     return 1;
70   }
71   return (*pf) (*pv);
72 }
73
74 int
75 main (void)
76 {
77   lt_dlhandle b1;
78
79   LTDL_SET_PRELOADED_SYMBOLS();
80   if (lt_dlinit() != 0) {
81     fprintf (stderr, "error during initialization: %s\n", lt_dlerror());
82     return 1;
83   }
84   if (lt_dlexit() != 0) {
85     fprintf (stderr, "error during first lt_dlexit: %s\n", lt_dlerror());
86     return 1;
87   }
88   if (lt_dlinit() != 0) {
89     fprintf (stderr, "error during second initialization: %s\n", lt_dlerror());
90     return 1;
91   }
92   if (!(b1 = xdlopen ("modb1.la"))) return 1;
93   if (xdlsymtest (b1, "fb1", "vb1")) return 1;
94   /* do not lt_dlclose here on purpose.  */
95
96   if (lt_dlexit() != 0) {
97     fprintf (stderr, "error during exit: %s\n", lt_dlerror());
98     return 1;
99   }
100   return 0;
101 }
102 ]])
103
104
105 AT_DATA([a1.c],
106 [[#ifdef __cplusplus
107 extern "C" {
108 #endif
109 int f1 (int x) { return x - 1; }
110 int v1 = 1;
111 #ifdef __cplusplus
112 }
113 #endif
114 ]])
115
116 AT_DATA([b1.c],
117 [[#ifdef __cplusplus
118 extern "C" {
119 #endif
120 #if defined DLL_EXPORT && defined _WIN32 && defined _MSC_VER
121 #  define LIBA1_SCOPE extern __declspec (dllimport)
122 #endif
123 #if !defined LIBA1_SCOPE
124 #  define LIBA1_SCOPE extern
125 #endif
126 LIBA1_SCOPE int f1 (int);
127 LIBA1_SCOPE int v1;
128 int fb1 (int x) { return f1 (v1) + x - 3; }
129 int vb1 = 3;
130 #ifdef __cplusplus
131 }
132 #endif
133 ]])
134
135
136 : ${LTDLINCL="-I$abs_top_srcdir/libltdl"}
137 : ${LIBLTDL="$abs_builddir/../libltdl/libltdlc.la"}
138
139 # Skip this test when called from:
140 #    make distcheck DISTCHECK_CONFIGURE_FLAGS=--disable-ltdl-install
141 AT_CHECK([case $LIBLTDL in #(
142  */_inst/lib/*) test -f $LIBLTDL || (exit 77) ;;
143 esac], [], [ignore])
144
145 CPPFLAGS="$LTDLINCL $CPPFLAGS"
146 LDFLAGS="$LDFLAGS -no-undefined"
147
148 for file in a1 b1; do
149   $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c $file.c
150 done
151 $CC $CPPFLAGS $CFLAGS -c main.c
152 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o liba1.la a1.lo \
153           -rpath /foo -avoid-version], [], [ignore], [ignore])
154 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o modb1.la b1.lo \
155           -rpath /foo -module -avoid-version liba1.la], [], [ignore], [ignore])
156
157 # TODO: test -dlpreopen
158 for dlopen in -dlopen; do
159   AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o main$EXEEXT main.$OBJEXT \
160            $dlopen modb1.la $LIBLTDL], [], [ignore], [ignore])
161   LT_AT_NOINST_EXEC_CHECK([./main], [-dlopen modb1.la])
162 done
163
164 AT_CLEANUP