1 from distutilsWrapping import *
2 from types import ListType
5 class SwigWrapper(Wrapper):
7 This distutils command is meant to be used with MyExtension extension,which
8 defines a swig_include attribute.
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.
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
26 ## Modified lines(compared to buil_exts.wig_sources original method)
28 target_ext='_wrap.cpp'
31 ## End of modification
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]
42 new_sources.append(source)
47 swig=distutil.find_swig()
49 ## Modified lines(compared to buil_exts.wig_sources original method)
50 swig_cmd=[swig,"-python"]
52 swig_cmd.append("-c++")
54 if extWrap.swig_include:
55 for pth in extWrap.swig_include:
56 swig_cmd.append("-I%s"%pth)
57 ## End of modification
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).
67 # Generate the full pathname of the shadow classes file
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).
77 infile=os.path.join(os.path.dirname(source),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)
87 distutil.announce("Warning: swig shadow classes not copied")
88 ## End of modification
92 class SwigExtension(ExtensionWrap):
94 This class extends basic distutils Extension class,adding two keyword
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.
100 def __init__(self,swig_include=None,swig_cpp=None,**args):
101 ExtensionWrap.__init__(self,SwigWrapper(),**args)
103 assert((swig_include==None or type(swig_include) is ListType),
104 "swig_include must be a list of strings")
106 self.swig_include=swig_include or []
107 self.swig_cpp=swig_cpp