From f5fb2f6a5fc67a25181f8eb5c777da07b9cbb359 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 20 Sep 2023 05:50:59 -0700 Subject: [PATCH] Remove protoc from proto_lang_toolchain rule The protoc should be provided with a new proto_toolchain rule. Issue: https://github.com/bazelbuild/rules_proto/issues/179 RELNOTES[INC]: proto_compiler attribute removed from proto_lang_toolchain (it was recently introduced, and there is no evidence of use) PiperOrigin-RevId: 566937038 Change-Id: I7133be4d5cbcc816764c0ba35607329ea50d2406 --- .../starlark/builtins_bzl/common/exports.bzl | 2 +- .../common/proto/proto_lang_toolchain.bzl | 78 ++++++++----------- .../proto_lang_toolchain_custom_protoc.bzl | 23 ------ .../proto_lang_toolchain_default_protoc.bzl | 23 ------ .../proto/proto_lang_toolchain_wrapper.bzl | 35 --------- .../rules/proto/ProtoLangToolchainTest.java | 34 -------- 6 files changed, 34 insertions(+), 161 deletions(-) delete mode 100644 src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_custom_protoc.bzl delete mode 100644 src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_default_protoc.bzl delete mode 100644 src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_wrapper.bzl diff --git a/src/main/starlark/builtins_bzl/common/exports.bzl b/src/main/starlark/builtins_bzl/common/exports.bzl index b96e515a124787..159485f223542b 100755 --- a/src/main/starlark/builtins_bzl/common/exports.bzl +++ b/src/main/starlark/builtins_bzl/common/exports.bzl @@ -29,7 +29,7 @@ load("@_builtins//:common/objc/linking_support.bzl", "linking_support") load("@_builtins//:common/proto/proto_common.bzl", "proto_common_do_not_use") load("@_builtins//:common/proto/proto_library.bzl", "proto_library") load("@_builtins//:common/proto/proto_info.bzl", "ProtoInfo") -load("@_builtins//:common/proto/proto_lang_toolchain_wrapper.bzl", "proto_lang_toolchain") +load("@_builtins//:common/proto/proto_lang_toolchain.bzl", "proto_lang_toolchain") load("@_builtins//:common/python/py_runtime_macro.bzl", "py_runtime") load("@_builtins//:common/python/providers.bzl", "PyCcLinkParamsProvider", "PyInfo", "PyRuntimeInfo") load("@_builtins//:common/java/proto/java_lite_proto_library.bzl", "java_lite_proto_library") 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 86438b2270ef17..872e7af0641c67 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 @@ -32,9 +32,6 @@ def _rule_impl(ctx): if ctx.attr.plugin != None: plugin = ctx.attr.plugin[DefaultInfo].files_to_run - proto_compiler = getattr(ctx.attr, "proto_compiler", None) - proto_compiler = getattr(ctx.attr, "_proto_compiler", proto_compiler) - return [ DefaultInfo( files = depset(), @@ -47,7 +44,7 @@ def _rule_impl(ctx): plugin = plugin, runtime = ctx.attr.runtime, provided_proto_sources = provided_proto_sources, - proto_compiler = proto_compiler.files_to_run, + proto_compiler = ctx.attr._proto_compiler.files_to_run, protoc_opts = ctx.fragments.proto.experimental_protoc_opts, progress_message = ctx.attr.progress_message, mnemonic = ctx.attr.mnemonic, @@ -55,44 +52,35 @@ def _rule_impl(ctx): ), ] -def make_proto_lang_toolchain(custom_proto_compiler): - return rule( - _rule_impl, - attrs = dict( - { - "progress_message": attr.string(default = "Generating proto_library %{label}"), - "mnemonic": attr.string(default = "GenProto"), - "command_line": attr.string(mandatory = True), - "output_files": attr.string(values = ["single", "multiple", "legacy"], default = "legacy"), - "plugin_format_flag": attr.string(), - "plugin": attr.label( - executable = True, - cfg = "exec", - ), - "runtime": attr.label(), - "blacklisted_protos": attr.label_list( - providers = [ProtoInfo], - ), - "allowlist_different_package": attr.label( - default = semantics.allowlist_different_package, - cfg = "exec", - providers = [PackageSpecificationInfo], - ), - }, - **({ - "proto_compiler": attr.label( - cfg = "exec", - executable = True, - ), - } if custom_proto_compiler else { - "_proto_compiler": attr.label( - cfg = "exec", - executable = True, - allow_files = True, - default = configuration_field("proto", "proto_compiler"), - ), - }) - ), - provides = [ProtoLangToolchainInfo], - fragments = ["proto"], - ) +proto_lang_toolchain = rule( + _rule_impl, + attrs = + { + "progress_message": attr.string(default = "Generating proto_library %{label}"), + "mnemonic": attr.string(default = "GenProto"), + "command_line": attr.string(mandatory = True), + "output_files": attr.string(values = ["single", "multiple", "legacy"], default = "legacy"), + "plugin_format_flag": attr.string(), + "plugin": attr.label( + executable = True, + cfg = "exec", + ), + "runtime": attr.label(), + "blacklisted_protos": attr.label_list( + providers = [ProtoInfo], + ), + "allowlist_different_package": attr.label( + default = semantics.allowlist_different_package, + cfg = "exec", + providers = [PackageSpecificationInfo], + ), + "_proto_compiler": attr.label( + cfg = "exec", + executable = True, + allow_files = True, + default = configuration_field("proto", "proto_compiler"), + ), + }, + provides = [ProtoLangToolchainInfo], + fragments = ["proto"], +) diff --git a/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_custom_protoc.bzl b/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_custom_protoc.bzl deleted file mode 100644 index b4157f6aef66ad..00000000000000 --- a/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_custom_protoc.bzl +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2021 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Defines a proto_lang_toolchain rule class with custom proto compiler. - -There are two physical rule classes for proto_lang_toolchain and we want both of them -to have a name string of "proto_lang_toolchain". -""" - -load(":common/proto/proto_lang_toolchain.bzl", "make_proto_lang_toolchain") - -proto_lang_toolchain = make_proto_lang_toolchain(custom_proto_compiler = True) diff --git a/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_default_protoc.bzl b/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_default_protoc.bzl deleted file mode 100644 index 7345bc37fc0013..00000000000000 --- a/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_default_protoc.bzl +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2021 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Defines a proto_lang_toolchain rule class with default proto compiler. - -There are two physical rule classes for proto_lang_toolchain and we want both of them -to have a name string of "proto_lang_toolchain". -""" - -load(":common/proto/proto_lang_toolchain.bzl", "make_proto_lang_toolchain") - -proto_lang_toolchain = make_proto_lang_toolchain(custom_proto_compiler = False) diff --git a/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_wrapper.bzl b/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_wrapper.bzl deleted file mode 100644 index 075e6d73c1c2c0..00000000000000 --- a/src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain_wrapper.bzl +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2021 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Macro encapsulating the proto_lang_toolchain implementation. - -This is needed since proto compiler can be defined, or used as a default one. -There are two implementations of proto_lang_toolchain - one with public proto_compiler attribute, and the other one with private compiler. -""" - -load(":common/proto/proto_lang_toolchain_default_protoc.bzl", toolchain_default_protoc = "proto_lang_toolchain") -load(":common/proto/proto_lang_toolchain_custom_protoc.bzl", toolchain_custom_protoc = "proto_lang_toolchain") - -def proto_lang_toolchain( - proto_compiler = None, - **kwargs): - if proto_compiler != None: - toolchain_custom_protoc( - proto_compiler = proto_compiler, - **kwargs - ) - else: - toolchain_default_protoc( - **kwargs - ) 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 89b9dfadb3fc4b..6536328afe1e67 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 @@ -99,40 +99,6 @@ public void protoToolchain() throws Exception { validateProtoCompiler(toolchain, ProtoConstants.DEFAULT_PROTOC_LABEL); } - @Test - public void protoToolchain_setProtoCompiler() throws Exception { - 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'])", - "cc_binary(name = 'compiler')"); - - 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',", - " proto_compiler = '//third_party/x:compiler',", - ")"); - - ProtoLangToolchainProvider toolchain = - ProtoLangToolchainProvider.get(getConfiguredTarget("//foo:toolchain")); - - validateProtoLangToolchain(toolchain); - validateProtoCompiler(toolchain, "//third_party/x:compiler"); - } - @Test public void protoToolchainBlacklistProtoLibraries() throws Exception { scratch.file(