Reverse the dependency from rules_proto to protobuf This way rules_proto are using rules defined in protobuf. The redirect to native rules is removed. PiperOrigin-RevId: 650525711
diff --git a/MODULE.bazel b/MODULE.bazel index 18041df..cc513c4 100644 --- a/MODULE.bazel +++ b/MODULE.bazel
@@ -7,7 +7,7 @@ compatibility_level = 1, ) -bazel_dep(name = "bazel_features", version = "1.4.1") +bazel_dep(name = "protobuf", version = "27.1", repo_name = "com_google_protobuf") bazel_dep(name = "bazel_skylib", version = "1.3.0") bazel_dep(name = "rules_cc", version = "0.0.1") bazel_dep(name = "rules_license", version = "0.0.7") @@ -15,5 +15,4 @@ # Dependencies needed in tests bazel_dep(name = "stardoc", version = "0.5.6", dev_dependency = True, repo_name = "io_bazel_stardoc") bazel_dep(name = "googletest", version = "1.11.0", dev_dependency = True, repo_name = "com_google_googletest") -bazel_dep(name = "protobuf", version = "23.1", dev_dependency = True, repo_name = "com_google_protobuf") bazel_dep(name = "platforms", version = "0.0.8", dev_dependency = True)
diff --git a/README.md b/README.md index cf25dae..b9e5578 100644 --- a/README.md +++ b/README.md
@@ -2,58 +2,15 @@ * Postsubmit [](https://buildkite.com/bazel/rules-proto) -This repository contains a Starlark implementation of `proto_library` and related "base layer" rules in Bazel. -It does **not** provide rules for any specific languages. These are typically expected to be provided in the ruleset for the language. +This repository is deprecated. -For a high-level overview of using Protocol Buffers and gRPC with Bazel, see -<https://bazel-contrib.github.io/SIG-rules-authors/proto-grpc.html>. - -For the list of Proto rules, see the Bazel -[documentation](https://docs.bazel.build/versions/master/be/overview.html). +The proto library implementation `proto_library` and rules for other languages like Java, Python, C++ are +in [protobuf repository](http://github.com/google/protobuf). ## Roadmap -As of September 2023, this repo is mostly empty and contains stubs for `proto_library`, `ProtoInfo` and other proto toolchain support rules. +As of May 2024, we decided to move the implementation of the rules together with proto compiler into protobuf repository. -This will change soon, around the time Bazel 7 is released (in Oct/Nov 2023), because the stubs will be replaced with actual implementations. -We plan to remove the native implementations in Bazel 8, at which point rules_proto will be the only way to build Protobuf rules. +As such this repository is deprecated and serves only as a helper in the migration. -As such it's recommended to use this repository now, so that the removal of implementations from Bazel 8 does not cause you problems. - -## Getting Started - -To get started with `rules_proto`, add the `WORKSPACE` file snippet from the -[latest release](https://github.com/bazelbuild/rules_proto/releases). - -Then, in your `BUILD` files, import and use the rules: - -```starlark -load("@rules_proto//proto:defs.bzl", "proto_library") -proto_library( - ... -) -``` - -If you're migrating from the native proto rules to `rules_proto`, you can use -the following [buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md) -command to automate the changes to your `BUILD` and `.bzl` files: - -```bash -buildifier --lint=fix --warnings=native-proto <path/to/BUILD> -``` - -## Contributing - -Bazel and `rules_proto` are the work of many contributors. -We appreciate your help! - -To contribute, please read the contribution guidelines: -[CONTRIBUTING.md](https://github.com/bazelbuild/rules_proto/blob/master/CONTRIBUTING.md). - -Note that the `rules_proto` use the GitHub issue tracker for bug reports and -feature requests only. - -For asking questions see: - -* [rules_proto mailing list](https://groups.google.com/forum/#!forum/proto-bazel-discuss) * Slack channel `#proto` on [slack.bazel.build](https://slack.bazel.build)
diff --git a/WORKSPACE b/WORKSPACE index 6ad42c0..2777c9b 100644 --- a/WORKSPACE +++ b/WORKSPACE
@@ -21,9 +21,6 @@ bazel_skylib_workspace() -load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") - -protobuf_deps() load("@bazelci_rules//:rbe_repo.bzl", "rbe_preconfig")
diff --git a/dev_deps.bzl b/dev_deps.bzl index 77f1b2d..ec42c21 100644 --- a/dev_deps.bzl +++ b/dev_deps.bzl
@@ -45,17 +45,3 @@ "https://github.com/bazelbuild/continuous-integration/releases/download/rules-1.0.0/bazelci_rules-1.0.0.tar.gz", ], ) - - http_archive( - name = "com_google_protobuf", - sha256 = "dc167b7d23ec0d6e4a3d4eae1798de6c8d162e69fa136d39753aaeb7a6e1289d", - strip_prefix = "protobuf-23.1", - urls = [ - "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v23.1.tar.gz", - "https://github.com/protocolbuffers/protobuf/archive/v23.1.tar.gz", - ], - remote_patch_strip = 1, - remote_patches = { - "https://raw.githubusercontent.com/bazelbuild/bazel-central-registry/main/modules/protobuf/23.1/patches/0007-bazel-Get-rid-of-exec_tools.-13401.patch": "sha256-Thj5ZYqMpgaUrjZv8XyWqyD+I6XQNcZjo4jI14a7QxE=", - }, - )
diff --git a/proto/BUILD b/proto/BUILD index bc863ea..0b6a340 100644 --- a/proto/BUILD +++ b/proto/BUILD
@@ -14,8 +14,8 @@ deps = [ ":proto_lang_toolchain", ":proto_toolchain", - "//proto/private:native", "//proto/private/rules:proto_descriptor_set", + "@com_google_protobuf//bazel:proto_library_bzl", ], ) @@ -31,7 +31,8 @@ "proto_lang_toolchain.bzl", ], deps = [ - ":proto_common", + "@com_google_protobuf//bazel/common:proto_lang_toolchain_info_bzl", + "@com_google_protobuf//bazel/toolchains:proto_lang_toolchain_bzl", ], ) @@ -40,6 +41,9 @@ srcs = [ "proto_common.bzl", ], + deps = [ + "@com_google_protobuf//bazel/common:proto_common_bzl", + ], ) bzl_library( @@ -49,7 +53,7 @@ ], visibility = ["//visibility:public"], deps = [ - "//proto/private/rules:proto_toolchain_bzl", + "@com_google_protobuf//bazel/toolchains:proto_toolchain_bzl", ], )
diff --git a/proto/defs.bzl b/proto/defs.bzl index 69a1969..0b52001 100644 --- a/proto/defs.bzl +++ b/proto/defs.bzl
@@ -14,20 +14,13 @@ """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/private:native.bzl", "NativeProtoInfo", "native_proto_common") +load("@com_google_protobuf//bazel:proto_library.bzl", _proto_library = "proto_library") +load("@com_google_protobuf//bazel/common:proto_common.bzl", _proto_common = "proto_common") +load("@com_google_protobuf//bazel/common:proto_info.bzl", _ProtoInfo = "ProtoInfo") +load("@com_google_protobuf//bazel/toolchains:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain") +load("@com_google_protobuf//bazel/toolchains:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain") load("//proto/private/rules:proto_descriptor_set.bzl", _proto_descriptor_set = "proto_descriptor_set") -_MIGRATION_TAG = "__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__" - -def _add_migration_tag(attrs): - if "tags" in attrs and attrs["tags"] != None: - attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG] - else: - attrs["tags"] = [_MIGRATION_TAG] - return attrs - def proto_library(**attrs): """Bazel proto_library rule. @@ -37,8 +30,7 @@ **attrs: Rule attributes """ - # buildifier: disable=native-proto - native.proto_library(**_add_migration_tag(attrs)) + _proto_library(**attrs) proto_descriptor_set = _proto_descriptor_set @@ -46,12 +38,6 @@ proto_toolchain = _proto_toolchain -# Encapsulates information provided by `proto_library`. -# -# https://docs.bazel.build/versions/master/skylark/lib/ProtoInfo.html -ProtoInfo = NativeProtoInfo +ProtoInfo = _ProtoInfo -# Utilities for protocol buffers. -# -# https://docs.bazel.build/versions/master/skylark/lib/proto_common.html -proto_common = native_proto_common +proto_common = _proto_common
diff --git a/proto/private/BUILD b/proto/private/BUILD deleted file mode 100644 index 92ef5fd..0000000 --- a/proto/private/BUILD +++ /dev/null
@@ -1,7 +0,0 @@ -load("@bazel_skylib//:bzl_library.bzl", "bzl_library") - -bzl_library( - name = "native", - srcs = ["native.bzl"], - visibility = ["//proto:__subpackages__"], -)
diff --git a/proto/private/native.bzl b/proto/private/native.bzl deleted file mode 100644 index 3185eae..0000000 --- a/proto/private/native.bzl +++ /dev/null
@@ -1,28 +0,0 @@ -# Copyright 2019 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. - -# Redefine native symbols with a new name as a workaround for -# exporting them in `//proto:defs.bzl` with their original name. -# -# While we cannot force users to load these symbol due to the lack of a -# allowlisting mechanism, we can still export them and tell users to -# load it to make a future migration to pure Starlark easier. - -"""Lovely workaround to be able to expose native constants pretending to be Starlark.""" - -# buildifier: disable=native-proto -NativeProtoInfo = ProtoInfo - -# buildifier: disable=native-proto -native_proto_common = proto_common_do_not_use
diff --git a/proto/private/rules/BUILD b/proto/private/rules/BUILD index 437109b..2626d29 100644 --- a/proto/private/rules/BUILD +++ b/proto/private/rules/BUILD
@@ -9,18 +9,6 @@ "//proto:__subpackages__", ], deps = [ - "//proto/private:native", - ], -) - -bzl_library( - name = "proto_toolchain_bzl", - srcs = [ - "proto_toolchain.bzl", - "proto_toolchain_rule.bzl", - "@bazel_features//:features", - ], - visibility = [ - "//proto:__subpackages__", + "@com_google_protobuf//bazel/common:proto_info_bzl", ], )
diff --git a/proto/private/rules/proto_descriptor_set.bzl b/proto/private/rules/proto_descriptor_set.bzl index 1b184fa..6d9cdff 100644 --- a/proto/private/rules/proto_descriptor_set.bzl +++ b/proto/private/rules/proto_descriptor_set.bzl
@@ -11,7 +11,6 @@ # 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. - """A rule for generating a `FileDescriptorSet` with all transitive dependencies. This module contains the definition of `proto_descriptor_set`, a rule that @@ -19,7 +18,7 @@ a single `FileDescriptorSet` containing all the `FileDescriptorProto` from them. """ -load("//proto/private:native.bzl", ProtoInfo = "NativeProtoInfo") +load("@com_google_protobuf//bazel/common:proto_info.bzl", "ProtoInfo") def _proto_descriptor_set_impl(ctx): args = ctx.actions.args()
diff --git a/proto/private/rules/proto_toolchain.bzl b/proto/private/rules/proto_toolchain.bzl deleted file mode 100644 index a091b80..0000000 --- a/proto/private/rules/proto_toolchain.bzl +++ /dev/null
@@ -1,40 +0,0 @@ -# Copyright 2023 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 wrapping the proto_toolchain implementation. - -The macro additionally creates toolchain target when toolchain_type is given. -""" - -load(":proto_toolchain_rule.bzl", _proto_toolchain_rule = "proto_toolchain") - -def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []): - """Creates a proto_toolchain and toolchain target for proto_library. - - Toolchain target is suffixed with "_toolchain". - - Args: - name: name of the toolchain - proto_compiler: (Label) of either proto compiler sources or prebuild binaries - exec_compatible_with: ([constraints]) List of constraints the prebuild binary is compatible with. - """ - _proto_toolchain_rule(name = name, proto_compiler = proto_compiler) - - native.toolchain( - name = name + "_toolchain", - toolchain_type = "//proto:toolchain_type", - exec_compatible_with = exec_compatible_with, - target_compatible_with = [], - toolchain = name, - )
diff --git a/proto/private/rules/proto_toolchain_rule.bzl b/proto/private/rules/proto_toolchain_rule.bzl deleted file mode 100644 index 70d55fc..0000000 --- a/proto/private/rules/proto_toolchain_rule.bzl +++ /dev/null
@@ -1,70 +0,0 @@ -# Copyright 2023 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. - -"""A Starlark implementation of the proto_toolchain rule.""" - -load("@bazel_features//:features.bzl", "bazel_features") -load("//proto:proto_common.bzl", "ProtoLangToolchainInfo", "proto_common") - -def _impl(ctx): - kwargs = {} - if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False): - kwargs["toolchain_type"] = "//proto:toolchain_type" - - # allowlist_different_package is only available after Bazel versions > 6.4.0 - # therefore needs to be set conditionally. We are using bazel_features for this - # but there's no specific feature flag for `allowlist_different_package` so we'll - # just use module_extension_has_os_arch_dependent which checks if Bazel version >= 6.4.0 - # See: https://github.com/protocolbuffers/protobuf/pull/14590#discussion_r1398778415 - # See: https://github.com/bazel-contrib/bazel_features/blob/443861571a389ddc16d17690ab8e46ee87b4ea57/features.bzl#L25C5-L25C43 - if bazel_features.external_deps.module_extension_has_os_arch_dependent: - kwargs["allowlist_different_package"] = None - - return [ - DefaultInfo( - files = depset(), - runfiles = ctx.runfiles(), - ), - platform_common.ToolchainInfo( - proto = ProtoLangToolchainInfo( - out_replacement_format_flag = ctx.attr.command_line, - output_files = ctx.attr.output_files, - plugin = None, - runtime = None, - 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, - **kwargs - ), - ), - ] - -proto_toolchain = rule( - _impl, - attrs = - { - "progress_message": attr.string(default = "Generating Descriptor Set proto_library %{label}"), - "mnemonic": attr.string(default = "GenProtoDescriptorSet"), - "command_line": attr.string(default = "--descriptor_set_out=%s"), - "output_files": attr.string(values = ["single", "multiple", "legacy"], default = "single"), - "proto_compiler": attr.label( - cfg = "exec", - executable = True, - allow_files = True, # Used by mocks in tests. Consider fixing tests and removing it. - ), - }, - provides = [platform_common.ToolchainInfo], - fragments = ["proto"], -)
diff --git a/proto/proto_common.bzl b/proto/proto_common.bzl index 3e15665..08d678b 100644 --- a/proto/proto_common.bzl +++ b/proto/proto_common.bzl
@@ -17,11 +17,10 @@ # https://docs.bazel.build/versions/master/skylark/lib/proto_common.html """proto_common module""" -load("//proto/private:native.bzl", "native_proto_common") +load("@com_google_protobuf//bazel/common:proto_common.bzl", _proto_common = "proto_common") +load("@com_google_protobuf//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") -proto_common = native_proto_common - -ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo +proto_common = _proto_common # reexport proto_common for current users def _incompatible_toolchains_enabled(): return getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False)
diff --git a/proto/proto_lang_toolchain.bzl b/proto/proto_lang_toolchain.bzl index a2cd479..00ac172 100644 --- a/proto/proto_lang_toolchain.bzl +++ b/proto/proto_lang_toolchain.bzl
@@ -13,35 +13,6 @@ # limitations under the License. """proto_lang_toolchain rule""" -load("//proto:proto_common.bzl", "proto_common") +load("@com_google_protobuf//bazel/toolchains:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain") -def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs): - """Creates a proto_lang_toolchain and corresponding toolchain target. - - Toolchain target is only created when toolchain_type is set. - - https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_lang_toolchain - - Args: - - name: name of the toolchain - toolchain_type: The toolchain type - exec_compatible_with: ([constraints]) List of constraints the prebuild binaries is compatible with. - target_compatible_with: ([constraints]) List of constraints the target libraries are compatible with. - **attrs: Rule attributes - """ - - if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False): - attrs["toolchain_type"] = toolchain_type - - # buildifier: disable=native-proto - native.proto_lang_toolchain(name = name, **attrs) - - if toolchain_type: - native.toolchain( - name = name + "_toolchain", - toolchain_type = toolchain_type, - exec_compatible_with = exec_compatible_with, - target_compatible_with = target_compatible_with, - toolchain = name, - ) +proto_lang_toolchain = _proto_lang_toolchain
diff --git a/proto/proto_toolchain.bzl b/proto/proto_toolchain.bzl index e1a853c..6d8175e 100644 --- a/proto/proto_toolchain.bzl +++ b/proto/proto_toolchain.bzl
@@ -14,6 +14,6 @@ """Export for proto_toolchain""" -load("//proto/private/rules:proto_toolchain.bzl", _proto_toolchain_macro = "proto_toolchain") +load("@com_google_protobuf//bazel/toolchains:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain") -proto_toolchain = _proto_toolchain_macro +proto_toolchain = _proto_toolchain
diff --git a/proto/repositories.bzl b/proto/repositories.bzl index 9c7249b..d6d48e0 100644 --- a/proto/repositories.bzl +++ b/proto/repositories.bzl
@@ -23,6 +23,15 @@ """ maybe( http_archive, + name = "com_google_protobuf", + sha256 = "6fbe2e6f703bcd3a246529c2cab586ca12a98c4e641f5f71d51fde09eb48e9e7", + strip_prefix = "protobuf-27.1", + urls = [ + "https://github.com/protocolbuffers/protobuf/archive/v27.1.tar.gz", + ], + ) + maybe( + http_archive, name = "bazel_skylib", sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506", urls = [ @@ -41,14 +50,6 @@ maybe( http_archive, - name = "bazel_features", - sha256 = "0f23d75c7623d6dba1fd30513a94860447de87c8824570521fcc966eda3151c2", - strip_prefix = "bazel_features-1.4.1", - url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.4.1/bazel_features-v1.4.1.tar.gz", - ) - - maybe( - http_archive, name = "rules_cc", sha256 = "4aeb102efbcfad509857d7cb9c5456731e8ce566bfbf2960286a2ec236796cc3", strip_prefix = "rules_cc-2f8c04c04462ab83c545ab14c0da68c3b4c96191", @@ -57,3 +58,11 @@ "https://github.com/bazelbuild/rules_cc/archive/2f8c04c04462ab83c545ab14c0da68c3b4c96191.tar.gz", ], ) + + maybe( + http_archive, + name = "rules_python", + sha256 = "d70cd72a7a4880f0000a6346253414825c19cdd40a28289bdf67b8e6480edff8", + strip_prefix = "rules_python-0.28.0", + url = "https://github.com/bazelbuild/rules_python/releases/download/0.28.0/rules_python-0.28.0.tar.gz", + )
diff --git a/proto/setup.bzl b/proto/setup.bzl index d1f7f16..9ee3506 100644 --- a/proto/setup.bzl +++ b/proto/setup.bzl
@@ -13,7 +13,9 @@ # limitations under the License. """Setup required to use rules_proto.""" -load("@bazel_features//:deps.bzl", "bazel_features_deps") +load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") +load("@rules_python//python:repositories.bzl", "py_repositories") def rules_proto_setup(): - bazel_features_deps() + protobuf_deps() + py_repositories()