Skip to content

Commit

Permalink
Add free-threading support (#39)
Browse files Browse the repository at this point in the history
* Add support for free-threaded Python

Contains a flag for setting `NB_FREE_THREADED`, and config settings for
each.

* Bump pre-commit buildifier

Via `pre-commit autoupdate`.

* Try again with pinned setuptools in nanobind_example CI

* Add integrity hash to nanobind v2.2.0
  • Loading branch information
nicholasjng authored Oct 14, 2024
1 parent 23d064f commit 13b6b8e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/keith/pre-commit-buildifier
rev: 6.4.0
rev: 7.3.1
hooks:
- id: buildifier
- id: buildifier-lint
15 changes: 15 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ config_setting(
flag_values = {":minsize": "False"},
)

bool_flag(
name = "free_threading",
build_setting_default = False,
)

config_setting(
name = "with_free_threading",
flag_values = {":free_threading": "True"},
)

config_setting(
name = "without_free_threading",
flag_values = {":free_threading": "False"},
)

string_flag(
name = "py-limited-api",
build_setting_default = "unset",
Expand Down
6 changes: 3 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module(
)

bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_python", version = "0.32.2")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "rules_cc", version = "0.0.10")
bazel_dep(name = "rules_python", version = "0.36.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")

# Creates a `http_archive` for nanobind and robin-map.
internal_configure = use_extension("//:internal_configure.bzl", "internal_configure_extension")
Expand Down
7 changes: 7 additions & 0 deletions helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ def extension_name(name):
"@nanobind_bazel//:stable-abi-unix": name + ".abi3.so",
"@nanobind_bazel//:unstable-abi-unix": name + ".so",
})

# Optionally add a define for free-threaded nanobind builds.
def nb_free_threading():
return select({
"@nanobind_bazel//:with_free_threading": ["NB_FREE_THREADED"],
"@nanobind_bazel//:without_free_threading": [],
})
4 changes: 2 additions & 2 deletions internal_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def _internal_configure_extension_impl(_):
urls = ["https://github.com/Tessil/robin-map/archive/refs/tags/v%s.tar.gz" % robin_map_version],
)

nanobind_version = "2.0.0"
nanobind_version = "2.2.0"
http_archive(
name = "nanobind",
build_file = "//:nanobind.BUILD",
strip_prefix = "nanobind-%s" % nanobind_version,
integrity = "sha256-LnBydITtt6hkXSb2qfZzUqZoZXw03npgO/nGjly/j/k=",
integrity = "sha256-v7/H5XWfFmnk3bSHUrHdxWR9FDDpRhTW+GJt8dUI5lo=",
urls = ["https://github.com/wjakob/nanobind/archive/refs/tags/v%s.tar.gz" % nanobind_version],
)

Expand Down
3 changes: 2 additions & 1 deletion nanobind.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load(
"@nanobind_bazel//:helpers.bzl",
"maybe_compact_asserts",
"nb_common_opts",
"nb_free_threading",
"nb_sizeopts",
"nb_stripopts",
"py_limited_api",
Expand All @@ -29,7 +30,7 @@ cc_library(
"//conditions:default": [],
}),
copts = nb_common_opts(mode = "library") + nb_sizeopts(),
defines = py_limited_api(),
defines = py_limited_api() + nb_free_threading(),
includes = ["include"],
linkopts = select({
"@platforms//os:linux": ["-Wl,--gc-sections"],
Expand Down

0 comments on commit 13b6b8e

Please sign in to comment.