Using distutils, where the swig interface file is in the src folder

I have setup.py that looks something like this:

from setuptools import setup, Extension import glob sources = glob.glob('src/*.cpp') + glob.glob('src/*.i') # this is ugly, but otherwise I get the wrapper included twice sources = [source for source in sources if '_wrap' not in source] setup( name = 'engine', ext_modules = [ Extension( '_engine', sources = sources, swig_opts = ['-c++'], include_dirs = ['src'] ) ], py_modules = ['engine'] package_dir = {'' : 'src'} ) 

Now this works as long as I run install twice. For the first time, swig creates an engine.py file in the src directory. But it is not copied to the target. The second time I run setup.py, engine.py finds and installs. Is there a way to get this to work for the first time?

+4
source share
1 answer

I agree that this should work out of the box and will consider this a mistake.

One way to get this job is to simply change the order in which things are built. By default, setup.py will first compile python modules and then create any external packages.

You can change the build order by subclassing the default build class, and then ask setup.py use its own build class using the cmdclass parameter.

 from setuptools import setup, Extension from distutils.command.build import build as _build #Define custom build order, so that the python interface module #created by SWIG is staged in build_py. class build(_build): # different order: build_ext *before* build_py sub_commands = [('build_ext', _build.has_ext_modules), ('build_py', _build.has_pure_modules), ('build_clib', _build.has_c_libraries), ('build_scripts', _build.has_scripts), ] setup( name = 'engine', cmdclass = {'build': build }, #Use your own build class ext_modules = [Extension('_engine', sources = sources, swig_opts = ['-c++'], include_dirs = ['src'] )], ... 
+2
source

Source: https://habr.com/ru/post/1491546/


All Articles