]> Creatis software - gdcm.git/blob - WrapVTK.py
* Modification of setup.py to compile vtk part too. Then, we have 2
[gdcm.git] / WrapVTK.py
1 from distutilsWrapping import *
2 import types
3 import string
4 import os
5
6 class VTKWrapper(Wrapper):
7         """
8         This distutils command is meant to be used with MyExtension extension, which
9         defines a swig_include attribute.
10         """
11         def WrapSources(self,distutil,extWrap,sources):
12                 """
13                 Walk the list of source files in 'sources', looking for VTK
14                 interface (vtk*.cxx) files. Compile vtkWrapPythonInit.
15                 Run vtkWrapPython on all that are found, and
16                 return a modified 'sources' list with SWIG source files replaced
17                 by the generated C (or C++) files.
18                 
19                 FIXME nierk
20                 """
21                 self.__extWrap=extWrap
22
23                 newSources=[]
24                 vtkSources=[]
25                 vtkTargets={}
26
27                 # Wrapping of sources
28                 target_ext='Python.cxx'
29
30                 for source in sources:
31                         (base,ext)=os.path.splitext(source)
32                         fileName=os.path.split(base)
33                         if((ext==".cxx")and(fileName[-1][0:3]=="vtk")):
34                                 newSources.append(source)
35                                 newSources.append(base+target_ext)
36                                 vtkSources.append(base+'.h')
37                                 vtkTargets[base+'.h']=newSources[-1]
38                         else:
39                                 newSources.append(source)
40
41                 # Find vtkWrapPython
42                 wrapper=self.FindvtkWrapPython()
43                 if(not self.__extWrap.vtkHints):
44                         self.__extWrap.vtkHints="toto"
45
46                 wrapCmd=[wrapper]
47                 for source in vtkSources:
48                         target=vtkTargets[source]
49                         distutil.announce("VTK wrapping %s to %s" % (source,target))
50                         distutil.spawn([wrapper,source,self.__extWrap.vtkHints,target])
51
52                 # Compilation of vtkWrapPythonInit
53                 vtkWrapInit=self.__extWrap.vtkModule+"Init"+target_ext
54                 distutil.announce("VTK init wrapping to %s" % vtkWrapInit)
55                 self.WrapInit(vtkSources,vtkWrapInit)
56                 newSources.append(vtkWrapInit)
57
58                 return newSources
59
60         def FindvtkWrapPython(self):
61                 assert(os.path.isfile(self.__extWrap.vtkWrapper),
62                        "Write an heuristic in FindvtkWrapPython")
63                 return(self.__extWrap.vtkWrapper)
64
65         def WrapInit(self,vtkSource,target):
66                 dllName=string.split(self.__extWrap.vtkModule,'.')[-1]
67                 f=open(target,"w")
68
69                 f.write('#include <string.h>\n')
70                 f.write('#include "Python.h"\n\n')
71
72                 for src in vtkSource:
73                         src=os.path.split(src)[-1]
74                         (src,_)=os.path.splitext(src)
75                         f.write('extern "C" { ')
76                         if(os.name!="posix"):
77                                 f.write('__declspec( dllexport ) ')
78                         f.write('PyObject *PyVTKClass_%sNew(char *); }\n'% src)
79
80                 # Lib Init
81                 f.write('\nstatic PyMethodDef Py%s_ClassMethods[] = {\n'% dllName)
82                 f.write('{NULL, NULL}};\n\n')
83
84                 f.write('extern "C" { ')
85                 if(os.name!="posix"):
86                         f.write('__declspec( dllexport ) ')
87                 f.write('void init%s();}\n\n'% dllName)
88
89                 f.write('void init%s()\n{\n'% dllName)
90                 f.write('  PyObject *m, *d, *c;\n\n')
91                 f.write('  static char modulename[] = "%s";\n'% dllName)
92                 f.write('  m = Py_InitModule(modulename, Py%s_ClassMethods);\n'% dllName)
93
94                 f.write('  d = PyModule_GetDict(m);\n')
95                 f.write('  if (!d) Py_FatalError("can''t get dictionary for module %s!");\n\n'% dllName)
96
97                 # New function
98                 for src in vtkSource:
99                         src=os.path.split(src)[-1]
100                         (src,_)=os.path.splitext(src)
101                         f.write('  if ((c = PyVTKClass_%sNew(modulename)))\n'% src)
102                         f.write('    if (-1 == PyDict_SetItemString(d, "%s", c))\n'% src)
103                         f.write('      Py_FatalError("can''t add class %s to dictionary!");\n\n'% src)
104                 f.write('}\n\n')
105
106                 f.close()
107
108 class VTKExtension(ExtensionWrap):
109         """
110         This class extends basic distutils Extension class, adding two keyword
111         arguments :
112                 * swig_cpp, which triggers -c++ mode when swigging
113                 * swig_include, which specifies -I flag when swigging
114         This class is meant to be build with mybuild_ext distutils command.
115         """
116         def __init__(self,name,vtkHints=None,
117                      vtkWrapper=None,**args):
118                 ExtensionWrap.__init__(self,name=name,wrapper=VTKWrapper(),**args)
119
120                 assert(type(name)==types.StringType,"vtk Module must be a string")
121
122                 self.vtkHints=vtkHints
123                 self.vtkModule=name
124                 self.vtkWrapper=vtkWrapper
125