]> Creatis software - gdcm.git/blob - WrapSwig.py
* Update the testSuite to work perfectly
[gdcm.git] / WrapSwig.py
1 from distutilsWrapping import *
2 from types import ListType
3 import os
4
5 class SwigWrapper(Wrapper):
6         """
7         This distutils command is meant to be used with MyExtension extension,which
8         defines a swig_include attribute.
9         """
10         def WrapSources(self,distutil,extWrap,sources):
11                 """Walk the list of source files in 'sources',looking for SWIG
12                 interface(.i) files.  Run SWIG on all that are found,and
13                 return a modified 'sources' list with SWIG source files replaced
14                 by the generated C(or C++) files.
15                 """
16
17                 new_sources=[]
18                 swig_sources=[]
19                 swig_targets={}
20
21                 # XXX this drops generated C/C++ files into the source tree,which
22                 # is fine for developers who want to distribute the generated
23                 # source -- but there should be an option to put SWIG output in
24                 # the temp dir.
25
26                 ## Modified lines(compared to buil_exts.wig_sources original method)
27                 if extWrap.swig_cpp:
28                         target_ext='_wrap.cpp'
29                 else:
30                         target_ext='_wrap.c'
31                 ## End of modification
32
33                 for source in sources:
34                         (base,ext)=os.path.splitext(source)
35                         if ext==".i":             # SWIG interface file
36                                 new_sources.append(base + target_ext)
37                                 swig_sources.append(source)
38                                 swig_targets[source]=new_sources[-1]
39                         elif ext==".h":
40                                 continue
41                         else:
42                                 new_sources.append(source)
43
44                 if not swig_sources:
45                         return new_sources
46
47                 swig=distutil.find_swig()
48
49                 ## Modified lines(compared to buil_exts.wig_sources original method)
50                 swig_cmd=[swig,"-python"]
51                 if extWrap.swig_cpp:
52                         swig_cmd.append("-c++")
53
54                 if extWrap.swig_include:
55                         for pth in extWrap.swig_include:
56                                 swig_cmd.append("-I%s"%pth)
57                 ## End of modification
58
59                 for source in swig_sources:
60                         target=swig_targets[source]
61                         distutil.announce("swigging %s to %s" %(source,target))
62                         distutil.spawn(swig_cmd + ["-o",target,source])
63                         ## Modified lines(compared to buil_exts.wig_sources original method)
64                         # When swig generated some shadow classes,place them under
65                         # self.build_lib(the build directory for Python source).
66                         if extWrap.swig_cpp:
67                                 # Generate the full pathname of the shadow classes file
68                                 import string
69                                 swig_shadow=string.split(os.path.basename(source),".")[0]
70                                 swig_shadow=swig_shadow + '.py'
71                                 # On win32 swig places the shadow classes in the directory
72                                 # where it was invoked. This is to be opposed to posix where
73                                 # swig places the shadow classes aside the C++ wrapping code
74                                 #(the target in our context).
75                                 if(os.name=='posix'):
76                                         infile=os.path.join(os.path.dirname(source),swig_shadow)
77                                 else:
78                                         infile=swig_shadow
79                                 if os.path.isfile(infile):
80                                         outfile=[distutil.build_lib,distutil.distribution.get_name()]
81                                         outfile.append(swig_shadow)
82                                         outfile=apply(os.path.join,outfile)
83                                         distutil.copy_file(infile,outfile,preserve_mode=0)
84                                 else:
85                                         distutil.announce("Warning: swig shadow classes not copied")
86                         ## End of modification
87
88                 return new_sources
89
90 class SwigExtension(ExtensionWrap):
91         """
92         This class extends basic distutils Extension class,adding two keyword
93         arguments :
94                 * swig_cpp,which triggers -c++ mode when swigging
95                 * swig_include,which specifies -I flag when swigging
96         This class is meant to be build with mybuild_ext distutils command.
97         """
98         def __init__(self,swig_include=None,swig_cpp=None,**args):
99                 ExtensionWrap.__init__(self,SwigWrapper(),**args)
100
101                 assert((swig_include==None or type(swig_include) is ListType),
102                                   "swig_include must be a list of strings")
103
104                 self.swig_include=swig_include or []
105                 self.swig_cpp=swig_cpp
106