最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to install python packages generate by CMake using setup tools? - Stack Overflow

programmeradmin0浏览0评论

I am working with a C++ project in which the CMake build process generates a directory structure of Python packages (i.e. Python source files, not shared object) in the chosen build directory under python_packages/my_package. I cannot change this approach. I have written a setup.py script that runs CMake and tries to add these to the generated package but I am stuck. Here's where I'm at:

import os
import subprocess
from pathlib import Path

from setuptools import (
    Extension,
    find_packages,
    setup,
)
from setuptoolsmand.build_ext import build_ext


class CMakeExtension(Extension):
    def __init__(self, name):
        super().__init__(name, sources=[])


class cmake_build_ext(build_ext):
    def build_extension(self, ext):
        cmake_source_dir = Path.cwd()

        cmake_build_dir = Path(self.build_temp)
        if not cmake_build_dir.exists():
            cmake_build_dir.mkdir(parents=True)

        cmake_preset = "debug" if self.debug else "release"

        cmake_env = os.environ.copy()

        cmake_cmd = [
            "/usr/bin/cmake",
            "-S", cmake_source_dir,
            "-B", cmake_build_dir,
            "--preset", cmake_preset
        ]

        subprocess.check_call(cmake_cmd, cwd=cmake_build_dir)

        cmake_build_cmd = [
            "/usr/bin/cmake",
            "--build", cmake_build_dir,
        ]

        subprocess.check_call(cmake_build_cmd, cwd=cmake_build_dir)

        packages_dir = cmake_build_dir / "python_packages"
        
        packages = find_packages(packages_dir)
        print("Packages:", packages)

        self.distribution.packages = packages


setup(
    name="my_project",
    version="1.0.0",
    author="Me Myself",
    author_email="[email protected]",
    description = "CMake experiment",
    python_requires=">=3.12",
    ext_modules=[CMakeExtension("my_project")],
    cmdclass={"build_ext": cmake_build_ext},
    zip_safe=False,
)

When calling pip install . this runs up until the running build_py step where I get the following error:

error: package directory 'my_package' does not exist

At the same time, Packages: ["my_package", "my_package.sub_package"] is printed during execution. There is likely something I should be doing here with package_dir but I am lost.

发布评论

评论列表(0)

  1. 暂无评论