Skip to content

Commit ca73014

Browse files
committed
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 bitsandbytes-foundation#1511
1 parent 86b6c37 commit ca73014

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build-system]
2-
requires = ["setuptools >= 63.0.0"]
3-
build-backend = "setuptools.build_meta"
2+
requires = ["scikit-build-core", "setuptools >= 63.0.0"]
3+
build-backend = "scikit_build_core.setuptools.build_meta"
44

55
[project]
66
name = "bitsandbytes"

setup.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
#
33
# This source code is licensed under the MIT license found in the
44
# LICENSE file in the root directory of this source tree.
5+
from distutils.errors import DistutilsModuleError
6+
from warnings import warn
7+
58
from setuptools import find_packages, setup
9+
from setuptools.command.build_py import build_py
610
from setuptools.dist import Distribution
711

812

@@ -12,4 +16,22 @@ def has_ext_modules(self):
1216
return True
1317

1418

15-
setup(version="0.45.3.dev0", packages=find_packages(), distclass=BinaryDistribution)
19+
class ExtBuildPy(build_py):
20+
def run(self):
21+
# build_cmake needs to be called prior to build_py, as the latter
22+
# collects the files output into the package directory.
23+
try:
24+
self.run_command("build_cmake")
25+
except DistutilsModuleError:
26+
warn("scikit-build-core not installed, CMake will not be invoked automatically. "
27+
"Please install scikit-build-core or run CMake manually to build extensions.")
28+
super().run()
29+
30+
31+
setup(version="0.45.3.dev0",
32+
packages=find_packages(),
33+
distclass=BinaryDistribution,
34+
cmake_source_dir=".",
35+
cmdclass={
36+
"build_py": ExtBuildPy,
37+
})

0 commit comments

Comments
 (0)