From ca730140bc7e73e7c18a60f28118ccdb4d7ce431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 12 Feb 2025 14:16:38 +0100 Subject: [PATCH 1/2] Automatically call CMake as part of PEP 517 build Call CMake and build the CPU extension when invoking the build via a PEP 517 backend, to ensure that at least some extension is built when users are building from source. This improves consistency with other Python packages, and reduces the risk of accidents. We are using `scikit-build-core` setuptools plugin to take care of CMake dependencies and call into CMake. However, we need to modify the `build_py` command to ensure that CMake is called prior to the setuptools command, as otherwise the newly built shared library won't be picked up by `build_py`. Since setuptools is still responsible for collecting the Python package, it also collects all other shared libraries that were built earlier, for example via manual CMake calls as done in the CI pipeline. Furthermore, if the user does not have `scikit-build-core` installed and calls `setup.py` directly, we output a warning but continue working as before. The logic can be further extended in the future, for example to detect the best COMPUTE_BACKEND default. Fixes #1511 --- pyproject.toml | 4 ++-- setup.py | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 515d90385..6139ca7c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] -requires = ["setuptools >= 63.0.0"] -build-backend = "setuptools.build_meta" +requires = ["scikit-build-core", "setuptools >= 63.0.0"] +build-backend = "scikit_build_core.setuptools.build_meta" [project] name = "bitsandbytes" diff --git a/setup.py b/setup.py index 74b9debb9..377c24453 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,11 @@ # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. +from distutils.errors import DistutilsModuleError +from warnings import warn + from setuptools import find_packages, setup +from setuptools.command.build_py import build_py from setuptools.dist import Distribution @@ -12,4 +16,22 @@ def has_ext_modules(self): return True -setup(version="0.45.3.dev0", packages=find_packages(), distclass=BinaryDistribution) +class ExtBuildPy(build_py): + def run(self): + # build_cmake needs to be called prior to build_py, as the latter + # collects the files output into the package directory. + try: + self.run_command("build_cmake") + except DistutilsModuleError: + warn("scikit-build-core not installed, CMake will not be invoked automatically. " + "Please install scikit-build-core or run CMake manually to build extensions.") + super().run() + + +setup(version="0.45.3.dev0", + packages=find_packages(), + distclass=BinaryDistribution, + cmake_source_dir=".", + cmdclass={ + "build_py": ExtBuildPy, + }) From 19cd26c8cb442a40db9ca44c4fc5311cb41aa16b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Mon, 17 Mar 2025 15:51:37 +0100 Subject: [PATCH 2/2] Include C sources and build files in source distribution --- MANIFEST.in | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 000000000..00bdaa214 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,3 @@ +include CMakeLists.txt +graft csrc +graft include