繁体   English   中英

我如何将第 3 方库包含到 python3 manylinux 构建中?

[英]How can i include 3rd party libraries into a python3 manylinux build?

我正在尝试 package cpython 的 C 扩展,我不确定如何继续。 据我了解,我需要首先使用python3 -m build生成 wheel 文件,然后执行auditwheel repair dist/my_wheel_file.whl -w dist/或类似的操作。 当我在本地构建 package 并执行auditwheel show dist/my_wheel_file.whl时,它说这个

winlin-1.0.0-cp310-cp310-linux_x86_64.whl is consistent with the
following platform tag: "linux_x86_64".

The wheel references external versioned symbols in these
system-provided shared libraries: libc.so.6 with versions
{'GLIBC_2.8', 'GLIBC_2.4', 'GLIBC_2.3', 'GLIBC_2.3.4', 'GLIBC_2.2.5',
'GLIBC_2.33', 'GLIBC_2.14', 'GLIBC_2.17'}, libxkbcommon.so.0 with
versions {'V_0.5.0'}

This constrains the platform tag to "manylinux_2_34_x86_64". In order
to achieve a more compatible tag, you would need to recompile a new
wheel from source on a system with earlier versions of these
libraries, such as a recent manylinux image.

当我在一个新构建的 wheel 文件上的 manylinux 容器中运行相同的命令时,我得到了这个 output。

winlin-1.0.0-cp310-cp310-linux_x86_64.whl is consistent with the
following platform tag: "linux_x86_64".

The wheel references external versioned symbols in these
system-provided shared libraries: libc.so.6 with versions
{'GLIBC_2.3.4', 'GLIBC_2.4', 'GLIBC_2.3', 'GLIBC_2.2.5'},
libxkbcommon.so.0 with versions {'V_0.5.0'}

The following external shared libraries are required by the wheel:
{
    "libX11.so.6": "/lib64/libX11.so.6.3.0",
    "libXau.so.6": "/lib64/libXau.so.6.0.0",
    "libXinerama.so.1": null,
    "libXtst.so.6": null,
    "libc.so.6": "/lib64/libc-2.17.so",
    "libdl.so.2": "/lib64/libdl-2.17.so",
    "libpthread.so.0": "/lib64/libpthread-2.17.so",
    "libxcb.so.1": "/lib64/libxcb.so.1.1.0",
    "libxdo.so.3": "/usr/local/lib/libxdo.so.3",
    "libxkbcommon.so.0": null
}

podman 挂载命令

podman run --rm -ti \
       -v "$PWD/winlin:/winlin:rw" \
       -v "/usr/local/include/xdo:/usr/local/include/xdo:ro" \
       -v "/usr/local/lib/xdo:/usr/local/lib/xdo:ro" \
       quay.io/pypa/manylinux2014_x86_64

这是我的 setup.py 文件

from setuptools import setup, Extension
#from distutils.core import setup, Extension
module1 = Extension(
    'winlin',
    define_macros = [('MAJOR_VERSION', '1'),('MINOR_VERSION', '0')],
    include_dirs = ['/usr/local/include/xdo/'],
    libraries = ['xdo'],
    library_dirs = ['/usr/local/lib/xdo/'],
    sources = ['src/winlin.c']
)

setup(
  name = 'winlin',
  version = '1.0',
  description = 'A tool kit for manipulating windows in linux',
  author = 'Kurt Godel',
  author_email = 'corndog@corn.dog',
  url = 'https://google.com',
  long_description = '#TODO',
  ext_modules = [module1]
)
``` C
and my c extension file winlin.c looks like this
```#include <Python.h>
#include <xdo.h>


static PyObject* resize(PyObject* self, PyObject *args){
    int wid, w, h;
    if (!PyArg_ParseTuple(args, "iii", &wid, &w, &h))
        return NULL;

    xdo_t *xdo_inst = xdo_new(NULL);
    xdo_set_window_size(xdo_inst, wid, w, h, 0);
    xdo_free(xdo_inst);
    Py_RETURN_NONE;
    //return Py_BuildValue("s", "it worked, maybe?");
}
static char resize_docs[] = "\
change the size of a given window given its id and a new height and width\n\
";
/*----------------------------------------------------------------------------*/

static PyObject* move(PyObject* self, PyObject *args){
    int wid, x, y;
    if (!PyArg_ParseTuple(args, "iii", &wid, &x, &y))
        return NULL;

    xdo_t *xdo_inst = xdo_new(NULL);
    xdo_move_window(xdo_inst, wid, x, y);
    xdo_free(xdo_inst);
    Py_RETURN_NONE;
}
static char move_docs[] = "\
change the position of a window given its id and a new x and y\n\
";

/*----------------------------------------------------------------------------------*/
static PyMethodDef winlin_funcs[] = {
    {"resize", (PyCFunction)resize, METH_VARARGS, resize_docs},
    {"move", (PyCFunction)move, METH_VARARGS, move_docs},
    {NULL}
};

static char winlin_module_docs[] = "Module used to manipulate windows in linux";

static struct PyModuleDef winlin_module = {
    PyModuleDef_HEAD_INIT,
    "winlin",
    winlin_module_docs,
    -1,
    winlin_funcs
};

PyMODINIT_FUNC
PyInit_winlin(void){
    PyObject* m = PyModule_Create(&winlin_module);
    return m;
}

通过将它们包含到 podman 命令中以挂载容器,我能够破解libxdo-dev标头。 但是,即使我要将 so 文件的 rest 挂载到容器中,这是否可行? 或者我是否需要从容器中的源代码编译 so 文件?

那么如何将 libxdo-dev package 的功能和依赖项包含到我的 python3 C 扩展中呢? 我知道目前这有点漫无目的和不连贯,但我的大脑也在努力弄清楚这一切。 任何帮助是极大的赞赏!

我通过在容器中运行以下命令解决了我的问题

yum install libxdo-devel

在我一直使用 linux 的整个过程中,我一直都有 apt,所以我猜没有它让我失去了平衡。 occams razor 再次获胜。

您可以通过build_ext将第三方库 package 添加到 wheel 文件中。

查看我的setup.py文件。

我创建了一个自定义的 class 将所有依赖库文件复制到打包文件夹中:

class CustomBuildExt(build_ext.build_ext):
        def run(self):
                build_ext.build_ext.run(self)
                dst =  os.path.join(self.build_lib, "barcodeQrSDK")
                copylibs(dbr_lib_dir, dst)
                filelist = os.listdir(self.build_lib)
                for file in filelist:
                    filePath = os.path.join(self.build_lib, file)
                    if not os.path.isdir(file):
                        copylibs(filePath, dst)
                        # delete file for wheel package
                        os.remove(filePath)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM