From 11311d1fa90f12b76b9491b426b24f6402168110 Mon Sep 17 00:00:00 2001 From: psalaberria002 Date: Tue, 21 May 2024 16:02:07 +0200 Subject: [PATCH] Add current_proto_toolchain After asking in https://bazelbuild.slack.com/archives/C04281DTLH0/p1716279966809639 I gave it a tried, and it seems to be working. The implementation is based on https://github.com/bazelbuild/rules_python/blob/730a2e39bd2702910f28629d4583b3ec49f4ee5e/python/current_py_toolchain.bzl --- proto/defs.bzl | 3 ++- proto/private/rules/proto_toolchain.bzl | 33 +++++++++++++++++++++++++ proto/proto_toolchain.bzl | 3 ++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/proto/defs.bzl b/proto/defs.bzl index 69a1969..b5219ae 100644 --- a/proto/defs.bzl +++ b/proto/defs.bzl @@ -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") @@ -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`. # diff --git a/proto/private/rules/proto_toolchain.bzl b/proto/private/rules/proto_toolchain.bzl index a091b80..0d68206 100644 --- a/proto/private/rules/proto_toolchain.bzl +++ b/proto/private/rules/proto_toolchain.bzl @@ -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")), + ], +) diff --git a/proto/proto_toolchain.bzl b/proto/proto_toolchain.bzl index e1a853c..f38df2f 100644 --- a/proto/proto_toolchain.bzl +++ b/proto/proto_toolchain.bzl @@ -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