1 # lt_dlopenext.at -- test libltdl functionality -*- Autotest -*-
3 # Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 # This file is part of GNU Libtool.
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.
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.
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.
23 AT_SETUP([lt_dlopenext error messages])
24 AT_KEYWORDS([libltdl])
26 # This test requires shared library support.
27 AT_CHECK([$LIBTOOL --features | grep 'enable shared libraries' || exit 77],
33 mkdir $prefix $libdir $bindir
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
40 AT_DATA([simple-module.c],
48 run (const char *argument)
50 printf ("Hello, %s!\n", argument);
55 AT_DATA([ltdl-loader.c],
60 # define EXIT_FAILURE 1
61 # define EXIT_SUCCESS 0
72 #ifndef MODULE_PATH_ENV
73 # define MODULE_PATH_ENV "MODULE_PATH"
76 typedef int entrypoint (const char *argument);
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);
83 main (int argc, const char *argv[])
85 char *errormsg = NULL;
86 lt_dlhandle module = NULL;
87 entrypoint *run = NULL;
92 fprintf (stderr, "USAGE: main MODULENAME ARGUMENT\n");
96 /* Initialise libltdl. */
97 errors = lt_dlinit ();
99 /* Set the module search path. */
102 const char *path = getenv (MODULE_PATH_ENV);
105 errors = lt_dlsetsearchpath (path);
108 /* Load the module. */
110 module = lt_dlopenext (argv[1]);
112 /* Find the entry point. */
115 run = (entrypoint *) lt_dlsym (module, "run");
117 /* In principle, run might legitimately be NULL, so
118 I don't use run == NULL as an error indicator
120 errormsg = dlerrordup (errormsg);
121 if (errormsg != NULL)
123 errors = lt_dlclose (module);
130 /* Call the entry point function. */
133 int result = (*run) (argv[2]);
135 errormsg = strdup ("module entry point execution failed");
137 printf ("\t=> %d\n", result);
140 /* Unload the module, now that we are done with it. */
142 errors = lt_dlclose (module);
146 /* Diagnose the encountered error. */
147 errormsg = dlerrordup (errormsg);
151 fprintf (stderr, "%s: dlerror() failed.\n", argv[0]);
156 /* Finished with ltdl now. */
158 if (lt_dlexit () != 0)
159 errormsg = dlerrordup (errormsg);
163 fprintf (stderr, "%s: %s.\n", argv[0], errormsg);
171 /* Be careful to save a copy of the error message,
172 since the next API call may overwrite the original. */
174 dlerrordup (char *errormsg)
176 char *error = (char *) lt_dlerror ();
177 if (error && !errormsg)
178 errormsg = strdup (error);
183 : ${LTDLINCL="-I$abs_top_srcdir/libltdl"}
184 : ${LIBLTDL="$abs_builddir/../libltdl/libltdlc.la"}
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) ;;
192 CPPFLAGS="$LTDLINCL $CPPFLAGS"
193 LDFLAGS="$LDFLAGS -no-undefined"
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])
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.
212 # Finding the module without the .la file will not work if MODULE_EXT
213 # aka. shared_ext is empty.
215 eval `$LIBTOOL --config | $EGREP '^(shlibpath_var|shrext_cmds)='`
218 eval shared_ext=\"$shrext_cmds\"
219 if test -n "$shared_ext"; then
220 have_lafile="with without"
225 if test "$shlibpath_var" = PATH; then
226 $unset shlibpath_var || shlibpath_var=
229 for lafile in $have_lafile; do
230 if test $lafile = without; then
231 rm $libdir/simple-module.la
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])
239 for var in MODULE_PATH LTDL_LIBRARY_PATH $shlibpath_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=