所以我有以下文件结构:
project/
├─ cpp_src/
│ ├─ src/
│ │ ├─ cpp source files
│ ├─ test/
│ │ ├─ cpp test files
│ ├─ CMakeLists.txt
│ ├─ stub.pyi
├─ python_src/
│ ├─ ...
├─ build.py
在我的
build.py
文件中,我使用 setuptools 使用自定义 cpp_src
命令在 build_ext
中编译和打包 C++ 扩展。但是,我似乎无法将其包含存根文件stub.pyi
。我如何修改 setuptools 命令来执行此操作?我不太关心文件结构,所以如果 setup.py
中需要另一个 cpp_src
文件,那很好。
如果有帮助,我也在使用 Poetry 来管理虚拟环境。此外,如果有另一个构建系统可以使这更容易,我很乐意使用它。
谢谢。
编辑:这是
build.py
文件的缩减版本(完整回购在这里https://github.com/Aspect1103/Hades/tree/generation-rust):
import subprocess
from pathlib import Path
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
class CMakeBuild(build_ext):
def build_extension(self, ext: Extension) -> None:
# Determine where the extension should be transferred to after it has been
# compiled
current_dir = Path.cwd()
build_dir = current_dir.joinpath(self.get_ext_fullpath(ext.name)).parent
# Determine the profile to build the CMake extension with
profile = "Release"
# Make sure the build directory exists
build_temp = Path(self.build_temp).joinpath(ext.name)
if not build_temp.exists():
build_temp.mkdir(parents=True)
# Compile and build the CMake extension
subprocess.run(
[
"cmake",
current_dir.joinpath(ext.sources[0]),
f"-DDO_TESTS=false",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{profile.upper()}={build_dir}",
],
cwd=build_temp,
check=True,
)
subprocess.run(
["cmake", "--build", ".", f"--config {profile}"], cwd=build_temp, check=True
)
def main():
setup(
name="hades_extensions",
script_args=["bdist_wheel"],
ext_modules=[Extension("hades_extensions", ["cpp_src"])],
cmdclass={"build_ext": CMakeBuild},
)
if __name__ == "__main__":
main()