4

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

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。有没有办法让这一切都在第一次工作?

4

1 回答 1

2

我确实同意这应该是开箱即用的,并且会认为它是一个错误。

让这个工作的一个选择是简单地改变构建的顺序。默认情况下,setup.py将首先收集 python 模块,然后构建任何外部包。

您可以通过子类化默认build类来更改构建顺序,然后通过选项要求setup.py使用您的自定义build类。cmdclass

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']
                   )],
    ...
于 2014-10-24T21:36:40.797 回答