扩展 setuptools 扩展以在 setup.py 中使用 CMake?

2025-03-20 08:48:00
admin
原创
41
摘要:问题描述:我正在编写一个链接 C++ 库的 Python 扩展,并使用 cmake 来帮助构建过程。这意味着现在,我知道的唯一捆绑方法是,我必须先用 cmake 编译它们,然后才能运行 setup.py bdist_wheel。一定有更好的方法。我想知道是否有可能(或有人尝试过)在 setup.py ext_...

问题描述:

我正在编写一个链接 C++ 库的 Python 扩展,并使用 cmake 来帮助构建过程。这意味着现在,我知道的唯一捆绑方法是,我必须先用 cmake 编译它们,然后才能运行 setup.py bdist_wheel。一定有更好的方法。

我想知道是否有可能(或有人尝试过)在 setup.py ext_modules 构建过程中调用 CMake?我猜有一种方法可以创建某个东西的子类,但我不确定在哪里可以找到。

我使用 CMake 是因为它让我能够更好地控制构建 c 和 c++ 库扩展,并按照我想要的方式执行复杂的构建步骤。此外,我可以使用 findPythonLibs.cmake 中的 PYTHON_ADD_MODULE() 命令直接使用 cmake 轻松构建 Python 扩展。我只是希望这一切都只需一步即可完成。


解决方案 1:

您基本上需要做的是覆盖build_ext您的命令类setup.py并将其注册到命令类中。在您的自定义实现中build_ext,配置并调用配置cmake,然后构建扩展模块。不幸的是,官方文档对于如何实现自定义命令相当简洁distutils(请参阅扩展 Distutils );我发现直接研究命令代码更有帮助。例如,这是build_ext命令的源代码。

示例项目

我准备了一个由一个 C 扩展foo和一个 python 模块组成的简单项目spam.eggs

so-42585210/
├── spam
│   ├── __init__.py  # empty
│   ├── eggs.py
│   ├── foo.c
│   └── foo.h
├── CMakeLists.txt
└── setup.py

用于测试设置的文件

这些只是我为测试安装脚本而编写的一些简单的存根。

spam/eggs.py(仅用于测试库调用):

from ctypes import cdll
import pathlib


def wrap_bar():
    foo = cdll.LoadLibrary(str(pathlib.Path(__file__).with_name('libfoo.dylib')))
    return foo.bar()

spam/foo.c

#include "foo.h"

int bar() {
    return 42;
}

spam/foo.h

#ifndef __FOO_H__
#define __FOO_H__

int bar();

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 3.10.1)
project(spam)
set(src "spam")
set(foo_src "spam/foo.c")
add_library(foo SHARED ${foo_src})

安装脚本

这就是奇迹发生的地方。当然,还有很大的改进空间 -CMakeExtension如果需要,你可以将其他选项传递给类(有关扩展的更多信息,请参阅构建 C 和 C++ 扩展setup.cfg),通过覆盖方法initialize_options等使 CMake 选项可配置finalize_options

import os
import pathlib

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as build_ext_orig


class CMakeExtension(Extension):

    def __init__(self, name):
        # don't invoke the original build_ext for this special extension
        super().__init__(name, sources=[])


class build_ext(build_ext_orig):

    def run(self):
        for ext in self.extensions:
            self.build_cmake(ext)
        super().run()

    def build_cmake(self, ext):
        cwd = pathlib.Path().absolute()

        # these dirs will be created in build_py, so if you don't have
        # any python sources to bundle, the dirs will be missing
        build_temp = pathlib.Path(self.build_temp)
        build_temp.mkdir(parents=True, exist_ok=True)
        extdir = pathlib.Path(self.get_ext_fullpath(ext.name))
        extdir.mkdir(parents=True, exist_ok=True)

        # example of cmake args
        config = 'Debug' if self.debug else 'Release'
        cmake_args = [
            '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + str(extdir.parent.absolute()),
            '-DCMAKE_BUILD_TYPE=' + config
        ]

        # example of build args
        build_args = [
            '--config', config,
            '--', '-j4'
        ]

        os.chdir(str(build_temp))
        self.spawn(['cmake', str(cwd)] + cmake_args)
        if not self.dry_run:
            self.spawn(['cmake', '--build', '.'] + build_args)
        # Troubleshooting: if fail on line above then delete all possible 
        # temporary CMake files including "CMakeCache.txt" in top level dir.
        os.chdir(str(cwd))


setup(
    name='spam',
    version='0.1',
    packages=['spam'],
    ext_modules=[CMakeExtension('spam/foo')],
    cmdclass={
        'build_ext': build_ext,
    }
)

测试

构建项目的wheel,并安装它。测试库是否安装:

$ pip show -f spam
Name: spam
Version: 0.1
Summary: UNKNOWN
Home-page: UNKNOWN
Author: UNKNOWN
Author-email: UNKNOWN
License: UNKNOWN
Location: /Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages
Requires: 
Files:
  spam-0.1.dist-info/DESCRIPTION.rst
  spam-0.1.dist-info/INSTALLER
  spam-0.1.dist-info/METADATA
  spam-0.1.dist-info/RECORD
  spam-0.1.dist-info/WHEEL
  spam-0.1.dist-info/metadata.json
  spam-0.1.dist-info/top_level.txt
  spam/__init__.py
  spam/__pycache__/__init__.cpython-36.pyc
  spam/__pycache__/eggs.cpython-36.pyc
  spam/eggs.py
  spam/libfoo.dylib

从模块运行包装器函数spam.eggs

$ python -c "from spam import eggs; print(eggs.wrap_bar())"
42

解决方案 2:

我想对此添加我自己的回答,作为对 hoefling 所述内容的一种补充。

谢谢,hoefling,因为你的回答帮助我以同样的方式为我自己的存储库编写安装脚本。

前言

写这个答案的主要动机是试图“拼凑”缺失的部分。OP 没有说明正在开发的 C/C++ Python 模块的性质;我想先明确一点,以下步骤适用于 C/C++ cmake 构建链,该链除了需要放置在脚本目录中的一些通用文件外,还创建多个.dll/.so文件以及预编译的*.pyd/文件。so`.py`

所有这些文件在运行 cmake build 命令后都会直接生效... 有趣。不建议以这种方式构建 setup.py。

因为 setup.py 意味着您的脚本将成为包/库的一部分,并且.dll需要构建的文件必须通过库部分声明,并列出源和包含目录,所以没有直观的方法告诉 setuptools,一次调用所产生的库、脚本和数据文件cmake -bbuild_ext应该放在各自的位置。更糟糕的是,如果您希望此模块由 setuptools 跟踪并完全可卸载,这意味着用户可以卸载它并从系统中删除所有痕迹(如果需要)。

我为其编写 setup.py 的模块是 bpy,它.pyd相当于.so将 blender 构建为 python 模块,如下所述:

https://wiki.blender.org/wiki//User:Ideasman42/BlenderAsPyModule(更好的说明,但现在链接已失效)
http://www.gizmoplex.com/wordpress/compile-blender-as-python-module/(可能说明更差,但似乎仍在线)

您可以在这里查看我在 github 上的存储库:

https://github.com/TylerGubala/blenderpy

这就是我写这个答案的动机,希望能帮助其他试图完成类似任务的人,而不是抛弃他们的 cmake 构建链,或者更糟的是,不得不维护两个独立的构建环境。如果偏离主题,我深表歉意。

那么我该怎么做才能实现这个目标呢?

  1. 使用我自己的类来扩展setuptools.Extension该类,该类不包含源或库属性的条目

  2. 使用我自己的类来扩展setuptools.commands.build_ext.build_ext该类,该类具有执行必要构建步骤的自定义方法(git、svn、cmake、cmake --build)

  3. 使用我自己的类来扩展distutils.command.install_data.install_data该类(呸,distutils...但是似乎没有与 setuputils 等效的类),以便在 setuptools 的记录创建(installed-files.txt)期间标记构建的二进制库,以便

* 这些库将被记录下来,并将使用`pip
uninstall package_name`
* 该命令`py setup.py bdist_wheel`也将在本地运行,并可用于提供源代码的预编译版本
  1. 使用我自己的类来扩展该类setuptools.command.install_lib.install_lib,这将确保构建的库从其生成的构建文件夹移动到 setuptools 期望的文件夹中(在 Windows 上,它会将.dll文件放在 bin/Release 文件夹中,而不是 setuptools 期望的位置)

  2. 使用我自己的类来扩展该类setuptools.command.install_scripts.install_scripts,以便将脚本文件复制到正确的目录(Blender 期望 2.79 或任何目录位于脚本位置)

  3. 执行构建步骤后,将这些文件复制到已知目录中,setuptools 会将该目录复制到我的环境的 site-packages 目录中。此时,其余的 setuptools 和 distutils 类可以接管写入已安装文件.txt 记录,并且可以完全删除!

样本

这是一个示例,或多或少来自我的存储库,但为了更具体的内容的清晰度而进行了修剪(您可以随时前往存储库并亲自查看)

from distutils.command.install_data import install_data
from setuptools import find_packages, setup, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.install_lib import install_lib
from setuptools.command.install_scripts import install_scripts
import struct

BITS = struct.calcsize("P") * 8
PACKAGE_NAME = "example"

class CMakeExtension(Extension):
    """
    An extension to run the cmake build

    This simply overrides the base extension class so that setuptools
    doesn't try to build your sources for you
    """

    def __init__(self, name, sources=[]):

        super().__init__(name = name, sources = sources)

class InstallCMakeLibsData(install_data):
    """
    Just a wrapper to get the install data into the egg-info

    Listing the installed files in the egg-info guarantees that
    all of the package files will be uninstalled when the user
    uninstalls your package through pip
    """

    def run(self):
        """
        Outfiles are the libraries that were built using cmake
        """

        # There seems to be no other way to do this; I tried listing the
        # libraries during the execution of the InstallCMakeLibs.run() but
        # setuptools never tracked them, seems like setuptools wants to
        # track the libraries through package data more than anything...
        # help would be appriciated

        self.outfiles = self.distribution.data_files

class InstallCMakeLibs(install_lib):
    """
    Get the libraries from the parent distribution, use those as the outfiles

    Skip building anything; everything is already built, forward libraries to
    the installation step
    """

    def run(self):
        """
        Copy libraries from the bin directory and place them as appropriate
        """

        self.announce("Moving library files", level=3)

        # We have already built the libraries in the previous build_ext step

        self.skip_build = True

        bin_dir = self.distribution.bin_dir

        # Depending on the files that are generated from your cmake
        # build chain, you may need to change the below code, such that
        # your files are moved to the appropriate location when the installation
        # is run

        libs = [os.path.join(bin_dir, _lib) for _lib in 
                os.listdir(bin_dir) if 
                os.path.isfile(os.path.join(bin_dir, _lib)) and 
                os.path.splitext(_lib)[1] in [".dll", ".so"]
                and not (_lib.startswith("python") or _lib.startswith(PACKAGE_NAME))]

        for lib in libs:

            shutil.move(lib, os.path.join(self.build_dir,
                                          os.path.basename(lib)))

        # Mark the libs for installation, adding them to 
        # distribution.data_files seems to ensure that setuptools' record 
        # writer appends them to installed-files.txt in the package's egg-info
        #
        # Also tried adding the libraries to the distribution.libraries list, 
        # but that never seemed to add them to the installed-files.txt in the 
        # egg-info, and the online recommendation seems to be adding libraries 
        # into eager_resources in the call to setup(), which I think puts them 
        # in data_files anyways. 
        # 
        # What is the best way?

        # These are the additional installation files that should be
        # included in the package, but are resultant of the cmake build
        # step; depending on the files that are generated from your cmake
        # build chain, you may need to modify the below code

        self.distribution.data_files = [os.path.join(self.install_dir, 
                                                     os.path.basename(lib))
                                        for lib in libs]

        # Must be forced to run after adding the libs to data_files

        self.distribution.run_command("install_data")

        super().run()

class InstallCMakeScripts(install_scripts):
    """
    Install the scripts in the build dir
    """

    def run(self):
        """
        Copy the required directory to the build directory and super().run()
        """

        self.announce("Moving scripts files", level=3)

        # Scripts were already built in a previous step

        self.skip_build = True

        bin_dir = self.distribution.bin_dir

        scripts_dirs = [os.path.join(bin_dir, _dir) for _dir in
                        os.listdir(bin_dir) if
                        os.path.isdir(os.path.join(bin_dir, _dir))]

        for scripts_dir in scripts_dirs:

            shutil.move(scripts_dir,
                        os.path.join(self.build_dir,
                                     os.path.basename(scripts_dir)))

        # Mark the scripts for installation, adding them to 
        # distribution.scripts seems to ensure that the setuptools' record 
        # writer appends them to installed-files.txt in the package's egg-info

        self.distribution.scripts = scripts_dirs

        super().run()

class BuildCMakeExt(build_ext):
    """
    Builds using cmake instead of the python setuptools implicit build
    """

    def run(self):
        """
        Perform build_cmake before doing the 'normal' stuff
        """

        for extension in self.extensions:

            if extension.name == 'example_extension':

                self.build_cmake(extension)

        super().run()

    def build_cmake(self, extension: Extension):
        """
        The steps required to build the extension
        """

        self.announce("Preparing the build environment", level=3)

        build_dir = pathlib.Path(self.build_temp)

        extension_path = pathlib.Path(self.get_ext_fullpath(extension.name))

        os.makedirs(build_dir, exist_ok=True)
        os.makedirs(extension_path.parent.absolute(), exist_ok=True)

        # Now that the necessary directories are created, build

        self.announce("Configuring cmake project", level=3)

        # Change your cmake arguments below as necessary
        # Below is just an example set of arguments for building Blender as a Python module

        self.spawn(['cmake', '-H'+SOURCE_DIR, '-B'+self.build_temp,
                    '-DWITH_PLAYER=OFF', '-DWITH_PYTHON_INSTALL=OFF',
                    '-DWITH_PYTHON_MODULE=ON',
                    f"-DCMAKE_GENERATOR_PLATFORM=x"
                    f"{'86' if BITS == 32 else '64'}"])

        self.announce("Building binaries", level=3)

        self.spawn(["cmake", "--build", self.build_temp, "--target", "INSTALL",
                    "--config", "Release"])

        # Build finished, now copy the files into the copy directory
        # The copy directory is the parent directory of the extension (.pyd)

        self.announce("Moving built python module", level=3)

        bin_dir = os.path.join(build_dir, 'bin', 'Release')
        self.distribution.bin_dir = bin_dir

        pyd_path = [os.path.join(bin_dir, _pyd) for _pyd in
                    os.listdir(bin_dir) if
                    os.path.isfile(os.path.join(bin_dir, _pyd)) and
                    os.path.splitext(_pyd)[0].startswith(PACKAGE_NAME) and
                    os.path.splitext(_pyd)[1] in [".pyd", ".so"]][0]

        shutil.move(pyd_path, extension_path)

        # After build_ext is run, the following commands will run:
        # 
        # install_lib
        # install_scripts
        # 
        # These commands are subclassed above to avoid pitfalls that
        # setuptools tries to impose when installing these, as it usually
        # wants to build those libs and scripts as well or move them to a
        # different place. See comments above for additional information

setup(name='my_package',
      version='1.0.0a0',
      packages=find_packages(),
      ext_modules=[CMakeExtension(name="example_extension")],
      description='An example cmake extension module',
      long_description=open("./README.md", 'r').read(),
      long_description_content_type="text/markdown",
      keywords="test, cmake, extension",
      classifiers=["Intended Audience :: Developers",
                   "License :: OSI Approved :: "
                   "GNU Lesser General Public License v3 (LGPLv3)",
                   "Natural Language :: English",
                   "Programming Language :: C",
                   "Programming Language :: C++",
                   "Programming Language :: Python",
                   "Programming Language :: Python :: 3.6",
                   "Programming Language :: Python :: Implementation :: CPython"],
      license='GPL-3.0',
      cmdclass={
          'build_ext': BuildCMakeExt,
          'install_data': InstallCMakeLibsData,
          'install_lib': InstallCMakeLibs,
          'install_scripts': InstallCMakeScripts
          }
    )

一旦以setup.py这种方式创作了,构建python模块就变得像运行一样简单py setup.py,它将运行构建并生成输出文件。

建议您为网速慢的用户或不想从源代码构建的用户制作一个 wheel。为此,您需要安装wheel包 ( py -m pip install wheel) 并通过执行 制作一个 wheel 分发包py setup.py bdist_wheel,然后twine像任何其他包一样使用它上传它。

解决方案 3:

我遇到了同样的问题,我通过编写一个自定义的 setuptools 构建命令解决了这个问题,该命令复制了预先存在的构建 pyd。使用 CMake 构建并使用 setuptools 打包似乎比尝试使用 setuptools 构建更简洁。

该包位于pypi上

代码在github上

希望这对您有帮助。

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   2482  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1533  
  PLM(产品生命周期管理)项目对于企业优化产品研发流程、提升产品质量以及增强市场竞争力具有至关重要的意义。然而,在项目推进过程中,范围蔓延是一个常见且棘手的问题,它可能导致项目进度延迟、成本超支以及质量下降等一系列不良后果。因此,有效避免PLM项目范围蔓延成为项目成功的关键因素之一。以下将详细阐述三大管控策略,助力企业...
plm系统   0  
  PLM(产品生命周期管理)项目管理在企业产品研发与管理过程中扮演着至关重要的角色。随着市场竞争的加剧和产品复杂度的提升,PLM项目面临着诸多风险。准确量化风险优先级并采取有效措施应对,是确保项目成功的关键。五维评估矩阵作为一种有效的风险评估工具,能帮助项目管理者全面、系统地评估风险,为决策提供有力支持。五维评估矩阵概述...
免费plm软件   0  
  引言PLM(产品生命周期管理)开发流程对于企业产品的全生命周期管控至关重要。它涵盖了从产品概念设计到退役的各个阶段,直接影响着产品质量、开发周期以及企业的市场竞争力。在当今快速发展的科技环境下,客户对产品质量的要求日益提高,市场竞争也愈发激烈,这就使得优化PLM开发流程成为企业的必然选择。缺陷管理工具和六西格玛方法作为...
plm产品全生命周期管理   0  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用