Skip to content

Commit af7151f

Browse files
authored
Initial support for Bzlmod. (#55)
1 parent ff261d2 commit af7151f

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

MODULE.bazel

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Bazel extensions for pybind11
2+
module(
3+
name = "pybind11_bazel",
4+
version = "2.11.1",
5+
)
6+
7+
bazel_dep(name = "platforms", version = "0.0.7")
8+
bazel_dep(name = "rules_cc", version = "0.0.8")

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,24 @@ python_configure(
6969
python_interpreter_target = "@python_interpreter//:python_bin",
7070
)
7171
```
72+
73+
## Bzlmod
74+
75+
In your `MODULE.bazel` file:
76+
77+
```starlark
78+
python_configure = use_extension("@pybind11_bazel//:python_configure.bzl", "extension")
79+
use_repo(python_configure, "local_config_python", "pybind11")
80+
```
81+
82+
The `toolchain` tag can only be used by the root module (that is, not by a
83+
module which is being used as a dependency) to set `python_version` or
84+
`python_interpreter_target`. For example:
85+
86+
```starlark
87+
python_configure = use_extension("@pybind11_bazel//:python_configure.bzl", "extension")
88+
python_configure.toolchain(python_version = "3")
89+
use_repo(python_configure, "local_config_python", "pybind11")
90+
```
91+
92+
Usage in your `BUILD` file is as described previously.

python_configure.bzl

+61
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* `PYTHON_LIB_PATH`: Location of python libraries.
77
"""
88

9+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
10+
911
_BAZEL_SH = "BAZEL_SH"
1012
_PYTHON_BIN_PATH = "PYTHON_BIN_PATH"
1113
_PYTHON_CONFIG_BIN_PATH = "PYTHON_CONFIG_BIN_PATH"
@@ -441,3 +443,62 @@ Args:
441443
will configure for that Python version instead of the installed
442444
binary. This configuration takes precedence over python_version.
443445
"""
446+
447+
def _parse_my_own_version_from_module_dot_bazel(ctx):
448+
lines = ctx.read(Label("//:MODULE.bazel")).split("\n")
449+
for line in lines:
450+
parts = line.split("\"")
451+
if parts[0] == " version = ":
452+
return parts[1]
453+
_fail("Failed to parse my own version from `MODULE.bazel`! " +
454+
"This should never happen!")
455+
456+
def _extension_impl(ctx):
457+
toolchain = None
458+
for module in ctx.modules:
459+
if module.is_root:
460+
if not module.tags.toolchain:
461+
pass
462+
elif len(module.tags.toolchain) == 1:
463+
toolchain = module.tags.toolchain[0]
464+
else:
465+
_fail("The root module may not specify multiple `toolchain` " +
466+
"tags. Found %r `toolchain` tags specified." % (
467+
len(module.tags.toolchain),
468+
))
469+
elif module.tags.toolchain:
470+
_fail("A non-root module may not specify any `toolchain` tags. " +
471+
"Found %r `toolchain` tags specified by module %r." % (
472+
len(module.tags.toolchain),
473+
module.name,
474+
))
475+
if toolchain == None:
476+
python_configure(
477+
name = "local_config_python",
478+
)
479+
else:
480+
python_configure(
481+
name = "local_config_python",
482+
python_version = toolchain.python_version,
483+
python_interpreter_target = toolchain.python_interpreter_target,
484+
)
485+
486+
version = _parse_my_own_version_from_module_dot_bazel(ctx)
487+
http_archive(
488+
name = "pybind11",
489+
build_file = "//:pybind11.BUILD",
490+
strip_prefix = "pybind11-%s" % version,
491+
urls = ["https://github.com/pybind/pybind11/archive/v%s.zip" % version],
492+
)
493+
494+
extension = module_extension(
495+
implementation = _extension_impl,
496+
tag_classes = {
497+
"toolchain": tag_class(
498+
attrs = {
499+
"python_version": attr.string(default = ""),
500+
"python_interpreter_target": attr.label(),
501+
},
502+
),
503+
},
504+
)

0 commit comments

Comments
 (0)