Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / lt_dlopenext.at
1 # lt_dlopenext.at -- test libltdl functionality             -*- Autotest -*-
2 #
3 #   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 #   This file is part of GNU Libtool.
5 #
6 # GNU Libtool is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of
9 # the License, or (at your option) any later version.
10 #
11 # GNU Libtool is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Libtool; see the file COPYING.  If not, a copy
18 # can be downloaded from  http://www.gnu.org/licenses/gpl.html,
19 # or obtained by writing to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 ####
22
23 AT_SETUP([lt_dlopenext error messages])
24 AT_KEYWORDS([libltdl])
25
26 # This test requires shared library support.
27 AT_CHECK([$LIBTOOL --features | grep 'enable shared libraries' || exit 77],
28          [], [ignore])
29
30 prefix=`pwd`/inst
31 libdir=$prefix/lib
32 bindir=$prefix/bin
33 mkdir $prefix $libdir $bindir
34
35 # This code is copied from the Autobook:
36 # <http://sources.redhat.com/autobook/autobook/autobook_169.html>
37 # so if it needs changes, be sure to notify the Autobook authors
38 # about them.
39
40 AT_DATA([simple-module.c],
41 [[
42 #include <stdio.h>
43
44 #ifdef __cplusplus
45 extern "C"
46 #endif
47 int
48 run (const char *argument)
49 {
50   printf ("Hello, %s!\n", argument);
51   return 0;
52 }
53 ]])
54
55 AT_DATA([ltdl-loader.c],
56 [[
57 #include <stdio.h>
58 #include <stdlib.h>
59 #ifndef EXIT_FAILURE
60 #  define EXIT_FAILURE        1
61 #  define EXIT_SUCCESS        0
62 #endif
63
64 #include <limits.h>
65 #ifndef PATH_MAX
66 #  define PATH_MAX 255
67 #endif
68
69 #include <string.h>
70 #include <ltdl.h>
71
72 #ifndef MODULE_PATH_ENV
73 #  define MODULE_PATH_ENV        "MODULE_PATH"
74 #endif
75
76 typedef int entrypoint (const char *argument);
77
78 /* Save and return a copy of the dlerror() error  message,
79    since the next API call may overwrite the original. */
80 static char *dlerrordup (char *errormsg);
81
82 int
83 main (int argc, const char *argv[])
84 {
85   char *errormsg = NULL;
86   lt_dlhandle module = NULL;
87   entrypoint *run = NULL;
88   int errors = 0;
89
90   if (argc != 3)
91     {
92       fprintf (stderr, "USAGE: main MODULENAME ARGUMENT\n");
93       exit (EXIT_FAILURE);
94     }
95
96   /* Initialise libltdl. */
97   errors = lt_dlinit ();
98
99   /* Set the module search path. */
100   if (!errors)
101     {
102       const char *path = getenv (MODULE_PATH_ENV);
103
104       if (path != NULL)
105         errors = lt_dlsetsearchpath (path);
106     }
107
108   /* Load the module. */
109   if (!errors)
110     module = lt_dlopenext (argv[1]);
111
112   /* Find the entry point. */
113   if (module)
114     {
115       run = (entrypoint *) lt_dlsym (module, "run");
116
117       /* In principle, run might legitimately be NULL, so
118          I don't use run == NULL as an error indicator
119          in general. */
120       errormsg = dlerrordup (errormsg);
121       if (errormsg != NULL)
122         {
123           errors = lt_dlclose (module);
124           module = NULL;
125         }
126     }
127   else
128     errors = 1;
129
130   /* Call the entry point function. */
131   if (!errors)
132     {
133       int result = (*run) (argv[2]);
134       if (result < 0)
135         errormsg = strdup ("module entry point execution failed");
136       else
137         printf ("\t=> %d\n", result);
138     }
139
140   /* Unload the module, now that we are done with it. */
141   if (!errors)
142     errors = lt_dlclose (module);
143
144   if (errors)
145     {
146       /* Diagnose the encountered error. */
147       errormsg = dlerrordup (errormsg);
148
149       if (!errormsg)
150         {
151           fprintf (stderr, "%s: dlerror() failed.\n", argv[0]);
152           return EXIT_FAILURE;
153         }
154     }
155
156   /* Finished with ltdl now. */
157   if (!errors)
158     if (lt_dlexit () != 0)
159       errormsg = dlerrordup (errormsg);
160
161   if (errormsg)
162     {
163       fprintf (stderr, "%s: %s.\n", argv[0], errormsg);
164       free (errormsg);
165       exit (EXIT_FAILURE);
166     }
167
168   return EXIT_SUCCESS;
169 }
170
171 /* Be careful to save a copy of the error message,
172    since the  next API call may overwrite the original. */
173 static char *
174 dlerrordup (char *errormsg)
175 {
176   char *error = (char *) lt_dlerror ();
177   if (error && !errormsg)
178     errormsg = strdup (error);
179   return errormsg;
180 }
181 ]])
182
183 : ${LTDLINCL="-I$abs_top_srcdir/libltdl"}
184 : ${LIBLTDL="$abs_builddir/../libltdl/libltdlc.la"}
185
186 # Skip this test when called from:
187 #    make distcheck DISTCHECK_CONFIGURE_FLAGS=--disable-ltdl-install
188 AT_CHECK([case $LIBLTDL in #(
189  */_inst/lib/*) test -f $LIBLTDL || (exit 77) ;;
190 esac], [], [ignore])
191
192 CPPFLAGS="$LTDLINCL $CPPFLAGS"
193 LDFLAGS="$LDFLAGS -no-undefined"
194
195 AT_CHECK([$LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c simple-module.c],
196          [], [ignore], [ignore])
197 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o simple-module.la ]dnl
198          [simple-module.lo -rpath $libdir -module -avoid-version],
199          [], [ignore], [ignore])
200 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c ltdl-loader.c],
201          [], [ignore], [ignore])
202 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o ltdl-loader$EXEEXT ]dnl
203          [ltdl-loader.$OBJEXT -dlopen self $LIBLTDL],
204          [], [ignore], [ignore])
205 AT_CHECK([$LIBTOOL --mode=install cp simple-module.la $libdir/simple-module.la], [], [ignore], [ignore])
206 AT_CHECK([$LIBTOOL --mode=clean rm -f simple-module.la], [], [ignore], [ignore])
207
208 # Try finding the module with and without the .la file, with absolute
209 # path, relative path, or one of the path variables.  Do not override
210 # $PATH for w32, see shlibpath.at for the hacks this requires.
211 #
212 # Finding the module without the .la file will not work if MODULE_EXT
213 # aka. shared_ext is empty.
214
215 eval `$LIBTOOL --config | $EGREP '^(shlibpath_var|shrext_cmds)='`
216
217 module=no
218 eval shared_ext=\"$shrext_cmds\"
219 if test -n "$shared_ext"; then
220   have_lafile="with without"
221 else
222   have="with"
223 fi
224
225 if test "$shlibpath_var" = PATH; then
226   $unset shlibpath_var || shlibpath_var=
227 fi
228
229 for lafile in $have_lafile; do
230   if test $lafile = without; then
231     rm $libdir/simple-module.la
232   fi
233
234   for dir in inst/lib "$libdir"; do
235     LT_AT_EXEC_CHECK([./ltdl-loader], [], [stdout], [ignore],
236                      [$dir/simple-module World])
237     AT_CHECK([grep "Hello, World" stdout], [], [ignore])
238
239     for var in MODULE_PATH LTDL_LIBRARY_PATH $shlibpath_var
240     do
241       eval $var=\$dir
242       export $var
243       LT_AT_EXEC_CHECK([./ltdl-loader], [], [stdout], [ignore],
244                        [simple-module World])
245       AT_CHECK([grep "Hello, World" stdout], [], [ignore])
246       $unset $var || eval $var=
247     done
248   done
249 done
250
251 AT_CLEANUP