build/toolchain/protobuf: fix duplicate protoc build

We were building way too many copies of protoc. Many of these are due to
a bug in rules_go when --incompatible_enable_proto_toolchain_resolution
is enabled, but not all, so we should do this anyway, and then the bug
in rules_go is no longer relevant.

Before:
bazel cquery "deps(//...) intersect @protobuf//:protoc"
@protobuf//:protoc (e5c4511)
@protobuf//:protoc (40d0309)
@protobuf//:protoc (3bbe29a)
@protobuf//:protoc (070fbf5)
@protobuf//:protoc (ee343a7)
@protobuf//:protoc (c3e4ff6)

After:
bazel cquery "deps(//... except //build/toolchain/protobuf/...) intersect @protobuf//:protoc"
@protobuf//:protoc (ee343a7)

Change-Id: I6a826b7bc01453284982204aea808f8f2902d84c
Reviewed-on: https://review.monogon.dev/c/monogon/+/4164
Reviewed-by: Tim Windelschmidt <tim@monogon.tech>
Tested-by: Jenkins CI
diff --git a/build/toolchain/protobuf/def.bzl b/build/toolchain/protobuf/def.bzl
new file mode 100644
index 0000000..ea6a9b5
--- /dev/null
+++ b/build/toolchain/protobuf/def.bzl
@@ -0,0 +1,34 @@
+load("//osbase/build:def.bzl", "forward_impl")
+
+# This is a copy of ignore_unused_configuration_target from
+# //osbase/build:def.bzl with specific settings for the protobuf toolchain.
+_new_settings = {
+    "@io_bazel_rules_go//go/config:race": False,
+    "@io_bazel_rules_go//go/config:pure": False,
+    "@io_bazel_rules_go//go/config:static": False,
+    "@io_bazel_rules_go//go/config:tags": [],
+
+    # These private configs show up because of a bug in rules_go, which is
+    # missing a non_go_tool_transition on the proto toolchain when
+    # --incompatible_enable_proto_toolchain_resolution is enabled.
+    "@io_bazel_rules_go//go/private/rules:original_pure": "",
+    "@io_bazel_rules_go//go/private/rules:original_tags": "",
+}
+
+def _ignore_unused_configuration_impl(_settings, _attr):
+    return _new_settings
+
+_ignore_unused_configuration = transition(
+    implementation = _ignore_unused_configuration_impl,
+    inputs = [],
+    outputs = list(_new_settings.keys()),
+)
+
+ignore_unused_configuration_target = rule(
+    cfg = _ignore_unused_configuration,
+    implementation = forward_impl,
+    attrs = {
+        "dep": attr.label(mandatory = True),
+    },
+    doc = """Applies ignore_unused_configuration transition to a target.""",
+)