From 36394f9c53c2d51e6a31a6483d25f369c9736c74 Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 22 Sep 2023 03:04:18 -0700 Subject: [PATCH] Use proto compiler from proto_toolchain rule Issue: https://github.com/bazelbuild/rules_proto/issues/179 PiperOrigin-RevId: 567570286 Change-Id: Ia7e1ff08a0123a325f1df00f56b81212c01e2588 --- .../build/lib/rules/proto/ProtoConstants.java | 2 +- .../common/proto/proto_lang_toolchain.bzl | 15 +++++++-- .../lib/packages/util/MockProtoSupport.java | 5 ++- .../rules/proto/ProtoLangToolchainTest.java | 33 +++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConstants.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConstants.java index 80ee27b6f3f5e0..e08307de3b0eac 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConstants.java +++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConstants.java @@ -17,7 +17,7 @@ /** Constants used in Proto rules. */ public final class ProtoConstants { /** Default label for proto compiler. */ - static final String DEFAULT_PROTOC_LABEL = "@bazel_tools//tools/proto:protoc"; + public static final String DEFAULT_PROTOC_LABEL = "@bazel_tools//tools/proto:protoc"; /** Default label for java proto toolchains. */ static final String DEFAULT_JAVA_PROTO_LABEL = "@bazel_tools//tools/proto:java_toolchain"; diff --git a/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain.bzl b/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain.bzl index ad46ce5f15d494..809cf78eba03f6 100644 --- a/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain.bzl +++ b/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain.bzl @@ -31,6 +31,13 @@ def _rule_impl(ctx): if ctx.attr.plugin != None: plugin = ctx.attr.plugin[DefaultInfo].files_to_run + if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION: + proto_compiler = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE].proto.proto_compiler + protoc_opts = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE].proto.protoc_opts + else: + proto_compiler = ctx.attr._proto_compiler.files_to_run + protoc_opts = ctx.fragments.proto.experimental_protoc_opts + return [ DefaultInfo( files = depset(), @@ -43,8 +50,8 @@ def _rule_impl(ctx): plugin = plugin, runtime = ctx.attr.runtime, provided_proto_sources = provided_proto_sources, - proto_compiler = ctx.attr._proto_compiler.files_to_run, - protoc_opts = ctx.fragments.proto.experimental_protoc_opts, + proto_compiler = proto_compiler, + protoc_opts = protoc_opts, progress_message = ctx.attr.progress_message, mnemonic = ctx.attr.mnemonic, ), @@ -67,13 +74,15 @@ proto_lang_toolchain = rule( "blacklisted_protos": attr.label_list( providers = [ProtoInfo], ), + } | ({} if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else { "_proto_compiler": attr.label( cfg = "exec", executable = True, allow_files = True, default = configuration_field("proto", "proto_compiler"), ), - }, + }), provides = [ProtoLangToolchainInfo], fragments = ["proto"], + toolchains = semantics.PROTO_TOOLCHAIN, # Used to obtain protoc ) diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java index 187c3596b889a7..93b61ab4433e74 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java +++ b/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java @@ -16,6 +16,7 @@ import com.google.devtools.build.lib.rules.proto.ProtoCommon; import com.google.devtools.build.lib.testutil.Scratch; +import com.google.devtools.build.lib.rules.proto.ProtoConstants; import com.google.devtools.build.lib.testutil.TestConstants; import java.io.IOException; @@ -49,7 +50,9 @@ private static void registerProtoToolchain(MockToolsConfig config) throws IOExce "tools/proto/toolchains/BUILD", TestConstants.LOAD_PROTO_TOOLCHAIN, "proto_toolchain(name = 'protoc_sources'," - + "proto_compiler = '//net/proto2/compiler/public:protocol_compiler')"); + + "proto_compiler = '" + + ProtoConstants.DEFAULT_PROTOC_LABEL + + "')"); } /** Create a dummy "net/proto2 compiler and proto APIs for all languages and versions. */ diff --git a/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoLangToolchainTest.java b/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoLangToolchainTest.java index 08f695c7aabd4f..8374ed89a8c5fd 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoLangToolchainTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoLangToolchainTest.java @@ -96,6 +96,39 @@ public void protoToolchain() throws Exception { validateProtoCompiler(toolchain, ProtoConstants.DEFAULT_PROTOC_LABEL); } + @Test + public void protoToolchainResolution_enabled() throws Exception { + setBuildLanguageOptions("--incompatible_enable_proto_toolchain_resolution"); + scratch.file( + "third_party/x/BUILD", + "licenses(['unencumbered'])", + "cc_binary(name = 'plugin', srcs = ['plugin.cc'])", + "cc_library(name = 'runtime', srcs = ['runtime.cc'])", + "filegroup(name = 'descriptors', srcs = ['metadata.proto', 'descriptor.proto'])", + "filegroup(name = 'any', srcs = ['any.proto'])", + "proto_library(name = 'denied', srcs = [':descriptors', ':any'])"); + scratch.file( + "foo/BUILD", + TestConstants.LOAD_PROTO_LANG_TOOLCHAIN, + "licenses(['unencumbered'])", + "proto_lang_toolchain(", + " name = 'toolchain',", + " command_line = 'cmd-line:$(OUT)',", + " plugin_format_flag = '--plugin=%s',", + " plugin = '//third_party/x:plugin',", + " runtime = '//third_party/x:runtime',", + " progress_message = 'Progress Message %{label}',", + " mnemonic = 'MyMnemonic',", + ")"); + + update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus()); + ProtoLangToolchainProvider toolchain = + ProtoLangToolchainProvider.get(getConfiguredTarget("//foo:toolchain")); + + validateProtoLangToolchain(toolchain); + validateProtoCompiler(toolchain, ProtoConstants.DEFAULT_PROTOC_LABEL); + } + @Test public void protoToolchainBlacklistProtoLibraries() throws Exception { scratch.file(