Skip to content

Commit

Permalink
Expose bindgen toolchain through bzlmod
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnrt committed Sep 13, 2024
1 parent 4d74cc8 commit caa3139
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
5 changes: 4 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ register_toolchains(
"//proto/prost:default_prost_toolchain",
)

bindgen = use_extension("//bindgen:extension.bzl", "bindgen")
use_repo(bindgen, "bindgen_toolchains")

register_toolchains(
"//bindgen:default_bindgen_toolchain",
"@bindgen_toolchains//:default_bindgen_toolchain",
)

rust_host_tools = use_extension("//rust:extensions.bzl", "rust_host_tools")
Expand Down
18 changes: 7 additions & 11 deletions bindgen/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//bindgen:defs.bzl", "rust_bindgen_toolchain")

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

Expand All @@ -11,21 +10,18 @@ bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
deps = [
"//bindgen/3rdparty:bzl_lib",
"//bindgen/private:bzl_lib",
"//rust:bzl_lib",
"@rules_rust//bindgen/3rdparty:bzl_lib",
"@rules_rust//bindgen/private:bzl_lib",
"@rules_rust//rust:bzl_lib",
],
)

rust_bindgen_toolchain(
alias(
name = "default_bindgen_toolchain_impl",
bindgen = "//bindgen/3rdparty:bindgen",
clang = "@llvm-project//clang:clang",
libclang = "@llvm-project//clang:libclang",
actual = "//bindgen/toolchain:default_bindgen_toolchain_impl",
)

toolchain(
alias(
name = "default_bindgen_toolchain",
toolchain = "default_bindgen_toolchain_impl",
toolchain_type = "//bindgen:toolchain_type",
actual = "//bindgen/toolchain:default_bindgen_toolchain",
)
47 changes: 47 additions & 0 deletions bindgen/extension.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Module extension for accessing a bindgen toolchain"""

load(":repositories.bzl", "rust_bindgen_dependencies")
load(":transitive_repositories.bzl", "rust_bindgen_transitive_dependencies")

def _bindgen_impl(_):
rust_bindgen_transitive_dependencies(repo_mapping = None)
rust_bindgen_dependencies()
bindgen_toolchains(
name = "bindgen_toolchains",
build_file = "//bindgen/toolchain:BUILD.bazel",
)

bindgen = module_extension(implementation = _bindgen_impl)

def _bindgen_toolchains_impl(rctx):
if (rctx.attr.build_file == None) == (rctx.attr.build_file_content == "_UNSET"):
fail("exactly one of `build_file` or `build_file_content` must be specified")

if rctx.attr.build_file != None:
rctx.file("BUILD.bazel", rctx.read(rctx.attr.build_file))
else:
rctx.file("BUILD.bazel", rctx.attr.build_file_content)

bindgen_toolchains = repository_rule(
implementation = _bindgen_toolchains_impl,
attrs = {
"build_file": attr.label(
doc =
"A file to use as a BUILD file for this repo." +
"<p>Exactly one of <code>build_file</code> or <code>build_file_content</code>" +
"must be specified.",
),
"build_file_content": attr.string(
doc =
"The content of a BUILD file to be created for this repo." +
"<p>Exactly one of <code>build_file</code> or <code>build_file_content</code>" +
"must be specified.",
default = "_UNSET",
),
},
doc =
"Creates a local repository containing a provided BUILD file to be used for defining " +
"bindgen toolchains." +
"<p>This rule is a replacement for the <code>local_repository</code> rule available " +
"in later supported versions of Bazel.",
)
16 changes: 16 additions & 0 deletions bindgen/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_toolchain")

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

rust_bindgen_toolchain(
name = "default_bindgen_toolchain_impl",
bindgen = "@rules_rust//bindgen/3rdparty:bindgen",
clang = "@llvm-project//clang:clang",
libclang = "@llvm-project//clang:libclang",
)

toolchain(
name = "default_bindgen_toolchain",
toolchain = "default_bindgen_toolchain_impl",
toolchain_type = "@rules_rust//bindgen:toolchain_type",
)

0 comments on commit caa3139

Please sign in to comment.