Add protobuf patch that has for_bazel_tests

This works towards running Bazel's unit tests against rules implemented in protobuf repository.

PiperOrigin-RevId: 684767641
Change-Id: I5fbb22b5d30dcd0dbdee6ca7b3170688fd85208a
diff --git a/BUILD b/BUILD
index df1a129..38a1725 100644
--- a/BUILD
+++ b/BUILD
@@ -89,6 +89,7 @@
         "//third_party/protobuf:remove_rules_rust.patch",
         "//third_party/protobuf:add_python_loads.patch",
         "//third_party/protobuf:add_rules_shell_loads.patch",
+        "//third_party/protobuf:bzl_library.patch",
     ],
     outs = ["MODULE.bazel.lock.dist"],
     cmd = " && ".join([
diff --git a/MODULE.bazel b/MODULE.bazel
index 12b52e2..0597439 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -49,6 +49,7 @@
         "//third_party/protobuf:remove_rules_rust.patch",
         "//third_party/protobuf:add_python_loads.patch",
         "//third_party/protobuf:add_rules_shell_loads.patch",
+        "//third_party/protobuf:bzl_library.patch",
     ],
     strip_prefix = "protobuf-29.0-rc1",
     urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v29.0-rc1/protobuf-29.0-rc1.zip"],
diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD
index 72aba48..2be5732 100644
--- a/src/test/shell/bazel/BUILD
+++ b/src/test/shell/bazel/BUILD
@@ -34,6 +34,7 @@
         "//src/test/shell:bin/bazel",
         "//third_party/protobuf:add_python_loads.patch",
         "//third_party/protobuf:add_rules_shell_loads.patch",
+        "//third_party/protobuf:bzl_library.patch",
         "//third_party/protobuf:proto_info_bzl_deps.patch",
         "//third_party/protobuf:remove_rules_rust.patch",
     ],
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index b54dce2..cfcc9e0 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -598,6 +598,7 @@
   cp "$(rlocation io_bazel/third_party/protobuf/proto_info_bzl_deps.patch)" third_party/protobuf/proto_info_bzl_deps.patch
   cp "$(rlocation io_bazel/third_party/protobuf/add_python_loads.patch)" third_party/protobuf/add_python_loads.patch
   cp "$(rlocation io_bazel/third_party/protobuf/add_rules_shell_loads.patch)" third_party/protobuf/add_rules_shell_loads.patch
+  cp "$(rlocation io_bazel/third_party/protobuf/bzl_library.patch)" third_party/protobuf/bzl_library.patch
   cat >> "$1" <<EOF
 archive_override(
     module_name = "protobuf",
@@ -611,6 +612,7 @@
         "//third_party/protobuf:remove_rules_rust.patch",
         "//third_party/protobuf:add_python_loads.patch",
         "//third_party/protobuf:add_rules_shell_loads.patch",
+        "//third_party/protobuf:bzl_library.patch",
     ],
     strip_prefix = "protobuf-29.0-rc1",
     urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v29.0-rc1/protobuf-29.0-rc1.zip"],
diff --git a/third_party/protobuf/BUILD b/third_party/protobuf/BUILD
index db98e52..125b410 100644
--- a/third_party/protobuf/BUILD
+++ b/third_party/protobuf/BUILD
@@ -12,6 +12,7 @@
     "remove_rules_rust.patch",
     "add_python_loads.patch",
     "add_rules_shell_loads.patch",
+    "bzl_library.patch",
 ])
 
 alias(
diff --git a/third_party/protobuf/bzl_library.patch b/third_party/protobuf/bzl_library.patch
new file mode 100644
index 0000000..407eef9
--- /dev/null
+++ b/third_party/protobuf/bzl_library.patch
@@ -0,0 +1,246 @@
+diff --git a/bazel/BUILD.bazel b/bazel/BUILD.bazel
+index d0546f9b3..71190a6ca 100644
+--- a/bazel/BUILD.bazel
++++ b/bazel/BUILD.bazel
+@@ -13,24 +13,31 @@ bzl_library(
+     name = "proto_library_bzl",
+     srcs = ["proto_library.bzl"],
+     visibility = ["//visibility:public"],
++    deps = [
++        "//bazel/private:bazel_proto_library_rule_bzl",
++        "@proto_bazel_features//:features",
++    ],
+ )
+ 
+ bzl_library(
+     name = "cc_proto_library_bzl",
+     srcs = ["cc_proto_library.bzl"],
+     visibility = ["//visibility:public"],
++    deps = ["//bazel/private:bazel_cc_proto_library_bzl"],
+ )
+ 
+ bzl_library(
+     name = "java_proto_library_bzl",
+     srcs = ["java_proto_library.bzl"],
+     visibility = ["//visibility:public"],
++    deps = ["//bazel/private:bazel_java_proto_library_rule_bzl"],
+ )
+ 
+ bzl_library(
+     name = "java_lite_proto_library_bzl",
+     srcs = ["java_lite_proto_library.bzl"],
+     visibility = ["//visibility:public"],
++    deps = ["//bazel/private:java_lite_proto_library_bzl"],
+ )
+ 
+ bzl_library(
+@@ -56,3 +63,19 @@ bzl_library(
+     visibility = ["//visibility:public"],
+     deps = ["//bazel/private:upb_proto_library_internal_bzl"],
+ )
++
++# The data in this target is exposed in //bazel/private:for_bazel_tests
++filegroup(
++    name = "for_bazel_tests",
++    testonly = True,
++    srcs = [
++        "BUILD.bazel",
++        ":cc_proto_library_bzl",
++        ":java_lite_proto_library_bzl",
++        ":proto_library_bzl",
++        ":py_proto_library_bzl",
++        "//bazel/common:for_bazel_tests",
++        "//bazel/toolchains:for_bazel_tests",
++    ],
++    visibility = ["//bazel/private:__pkg__"],
++)
+diff --git a/bazel/common/BUILD b/bazel/common/BUILD
+index 9ab40ff05..073e56411 100644
+--- a/bazel/common/BUILD
++++ b/bazel/common/BUILD
+@@ -10,6 +10,7 @@ bzl_library(
+     visibility = ["//visibility:public"],
+     deps = [
+         ":proto_lang_toolchain_info_bzl",
++        "//bazel/private:native_bzl",
+         "//bazel/private:toolchain_helpers_bzl",
+         "@proto_bazel_features//:features",
+     ],
+@@ -39,9 +40,13 @@ bzl_library(
+ )
+ 
+ filegroup(
+-    name = "bazel_osx_p4deps",
+-    srcs = glob(["**"]) + ["@proto_bazel_features//:features"],
+-    visibility = [
+-        "//bazel:__pkg__",
++    name = "for_bazel_tests",
++    testonly = True,
++    srcs = [
++        "BUILD",
++        "proto_common_bzl",
++        "proto_info_bzl",
++        "proto_lang_toolchain_info_bzl",
+     ],
++    visibility = ["//bazel:__pkg__"],
+ )
+diff --git a/bazel/private/BUILD b/bazel/private/BUILD
+index 8e5207f3a..16c2bc536 100644
+--- a/bazel/private/BUILD
++++ b/bazel/private/BUILD
+@@ -1,5 +1,5 @@
+ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+-load("//bazel/private:native_bool_flag.bzl", "native_bool_flag")
++load(":native_bool_flag.bzl", "native_bool_flag")
+ 
+ package(default_applicable_licenses = ["//:license"])
+ 
+@@ -59,15 +59,60 @@ bzl_library(
+     ],
+     visibility = ["//bazel:__subpackages__"],
+     deps = [
++        ":toolchain_helpers_bzl",
+         "//bazel/common:proto_common_bzl",
+         "//bazel/common:proto_info_bzl",
+-        "//bazel/private:toolchain_helpers_bzl",
+         "@bazel_skylib//lib:paths",
+         "@bazel_skylib//rules:common_settings",
+         "@proto_bazel_features//:features",
+     ],
+ )
+ 
++bzl_library(
++    name = "bazel_java_proto_library_rule_bzl",
++    srcs = [
++        "bazel_java_proto_library_rule.bzl",
++        "java_proto_support.bzl",
++    ],
++    visibility = ["//bazel:__subpackages__"],
++    deps = [
++        ":toolchain_helpers_bzl",
++        "//bazel/common:proto_common_bzl",
++        "//bazel/common:proto_info_bzl",
++        "@rules_java//java/common",
++    ],
++)
++
++bzl_library(
++    name = "java_lite_proto_library_bzl",
++    srcs = [
++        "java_lite_proto_library.bzl",
++        "java_proto_support.bzl",
++    ],
++    visibility = ["//bazel:__subpackages__"],
++    deps = [
++        ":toolchain_helpers_bzl",
++        "//bazel/common:proto_common_bzl",
++        "//bazel/common:proto_info_bzl",
++        "@rules_java//java/common",
++    ],
++)
++
++bzl_library(
++    name = "bazel_cc_proto_library_bzl",
++    srcs = [
++        "bazel_cc_proto_library.bzl",
++        "cc_proto_support.bzl",
++    ],
++    visibility = ["//bazel:__subpackages__"],
++    deps = [
++        ":toolchain_helpers_bzl",
++        "//bazel/common:proto_common_bzl",
++        "//bazel/common:proto_info_bzl",
++        "@proto_bazel_features//:features",
++    ],
++)
++
+ bzl_library(
+     name = "proto_toolchain_rule_bzl",
+     srcs = [
+@@ -75,9 +120,9 @@ bzl_library(
+     ],
+     visibility = ["//bazel:__subpackages__"],
+     deps = [
++        ":toolchain_helpers_bzl",
+         "//bazel/common:proto_common_bzl",
+         "//bazel/common:proto_lang_toolchain_info_bzl",
+-        "//bazel/private:toolchain_helpers_bzl",
+     ],
+ )
+ 
+@@ -139,10 +184,21 @@ native_bool_flag(
+     visibility = ["//bazel:__subpackages__"],
+ )
+ 
++bzl_library(
++    name = "native_bool_flag_bzl",
++    srcs = ["native_bool_flag.bzl"],
++    visibility = ["//visibility:private"],
++    deps = ["@bazel_skylib//rules:common_settings"],
++)
++
+ filegroup(
+-    name = "bazel_osx_p4deps",
+-    srcs = glob(["**"]),
+-    visibility = [
+-        "//bazel:__pkg__",
++    name = "for_bazel_tests",
++    testonly = True,
++    srcs = [
++        "BUILD",
++        ":native_bool_flag_bzl",
++        "//bazel:for_bazel_tests",
++        "//bazel/private/toolchains:for_bazel_tests",
+     ],
++    visibility = ["//visibility:public"],
+ )
+diff --git a/bazel/private/toolchain_helpers.bzl b/bazel/private/toolchain_helpers.bzl
+index aa49eb8cf..edf3a8b69 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 = "//bazel/private:proto_toolchain_type",
++    PROTO_TOOLCHAIN = Label("//bazel/private:proto_toolchain_type"),
+ )
+diff --git a/bazel/private/toolchains/BUILD.bazel b/bazel/private/toolchains/BUILD.bazel
+index b727a4a97..b21e46276 100644
+--- a/bazel/private/toolchains/BUILD.bazel
++++ b/bazel/private/toolchains/BUILD.bazel
+@@ -72,3 +72,14 @@ toolchain(
+     toolchain = "//java/lite:toolchain",
+     toolchain_type = "@rules_java//java/proto:lite_toolchain_type",
+ )
++
++filegroup(
++    name = "for_bazel_tests",
++    testonly = True,
++    srcs = [
++        "BUILD.bazel",
++    ],
++    visibility = [
++        "//bazel/private:__pkg__",
++    ],
++)
+diff --git a/bazel/toolchains/BUILD b/bazel/toolchains/BUILD
+index 6353a6544..2f629a69e 100644
+--- a/bazel/toolchains/BUILD
++++ b/bazel/toolchains/BUILD
+@@ -28,8 +28,13 @@ bzl_library(
+ )
+ 
+ filegroup(
+-    name = "bazel_osx_p4deps",
+-    srcs = glob(["**"]),
++    name = "for_bazel_tests",
++    testonly = True,
++    srcs = [
++        "BUILD",
++        "proto_lang_toolchain_bzl",
++        "proto_toolchain_bzl",
++    ],
+     visibility = [
+         "//bazel:__pkg__",
+     ],