build/toolchain/toolchain-bundle: Add and use supported targets list

This makes the definitions less verbose.

Change-Id: Ifc4d9853b408da876100c41e79bc9d4f4d800d63
Reviewed-on: https://review.monogon.dev/c/monogon/+/4410
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
Tested-by: Jenkins CI
diff --git a/build/toolchain/toolchain-bundle/BUILD.bazel b/build/toolchain/toolchain-bundle/BUILD.bazel
index 06e2cb8..82437da 100644
--- a/build/toolchain/toolchain-bundle/BUILD.bazel
+++ b/build/toolchain/toolchain-bundle/BUILD.bazel
@@ -1,5 +1,5 @@
 load("@rules_perl//perl:toolchain.bzl", "perl_toolchain")
-load(":toolchain.bzl", "TOOLCHAINS", "toolchain_for")
+load(":toolchain.bzl", "SUPPORTED_TARGETS", "TOOLCHAINS", "toolchain_for")
 
 package(default_visibility = ["//visibility:public"])
 
@@ -11,36 +11,22 @@
     for name, config in TOOLCHAINS.items()
 ]
 
-perl_toolchain(
-    name = "rules_perl_linux_amd64_toolchain_impl",
-    runtime = [
-        "@toolchain-bundle-x86_64-unknown-linux-musl//:perl_runtime",
-    ],
-)
+# rules_perl wiring
 
-perl_toolchain(
-    name = "rules_perl_linux_aarch64_toolchain_impl",
-    runtime = [
-        "@toolchain-bundle-aarch64-unknown-linux-musl//:perl_runtime",
-    ],
-)
-
-toolchain(
-    name = "rules_perl_linux_amd64_toolchain",
-    exec_compatible_with = [
-        "@platforms//os:linux",
-        "@platforms//cpu:x86_64",
-    ],
-    toolchain = ":rules_perl_linux_amd64_toolchain_impl",
-    toolchain_type = "@rules_perl//perl:toolchain_type",
-)
-
-toolchain(
-    name = "rules_perl_linux_aarch64_toolchain",
-    exec_compatible_with = [
-        "@platforms//os:linux",
-        "@platforms//cpu:aarch64",
-    ],
-    toolchain = ":rules_perl_linux_aarch64_toolchain_impl",
-    toolchain_type = "@rules_perl//perl:toolchain_type",
-)
+[
+    [
+        perl_toolchain(
+            name = "rules_perl_%s_toolchain_impl" % target.tuple,
+            runtime = [
+                "@toolchain-bundle-%s//:perl_runtime" % target.triple,
+            ],
+        ),
+        toolchain(
+            name = "rules_perl_%s_toolchain" % target.tuple,
+            exec_compatible_with = target.constrain,
+            toolchain = ":rules_perl_%s_toolchain_impl" % target.tuple,
+            toolchain_type = "@rules_perl//perl:toolchain_type",
+        ),
+    ]
+    for target in SUPPORTED_TARGETS
+]
diff --git a/build/toolchain/toolchain-bundle/toolchain.bzl b/build/toolchain/toolchain-bundle/toolchain.bzl
index a2b7df2..405be8d 100644
--- a/build/toolchain/toolchain-bundle/toolchain.bzl
+++ b/build/toolchain/toolchain-bundle/toolchain.bzl
@@ -1,5 +1,18 @@
 load("@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
 
+SUPPORTED_TARGETS = [
+    struct(
+        tuple = "linux_x86_64",
+        triple = "x86_64-unknown-linux-musl",
+        constrain = ["@platforms//os:linux", "@platforms//cpu:x86_64"],
+    ),
+    struct(
+        tuple = "linux_aarch64",
+        triple = "aarch64-unknown-linux-musl",
+        constrain = ["@platforms//os:linux", "@platforms//cpu:aarch64"],
+    ),
+]
+
 # Copied from bazel-contrib/rules_foreign_cc licensed under Apache-2.0
 def _current_toolchain_impl(ctx):
     toolchain = ctx.toolchains[ctx.attr._toolchain]
@@ -39,41 +52,21 @@
         name = name,
     )
 
-    native.toolchain(
-        name = "%s_linux_x86_64_toolchain" % name,
-        exec_compatible_with = [
-            "@platforms//os:linux",
-            "@platforms//cpu:x86_64",
-        ],
-        toolchain = ":%s_linux_x86_64" % name,
-        toolchain_type = ":%s_toolchain" % name,
-    )
+    for target in SUPPORTED_TARGETS:
+        native.toolchain(
+            name = "%s_%s_toolchain" % (name, target.tuple),
+            exec_compatible_with = target.constrain,
+            toolchain = ":%s_%s" % (name, target.tuple),
+            toolchain_type = ":%s_toolchain" % name,
+        )
 
-    native.toolchain(
-        name = "%s_linux_aarch64_toolchain" % name,
-        exec_compatible_with = [
-            "@platforms//os:linux",
-            "@platforms//cpu:aarch64",
-        ],
-        toolchain = ":%s_linux_aarch64" % name,
-        toolchain_type = ":%s_toolchain" % name,
-    )
-
-    native_tool_toolchain(
-        name = "%s_linux_aarch64" % name,
-        env = {
-            name.upper(): "$(execpath @toolchain-bundle-aarch64-unknown-linux-musl//:%s)" % config.target,
-        },
-        target = "@toolchain-bundle-aarch64-unknown-linux-musl//:%s" % config.target,
-    )
-
-    native_tool_toolchain(
-        name = "%s_linux_x86_64" % name,
-        env = {
-            name.upper(): "$(execpath @toolchain-bundle-x86_64-unknown-linux-musl//:%s)" % config.target,
-        },
-        target = "@toolchain-bundle-x86_64-unknown-linux-musl//:%s" % config.target,
-    )
+        native_tool_toolchain(
+            name = "%s_%s" % (name, target.tuple),
+            env = {
+                name.upper(): "$(execpath @toolchain-bundle-%s//:%s)" % (target.triple, config.target),
+            },
+            target = "@toolchain-bundle-%s//:%s" % (target.triple, config.target),
+        )
 
 current_qemu_img_toolchain = current_toolchain("qemu-img")
 current_qemu_kvm_toolchain = current_toolchain("qemu-kvm")