【问题标题】:How to include (script-built) libraries with package installation?如何在包安装中包含(脚本构建的)库?
【发布时间】:2018-03-02 04:09:39
【问题描述】:

我正在制作一个 Python 包,其中包含一个 C++ 扩展模块和它需要的其他人的共享库。我希望通过pip 安装所有东西。我当前的setup.py 文件在我使用pip install -e . 时有效,但是当我不使用开发模式时(ei 省略-e)我在导入模块时得到“无法打开共享对象文件”在 Python 中。我相信原因是 setuptools 不认为共享库是我的包的一部分,所以当文件复制到安装目录时,到库的相对链接在安装过程中被破坏。

这是我的setup.py 文件的样子:

from setuptools import setup, Extension, Command
import setuptools.command.develop
import setuptools.command.build_ext
import setuptools.command.install
import distutils.command.build
import subprocess
import sys
import os

# This function downloads and builds the shared-library
def run_clib_install_script():
    build_clib_cmd = ['bash', 'clib_install.sh']
    if subprocess.call(build_clib_cmd) != 0:
        sys.exit("Failed to build C++ dependencies")

# I make a new command that will build the shared-library
class build_clib(Command):
    user_options = []
    def initialize_options(self):
        pass
    def finalize_options(self):
        pass
    def run(self):
        run_clib_install_script()

# I subclass install so that it will call my new command
class install(setuptools.command.install.install):
    def run(self):
        self.run_command('build_clib')
        setuptools.command.install.install.run(self)

# I do the same for build...
class build(distutils.command.build.build):
    sub_commands = [
        ('build_clib', lambda self: True),
        ] + distutils.command.build.build.sub_commands

# ...and the same for develop
class develop(setuptools.command.develop.develop):
    def run(self):
        self.run_command('build_clib')
        setuptools.command.develop.develop.run(self)

# These are my includes...
# note that /clib/include only exists after calling clib_install.sh
cwd = os.path.dirname(os.path.abspath(__file__))
include_dirs = [
    cwd,
    cwd + '/clib/include',
    cwd + '/common',
]

# These are my arguments for the compiler to my shared-library
lib_path = os.path.join(cwd, "clib", "lib")
library_dirs = [lib_path]
link_args = [os.path.join(lib_path, "libclib.so")]

# My extension module gets these arguments so it can link to clib
mygen_module = Extension('mygen',
                    language="c++14",
                    sources=["common/mygen.cpp"],
                    libraries=['clib'],
                    extra_compile_args=['-std=c++14'],
                    include_dirs=include_dirs,
                    library_dirs=library_dirs,
                    extra_link_args=link_args
                        + ['-Wl,-rpath,$ORIGIN/../clib/lib'])

# I use cmdclass to override the default setuptool commands
setup(name='mypack',
      cmdclass = {'install': install,
                  'build_clib': build_clib, 'build': build,
                  'develop': develop},
      packages=['mypack'],
      ext_package='mypack',
      ext_modules=[mygen_module],
      # package_dir={'mypack': '.'},
      # package_data={'mypack': ['docs/*md']},
      include_package_data=True)

我将一些 setuptools 命令子类化,以便在编译扩展之前构建共享库。 clib_install.sh 是一个 bash 脚本,它在本地下载和构建 /clib 中的共享库,创建标头(在 /clib/include 中)和 .so 文件(在 /clib/lib 中)。为了解决链接到共享库依赖项的问题,我使用$ORIGIN/../clib/lib 作为链接参数,这样就不需要clib 的绝对路径。

不幸的是,/clib 目录没有被复制到安装位置。我尝试修改package_data,但它没有复制我的目录。事实上,我什至不知道 pip/setuptools 在调用脚本后对/clib 做了什么,我猜它是在某个临时构建目录中创建的,之后会被删除。我不确定如何将/clib 放到它制作后需要的位置。

【问题讨论】:

    标签: python pip setuptools setup.py python-c-api


    【解决方案1】:
    package_data={
        'mypack': [
            'clib/include/*.h',
            'clib/lib/*.so',
            'docs/*md',
        ]
    },
    

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 2016-06-24
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      相关资源
      最近更新 更多