【发布时间】:2023-01-30 16:12:11
【问题描述】:
我正在尝试为 cpython 打包一个 C 扩展,但我不确定如何进行。据我了解,我需要先使用python3 -m build 生成轮文件,然后执行auditwheel repair dist/my_wheel_file.whl -w dist/ 或类似的操作。
当我在本地构建包并执行 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 容器中运行相同的命令时,我得到了这个输出。
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;
}
通过将 libxdo-dev 标头包含到 podman 命令中以挂载容器,我能够破解这些标头。
但是,即使我要将其余的 so 文件挂载到容器中,这是否可行?或者我是否需要从容器中的源代码编译 so 文件?
那么如何将 libxdo-dev 包的功能和依赖项包含到我的 python3 C 扩展中呢? 我知道目前这有点漫无目的和不连贯,但我的大脑也在努力弄清楚这一切。任何帮助是极大的赞赏!
【问题讨论】:
标签: python c linux x11 xdotool