Added dynamic module loading

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@914 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-10-04 03:37:52 +00:00
commit 3ee7a76ef0
2 changed files with 54 additions and 0 deletions

View file

@ -22,6 +22,15 @@ int test_file(DOH *node, void *clientdata) {
return 0;
}
int test_scope(DOH *node, void *clientdata) {
DOH *c;
Printf(stdout,"::: Scope\n");
Printf(stdout," name = '%s'\n", Getattr(node,"name"));
c = Getchild(node);
Swig_emit_all(c,clientdata);
return 0;
}
int test_module(DOH *node, void *clientdata) {
Printf(stdout,"::: Module\n");
Printf(stdout," name = '%s'\n", Getname(node));
@ -172,6 +181,7 @@ int test_access(DOH *node, void *clientdata) {
static SwigRule rules[] = {
{ "file", test_file},
{ "module", test_module},
{ "scope", test_scope},
{ "insert", test_insert},
{ "pragma", test_pragma},
{ "typemap", test_typemap},

View file

@ -22,6 +22,10 @@
#include "swig.h"
#ifdef DYNAMIC_MODULES
#include <dlfcn.h>
#endif
static char cvsroot[] = "$Header$";
struct Module {
@ -65,7 +69,9 @@ Swig_register_module(const String_or_char *modname, const String_or_char *startt
Module *
Swig_load_module(const String_or_char *modname) {
Module *m;
static int dlcheck = 0;
m = Modules;
while (m) {
if (Cmp(m->modname, modname) == 0) {
/* Create a new entry in the loaded modules table */
@ -81,7 +87,45 @@ Swig_load_module(const String_or_char *modname) {
}
m = m->next;
}
/* Module is not a built-in module. See if we can dynamically load it */
#ifdef DYNAMIC_MODULES
if (dlcheck) return 0;
{
DOH *filename;
void *handle;
void (*init)(void) = 0;
char initfunc[256];
FILE *f;
filename = NewStringf("./swig%s.so", modname);
f = Swig_open(filename);
if (!f) return 0;
fclose(f);
Clear(filename);
Append(filename,Swig_last_file());
sprintf(initfunc,"%smodule",Char(modname));
handle = dlopen(Char(filename), RTLD_NOW | RTLD_GLOBAL);
if (!handle) {
Printf(stdout,"%s\n", dlerror());
return 0;
}
init = (void (*)(void)) dlsym(handle,initfunc);
if (!init) {
Printf(stdout,"Dynamic module %s doesn't define %s()\n", initfunc);
return 0;
}
(*init)(); /* Register function */
dlcheck = 1;
m = Swig_load_module(modname);
dlcheck = 0;
return m;
}
#else
return 0;
#endif
}
/* -----------------------------------------------------------------------------