使用 SCons 作为 distutils 的构建引擎
我有一个 python 包,其中包含构建扩展所需的一些 C 代码(以及一些重要的构建需求)。我使用 SCons 作为我的构建系统,因为它非常好且灵活。
我正在寻找一种方法来编译我的 python 扩展,并准备好与 distutils 一起分发。我希望用户只需键入 setup.py install 并获得使用 SCons 而不是默认的 distutils 构建引擎编译的扩展。
我想到的一个想法是在 distutils 中重新定义 build_ext 命令,但我找不到它的详细文档。
有什么建议吗?
I have a python package with some C code needed to build an extension (with some non-trivial building needs). I have used SCons as my build system because it's really good and flexible.
I'm looking for a way to compile my python extensions with SCons ready to be distributed with distutils. I want that the user simply types setup.py install and get the extension compiled with SCons instead of the default distutils build engine.
An idea that comes to mind is to redefine build_ext command in distutils, but I can't find extensive documentation for it.
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
enscons 包 似乎旨在执行问题所要求的操作。 此处是使用它构建具有 C 扩展的包的示例。
您可以有一个基本的包结构,如下
所示: 在这个
pyproject.toml
文件中,[tool.enscons]
部分包含许多与 setuptools 熟悉的内容/distutilssetup
函数。从此处复制,setup.py
函数可能包含类似的内容:最后,
SConstruct
文件可能类似于:之后,您应该能够运行
编译 C 扩展并构建轮子,并安装 python 包,或者
构建源代码发行版。
我认为您基本上可以使用
SConstruct
文件中的 SCons 做任何事情。The enscons package seems to be designed to do what the question asked. An example of using it to build a package with C extensions is here.
You could have a basic package structure like:
In this the
pyproject.toml
file could look something like:where the
[tool.enscons]
section contains many things familiar to the setuptools/distutilssetup
functions. Copying from here, thesetup.py
function could contain something like:Finally, the
SConstruct
file could look something like:After this you should be able to run
to compile the C extension and build a wheel, and install the python package, or
to build a source distribution.
I think you can basically do anything you could with SCons in the
SConstruct
file though.请参阅页面: http://www.scons.org/wiki/PythonExtensions
我是使用稍微修改过的版本来构建 python 的pyrex-c 扩展。
See the page: http://www.scons.org/wiki/PythonExtensions
I'm using slightly modified version to build pyrex-c extensions for python.
我使用 scons 生成 setup.py 文件。因此我创建了一个名为 setup.py.in 的模板,并使用 scons 扩展该模板以生成 setup.py。
以下是我的项目的一些链接:
setup.py.in 模板
SConstruct
这会计算一个键、值对的字典,以替换到 setup.py.in 模板中以生成 setup.py。
因此,最终用户做了两件事:
警告:我的 sconstruct 内容有点混乱,因为我之前写过它,但它应该演示这个概念。
如果您学习如何编写正确的 scons 工具,那么可以将其转换为单个目标,例如:
这是一个 scons 工具,它使用 SWIG 生成 Python 包装器:
现在,您可以从 SConstruct 文件生成包装器代码:
如果你修改 foobar.i 它将重新生成 foobar_wrap.cc。一旦有了这个,您就可以编写其他工具来实际执行 python setup.py install ,因此当提供 --pymod 时,它将构建 python 模块。
I use scons to generate a setup.py file. So I created a template called setup.py.in and use scons to expand the template to generate setup.py.
Here are some links to my project that does this:
setup.py.in template
The SConstruct
This computes a dictionary of key, value pairs to substitute into the setup.py.in template to generate setup.py.
So the end user does two things:
Warning: my sconstruct stuff is a bit messy as I wrote it awhile ago, but it should demonstrate the concept.
If you learn how to write proper scons tools, then this can be transformed into a single target, for example:
Here is a scons tool the generates a Python wrapper with SWIG:
Now from your SConstruct file, you can generate the wrapper code:
If you modify foobar.i it will regenerate foobar_wrap.cc. Once you have this, you can write other tools for actually executing python setup.py install for you, so when --pymod is provided it will build the python module.
解决方案是提供从 distutils.cmd.Commmand 派生的自定义 cmdclass,它可以按照您想要的方式构建模块:
The solution is to provide custom cmdclass derived from distutils.cmd.Commmand which builds the module how you want it: