Skip to content

Commit

Permalink
Merge branch 'bazel' into bazel-recursive-stubgen
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjng authored Dec 17, 2024
2 parents 0da8033 + 5dadbc5 commit 78a22af
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ build --flag_alias=minsize=@nanobind_bazel//:minsize
build --flag_alias=py_limited_api=@nanobind_bazel//:py-limited-api
# rules_python's Python version, should not collide with builtin --python_version.
build --flag_alias=target_python_version=@rules_python//python/config_settings:python_version
# rules_python's indicator to only use free-threaded toolchains for CPython 3.13+.
# Needs to be given _together with_ nanobind-bazel's `free_threading` flag.
build --flag_alias=free_threaded=@rules_python//python/config_settings:py_freethreaded
2 changes: 1 addition & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
with:
submodules: true

- uses: pypa/cibuildwheel@v2.19
- uses: pypa/cibuildwheel@v2.22

- name: Verify clean directory
run: git diff --exit-code
Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ python.toolchain(
is_default = True,
python_version = "3.12",
)
python.toolchain(python_version = "3.13")

use_repo(python, python = "python_versions")
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ was derived from the corresponding _pybind11_ [example
project](https://github.com/pybind/scikit_build_example/) developed by
[@henryiii](https://github.com/henryiii).

Furthermore, the [bazel](https://github.com/wjakob/nanobind_example/tree/bazel) branch contains an example
on how to build nanobind bindings extensions with Bazel using the [nanobind-bazel](https://github.com/nicholasjng/nanobind-bazel/) project.

Installation
------------

Expand Down
13 changes: 12 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

IS_WINDOWS = platform.system() == "Windows"

# free-threaded build option, requires Python 3.13+.
# Source: https://docs.python.org/3/howto/free-threading-python.html#identifying-free-threaded-python
free_threaded = "experimental free-threading build" in sys.version
# hardcoded SABI-related options. Requires that each Python interpreter
# (hermetic or not) participating is of the same major-minor version.
py_limited_api = sys.version_info >= (3, 12)
# Cannot be used together with free-threading.
py_limited_api = sys.version_info >= (3, 12) and not free_threaded
options = {"bdist_wheel": {"py_limited_api": "cp312"}} if py_limited_api else {}


Expand All @@ -21,6 +25,7 @@ class BazelExtension(setuptools.Extension):
def __init__(self, name: str, bazel_target: str, **kwargs):
super().__init__(name=name, sources=[], **kwargs)

self.free_threaded = free_threaded
self.bazel_target = bazel_target
stripped_target = bazel_target.split("//")[-1]
self.relpath, self.target_name = stripped_target.split(":")
Expand Down Expand Up @@ -62,6 +67,11 @@ def bazel_build(self, ext: BazelExtension) -> None:

if ext.py_limited_api:
bazel_argv += ["--py_limited_api=cp312"]
if ext.free_threaded:
bazel_argv += [
"--@nanobind_bazel//:free_threading",
"--free_threaded=yes",
]

self.spawn(bazel_argv)

Expand Down Expand Up @@ -106,6 +116,7 @@ def bazel_build(self, ext: BazelExtension) -> None:
BazelExtension(
name="nanobind_example.nanobind_example_ext",
bazel_target="//src:nanobind_example_ext_stubgen",
free_threaded=free_threaded,
py_limited_api=py_limited_api,
)
],
Expand Down

0 comments on commit 78a22af

Please sign in to comment.