如何在软件包安装中包含(脚本构建的)库?

时间:2017-09-20 23:32:50

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

我正在制作一个包含C ++扩展模块的Python包,以及它需要的其他共享库。我想通过pip安装所有内容。当我使用setup.py时,我当前的pip install -e .文件有效,但当我不使用开发模式时(省略-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放到制作完成后的所需位置。

1 个答案:

答案 0 :(得分:0)

package_data={
    'mypack': [
        'clib/include/*.h',
        'clib/lib/*.so',
        'docs/*md',
    ]
},