Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current_proto_toolchain #214

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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