File: vtkPythonAppInit.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (108 lines) | stat: -rw-r--r-- 3,226 bytes parent folder | download
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

/* Minimal main program -- everything is loaded from the library */

#include "vtkPython.h"
#include "vtkPythonCompatibility.h"

#ifdef VTK_COMPILED_USING_MPI
#include "vtkMPIController.h"
#include <vtk_mpi.h>
#endif // VTK_COMPILED_USING_MPI

#include "vtkBuild.h"
#include "vtkOutputWindow.h"
#include "vtkPythonInterpreter.h"
#include "vtkVersion.h"
#include "vtkpythonmodules.h"
#include <sys/stat.h>
#include <vtksys/SystemTools.hxx>

#include <string>

#ifdef VTK_COMPILED_USING_MPI
class vtkMPICleanup
{
public:
  vtkMPICleanup() { this->Controller = nullptr; }
  void Initialize(int* argc, char*** argv)
  {
    MPI_Init(argc, argv);
    this->Controller = vtkMPIController::New();
    this->Controller->Initialize(argc, argv, 1);
    vtkMultiProcessController::SetGlobalController(this->Controller);
  }
  void Cleanup()
  {
    if (this->Controller)
    {
      this->Controller->Finalize();
      this->Controller->Delete();
      this->Controller = nullptr;
      vtkMultiProcessController::SetGlobalController(nullptr);
    }
  }
  ~vtkMPICleanup() { this->Cleanup(); }

private:
  vtkMPIController* Controller;
};

static vtkMPICleanup VTKMPICleanup;
// AtExitCallback is needed to finalize the MPI controller if the python script
// calls sys.exit() directly.
static void AtExitCallback()
{
  VTKMPICleanup.Cleanup();
}
#endif // VTK_COMPILED_USING_MPI

#if defined(_WIN32) && !defined(__MINGW32__)
int wmain(int argc, wchar_t* wargv[])
#else
int main(int argc, char** argv)
#endif
{
#if defined(_WIN32) && !defined(__MINGW32__)
  vtkWideArgsConverter converter(argc, wargv);
  char** argv = converter.GetArgs();
#endif

#ifdef VTK_COMPILED_USING_MPI
  VTKMPICleanup.Initialize(&argc, &argv);
  vtkPythonInterpreter::AddAtExitCallback(::AtExitCallback);
#endif // VTK_COMPILED_USING_MPI

  /**
   * This function is generated and exposed in vtkpythonmodules.h.
   * This registers any Python modules for VTK for static builds.
   */
  vtkpythonmodules_load();

  // Setup the output window to be vtkOutputWindow, rather than platform
  // specific one. This avoids creating vtkWin32OutputWindow on Windows, for
  // example, which puts all Python errors in a window rather than the terminal
  // as one would expect.
  auto opwindow = vtkOutputWindow::New();
  vtkOutputWindow::SetInstance(opwindow);
  opwindow->Delete();

  // For static builds, help with finding `vtk` packages.
  std::string fullpath;
  std::string error;
  if (argc > 0 && vtksys::SystemTools::FindProgramPath(argv[0], fullpath, error))
  {
    const auto dir = vtksys::SystemTools::GetProgramPath(fullpath);
#if defined(VTK_BUILD_SHARED_LIBS)
    vtkPythonInterpreter::PrependPythonPath(dir.c_str(), "vtkmodules/__init__.py");
#else
    // since there may be other packages not zipped (e.g. mpi4py), we added path to _vtk.zip
    // to the search path as well.
    vtkPythonInterpreter::PrependPythonPath(dir.c_str(), "_vtk.zip", /*add_landmark=*/false);
    vtkPythonInterpreter::PrependPythonPath(dir.c_str(), "_vtk.zip", /*add_landmark=*/true);
#endif
  }

  return vtkPythonInterpreter::PyMain(argc, argv);
}