1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include "vtkParseMain.h"
#include "vtkParseSystem.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* this routine creates the init file */
static void CreateInitFile(
const char* libName, int numDepends, char** depends, int numFiles, char** files, FILE* fout)
{
int i;
fprintf(fout, "// Generated by vtkWrapPythonInit (see Wrapping/Tools/vtkWrapPythonInit.c)\n");
fprintf(fout, "\n#include \"vtkPythonUtil.h\"\n");
fprintf(fout, "#include \"vtkSystemIncludes.h\"\n");
fprintf(fout, "#include <cstring>\n");
fprintf(fout,
"\n// Handle compiler warning messages, etc.\n"
"#if defined( _MSC_VER ) && !defined(VTK_DISPLAY_WIN32_WARNINGS)\n"
"#pragma warning ( disable : 4706 )\n"
"#endif // Windows Warnings\n");
fprintf(fout, "\n// Export the module initialization function\n");
fprintf(fout, "extern \"C\" { %s PyObject *PyInit_%s(); }\n", "VTK_ABI_EXPORT", libName);
fprintf(fout, "\n// Each PyVTKAddFile_<name>() is implemented in <name>Python.cxx,\n");
fprintf(fout, "// where <name>Python.cxx is generated by running the vtkWrapPython\n");
fprintf(fout, "// executable on the <name>.h header file.\n");
for (i = 0; i < numFiles; i++)
{
fprintf(fout, "extern \"C\" { void PyVTKAddFile_%s(PyObject *dict); }\n", files[i]);
}
fprintf(fout, "\nstatic PyMethodDef Py%s_Methods[] = {\n", libName);
fprintf(fout, " { nullptr, nullptr, 0, nullptr }\n};\n");
fprintf(fout, "\nstatic PyModuleDef Py%s_Module = {\n", libName);
fprintf(fout, " PyModuleDef_HEAD_INIT,\n");
fprintf(fout, " \"%s\", // m_name\n", libName);
fprintf(fout, " nullptr, // m_doc\n");
fprintf(fout, " 0, // m_size\n");
fprintf(fout, " Py%s_Methods, //m_methods\n", libName);
fprintf(fout, " nullptr, // m_reload\n");
fprintf(fout, " nullptr, // m_traverse\n");
fprintf(fout, " nullptr, // m_clear\n");
fprintf(fout, " nullptr // m_free\n");
fprintf(fout, "};\n");
/* module init function */
fprintf(fout, "\nPyObject *PyInit_%s()\n", libName);
fprintf(fout, "{\n");
fprintf(fout, " PyObject *m = PyModule_Create(&Py%s_Module);\n", libName);
fprintf(fout, " PyObject *d = PyModule_GetDict(m);\n");
fprintf(fout, " if (!d)\n");
fprintf(fout, " {\n");
fprintf(fout, " Py_FatalError(\"can't get dictionary for module %s\");\n", libName);
fprintf(fout, " }\n\n");
/* import all the modules that we depend on */
if (numDepends > 0)
{
fprintf(fout, " const char *depends[%d] = {\n", numDepends);
for (i = 0; i < numDepends; i++)
{
fprintf(fout, " \"%s\",\n", depends[i]);
}
fprintf(fout, " };\n\n");
fprintf(fout, " for (int i = 0; i < %d; i++)\n", numDepends);
fprintf(fout, " {\n");
fprintf(fout, " if (!vtkPythonUtil::ImportModule(depends[i], d))\n");
fprintf(fout, " {\n");
fprintf(fout, " Py_DECREF(m);\n");
fprintf(fout,
" return PyErr_Format(PyExc_ImportError,\n"
" \"Failed to load %s: No module named %%s\",\n"
" depends[i]);\n",
libName);
fprintf(fout, " }\n");
fprintf(fout, " }\n\n");
/* vtkPythonUtil should have been initialized by one of our dependencies */
fprintf(fout, " if (!vtkPythonUtil::IsInitialized())\n");
fprintf(fout, " {\n");
fprintf(fout, " Py_DECREF(m);\n");
fprintf(fout,
" return PyErr_Format(PyExc_ImportError,\n"
" \"Initialization failed for %s, not compatible with %%s\",\n"
" depends[0]);\n",
libName);
fprintf(fout, " }\n\n");
}
else
{
/* if we have no dependencies, ensure wrappers are initialized */
fprintf(fout, " vtkPythonUtil::Initialize();\n\n");
}
for (i = 0; i < numFiles; i++)
{
fprintf(fout, " PyVTKAddFile_%s(d);\n", files[i]);
}
fprintf(fout, "\n");
fprintf(fout, " vtkPythonUtil::AddModule(\"%s\");\n\n", libName);
fprintf(fout, " return m;\n");
fprintf(fout, "}\n");
}
int VTK_PARSE_MAIN(int argc, const char* argv[])
{
FILE* file;
FILE* fout_init;
int numFiles = 0;
int numDepends = 0;
int i;
char libName[250];
char tmpVal[250];
char* files[4000];
char* depends[400];
int doDepends = 0;
if (argc < 3)
{
fprintf(stderr, "Usage: %s input_file init_file\n", argv[0]);
return 1;
}
file = vtkParse_FileOpen(argv[1], "r");
if (!file)
{
fprintf(stderr, "Input file %s could not be opened\n", argv[1]);
return 1;
}
/* read the info from the file */
if (fscanf(file, "%249s", libName) != 1)
{
fprintf(stderr, "Error getting libName\n");
fclose(file);
return 1;
}
/* read in the classes */
while (fscanf(file, "%249s", tmpVal) != EOF)
{
if (strcmp(tmpVal, "DEPENDS") == 0)
{
doDepends = 1;
}
else if (doDepends)
{
depends[numDepends++] = strdup(tmpVal);
}
else
{
files[numFiles++] = strdup(tmpVal);
}
}
/* close the file */
fclose(file);
file = NULL;
fout_init = vtkParse_FileOpen(argv[2], "w");
if (!fout_init)
{
return 1;
}
/* extra functions, types, etc. for the CommonCore module */
if (strcmp(libName, "vtkCommonCore") == 0 || strcmp(libName, "vtkCommonKit") == 0)
{
files[numFiles] = strdup("PyVTKExtras");
numFiles++;
}
CreateInitFile(libName, numDepends, depends, numFiles, files, fout_init);
for (i = 0; i < numFiles; i++)
{
free(files[i]);
}
for (i = 0; i < numDepends; i++)
{
free(depends[i]);
}
fclose(fout_init);
return 0;
}
|