如何使用 autotools 构建 Python C 扩展模块

发布于 2024-07-08 00:28:46 字数 229 浏览 7 评论 0原文

大多数可用于构建 Python 扩展模块的文档 使用 distutils,但我想通过使用适当的方法来实现这一点 python autoconf & 改为 automake 宏。

我想知道是否有一个开源项目可以做到这一点 正是这个。 我发现的大多数最终都依赖于 setup.py 文件。 使用这种方法是有效的,但不幸的是最终重建了整个 每当我对模块源文件进行修改时,都会显示源树。

Most of the documentation available for building Python extension modules
uses distutils, but I would like to achieve this by using the appropriate
python autoconf & automake macros instead.

I'd like to know if there is an open source project out there that does
exactly this. Most of the ones I've found end up relying on a setup.py file.
Using that approach works, but unfortunately ends up rebuilding the entire
source tree any time I make a modification to the module source files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筑梦 2024-07-15 00:28:46

假设您有一个项目,其目录名为 src,那么让我们按照以下步骤使用 autotools 构建和打包 python 扩展:

创建 Makefile.am 文件

首先,您需要创建一个 Makefile .am 在项目的根目录中,基本上(但不限于)列出也应该处理的子目录。 你最终会得到这样的结果:

SUBDIRS = src

第二个,在 src 目录中,将保存实际编译 python 扩展的指令。 它看起来像这样:

myextdir = $(pkgpythondir)
myext_PYTHON = file1.py file2.py

pyexec_LTLIBRARIES = _myext.la

_myext_la_SOURCES = myext.cpp
_myext_la_CPPFLAGS = $(PYTHON_CFLAGS)
_myext_la_LDFLAGS = -module -avoid-version -export-symbols-regex initmyext
_myext_la_LIBADD = $(top_builddir)/lib/libhollow.la

EXTRA_DIST = myext.h

编写configure.ac

该文件必须在项目的根目录中创建,并且必须列出项目需要构建的所有库、程序或任何类型的工具,例如编译器、链接器、 像我这样

的懒人通常不会从头开始创建它,我更喜欢使用 autoscan 工具,它会查找您正在使用的东西并生成配置。扫描文件,该文件可用作真实configure.ac的基础。

要通知 automake 您将需要 Python 的东西,您可以将其添加到您的 configure.ac 中:

dnl python checks (you can change the required python version bellow)
AM_PATH_PYTHON(2.7.0)
PY_PREFIX=`$PYTHON -c 'import sys ; print sys.prefix'`
PYTHON_LIBS="-lpython$PYTHON_VERSION"
PYTHON_CFLAGS="-I$PY_PREFIX/include/python$PYTHON_VERSION"
AC_SUBST([PYTHON_LIBS])
AC_SUBST([PYTHON_CFLAGS])

总结

基本上,automake 有一个内置的 -在知道如何处理 python 内容的扩展中,您只需将其添加到您的 configure.ac 文件中,然后在您的 Makefile.am 中利用此功能。

PyGtk 绝对是一个很棒的例子,但它相当大,所以也许你会想检查另一个项目,比如 Guake

Supposing that you have a project with a directory called src, so let's follow the follow steps to get a python extension built and packaged using autotools:

Create the Makefile.am files

First, you need to create one Makefile.am in the root of your project, basically (but not exclusively) listing the subdirectories that should also be processed. You will end up with something like this:

SUBDIRS = src

The second one, inside the src directory will hold the instructions to actually compile your python extension. It will look like this:

myextdir = $(pkgpythondir)
myext_PYTHON = file1.py file2.py

pyexec_LTLIBRARIES = _myext.la

_myext_la_SOURCES = myext.cpp
_myext_la_CPPFLAGS = $(PYTHON_CFLAGS)
_myext_la_LDFLAGS = -module -avoid-version -export-symbols-regex initmyext
_myext_la_LIBADD = $(top_builddir)/lib/libhollow.la

EXTRA_DIST = myext.h

Write the configure.ac

This file must be created in the root directory of the project and must list all libraries, programs or any kind of tool that your project needs to be built, such as a compiler, linker, libraries, etc.

Lazy people, like me, usually don't create it from scratch, I prefer to use the autoscan tool, that looks for things that you are using and generate a configure.scan file that can be used as the basis for your real configure.ac.

To inform automake that you will need python stuff, you can add this to your configure.ac:

dnl python checks (you can change the required python version bellow)
AM_PATH_PYTHON(2.7.0)
PY_PREFIX=`$PYTHON -c 'import sys ; print sys.prefix'`
PYTHON_LIBS="-lpython$PYTHON_VERSION"
PYTHON_CFLAGS="-I$PY_PREFIX/include/python$PYTHON_VERSION"
AC_SUBST([PYTHON_LIBS])
AC_SUBST([PYTHON_CFLAGS])

Wrap up

Basically, automake has a built-in extension that knows how to deal with python stuff, you just need to add it to your configure.ac file and then take the advantage of this feature in your Makefile.am.

PyGtk is definitely an awesome example, but it's pretty big, so maybe you will want to check another project, like Guake

空‖城人不在 2024-07-15 00:28:46

所有 PyGTK 扩展都使用自动工具,因此如果 PyGTK 方面不能为您解决所有问题,那么可能值得查看 PyGTK 源代码. 另外,这是我写的一个更简单的。

All PyGTK extensions use autotools, so if the PyGTK aspects don't kill the whole thing for you, it might be worth having a look at the PyGTK source code. Additionally, here is one I wrote which is more simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文