From 9f9cb7a10e5c49c2e81b8352292e4e84a5eb7cfa Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 22 Jul 2024 14:32:10 -0700 Subject: [PATCH] Move proto_toolchain from rules_proto to protobuf The toolchain type is consumed by proto_library and produced by proto_toolchain rule. As such it's a private dependency, because both rules are now part of protobuf repo. There are some early adopters of --incompatible_enable_proto_toolchain_resolution that might be broken from this: grpc, rules_go, rules_ts, rules_rust, rules_lint, because they have implementation that is not using proto_common. Those repositories need to define their own proto_lang_toolchain and consume it with proto_common.compile. PiperOrigin-RevId: 654897871 --- MODULE.bazel | 5 ++--- bazel/private/BUILD.bazel | 9 +++++++++ bazel/private/proto_toolchain_rule.bzl | 8 ++------ bazel/private/toolchain_helpers.bzl | 2 +- bazel/toolchains/BUILD.bazel | 1 + bazel/toolchains/proto_toolchain.bzl | 3 ++- protobuf_deps.bzl | 11 ----------- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index fcf2eeaa958a..05128b355cc1 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -27,9 +27,6 @@ bazel_dep(name = "platforms", version = "0.0.8") bazel_dep(name = "zlib", version = "1.3.1") bazel_dep(name = "bazel_features", version = "1.13.0", repo_name = "proto_bazel_features") -# TODO: remove after toolchain types are moved to protobuf -bazel_dep(name = "rules_proto", version = "4.0.0") - SUPPORTED_PYTHON_VERSIONS = [ "3.8", "3.9", @@ -99,3 +96,5 @@ use_repo(maven, "maven") # Development dependencies bazel_dep(name = "googletest", version = "1.14.0", repo_name = "com_google_googletest", dev_dependency = True) bazel_dep(name = "rules_testing", version = "0.6.0", dev_dependency = True) +# rules_proto are needed for @com_google_protobuf_v25.0 used in //compatibility/... tests +bazel_dep(name = "rules_proto", version = "4.0.0", dev_dependency = True) \ No newline at end of file diff --git a/bazel/private/BUILD.bazel b/bazel/private/BUILD.bazel index 407c32591612..c90632d7b0ed 100644 --- a/bazel/private/BUILD.bazel +++ b/bazel/private/BUILD.bazel @@ -10,6 +10,10 @@ load("//bazel/private:native_bool_flag.bzl", "native_bool_flag") licenses(["notice"]) +toolchain_type( + name = "proto_toolchain_type", +) + bzl_library( name = "upb_proto_library_internal_bzl", srcs = [ @@ -40,6 +44,11 @@ bzl_library( "proto_toolchain_rule.bzl", ], visibility = ["//bazel:__subpackages__"], + deps = [ + ":toolchain_helpers_bzl", + "//bazel/common:proto_common_bzl", + "//bazel/common:proto_lang_toolchain_info_bzl", + ], ) bzl_library( diff --git a/bazel/private/proto_toolchain_rule.bzl b/bazel/private/proto_toolchain_rule.bzl index 9487a06026de..8a11fac9db02 100644 --- a/bazel/private/proto_toolchain_rule.bzl +++ b/bazel/private/proto_toolchain_rule.bzl @@ -1,13 +1,9 @@ """A Starlark implementation of the proto_toolchain rule.""" -load("//bazel/common:proto_common.bzl", "proto_common") load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") +load("//bazel/private:toolchain_helpers.bzl", "toolchains") def _impl(ctx): - kwargs = {} - if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False): - kwargs["toolchain_type"] = "@rules_proto//proto:toolchain_type" - return [ DefaultInfo( files = depset(), @@ -23,7 +19,7 @@ def _impl(ctx): protoc_opts = ctx.fragments.proto.experimental_protoc_opts, progress_message = ctx.attr.progress_message, mnemonic = ctx.attr.mnemonic, - **kwargs + toolchain_type = toolchains.PROTO_TOOLCHAIN, ), ), ] diff --git a/bazel/private/toolchain_helpers.bzl b/bazel/private/toolchain_helpers.bzl index e58c9d6a0ece..aa49eb8cf4c8 100644 --- a/bazel/private/toolchain_helpers.bzl +++ b/bazel/private/toolchain_helpers.bzl @@ -45,5 +45,5 @@ toolchains = struct( find_toolchain = _find_toolchain, if_legacy_toolchain = _if_legacy_toolchain, INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION = _incompatible_toolchain_resolution, - PROTO_TOOLCHAIN = "@rules_proto//proto:toolchain_type", + PROTO_TOOLCHAIN = "//bazel/private:proto_toolchain_type", ) diff --git a/bazel/toolchains/BUILD.bazel b/bazel/toolchains/BUILD.bazel index 34a45d5bbd83..2c21426b1534 100644 --- a/bazel/toolchains/BUILD.bazel +++ b/bazel/toolchains/BUILD.bazel @@ -8,6 +8,7 @@ bzl_library( visibility = ["//visibility:public"], deps = [ "//bazel/private:proto_toolchain_rule_bzl", + "//bazel/private:toolchain_helpers_bzl", ], ) diff --git a/bazel/toolchains/proto_toolchain.bzl b/bazel/toolchains/proto_toolchain.bzl index 171e08cad5ac..1bad37b3a65c 100644 --- a/bazel/toolchains/proto_toolchain.bzl +++ b/bazel/toolchains/proto_toolchain.bzl @@ -4,6 +4,7 @@ The macro additionally creates toolchain target when toolchain_type is given. """ load("//bazel/private:proto_toolchain_rule.bzl", _proto_toolchain_rule = "proto_toolchain") +load("//bazel/private:toolchain_helpers.bzl", "toolchains") def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []): """Creates a proto_toolchain and toolchain target for proto_library. @@ -19,7 +20,7 @@ def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []): native.toolchain( name = name + "_toolchain", - toolchain_type = "@rules_proto//proto:toolchain_type", + toolchain_type = toolchains.PROTO_TOOLCHAIN, exec_compatible_with = exec_compatible_with, target_compatible_with = [], toolchain = name, diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl index 97f95e3ac4a1..0c90a525d4a3 100644 --- a/protobuf_deps.bzl +++ b/protobuf_deps.bzl @@ -120,17 +120,6 @@ def protobuf_deps(): sha256 = "160d1ebf33763124766fb35316329d907ca67f733238aa47624a8e3ff3cf2ef4", ) - # TODO: remove after toolchain types are moved to protobuf - if not native.existing_rule("rules_proto"): - http_archive( - name = "rules_proto", - sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd", - strip_prefix = "rules_proto-5.3.0-21.7", - urls = [ - "https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz", - ], - ) - if not native.existing_rule("proto_bazel_features"): proto_bazel_features(name = "proto_bazel_features")