使用 setuptools 编译扩展模块的困难

difficulties using setuptools to compile extension module

我正在开发一个旨在通过 pip 安装的包(模块?)。该软件包包含一个 C++ 扩展,使用 pybind11 包装,link 用于其他一些库。

我能够编译 link 扩展库 手动 没有问题,但是我无法正确配置我的 setup.py 文件,这样调用 pip3 install my_package.

时安装扩展模块

我一直在关注 this code(由 pybind11 文档 link 编辑)来配置我的 setup.py 文件,该文件包含在下面。

问题描述

为了完整起见,我的包目录如下所示:

my_package
    |-setup.py
    |-my_package
        |-__init__.py
        |-some_code.py
        |-src
            | my_extension.cpp
            | some_header.h

我调用 pip3 install 如下:

pip3 install --user --upgrade --disable-pip-version-check . -vvv

(由于机器无法访问互联网,版本检查被禁用).

我可以从控制台的输出中看到 gcc 似乎被调用了,但我对输出的了解还不够,无法理解它是否被正确调用。 my_extension 似乎 构建没有任何错误:

building 'my_extension' extension
    creating build/temp.linux-x86_64-3.7
    creating build/temp.linux-x86_64-3.7/my_package
    creating build/temp.linux-x86_64-3.7/my_package/src
    gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/local/include/python3.7m -I/home/<my_user>/.local/include/python3.7m -I/usr/include/eigen3 -I/usr/include/python3.7m -c my_package/src/my_extension.cpp -o build/temp.linux-x86_64-3.7/my_package/src/my_extension.o -O3 -fPIC -march=native -DVERSION_INFO="0.0.1" -std=c++14 -fvisibility=hidden

但是,无论出于何种原因,python 解释器都无法找到 my_extension。找到 src 子模块:

>>> from my_package import src
>>> src.__path__
['/home/<my_user>/.local/lib/python3.7/site-packages/my_package/src']

我可以看到它是空的:

>>> ls /home/<my_user>/.local/lib/python3.7/site-packages/my_package/src
__init__.py  __pycache__

也就是说,这里似乎没有 my_extension.so 文件。

问题

我的setup.py

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
import sys
import setuptools

__version__ = '0.0.1'

class get_pybind_include(object):

    def __init__(self, user=False):
        self.user = user

    def __str__(self):
        import pybind11
        return pybind11.get_include(self.user)

ext_modules = [
    Extension(
        'my_extension',
        ['my_package/src/my_extension.cpp'],
        include_dirs=[
            get_pybind_include(),
            get_pybind_include(user=True),
            '/usr/include/eigen3'
        ],
        language='c++'
    ),
]

def has_flag(compiler, flagname):
    import tempfile
    with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f:
        f.write('int main (int argc, char **argv) { return 0; }')
        try:
            compiler.compile([f.name], extra_postargs=[flagname])
        except setuptools.distutils.errors.CompileError:
            return False
    return True


def cpp_flag(compiler):
    if has_flag(compiler, '-std=c++14'):
        return '-std=c++14'
    elif has_flag(compiler, '-std=c++11'):
        return '-std=c++11'
    else:
        raise RuntimeError('Unsupported compiler -- at least C++11 support is needed!')


class BuildExt(build_ext):
    """A custom build extension for adding compiler-specific options."""
    c_opts = {
        'msvc': ['/EHsc'],
        'unix': [],
    }

    if sys.platform == 'darwin':
        c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']

    def build_extensions(self):
        ct = self.compiler.compiler_type
        opts = self.c_opts.get(ct, [])

        # extra compiler options
        opts += [
            '-O3',
            '-fPIC',
            '-march=native'
        ]

        if ct == 'unix':
            opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
            opts.append(cpp_flag(self.compiler))
            if has_flag(self.compiler, '-fvisibility=hidden'):
                opts.append('-fvisibility=hidden')
        elif ct == 'msvc':
            opts.append('/DVERSION_INFO=\"%s\"' % self.distribution.get_version())
        for ext in self.extensions:
            ext.extra_compile_args = opts
        build_ext.build_extensions(self)

setup(
    name='my_package',
    version=__version__,
    author='',
    author_email='',
    url='',
    description='',
    long_description='',
    packages = setuptools.find_packages(),
    ext_modules=ext_modules,
    install_requires=['pybind11>=2.2'],
    cmdclass={'build_ext': BuildExt},
    zip_safe=False,
)

Extension('my_extension', …) 创建一个顶级扩展,您可以使用

导入
import my_extension

要使其成为您的包的子模块,请在包名称前添加:

Extension('my_package.my_extension', …)