forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPythonAppInit.cxx
304 lines (267 loc) · 9.48 KB
/
vtkPythonAppInit.cxx
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPythonAppInit.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/* Minimal main program -- everything is loaded from the library */
#include "vtkPython.h"
#ifdef VTK_COMPILED_USING_MPI
# include <mpi.h>
# include "vtkMPIController.h"
#endif // VTK_COMPILED_USING_MPI
#include "vtkVersion.h"
#include "vtkPythonAppInitConfigure.h"
#include "vtkpythonmodules.h"
// Include the instantiators, this will be an empty file when instantiators
// are not turned on. It will contain all wrapped modules otherwise.
// Commenting out for now, as in my tests it made things slower.
//#include "vtkInstantiators.h"
#include <sys/stat.h>
#include <string>
#include <vtksys/SystemTools.hxx>
#ifdef VTK_COMPILED_USING_MPI
class vtkMPICleanup {
public:
vtkMPICleanup()
{
this->Controller = 0;
}
void Initialize(int* argc, char ***argv)
{
MPI_Init(argc, argv);
this->Controller = vtkMPIController::New();
this->Controller->Initialize(argc, argv, 1);
vtkMultiProcessController::SetGlobalController(this->Controller);
}
~vtkMPICleanup()
{
if ( this->Controller )
{
this->Controller->Finalize();
this->Controller->Delete();
}
}
private:
vtkMPIController *Controller;
};
static vtkMPICleanup VTKMPICleanup;
#endif // VTK_COMPILED_USING_MPI
extern "C" {
extern DL_IMPORT(int) Py_Main(int, char **);
}
static void vtkPythonAppInitEnableMSVCDebugHook();
static void vtkPythonAppInitPrependPath(const char* self_dir);
/* The maximum length of a file name. */
#if defined(PATH_MAX)
# define VTK_PYTHON_MAXPATH PATH_MAX
#elif defined(MAXPATHLEN)
# define VTK_PYTHON_MAXPATH MAXPATHLEN
#else
# define VTK_PYTHON_MAXPATH 16384
#endif
/* Python major.minor version string. */
#define VTK_PYTHON_TO_STRING(x) VTK_PYTHON_TO_STRING0(x)
#define VTK_PYTHON_TO_STRING0(x) VTK_PYTHON_TO_STRING1(x)
#define VTK_PYTHON_TO_STRING1(x) #x
#define VTK_PYTHON_VERSION VTK_PYTHON_TO_STRING(PY_MAJOR_VERSION.PY_MINOR_VERSION)
int main(int argc, char **argv)
{
vtkPythonAppInitEnableMSVCDebugHook();
#ifdef VTK_COMPILED_USING_MPI
VTKMPICleanup.Initialize(&argc, &argv);
#endif // VTK_COMPILED_USING_MPI
int displayVersion = 0;
if ( argc > 1 )
{
int cc;
for ( cc = 1; cc < argc; cc ++ )
{
if ( strcmp(argv[cc], "-V") == 0 )
{
displayVersion = 1;
break;
}
}
}
else
{
displayVersion = 1;
}
if ( displayVersion )
{
cout << vtkVersion::GetVTKSourceVersion() << endl;
}
// The following code will hack in the path for running VTK/Python
// from the build tree. Do not try this at home. We are
// professionals.
// Set the program name, so that we can ask python to provide us
// full path. We need to collapse the path name to aid relative
// path computation for the VTK python module installation.
static char argv0[VTK_PYTHON_MAXPATH];
std::string av0 = vtksys::SystemTools::CollapseFullPath(argv[0]);
strcpy(argv0, av0.c_str());
Py_SetProgramName(argv0);
// This function is generated, and will register any static Python modules for VTK
// This needs to be done *before* Py_Initialize().
CMakeLoadAllPythonModules();
// Initialize interpreter.
Py_Initialize();
// Initialize python thread support. This function should first be
// called from the main thread, after Py_Initialize.
#ifndef VTK_NO_PYTHON_THREADS
PyEval_InitThreads();
#endif
// Compute the directory containing this executable. The python
// sys.executable variable contains the full path to the interpreter
// executable.
char tmpExe[] = "executable";
PyObject* executable = PySys_GetObject(tmpExe);
if(const char* exe_str = PyString_AsString(executable))
{
// Use the executable location to try to set sys.path to include
// the VTK python modules.
std::string self_dir = vtksys::SystemTools::GetFilenamePath(exe_str);
vtkPythonAppInitPrependPath(self_dir.c_str());
}
// Ok, all done, now enter python main.
return Py_Main(argc, argv);
}
// For a DEBUG build on MSVC, add a hook to prevent error dialogs when
// being run from DART.
#if defined(_MSC_VER) && defined(_DEBUG)
# include <crtdbg.h>
static int vtkPythonAppInitDebugReport(int, char* message, int*)
{
fprintf(stderr, message);
exit(1);
}
void vtkPythonAppInitEnableMSVCDebugHook()
{
if(getenv("DART_TEST_FROM_DART") ||
getenv("DASHBOARD_TEST_FROM_CTEST"))
{
_CrtSetReportHook(vtkPythonAppInitDebugReport);
}
}
#else
void vtkPythonAppInitEnableMSVCDebugHook()
{
}
#endif
//----------------------------------------------------------------------------
static void vtkPythonAppInitPrependPythonPath(const char* dir)
{
// Convert slashes for this platform.
std::string out_dir = dir;
#if defined(_WIN32) && !defined(__CYGWIN__)
for(std::string::size_type i = 0; i < out_dir.length(); ++i)
{
if(out_dir[i] == '/')
{
out_dir[i] = '\\';
}
}
#endif
// Append the path to the python sys.path object.
char tmpPath[] = "path";
PyObject* path = PySys_GetObject(tmpPath);
PyObject* newpath;
newpath = PyString_FromString(out_dir.c_str());
PyList_Insert(path, 0, newpath);
Py_DECREF(newpath);
}
//----------------------------------------------------------------------------
static void vtkPythonAppInitPrependPath(const char* self_dir)
{
// Try to put the VTK python module location in sys.path.
const char* build_dirs[] = {
"/../Wrapping/Python",
"/../VTK/Wrapping/Python",
0
};
int found_vtk = 0;
for (const char** build_dir = build_dirs; *build_dir && !found_vtk; ++build_dir)
{
std::string package_dir = self_dir;
#if defined(CMAKE_INTDIR)
package_dir += "/..";
#endif
package_dir += (*build_dir);
package_dir = vtksys::SystemTools::CollapseFullPath(package_dir.c_str());
// We try to locate the directory containing vtk python module files.
std::string vtk_module_dir = package_dir + "/vtk";
if(vtksys::SystemTools::FileIsDirectory(vtk_module_dir.c_str()))
{
// This executable is running from the build tree. Prepend the
// library directory and package directory to the search path.
vtkPythonAppInitPrependPythonPath(package_dir.c_str());
vtkPythonAppInitPrependPythonPath(VTK_PYTHON_LIBRARY_DIR);
found_vtk = 1;
}
}
if (!found_vtk)
{
// This executable is running from an install tree. Check for
// possible VTK python module locations. See
// http://python.org/doc/2.4.1/inst/alt-install-windows.html for
// information about possible install locations. If the user
// changes the prefix to something other than VTK's prefix or
// python's native prefix then he/she will have to get the
// packages in sys.path himself/herself.
const char* inst_dirs[] = {
"/lib/python" VTK_PYTHON_VERSION "/site-packages/vtk", // UNIX --prefix
"/python" VTK_PYTHON_VERSION "/site-packages/vtk", // UNIX + Forwarding exe --prefix
"/../Library/Python/" VTK_PYTHON_VERSION "/site-packages/vtk", // Apple + Forwarding exe
"/lib/python/vtk", // UNIX --home
"/Lib/site-packages/vtk", "/Lib/vtk", // Windows
"/site-packages/vtk", "/vtk", // Windows
0
};
std::string prefix = vtksys::SystemTools::GetFilenamePath(self_dir);
for(const char** dir = inst_dirs; *dir; ++dir)
{
std::string package_dir = prefix;
package_dir += *dir;
package_dir = vtksys::SystemTools::CollapseFullPath(package_dir.c_str());
if(vtksys::SystemTools::FileIsDirectory(package_dir.c_str()))
{
// We found the modules. Add the location to sys.path, but
// without the "/vtk" suffix.
std::string path_dir =
vtksys::SystemTools::GetFilenamePath(package_dir);
vtkPythonAppInitPrependPythonPath(path_dir.c_str());
break;
}
}
// This executable does not actually link to the python wrapper
// libraries, though it probably should now that the stub-modules
// are separated from them. Since it does not we have to make
// sure the wrapper libraries can be found by the dynamic loader
// when the stub-modules are loaded. On UNIX this executable must
// be running in an environment where the main VTK libraries (to
// which this executable does link) have been found, so the
// wrapper libraries will also be found. On Windows this
// executable may have simply found its .dll files next to itself
// so the wrapper libraries may not be found when the wrapper
// modules are loaded. Solve this problem by adding this
// executable's location to the system PATH variable. Note that
// this need only be done for an installed VTK because in the
// build tree the wrapper modules are in the same directory as the
// wrapper libraries.
#if defined(_WIN32)
static char system_path[(VTK_PYTHON_MAXPATH+1)*10] = "PATH=";
strcat(system_path, self_dir);
if(char* oldpath = getenv("PATH"))
{
strcat(system_path, ";");
strcat(system_path, oldpath);
}
putenv(system_path);
#endif
}
}