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:
parent
650baa6f2b
commit
3ee7a76ef0
2 changed files with 54 additions and 0 deletions
|
|
@ -22,6 +22,15 @@ int test_file(DOH *node, void *clientdata) {
|
||||||
return 0;
|
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) {
|
int test_module(DOH *node, void *clientdata) {
|
||||||
Printf(stdout,"::: Module\n");
|
Printf(stdout,"::: Module\n");
|
||||||
Printf(stdout," name = '%s'\n", Getname(node));
|
Printf(stdout," name = '%s'\n", Getname(node));
|
||||||
|
|
@ -172,6 +181,7 @@ int test_access(DOH *node, void *clientdata) {
|
||||||
static SwigRule rules[] = {
|
static SwigRule rules[] = {
|
||||||
{ "file", test_file},
|
{ "file", test_file},
|
||||||
{ "module", test_module},
|
{ "module", test_module},
|
||||||
|
{ "scope", test_scope},
|
||||||
{ "insert", test_insert},
|
{ "insert", test_insert},
|
||||||
{ "pragma", test_pragma},
|
{ "pragma", test_pragma},
|
||||||
{ "typemap", test_typemap},
|
{ "typemap", test_typemap},
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,10 @@
|
||||||
|
|
||||||
#include "swig.h"
|
#include "swig.h"
|
||||||
|
|
||||||
|
#ifdef DYNAMIC_MODULES
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
static char cvsroot[] = "$Header$";
|
static char cvsroot[] = "$Header$";
|
||||||
|
|
||||||
struct Module {
|
struct Module {
|
||||||
|
|
@ -65,7 +69,9 @@ Swig_register_module(const String_or_char *modname, const String_or_char *startt
|
||||||
Module *
|
Module *
|
||||||
Swig_load_module(const String_or_char *modname) {
|
Swig_load_module(const String_or_char *modname) {
|
||||||
Module *m;
|
Module *m;
|
||||||
|
static int dlcheck = 0;
|
||||||
m = Modules;
|
m = Modules;
|
||||||
|
|
||||||
while (m) {
|
while (m) {
|
||||||
if (Cmp(m->modname, modname) == 0) {
|
if (Cmp(m->modname, modname) == 0) {
|
||||||
/* Create a new entry in the loaded modules table */
|
/* Create a new entry in the loaded modules table */
|
||||||
|
|
@ -81,7 +87,45 @@ Swig_load_module(const String_or_char *modname) {
|
||||||
}
|
}
|
||||||
m = m->next;
|
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;
|
return 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue