Skip to content

Commit a39df01

Browse files
committed
Expose bindgen toolchain through bzlmod
1 parent 1d0a512 commit a39df01

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

MODULE.bazel

+4-1
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ register_toolchains(
175175
"//proto/prost:default_prost_toolchain",
176176
)
177177

178+
bindgen = use_extension("//bindgen:extension.bzl", "bindgen")
179+
use_repo(bindgen, "bindgen_toolchains")
180+
178181
register_toolchains(
179-
"//bindgen:default_bindgen_toolchain",
182+
"@bindgen_toolchains//:default_bindgen_toolchain",
180183
)
181184

182185
rust_host_tools = use_extension("//rust:extensions.bzl", "rust_host_tools")

bindgen/BUILD.bazel

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
load("//bindgen:defs.bzl", "rust_bindgen_toolchain")
32

43
package(default_visibility = ["//visibility:public"])
54

@@ -11,21 +10,18 @@ bzl_library(
1110
name = "bzl_lib",
1211
srcs = glob(["**/*.bzl"]),
1312
deps = [
14-
"//bindgen/3rdparty:bzl_lib",
15-
"//bindgen/private:bzl_lib",
16-
"//rust:bzl_lib",
13+
"@rules_rust//bindgen/3rdparty:bzl_lib",
14+
"@rules_rust//bindgen/private:bzl_lib",
15+
"@rules_rust//rust:bzl_lib",
1716
],
1817
)
1918

20-
rust_bindgen_toolchain(
19+
alias(
2120
name = "default_bindgen_toolchain_impl",
22-
bindgen = "//bindgen/3rdparty:bindgen",
23-
clang = "@llvm-project//clang:clang",
24-
libclang = "@llvm-project//clang:libclang",
21+
actual = "//bindgen/toolchain:default_bindgen_toolchain_impl",
2522
)
2623

27-
toolchain(
24+
alias(
2825
name = "default_bindgen_toolchain",
29-
toolchain = "default_bindgen_toolchain_impl",
30-
toolchain_type = "//bindgen:toolchain_type",
26+
actual = "//bindgen/toolchain:default_bindgen_toolchain",
3127
)

bindgen/extension.bzl

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Module extension for accessing a bindgen toolchain"""
2+
3+
load(":repositories.bzl", "rust_bindgen_dependencies")
4+
load(":transitive_repositories.bzl", "rust_bindgen_transitive_dependencies")
5+
6+
def _bindgen_impl(_):
7+
rust_bindgen_transitive_dependencies(repo_mapping = None)
8+
rust_bindgen_dependencies()
9+
bindgen_toolchains(
10+
name = "bindgen_toolchains",
11+
build_file = "//bindgen/toolchain:BUILD.bazel",
12+
)
13+
14+
bindgen = module_extension(implementation = _bindgen_impl)
15+
16+
def _bindgen_toolchains_impl(rctx):
17+
if (rctx.attr.build_file == None) == (rctx.attr.build_file_content == "_UNSET"):
18+
fail("exactly one of `build_file` or `build_file_content` must be specified")
19+
20+
if rctx.attr.build_file != None:
21+
rctx.file("BUILD.bazel", rctx.read(rctx.attr.build_file))
22+
else:
23+
rctx.file("BUILD.bazel", rctx.attr.build_file_content)
24+
25+
bindgen_toolchains = repository_rule(
26+
implementation = _bindgen_toolchains_impl,
27+
attrs = {
28+
"build_file": attr.label(
29+
doc =
30+
"A file to use as a BUILD file for this repo." +
31+
"<p>Exactly one of <code>build_file</code> or <code>build_file_content</code>" +
32+
"must be specified.",
33+
),
34+
"build_file_content": attr.string(
35+
doc =
36+
"The content of a BUILD file to be created for this repo." +
37+
"<p>Exactly one of <code>build_file</code> or <code>build_file_content</code>" +
38+
"must be specified.",
39+
default = "_UNSET",
40+
),
41+
},
42+
doc =
43+
"Creates a local repository containing a provided BUILD file to be used for defining " +
44+
"bindgen toolchains." +
45+
"<p>This rule is a replacement for the <code>local_repository</code> rule available " +
46+
"in later supported versions of Bazel.",
47+
)

bindgen/toolchain/BUILD.bazel

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_toolchain")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
rust_bindgen_toolchain(
6+
name = "default_bindgen_toolchain_impl",
7+
bindgen = "@rules_rust//bindgen/3rdparty:bindgen",
8+
clang = "@llvm-project//clang:clang",
9+
libclang = "@llvm-project//clang:libclang",
10+
)
11+
12+
toolchain(
13+
name = "default_bindgen_toolchain",
14+
toolchain = "default_bindgen_toolchain_impl",
15+
toolchain_type = "@rules_rust//bindgen:toolchain_type",
16+
)

0 commit comments

Comments
 (0)