Unverified Commit ed9c8fca authored by Michał Górny's avatar Michał Górny Committed by GitHub
Browse files

Automatically call CMake as part of PEP 517 build (#1512)

* 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

* Include C sources and build files in source distribution

* Fix formatting
parent ed398d28
include CMakeLists.txt
graft csrc
graft include
[build-system] [build-system]
requires = ["setuptools >= 63.0.0"] requires = ["scikit-build-core", "setuptools >= 63.0.0"]
build-backend = "setuptools.build_meta" build-backend = "scikit_build_core.setuptools.build_meta"
[project] [project]
name = "bitsandbytes" name = "bitsandbytes"
......
...@@ -2,7 +2,11 @@ ...@@ -2,7 +2,11 @@
# #
# This source code is licensed under the MIT license found in the # This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree. # 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 import find_packages, setup
from setuptools.command.build_py import build_py
from setuptools.dist import Distribution from setuptools.dist import Distribution
...@@ -12,4 +16,26 @@ class BinaryDistribution(Distribution): ...@@ -12,4 +16,26 @@ class BinaryDistribution(Distribution):
return True return True
setup(version="0.47.0.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.47.0.dev0",
packages=find_packages(),
distclass=BinaryDistribution,
cmake_source_dir=".",
cmdclass={
"build_py": ExtBuildPy,
},
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment