扩展 setuptools 扩展以在 setup.py 中使用 CMake?
问题描述
我正在编写一个链接 C++ 库的 Python 扩展,我正在使用 cmake 来帮助构建过程.这意味着现在,我知道如何捆绑它的唯一方法,我必须先用 cmake 编译它们,然后才能运行 setup.py bdist_wheel.一定有更好的方法.
I'm writing a Python extension that links a C++ library and I'm using cmake to help with the build process. This means that right now, the only way I know how to bundle it, I have to first compile them with cmake before I can run setup.py bdist_wheel. There must be a better way.
我想知道是否有可能(或有人尝试过)在 setup.py ext_modules 构建过程中调用 CMake?我猜有一种方法可以创建某个东西的子类,但我不确定在哪里看.
I was wondering if it's possible (or anybody has tried) to invoke CMake as part of the setup.py ext_modules build process? I'm guessing there is a way to create a subclass of something but I'm not sure where to look.
我使用 CMake 是因为它给了我更多的控制权,可以完全按照我的需要使用复杂的构建步骤构建 c 和 c++ 库扩展.另外,我可以使用 findPythonLibs.cmake 中的 PYTHON_ADD_MODULE() 命令直接使用 cmake 轻松构建 Python 扩展.我只是希望这只是一步.
I'm using CMake because it gives me so much more control for building c and c++ libraries extensions with complex build steps exactly as I want it. Plus, I can easily build Python extensions directly with cmake with the PYTHON_ADD_MODULE() command in the findPythonLibs.cmake. I just wish this was all one step.
推荐答案
您基本上需要做的是覆盖 setup.py
中的 build_ext
命令类和在命令类中注册它.在您的 build_ext
自定义实现中,配置并调用 cmake
来配置和构建扩展模块.不幸的是,官方文档对于如何实现自定义 distutils
命令相当简洁(参见 扩展 Distutils);我发现直接研究命令代码更有帮助.例如,这里是 的源代码build_ext
命令.
What you basically need to do is to override the build_ext
command class in your setup.py
and register it in the command classes. In your custom impl of build_ext
, configure and call cmake
to configure and then build the extension modules. Unfortunately, the official docs are rather laconic about how to implement custom distutils
commands (see Extending Distutils); I find it much more helpful to study the commands code directly. For example, here is the source code for the build_ext
command.
我准备了一个简单的项目,由一个 C 扩展 foo
和一个 python 模块 spam.eggs
组成:
I have prepared a simple project consisting out of a single C extension foo
and a python module spam.eggs
:
用于测试设置的文件
这些只是我为测试设置脚本而编写的一些简单存根.
Filesfortestingthesetup
These are just some simple stubs I wrote to test the setup script.
spam/eggs.py
(仅用于测试库调用):
spam/eggs.py
(only for testing the library calls):
spam/foo.c
:
spam/foo.h
:
CMakeLists.txt
:
设置脚本
这就是魔法发生的地方.当然,还有很大的改进空间 - 如果需要,您可以将其他选项传递给 CMakeExtension
类(有关扩展的更多信息,请参阅 构建 C 和 C++ 扩展),通过覆盖方法 setup.cfg
使 CMake 选项可配置 initialize_options
和 finalize_options
等
Setup script
This is where the magic happens. Of course, there is a lot of room for improvements - you could pass additional options to CMakeExtension
class if you need to (for more info on the extensions, see Building C and C++ Extensions), make the CMake options configurable via setup.cfg
by overriding methods initialize_options
and finalize_options
etc.
测试
构建项目的轮子,安装它.测试库是否安装:
Testing
Build the project's wheel, install it. Test the library is installed:
从 spam.eggs
模块运行包装函数:
Run the wrapper function from spam.eggs
module:
这篇关于扩展 setuptools 扩展以在 setup.py 中使用 CMake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!