1 from distutils.command.build_ext import build_ext
2 from distutils.core import Extension
3 from distutils.file_util import copy_file
4 from types import ListType
8 class build_extWrap(build_ext):
10 This distutils command is meant to be used with all wrapper defined for
12 To realize it,we can't have command-line parameters
14 def build_extension(self,ext):
15 # command-line arguments prevail over extension arguments
16 # but if no command-line argument is defined,extension argument is
19 build_ext.build_extension(self,ext)
23 fullname = self.get_ext_fullname(ext.name)
24 modpath = string.split(fullname, '.')
25 package = string.join(modpath[0:-1], '.')
28 # ignore build-lib -- put the compiled extension into
29 # the source tree along with pure Python modules
30 build_py = self.get_finalized_command('build_py')
31 package_dir = build_py.get_package_dir(package)
36 srcLib=os.path.join(self.build_temp,base+".lib")
37 dstLib=os.path.join(dstLib,package)
39 copy_file(srcLib,dstLib)
41 def swig_sources(self,sources):
42 """Walk the list of source files in 'sources',looking for SWIG
43 interface(.i) files. Run SWIG on all that are found,and
44 return a modified 'sources' list with SWIG source files replaced
45 by the generated C(or C++) files.
48 return(self.__ext.wrapper.WrapSources(self,self.__ext,sources))
51 self.announce("Warning: wrapping error")
55 def WrapSources(self,distutil,extWrap,sources):
58 class ExtensionWrap(Extension):
60 This class extends basic distutils Extension class,adding two keyword
62 * swig_cpp,which triggers -c++ mode when swigging
63 * swig_include,which specifies -I flag when swigging
64 This class is meant to be build with mybuild_ext distutils command.
66 def __init__(self,wrapper=None,**args):
67 Extension.__init__(self,**args)
72 Example of use of these classes in distutils setup method :
74 from Transfert.tcDistUtils import mybuild_ext,MyExtension
77 description="blah blah blah",
79 author_email="i.hate@spam.com",
80 url="http://www.fakeurl.com",
82 cmdclass={'build_ext':build_extWrap},# redirects default build_ext
83 ext_modules=[ExtensionWrap(name="src/xxx,# instance of our Extension class
84 sources=["src/xxx.cpp",
86 include_dirs=["/usr/include/python2.1",
88 libraries=["vtkGraphics"],
93 and then run "python setup.py build"...