]> Creatis software - gdcm.git/blob - gdcmPython/SetupOldies/WrapSwig.py
Should avoid warnings
[gdcm.git] / gdcmPython / SetupOldies / 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=os.path.splitext(source)[0]
71                                 swig_shadow=swig_shadow + '.py'
72                                 # On win32 swig places the shadow classes in the directory
73                                 # where it was invoked. This is to be opposed to posix where
74                                 # swig places the shadow classes aside the C++ wrapping code
75                                 #(the target in our context).
76                                 if(os.name=='posix'):
77                                         infile=os.path.join(os.path.dirname(source),swig_shadow)
78                                 else:
79                                         infile=swig_shadow
80                                 if os.path.isfile(infile):
81                                         outfile=[distutil.build_lib,distutil.distribution.get_name()]
82 #                                       outfile.append(swig_shadow)
83                                         outfile.append(os.path.basename(swig_shadow))
84                                         outfile=apply(os.path.join,outfile)
85                                         distutil.copy_file(infile,outfile,preserve_mode=0)
86                                 else:
87                                         distutil.announce("Warning: swig shadow classes not copied")
88                         ## End of modification
89
90                 return new_sources
91
92 class SwigExtension(ExtensionWrap):
93         """
94         This class extends basic distutils Extension class,adding two keyword
95         arguments :
96                 * swig_cpp,which triggers -c++ mode when swigging
97                 * swig_include,which specifies -I flag when swigging
98         This class is meant to be build with mybuild_ext distutils command.
99         """
100         def __init__(self,swig_include=None,swig_cpp=None,**args):
101                 ExtensionWrap.__init__(self,SwigWrapper(),**args)
102
103                 assert((swig_include==None or type(swig_include) is ListType),
104                                   "swig_include must be a list of strings")
105
106                 self.swig_include=swig_include or []
107                 self.swig_cpp=swig_cpp
108