在src文件夹中的swig接口文件使用distutils [英] Using distutils where swig interface file is in src folder

查看:96
本文介绍了在src文件夹中的swig接口文件使用distutils的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个setup.py,它看起来像这样:

I've got a 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'}
)

现在,只要我运行 install 两次,此方法就可以使用。 swig第一次在src目录中创建engine.py。但是不会复制到目标。第二次运行setup.py文件时,找到并安装了engine.py。有没有办法让所有功能都可以首次使用?

Now this works as long as I run install twice. The first time, swig creates the engine.py in the src directory. But it doesn't get copied to the target. The second time I run the setup.py file, the engine.py gets found and is installed. Is there a way to make it all work the first time?

推荐答案

我确实同意应该立即使用,并会认为这是一个错误。

I do agree that this should work out of the box, and would consider it a bug.

一个可行的选择就是简单地更改构建顺序。默认情况下, setup.py 将首先收集python模块,然后构建任何外部软件包。

One option to get this working is to simply change the order in which things are build. By default, setup.py will first collect the python modules, then build any external packages.

您可以通过将默认的 build 类子类化,然后询问 setup.py 通过 cmdclass 选项使用自定义的 build 类。

You can change the build order by sub-classing the default build class, and then ask setup.py to use your custom build class via the cmdclass option.

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']
                   )],
    ...

这篇关于在src文件夹中的swig接口文件使用distutils的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆