scilab: generate gateway source with swig

This commit is contained in:
Simon Marchetto 2014-10-17 19:46:29 +02:00
commit 78b3e5bc46

View file

@ -53,9 +53,10 @@ protected:
String *verboseBuildLevel; String *verboseBuildLevel;
String *buildFlagsScript; String *buildFlagsScript;
bool createGatewayGenerator; bool createGatewaySource;
File *gatewayGeneratorFile; File *gatewaySourceFile;
String *gatewayGeneratorCode; String *gatewaySourceWrapperDeclaration;
String *gatewaySourceFunctionTable;
bool createGatewayXML; bool createGatewayXML;
File *gatewayXMLFile; File *gatewayXMLFile;
@ -63,6 +64,7 @@ protected:
String *gatewayID; String *gatewayID;
int primitiveID; int primitiveID;
String *libraryName; String *libraryName;
public: public:
@ -72,20 +74,24 @@ public:
virtual void main(int argc, char *argv[]) { virtual void main(int argc, char *argv[]) {
generateBuilder = true;
sourceFileList = NewList(); sourceFileList = NewList();
cflags = NewList(); cflags = NewList();
ldflags = NewList(); ldflags = NewList();
verboseBuildLevel = NULL; verboseBuildLevel = NULL;
buildFlagsScript = NULL; buildFlagsScript = NULL;
gatewayID = NULL;
createGatewaySource = false;
gatewaySourceWrapperDeclaration = NULL;
gatewaySourceFunctionTable = NULL;
gatewaySourceFile = NULL;
createGatewayXML = false;
gatewayXML = NULL; gatewayXML = NULL;
gatewayXMLFile = NULL; gatewayXMLFile = NULL;
gatewayGeneratorCode = NULL; gatewayID = NULL;
gatewayGeneratorFile = NULL;
libraryName = NULL; libraryName = NULL;
generateBuilder = true;
createGatewayGenerator = false;
createGatewayXML = false;
/* Manage command line arguments */ /* Manage command line arguments */
for (int argIndex = 1; argIndex < argc; argIndex++) { for (int argIndex = 1; argIndex < argc; argIndex++) {
@ -125,6 +131,7 @@ public:
} else if (strcmp(argv[argIndex], "-nobuilder") == 0) { } else if (strcmp(argv[argIndex], "-nobuilder") == 0) {
Swig_mark_arg(argIndex); Swig_mark_arg(argIndex);
generateBuilder = false; generateBuilder = false;
createGatewaySource = true;
} else if (strcmp(argv[argIndex], "-gatewayxml") == 0) { } else if (strcmp(argv[argIndex], "-gatewayxml") == 0) {
Swig_mark_arg(argIndex); Swig_mark_arg(argIndex);
createGatewayXML = true; createGatewayXML = true;
@ -201,9 +208,9 @@ public:
startBuilderCode(outputFilename); startBuilderCode(outputFilename);
} }
// Create gateway generator script if required // Create gateway source if required
if (createGatewayGenerator) { if (createGatewaySource) {
createGatewayGeneratorFile(); createGatewaySourceFile(moduleName);
} }
// Create gateway XML if required // Create gateway XML if required
@ -257,9 +264,8 @@ public:
Dump(variablesCode, beginSection); Dump(variablesCode, beginSection);
Wrapper_pretty_print(initSection, beginSection); Wrapper_pretty_print(initSection, beginSection);
// Save gateway generator script if (createGatewaySource) {
if (createGatewayGenerator) { saveGatewaySourceFile();
saveGatewayGeneratorFile(moduleName);
} }
if (createGatewayXML) { if (createGatewayXML) {
@ -825,6 +831,26 @@ public:
addFunctionToScilab("SWIG_ptr", "SWIG_ptr"); addFunctionToScilab("SWIG_ptr", "SWIG_ptr");
} }
/* -----------------------------------------------------------------------
* addFunctionToScilab()
* Declare a wrapped function in Scilab (builder, gateway, XML, ...)
* ----------------------------------------------------------------------- */
void addFunctionToScilab(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr wrapperFunctionName) {
if (generateBuilder) {
addFunctionInScriptTable(scilabFunctionName, wrapperFunctionName, builderCode);
}
if (gatewaySourceFile) {
addFunctionInGatewaySource(scilabFunctionName, wrapperFunctionName);
}
if (gatewayXMLFile) {
Printf(gatewayXML, "<PRIMITIVE gatewayId=\"%s\" primitiveId=\"%d\" primitiveName=\"%s\"/>\n", gatewayID, primitiveID++, scilabFunctionName);
}
}
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* createBuilderCode() * createBuilderCode()
* ----------------------------------------------------------------------- */ * ----------------------------------------------------------------------- */
@ -900,6 +926,18 @@ public:
Printf(builderCode, "table = ["); Printf(builderCode, "table = [");
} }
/* -----------------------------------------------------------------------
* addFunctionInBuilderCode()
* Add a function wrapper in the function table of generated builder script
* ----------------------------------------------------------------------- */
void addFunctionInScriptTable(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr wrapperFunctionName, String *scriptCode) {
if (++builderFunctionCount % 10 == 0) {
Printf(scriptCode, "];\ntable = [table;");
}
Printf(scriptCode, "\"%s\",\"%s\";", scilabFunctionName, wrapperFunctionName);
}
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* terminateBuilderCode() * terminateBuilderCode()
* ----------------------------------------------------------------------- */ * ----------------------------------------------------------------------- */
@ -976,66 +1014,81 @@ public:
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* createGatewayGenerator() * createGatewaySourceFile()
* Creates a Scilab macro to generate the gateway source (entry point gw_<module>.c) * Creates the gateway entry point source file (entry point gw_<module>.c)
* Used in the context of internal module generation (-internalmodule)
* ----------------------------------------------------------------------- */ * ----------------------------------------------------------------------- */
void createGatewayGeneratorFile() { void createGatewaySourceFile(String *moduleName) {
String *gatewayGeneratorFilename = NewString("generate_gateway.sce"); String *gatewaySourceFilename = NewString("");
gatewayGeneratorFile = NewFile(gatewayGeneratorFilename, "w", SWIG_output_files()); Printf(gatewaySourceFilename, "gw_%s.c", moduleName);
if (!gatewayGeneratorFile) { gatewaySourceFile = NewFile(gatewaySourceFilename, "w", SWIG_output_files());
FileErrorDisplay(gatewayGeneratorFilename); if (!gatewaySourceFile) {
FileErrorDisplay(gatewaySourceFilename);
SWIG_exit(EXIT_FAILURE); SWIG_exit(EXIT_FAILURE);
} }
gatewayGeneratorCode = NewString("table = [");
emitBanner(gatewaySourceFile);
Printv(gatewaySourceFile, "#ifdef __cplusplus\n", NIL);
Printv(gatewaySourceFile, "extern \"C\" {\n", NIL);
Printv(gatewaySourceFile, "#endif\n", NIL);
Printv(gatewaySourceFile, "\n", NIL);
Printv(gatewaySourceFile, "#include <sci_gateway.h>\n", NIL);
Printv(gatewaySourceFile, "#include <api_scilab.h>\n", NIL);
Printv(gatewaySourceFile, "#include <MALLOC.h>\n", NIL);
Printv(gatewaySourceFile, "\n", NIL);
Printv(gatewaySourceFile, "static int direct_gateway(char *fname, void F(void)) { F(); return 0; };\n", NIL);
gatewaySourceWrapperDeclaration = NewString("");
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* saveGatewayGenerator() * addFunctionInGatewaySource()
* Add a function in the gateway entry point source
* ----------------------------------------------------------------------- */ * ----------------------------------------------------------------------- */
void saveGatewayGeneratorFile(String *moduleName) { void addFunctionInGatewaySource(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr wrapperFunctionName) {
Printf(gatewaySourceWrapperDeclaration, "extern Gatefunc %s;\n", wrapperFunctionName);
Printf(gatewayGeneratorCode, "];\n"); if (gatewaySourceFunctionTable == NULL) {
Printv(gatewayGeneratorFile, gatewayGeneratorCode, NIL); gatewaySourceFunctionTable = NewString("static GenericTable Tab[] = {\n");
String *gatewayGenerateCommand = NewStringf("ilib_gen_gateway('gw_%s.c', table);\n", moduleName); Printf(gatewaySourceFunctionTable, " {(Myinterfun)sci_gateway, %s, \"%s\"}\n", wrapperFunctionName, scilabFunctionName);
Printv(gatewayGeneratorFile, gatewayGenerateCommand, NIL); } else
Delete(gatewayGeneratorFile); Printf(gatewaySourceFunctionTable, " ,{(Myinterfun)sci_gateway, %s, \"%s\"}\n", wrapperFunctionName, scilabFunctionName);
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* addFunctionToScilab() * saveGatewaySourceFile()
* Add a function wrapper in Scilab file (builder, XML, ...) * Saves the gateway entry point source file
* ----------------------------------------------------------------------- */ * ----------------------------------------------------------------------- */
void addFunctionToScilab(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr wrapperFunctionName) { void saveGatewaySourceFile() {
if (generateBuilder) { Printv(gatewaySourceFile, gatewaySourceWrapperDeclaration, NIL);
addFunctionInScriptTable(scilabFunctionName, wrapperFunctionName, builderCode); Printf(gatewaySourceFunctionTable, "};\n");
} Printv(gatewaySourceFile, gatewaySourceFunctionTable, NIL);
if (createGatewayGenerator) { Printv(gatewaySourceFile, "\n", NIL);
addFunctionInScriptTable(scilabFunctionName, wrapperFunctionName, gatewayGeneratorCode); Printv(gatewaySourceFile, "int C2F(libtypedef_array_member)()\n", NIL);
} Printv(gatewaySourceFile, "{\n", NIL);
Printv(gatewaySourceFile, " Rhs = Max(0, Rhs);\n", NIL);
Printv(gatewaySourceFile, " if (*(Tab[Fin-1].f) != NULL)\n", NIL);
Printv(gatewaySourceFile, " {\n", NIL);
Printv(gatewaySourceFile, " if(pvApiCtx == NULL)\n", NIL);
Printv(gatewaySourceFile, " {\n", NIL);
Printv(gatewaySourceFile, " pvApiCtx = (StrCtx*)MALLOC(sizeof(StrCtx));\n", NIL);
Printv(gatewaySourceFile, " }\n", NIL);
Printv(gatewaySourceFile, " pvApiCtx->pstName = (char*)Tab[Fin-1].name;\n", NIL);
Printv(gatewaySourceFile, " (*(Tab[Fin-1].f))(Tab[Fin-1].name,Tab[Fin-1].F);\n", NIL);
Printv(gatewaySourceFile, " }\n", NIL);
Printv(gatewaySourceFile, " return 0;\n", NIL);
Printv(gatewaySourceFile, "}\n", NIL);
Printv(gatewaySourceFile, "\n", NIL);
Printv(gatewaySourceFile, "#ifdef __cplusplus\n", NIL);
Printv(gatewaySourceFile, "}\n", NIL);
Printv(gatewaySourceFile, "#endif\n", NIL);
if (gatewayXMLFile) { Delete(gatewaySourceFile);
Printf(gatewayXML, "<PRIMITIVE gatewayId=\"%s\" primitiveId=\"%d\" primitiveName=\"%s\"/>\n", gatewayID, primitiveID++, scilabFunctionName);
}
} }
/* -----------------------------------------------------------------------
* addFunctionInScriptTable()
* Add a function wrapper in a table in a generated script
* This table will be either given in parameter to ilib_build or ilib_gen_gateway,
* called later in the script
* ----------------------------------------------------------------------- */
void addFunctionInScriptTable(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr wrapperFunctionName, String *scriptCode) {
if (++builderFunctionCount % 10 == 0) {
Printf(scriptCode, "];\ntable = [table;");
}
Printf(scriptCode, "\"%s\",\"%s\";", scilabFunctionName, wrapperFunctionName);
}
}; };
extern "C" Language *swig_scilab(void) { extern "C" Language *swig_scilab(void) {