""" ViewDesignEngine — CMake-based Python bindings. Build & install: pip install -e . # editable install pip install . # regular install Requires: CMake ≥ 3.16, C++17, Eigen3 (auto-fetched), pybind11. """ from setuptools import setup from setuptools.command.build_ext import build_ext from setuptools.command.build_py import build_py import subprocess import sys import shutil import os import re from pathlib import Path ROOT = Path(__file__).parent.resolve() PKG_DIR = ROOT / "vde" def _project_version() -> str: """从 pyproject.toml 读取版本,避免 setup.py 与 PEP 621 元数据版本漂移。""" pyproject = ROOT / "pyproject.toml" if pyproject.exists(): m = re.search(r'^version\s*=\s*"([^"]+)"', pyproject.read_text(encoding="utf-8"), re.MULTILINE) if m: return m.group(1) return "0.0.0" class CMakeBuildExt(build_ext): """Build the native _vde module via CMake.""" def run(self): build_dir = ROOT / "build_py" build_dir.mkdir(exist_ok=True) # ── Configure ── subprocess.check_call( [ "cmake", "-B", str(build_dir), "-DVDE_BUILD_PYTHON=ON", "-DCMAKE_BUILD_TYPE=Release", "-DBUILD_TESTS=OFF", "-DBUILD_BENCHMARKS=OFF", "-DBUILD_EXAMPLES=OFF", ], cwd=str(ROOT), ) # ── Build ── parallel = str(os.cpu_count() or 4) subprocess.check_call( ["cmake", "--build", str(build_dir), "-j", parallel], cwd=str(ROOT), ) # ── Copy .so / .pyd to package dir ── so_suffix = ".cpython-*-linux-gnu.so" if sys.platform == "linux" else "*.so" so_glob = "*.pyd" if sys.platform == "win32" else so_suffix so_files = list(build_dir.glob(f"python/{so_glob}")) so_files += list(build_dir.glob(f"python/**/{so_glob}")) if not so_files: # fallback: search whole build tree so_files = list(build_dir.rglob(f"_vde{so_glob}")) so_files += list(build_dir.rglob(f"_vde*.so")) for so in so_files: dest = PKG_DIR / so.name print(f"Installing {so.name} → {dest}") shutil.copy2(str(so), str(dest)) break # copy first match class CMakeBuildPy(build_py): """Ensure native build runs before packaging.""" def run(self): self.run_command("build_ext") super().run() setup( name="vde", version=_project_version(), description="High-performance C++ CAD geometry engine", long_description=(ROOT / "README.md").read_text(encoding="utf-8") if (ROOT / "README.md").exists() else "", long_description_content_type="text/markdown", url="https://github.com/ViewDesignEngine/ViewDesignEngine", packages=["vde"], package_dir={"vde": "vde"}, cmdclass={ "build_ext": CMakeBuildExt, "build_py": CMakeBuildPy, }, python_requires=">=3.8", zip_safe=False, )