Skip to content

Commit

Permalink
Add current_proto_toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
psalaberria002 committed May 21, 2024
1 parent d205d37 commit 11311d1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion proto/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""Starlark rules for building protocol buffers."""

load("//proto:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
load("//proto:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain")
load("//proto:proto_toolchain.bzl", _current_proto_toolchain = "current_proto_toolchain", _proto_toolchain = "proto_toolchain")
load("//proto/private:native.bzl", "NativeProtoInfo", "native_proto_common")
load("//proto/private/rules:proto_descriptor_set.bzl", _proto_descriptor_set = "proto_descriptor_set")

Expand Down Expand Up @@ -45,6 +45,7 @@ proto_descriptor_set = _proto_descriptor_set
proto_lang_toolchain = _proto_lang_toolchain

proto_toolchain = _proto_toolchain
current_proto_toolchain = _current_proto_toolchain

# Encapsulates information provided by `proto_library`.
#
Expand Down
33 changes: 33 additions & 0 deletions proto/private/rules/proto_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,36 @@ def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []):
target_compatible_with = [],
toolchain = name,
)

def _current_proto_toolchain_impl(ctx):
toolchain = ctx.toolchains[ctx.attr._toolchain]

direct = [toolchain.proto.proto_compiler.executable]
transitive = []
files = depset(direct, transitive = transitive)
return [
toolchain,
platform_common.TemplateVariableInfo({
"PROTOC": str(toolchain.proto.proto_compiler.executable.path),
}),
DefaultInfo(
runfiles = ctx.runfiles(transitive_files = files),
files = files,
),
]

current_proto_toolchain = rule(
doc = """
This rule exists so that the current protoc toolchain can be used in the `toolchains` attribute of
other rules, such as genrule. It allows exposing a protoc toolchain after toolchain resolution has
happened, to a rule which expects a concrete implementation of a toolchain, rather than a
toolchain_type which could be resolved to that toolchain.
""",
implementation = _current_proto_toolchain_impl,
attrs = {
"_toolchain": attr.string(default = str(Label("@rules_proto//proto:toolchain_type"))),
},
toolchains = [
str(Label("@rules_proto//proto:toolchain_type")),
],
)
3 changes: 2 additions & 1 deletion proto/proto_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Export for proto_toolchain"""

load("//proto/private/rules:proto_toolchain.bzl", _proto_toolchain_macro = "proto_toolchain")
load("//proto/private/rules:proto_toolchain.bzl", _current_proto_toolchain = "current_proto_toolchain", _proto_toolchain_macro = "proto_toolchain")

proto_toolchain = _proto_toolchain_macro
current_proto_toolchain = _current_proto_toolchain

0 comments on commit 11311d1

Please sign in to comment.