Modifications to the module system

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@961 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-12-19 04:38:06 +00:00
commit d66f561cfc
20 changed files with 561 additions and 571 deletions

View file

@ -5,8 +5,8 @@
srcdir = @srcdir@
VPATH = @srcdir@
SRCS = init.c test.c
OBJS = init.o test.o
SRCS = init.c inputmodule.c cppmodule.c cparsemodule.c
OBJS = init.o inputmodule.o cppmodule.o cparsemodule.o
prefix = @prefix@
exec_prefix = @exec_prefix@

View file

@ -0,0 +1,55 @@
/* -----------------------------------------------------------------------------
* cparsemodule.c
*
* This module is responsible for running the SWIG C Parsing module.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
#include "swig.h"
#include "swigconfig.h"
#include "lparse.h"
static const char *usage = "C Parsing options:\n\
";
static
int cparse_init(int argc, char **argv) {
int i;
/* Look for command line options */
for (i = 1; i < argc; i++) {
if (argv[i]) {
if (strcmp(argv[i],"-c++") == 0) {
LParse_cplusplus(1);
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-help") == 0) {
Printf(stderr,"%s",usage);
Swig_mark_arg(i);
}
}
}
return 0;
}
static
DOH *cparse_run(DOH *node) {
String *data;
DOH *result;
data = Getattr(node,"data");
if (!data) {
Printf(stderr,"SWIG: cparse error. no data.\n");
Swig_exit(1);
}
Seek(data,0, SEEK_SET);
result = LParse_parse(data);
Setattr(result,"last",node);
return result;
}
void cparsemodule() {
Swig_register_module("cparse","swig:preprocess", cparse_init, cparse_run);
}

View file

@ -0,0 +1,92 @@
/* -----------------------------------------------------------------------------
* cppmodule.c
*
* This module is responsible for running the SWIG preprocessor module.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
#include "swig.h"
#include "swigconfig.h"
#include "preprocessor.h"
static int cpp_only = 0;
static const char *usage = "Preprocessor options:\n\
-E - Run the preprocessor only\n\
-Dmacro - Define a new macro\n\
";
static
int preprocessor_init(int argc, char **argv) {
int i;
/* Look for command line options */
for (i = 1; i < argc; i++) {
if (argv[i]) {
if (strcmp(argv[i],"-E") == 0) {
cpp_only = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-c++") == 0) {
Preprocessor_define("__cplusplus 1", 0);
Swig_mark_arg(i);
} else if (strncmp(argv[i],"-D",2) == 0) {
DOH *d = NewString(argv[i]+2);
Replace(d,"="," ", DOH_REPLACE_ANY | DOH_REPLACE_FIRST);
Preprocessor_define(d,0);
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-includeall") == 0) {
Preprocessor_include_all(1);
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-help") == 0) {
Printf(stderr,"%s",usage);
Swig_mark_arg(i);
}
}
}
return 0;
}
static
DOH *preprocessor_run(DOH *node) {
String *cpps, *data;
DOH *result;
data = Getattr(node,"data");
if (!data) {
Printf(stderr,"SWIG: preprocessor error. no data.\n");
Swig_exit(1);
}
Preprocessor_define("SWIG 1", 0);
Preprocessor_define("__STDC__ 1", 0);
#ifdef MACSWIG
Preprocessor_define("SWIGMAC 1", 0);
#endif
#ifdef SWIGWIN32
Preprocessor_define("SWIGWIN32 1", 0);
#endif
Seek(data,0, SEEK_SET);
cpps = Preprocessor_parse(data);
if (cpp_only) {
Printf(stdout,"%s", cpps);
return 0;
}
result = NewHash();
Settag(result,"swig:preprocess");
Setfile(cpps,Getfile(data));
Setline(cpps,Getline(data));
Seek(cpps,0,SEEK_SET);
Setattr(result,"data",cpps);
Setattr(result,"last",node);
return result;
}
void preprocessormodule() {
Swig_register_module("preprocessor","swig:input", preprocessor_init, preprocessor_run);
Preprocessor_init();
}

View file

@ -11,12 +11,16 @@
#include "swig.h"
extern void testmodule();
extern void pythonmodule();
extern void inputmodule();
extern void preprocessormodule();
extern void cparsemodule();
extern void swig11module();
static void (*modules[])(void) = {
testmodule,
pythonmodule,
inputmodule,
preprocessormodule,
cparsemodule,
swig11module,
0
};
@ -33,7 +37,16 @@ void init_modules() {
}
}
/* This array contains the names of modules that should be loaded by default */
static char *default_modules[] = {
"input",
"preprocessor",
"cparse",
0
};
int main(int argc, char **argv) {
init_modules();
return Swig_main(argc, argv);
return Swig_main(argc, argv, default_modules);
}

View file

@ -0,0 +1,186 @@
/* -----------------------------------------------------------------------------
* inputmodule.c
*
* This module is responsible for reading input files and setting up all
* of the proper search paths.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
#include "swig.h"
#include "swigconfig.h"
#ifndef SWIG_LIB
#define SWIG_LIB "./swiglib"
#endif
static int checkout = 0;
static List *includedirs;
static List *libfiles;
static String *outfile = 0;
static int debug_path = 0;
static int debug_input = 0;
static const char *usage = "File options:\n\
-I<dir> - Look for SWIG files in <dir>\n\
-l<ifile> - Include SWIG library file.\n\
-o outfile - Set name of the output file.\n\
-co - Check file out of SWIG library.\n\
";
static
int input_init(int argc, char **argv) {
char *c;
int i;
/* Directories included with the -I option */
includedirs = NewList();
/* Files added with the -l option */
libfiles = NewList();
/* Look for command line options */
for (i = 1; i < argc; i++) {
if (argv[i]) {
if (strncmp(argv[i],"-I",2) == 0) {
Append(includedirs,argv[i]+2);
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-debug_path") == 0) {
debug_path = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-debug_input") == 0) {
debug_input = 1;
Swig_mark_arg(i);
} else if (strncmp(argv[i],"-l",2) == 0) {
Append(libfiles, argv[i]+2);
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-co") == 0) {
checkout = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-o") == 0) {
Swig_mark_arg(i);
if (argv[i+1]) {
outfile = NewString(argv[i+1]);
Swig_mark_arg(i+1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i],"-help") == 0) {
Printf(stderr,"%s",usage);
Swig_mark_arg(i);
}
}
}
/* Set the location of the SWIG library */
if (!(c = getenv("SWIG_LIB"))) {
Append(includedirs,SWIG_LIB);
} else {
Append(includedirs,c);
}
return 0;
}
static
DOH *input_run(DOH *node) {
int i;
String *infile;
FILE *f;
String *input;
DOH *result;
String *swiglib;
String *lang_config;
infile = Getname(node);
/* Register all of the include directories */
swiglib = Swig_swiglib_get();
for (i = Len(includedirs); i > 0; i--) {
if (swiglib) {
String *l = NewStringf("%s%s%s", Getitem(includedirs,i-1),SWIG_FILE_DELIMETER,swiglib);
Swig_add_directory(l);
}
Swig_add_directory(Getitem(includedirs,i-1));
}
if (debug_path) {
List *l;
Printf(stdout,"SWIG search path:\n");
l = Swig_search_path();
if (l) {
String *s;
for (s = Firstitem(l); s; s = Nextitem(l)) {
Printf(stdout," %s\n", s);
}
}
}
/* user has requested to simply check out a file */
if (checkout) {
String *outfilename;
String *s;
outfilename = outfile ? outfile : infile;
/* Grab the file */
s = Swig_include(infile);
if (!s) {
Printf(stderr,"Unable to locate '%s' in the SWIG library.\n", infile);
Swig_exit(EXIT_FAILURE);
} else {
File *f = NewFile(outfilename,"r");
if (f) {
Delete(f);
Printf(stderr,"File '%s' already exists. Checkout aborted.\n", outfilename);
} else {
f = NewFile(outfilename,"w");
if (!f) {
Printf(stderr,"Unable to create file '%s'\n", outfilename);
} else {
Printf(stderr,"'%s' checked out from the SWIG library.\n", infile);
Dump(s,f);
Delete(f);
}
}
}
return 0;
}
/* Try to find input files */
f = Swig_open(infile);
if (!f) {
Printf(stderr,"Unable to find '%s'\n", infile);
Swig_exit (EXIT_FAILURE);
}
fclose(f);
input = NewString("%include \"swig.swg\"\n");
lang_config = Swig_get_config_file();
if (lang_config) {
Printf(input,"\n%%include \"%s\"\n", lang_config);
}
Printf(input,"\n%%include \"%s\"\n", infile);
for (i = 0; i < Len(libfiles); i++) {
Printf(input,"\n%%include \"%s\"\n", Getitem(libfiles,i));
}
result = NewHash();
Settag(result,"swig:input");
Setattr(result,"name", infile);
Setattr(result,"path", Getfile(input));
Setattr(result,"outfile", outfile);
Setattr(result,"data",input);
Setattr(result,"last",node);
if (debug_input) {
Printf(stdout,"::: inputmodule :::\n");
Printf(stdout,"%s\n", input);
}
return result;
}
void inputmodule() {
Swig_register_module("input","swig:initial", input_init, input_run);
}