ltmain.in: Use func_warning for all warnings
[libtool.git] / tests / dlloader-api.at
blob13147fa8f85cf5218aaca715cf0bfa26a6885651
1 # dlloader.at -- test dlloader functionality                -*- Autotest -*-
3 #   Copyright (C) 2010-2019, 2021-2024 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.
21 ####
23 AT_SETUP([dlloader API])
24 AT_KEYWORDS([libltdl])
26 AT_DATA([main.c],
27 [[#include <ltdl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 static int
33 first_init (lt_user_data data)
35   const char *ctx = (const char *) data;
37   printf ("first_init: %s\n", ctx);
39   return 0;
42 static lt_module
43 first_open (lt_user_data data, const char *filename, lt_dladvise advise)
45   static const char *first_module = "first";
46   const char *ctx = (const char *) data;
48   /* Use a magic string to avoid possible interactions with file system
49    * objects.  Prepend '/' to short-circuit libltdl's search of
50    * $shlibpath_var (e.g. PATH, LD_LIBRARY_PATH, or similar).
51    */
52   if (!filename || strcmp (filename, "/libltdl_dlloader_api_test_first"))
53     {
54       printf ("first_open denies a request\n");
55       lt_dlseterror (LT_ERROR_FILE_NOT_FOUND);
56       return NULL;
57     }
59   printf ("first_open (\"%s\"): %s\n", filename, ctx);
61   return (lt_module) first_module;
64 static const char *
65 first_symbol (void)
67   return "first_symbol";
70 static void *
71 first_sym (lt_user_data data, lt_module module, const char *symbolname)
73   const char *ctx = (const char *) data;
74   const char *filename = (const char *) module;
76   printf ("first_sym (%s): %s\n", filename, ctx);
78   return (void *) first_symbol;
81 static int
82 first_close (lt_user_data data, lt_module module)
84   const char *ctx = (const char *) data;
85   const char *filename = (const char *) module;
87   printf ("first_close (%s): %s\n", filename, ctx);
89   return 0;
92 static int
93 first_exit (lt_user_data data)
95   const char *ctx = (const char *) data;
97   printf ("first_exit: %s\n", ctx);
99   return 0;
102 static int
103 last_init (lt_user_data data)
105   const char *ctx = (const char *) data;
107   printf ("last_init: %s\n", ctx);
109   return 0;
112 static lt_module
113 last_open (lt_user_data data, const char *filename, lt_dladvise advise)
115   static const char *last_module = "last";
116   const char *ctx = (const char *) data;
118   /* Use a magic string to avoid possible interactions with file system
119    * objects.  Prepend '/' to short-circuit libltdl's search of
120    * $shlibpath_var (e.g. PATH, LD_LIBRARY_PATH, or similar).
121    */
122   if (!filename || strcmp (filename, "/libltdl_dlloader_api_test_last"))
123     {
124       printf ("last_open denies a request\n");
125       lt_dlseterror (LT_ERROR_FILE_NOT_FOUND);
126       return NULL;
127     }
129   printf ("last_open (\"%s\"): %s\n", filename, ctx);
131   return (lt_module) last_module;
134 static const char *
135 last_symbol (void)
137   return "last_symbol";
140 static void *
141 last_sym (lt_user_data data, lt_module module, const char *symbolname)
143   const char *ctx = (const char *) data;
144   const char *filename = (const char *) module;
146   printf ("last_sym (%s): %s\n", filename, ctx);
148   return (void *) last_symbol;
151 static int
152 last_close (lt_user_data data, lt_module module)
154   const char *ctx = (const char *) data;
155   const char *filename = (const char *) module;
157   printf ("last_close (%s): %s\n", filename, ctx);
159   return 0;
162 static int
163 last_exit (lt_user_data data)
165   const char *ctx = (const char *) data;
167   printf ("last_exit: %s\n", ctx);
169   return 0;
172 typedef const char *module_func (void);
175 main (int argc, char* argv[])
177   int err = 0;
178   lt_dlvtable *first;
179   lt_dlvtable *last;
180   lt_dlhandle module = NULL;
181   module_func *symbol;
182   const char *first_ctx = "first_ctx";
183   const char *last_ctx = "last_ctx";
184   const lt_dlvtable *finder;
186   LTDL_SET_PRELOADED_SYMBOLS ();
188   if (lt_dlinit ())
189     {
190       printf ("lt_dlinit failed\n");
191       return 1;
192     }
194   first = (lt_dlvtable *) malloc (sizeof (*first));
195   if (!first)
196     {
197       printf ("malloc failed\n");
198       err = 1;
199       goto cleanup;
200     }
202   first->name = "first";
203   first->sym_prefix = NULL;
204   first->module_open = first_open;
205   first->module_close = first_close;
206   first->find_sym = first_sym;
207   first->dlloader_init = first_init; /* test that it isn't called twice */
208   first->dlloader_exit = first_exit;
209   first->dlloader_data = (lt_user_data) first_ctx;
210   first->priority = LT_DLLOADER_PREPEND;
212   if (first_init (first->dlloader_data))
213     {
214       printf ("first_init failed\n");
215       err = 1;
216       goto cleanup;
217     }
219   if (lt_dlloader_add (first))
220     {
221       printf ("lt_dlloader_add failed: %s\n", lt_dlerror ());
222       err = 1;
223       goto cleanup;
224     }
226   finder = lt_dlloader_find ("first");
228   if (!finder)
229     {
230       printf ("lt_dlloader_find failed: %s\n", lt_dlerror ());
231       err = 1;
232       goto cleanup;
233     }
235   printf ("Found loader \"%s\"\n", finder->name);
237   last = (lt_dlvtable *) malloc (sizeof (*last));
238   if (!last)
239     {
240       printf ("malloc failed\n");
241       err = 1;
242       goto cleanup;
243     }
245   last->name = "last";
246   last->sym_prefix = NULL;
247   last->module_open = last_open;
248   last->module_close = last_close;
249   last->find_sym = last_sym;
250   last->dlloader_init = last_init; /* test that it isn't called twice */
251   last->dlloader_exit = last_exit;
252   last->dlloader_data = (lt_user_data) last_ctx;
253   last->priority = LT_DLLOADER_APPEND;
255   if (last_init (last->dlloader_data))
256     {
257       printf ("last_init failed\n");
258       err = 1;
259       goto cleanup;
260     }
262   if (lt_dlloader_add (last))
263     {
264       printf ("lt_dlloader_add failed: %s\n", lt_dlerror ());
265       err = 1;
266       goto cleanup;
267     }
269   finder = lt_dlloader_find ("last");
271   if (!finder)
272     {
273       printf ("lt_dlloader_find failed: %s\n", lt_dlerror ());
274       err = 1;
275       goto cleanup;
276     }
278   printf ("Found loader \"%s\"\n", finder->name);
280   /* Use a magic string to avoid possible interactions with file system
281    * objects.  Prepend '/' to short-circuit libltdl's search of
282    * $shlibpath_var (e.g. PATH, LD_LIBRARY_PATH, or similar).
283    */
284   module = lt_dlopen ("/libltdl_dlloader_api_test_first");
286   if (!module)
287     {
288       printf ("lt_dlopen failed: %s\n", lt_dlerror ());
289       err = 1;
290       goto cleanup;
291     }
293   symbol = (module_func *) lt_dlsym (module, "symbol");
295   if (!symbol)
296     {
297       printf ("lt_dlsym failed: %s\n", lt_dlerror ());
298       err = 1;
299       goto cleanup;
300     }
302   printf ("result: %s\n", symbol ());
304   lt_dlclose (module);
305   module = lt_dlopen ("./module.la");
307   if (!module)
308     {
309       printf ("lt_dlopen failed: %s\n", lt_dlerror ());
310       err = 1;
311       goto cleanup;
312     }
314   symbol = (module_func *) lt_dlsym (module, "symbol");
316   if (!symbol)
317     {
318       printf ("lt_dlsym failed: %s\n", lt_dlerror ());
319       err = 1;
320       goto cleanup;
321     }
323   printf ("result: %s\n", symbol ());
325   lt_dlclose (module);
327   /* Use a magic string to avoid possible interactions with file system
328    * objects.  Prepend '/' to short-circuit libltdl's search of
329    * $shlibpath_var (e.g. PATH, LD_LIBRARY_PATH, or similar).
330    */
331   module = lt_dlopen ("/libltdl_dlloader_api_test_last");
333   if (!module)
334     {
335       printf ("lt_dlopen failed: %s\n", lt_dlerror ());
336       err = 1;
337       goto cleanup;
338     }
340   symbol = (module_func *) lt_dlsym (module, "symbol");
342   if (!symbol)
343     {
344       printf ("lt_dlsym failed: %s\n", lt_dlerror ());
345       err = 1;
346       goto cleanup;
347     }
349   printf ("result: %s\n", symbol ());
351   if (lt_dlopen ("no-module"))
352     {
353       printf ("lt_dlopen unexpectedly opened \"no-module\"\n");
354       err = 1;
355       goto cleanup;
356     }
358   if (lt_dlloader_remove ("first") != first)
359     {
360       printf ("vtable of first loader has changed\n");
361       err = 1;
362       goto cleanup;
363     }
365   free (first);
367 cleanup:
368   if (module)
369     {
370       lt_dlclose (module);
371     }
372   lt_dlexit ();
373   return err;
377 AT_DATA([module.c],
379 #ifdef __cplusplus
380 extern "C"
381 #endif
382 const char *symbol (void);
383 const char *
384 symbol (void)
386   return "module_symbol";
390 LT_AT_HOST_DATA(expout,
391 [[first_init: first_ctx
392 Found loader "first"
393 last_init: last_ctx
394 Found loader "last"
395 first_open ("/libltdl_dlloader_api_test_first"): first_ctx
396 first_sym (first): first_ctx
397 result: first_symbol
398 first_close (first): first_ctx
399 first_open denies a request
400 result: module_symbol
401 first_open denies a request
402 last_open ("/libltdl_dlloader_api_test_last"): last_ctx
403 last_sym (last): last_ctx
404 result: last_symbol
405 first_open denies a request
406 last_open denies a request
407 first_exit: first_ctx
408 last_close (last): last_ctx
409 last_exit: last_ctx
412 : ${LTDLINCL="-I$abs_top_srcdir/libltdl"}
413 : ${LIBLTDL="$abs_builddir/../libltdl/libltdlc.la"}
415 # Skip this test when called from:
416 #    make distcheck DISTCHECK_CONFIGURE_FLAGS=--disable-ltdl-install
417 AT_CHECK([case $LIBLTDL in #(
418  */_inst/lib/*) test -f "$LIBLTDL" || (exit 77) ;;
419 esac], [], [ignore])
421 CPPFLAGS="$LTDLINCL $CPPFLAGS"
423 AT_CHECK([$LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c module.c],
424          [], [ignore], [ignore])
425 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o module.la ]dnl
426          [-rpath /nowhere -module -avoid-version -no-undefined ]dnl
427          [module.lo],
428          [], [ignore], [ignore])
430 dnl Not possible to override the preopen loader, so skip if not shared.
431 . ./module.la
432 AT_CHECK([test -n "$dlname" || (exit 77)])
434 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c main.c], [], [ignore], [ignore])
435 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o main$EXEEXT ]dnl
436          [main.$OBJEXT -dlopen module.la $LIBLTDL],
437          [], [ignore], [ignore])
439 LT_AT_EXEC_CHECK([./main], [], [expout], [ignore], [])
441 AT_CLEANUP