bazel: Remove hardcoded dependency on `//:protoc` from language runtimes (#19679)

Without this change, language runtimes still result in a build of `//:protoc` even with a prebuilt `proto_toolchain` registered or `--proto_compiler` set to a precompiled protoc. Removing this hardcoded dependency allows a (fast) build of `java_proto_library` targets without a C++ toolchain assuming a prebuilt protoc.

Work towards #19558

Closes #19679

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/19679 from fmeum:19558-dont-hardcode-protoc 3bd939debed27dc8eb092a8d4419cdcf5e571594
FUTURE_COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/19679 from fmeum:19558-dont-hardcode-protoc 3bd939debed27dc8eb092a8d4419cdcf5e571594
PiperOrigin-RevId: 739382145
diff --git a/protobuf.bzl b/protobuf.bzl
index fdf09bd..acb1909 100644
--- a/protobuf.bzl
+++ b/protobuf.bzl
@@ -1,7 +1,9 @@
 load("@bazel_skylib//lib:versions.bzl", "versions")
 load("@rules_cc//cc:defs.bzl", "objc_library")
 load("@rules_python//python:defs.bzl", "py_library")
+load("//bazel/common:proto_common.bzl", "proto_common")
 load("//bazel/common:proto_info.bzl", "ProtoInfo")
+load("//bazel/private:toolchain_helpers.bzl", "toolchains")
 
 def _GetPath(ctx, path):
     if ctx.label.workspace_root:
@@ -71,6 +73,26 @@
         for src in srcs
     ]
 
+_PROTOC_ATTRS = toolchains.if_legacy_toolchain({
+    "_proto_compiler": attr.label(
+        cfg = "exec",
+        executable = True,
+        allow_files = True,
+        default = configuration_field("proto", "proto_compiler"),
+    ),
+})
+_PROTOC_FRAGMENTS = ["proto"]
+_PROTOC_TOOLCHAINS = toolchains.use_toolchain(toolchains.PROTO_TOOLCHAIN)
+
+def _protoc_files_to_run(ctx):
+    if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
+        toolchain = ctx.toolchains[toolchains.PROTO_TOOLCHAIN]
+        if not toolchain:
+            fail("Protocol compiler toolchain could not be resolved.")
+        return toolchain.proto.proto_compiler
+    else:
+        return ctx.attr._proto_compiler[DefaultInfo].files_to_run
+
 ProtoGenInfo = provider(
     fields = ["srcs", "import_flags", "deps"],
 )
@@ -310,7 +332,7 @@
             args.add_all([src.path[offset:] for src in dep.direct_sources])
 
     ctx.actions.run(
-        executable = ctx.executable._protoc,
+        executable = _protoc_files_to_run(ctx),
         inputs = descriptors,
         outputs = [srcjar],
         arguments = [args],
@@ -334,12 +356,9 @@
         "javalite": attr.bool(
             default = False,
         ),
-        "_protoc": attr.label(
-            executable = True,
-            cfg = "exec",
-            default = "//:protoc",
-        ),
-    },
+    } | _PROTOC_ATTRS,
+    fragments = _PROTOC_FRAGMENTS,
+    toolchains = _PROTOC_TOOLCHAINS,
 )
 
 def _internal_gen_kt_protos(ctx):
@@ -373,7 +392,7 @@
             args.add_all([src.path[offset:] for src in dep.direct_sources])
 
     ctx.actions.run(
-        executable = ctx.executable._protoc,
+        executable = _protoc_files_to_run(ctx),
         inputs = descriptors,
         outputs = [srcjar],
         arguments = [args],
@@ -397,12 +416,9 @@
         "lite": attr.bool(
             default = False,
         ),
-        "_protoc": attr.label(
-            executable = True,
-            cfg = "exec",
-            default = "//:protoc",
-        ),
-    },
+    } | _PROTOC_ATTRS,
+    fragments = _PROTOC_FRAGMENTS,
+    toolchains = _PROTOC_TOOLCHAINS,
 )
 
 def internal_objc_proto_library(