[英]How can i include 3rd party libraries into a python3 manylinux build?
I'm trying to package a C extension for cpython and I'm not sure how to proceed.我正在尝试 package cpython 的 C 扩展,我不确定如何继续。 From what I understand I need to first generate the wheel file with python3 -m build
and then do auditwheel repair dist/my_wheel_file.whl -w dist/
or something to that effect.据我了解,我需要首先使用python3 -m build
生成 wheel 文件,然后执行auditwheel repair dist/my_wheel_file.whl -w dist/
或类似的操作。 when I build the package locally and do auditwheel show dist/my_wheel_file.whl
it says this当我在本地构建 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.
when I run the same command in a manylinux container on a freshly built wheel file I get this output.当我在一个新构建的 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 mount command 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
this is my setup.py file这是我的 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;
}
I was able to hack in the libxdo-dev
headers by including them into the podman command to mount the container.通过将它们包含到 podman 命令中以挂载容器,我能够破解libxdo-dev
标头。 But even if I was to mount the rest of the so files into the containers would that even work?但是,即使我要将 so 文件的 rest 挂载到容器中,这是否可行? or do I need to compile the so files from source in the container?或者我是否需要从容器中的源代码编译 so 文件?
So how do I include the functionality and dependencies of the libxdo-dev package into my python3 C extension?那么如何将 libxdo-dev package 的功能和依赖项包含到我的 python3 C 扩展中呢? I know this is a bit rambly and incoherent at the moment but so is my brain trying to figure all this out.我知道目前这有点漫无目的和不连贯,但我的大脑也在努力弄清楚这一切。 Any help is greatly appreciated!任何帮助是极大的赞赏!
I have solved my issue by running the following command in the container我通过在容器中运行以下命令解决了我的问题
yum install libxdo-devel
I've always had apt the whole time I've been using linux so not having it threw me off balance I guess.在我一直使用 linux 的整个过程中,我一直都有 apt,所以我猜没有它让我失去了平衡。 occams razor prevails again. occams razor 再次获胜。
You can package the third-party libraries to the wheel file via build_ext
.您可以通过build_ext
将第三方库 package 添加到 wheel 文件中。
Check out my setup.py file.查看我的setup.py文件。
I created a custom class to copy all dependent library files to the packaging folder:我创建了一个自定义的 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.