workspace: update rules_rust and add UEFI platform

This updates rules_rust from 0.16 to 0.26 and adds a patch sent upstream
for UEFI support.

Explicit toolchain definitions are needed for UEFI, so three toolchains
for all currently-supported host OSs (Linux and macOS) are added.

Rust UEFI support libraries will be added in a followup CL.

Change-Id: I52175f69f6a5c424f1f232748ff96dd6fcbbe92a
Reviewed-on: https://review.monogon.dev/c/monogon/+/2104
Reviewed-by: Serge Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/WORKSPACE b/WORKSPACE
index 97e2e3e..47c12b5 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -130,13 +130,51 @@
 # Rust rules
 http_archive(
     name = "rules_rust",
-    sha256 = "aaaa4b9591a5dad8d8907ae2dbe6e0eb49e6314946ce4c7149241648e56a1277",
-    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.16.1/rules_rust-v0.16.1.tar.gz"],
+    patch_args = ["-p1"],
+    patches = [
+        "//third_party:rust-uefi-platform.patch",
+    ],
+    sha256 = "9d04e658878d23f4b00163a72da3db03ddb451273eb347df7d7c50838d698f49",
+    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.26.0/rules_rust-v0.26.0.tar.gz"],
 )
 
-load("@rules_rust//rust:repositories.bzl", "rust_repositories")
+load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_repository_set")
 
-rust_repositories()
+rules_rust_dependencies()
+
+# Rust Toolchains
+rust_repository_set(
+    name = "rust_linux_x86_64",
+    edition = "2021",
+    exec_triple = "x86_64-unknown-linux-gnu",
+    extra_target_triples = [
+        "x86_64-unknown-linux-gnu",
+        "x86_64-unknown-uefi",
+    ],
+    versions = ["1.71.0"],  # Default for rules_go 0.26.
+)
+
+rust_repository_set(
+    name = "rust_macos_x86_64",
+    edition = "2021",
+    exec_triple = "x86_64-apple-darwin",
+    extra_target_triples = [
+        "x86_64-unknown-linux-gnu",
+        "x86_64-unknown-uefi",
+    ],
+    versions = ["1.71.0"],  # Default for rules_go 0.26.
+)
+
+rust_repository_set(
+    name = "rust_macos_arm64",
+    edition = "2021",
+    exec_triple = "aarch64-apple-darwin",
+    extra_target_triples = [
+        "x86_64-unknown-linux-gnu",
+        "x86_64-unknown-uefi",
+    ],
+    versions = ["1.71.0"],  # Default for rules_go 0.26.
+)
 
 load("//third_party/rust/cargo:crates.bzl", "raze_fetch_remote_crates")
 
diff --git a/third_party/rust-uefi-platform.patch b/third_party/rust-uefi-platform.patch
new file mode 100644
index 0000000..e70f7f3
--- /dev/null
+++ b/third_party/rust-uefi-platform.patch
@@ -0,0 +1,72 @@
+From 28b8651b0ccba1f41802a2aff12643b51436b32b Mon Sep 17 00:00:00 2001
+From: Lorenz Brun <lorenz@monogon.tech>
+Date: Tue, 29 Aug 2023 19:02:45 +0200
+Subject: [PATCH] Add support for UEFI targets and OS
+
+---
+ rust/platform/triple_mappings.bzl | 7 +++++++
+ 1 file changed, 7 insertions(+)
+
+diff --git a/rust/platform/triple_mappings.bzl b/rust/platform/triple_mappings.bzl
+index 61a71c73..ce594522 100644
+--- a/rust/platform/triple_mappings.bzl
++++ b/rust/platform/triple_mappings.bzl
+@@ -27,6 +27,7 @@ SUPPORTED_T2_PLATFORM_TRIPLES = [
+     "aarch64-fuchsia",
+     "aarch64-linux-android",
+     "aarch64-pc-windows-msvc",
++    "aarch64-unknown-uefi",
+     "arm-unknown-linux-gnueabi",
+     "armv7-linux-androideabi",
+     "armv7-unknown-linux-gnueabi",
+@@ -45,6 +46,7 @@ SUPPORTED_T2_PLATFORM_TRIPLES = [
+     "x86_64-linux-android",
+     "x86_64-unknown-freebsd",
+     "x86_64-unknown-none",
++    "x86_64-unknown-uefi",
+ ]
+ 
+ SUPPORTED_PLATFORM_TRIPLES = SUPPORTED_T1_PLATFORM_TRIPLES + SUPPORTED_T2_PLATFORM_TRIPLES
+@@ -97,6 +99,7 @@ _SYSTEM_TO_BUILTIN_SYS_SUFFIX = {
+     "none": "none",
+     "openbsd": "openbsd",
+     "solaris": None,
++    "uefi": "uefi",
+     "unknown": None,
+     "wasi": None,
+     "windows": "windows",
+@@ -113,6 +116,7 @@ _SYSTEM_TO_BINARY_EXT = {
+     "ios": "",
+     "linux": "",
+     "none": "",
++    "uefi": ".efi",
+     # This is currently a hack allowing us to have the proper
+     # generated extension for the wasm target, similarly to the
+     # windows target
+@@ -132,6 +136,7 @@ _SYSTEM_TO_STATICLIB_EXT = {
+     "ios": ".a",
+     "linux": ".a",
+     "none": ".a",
++    "uefi": ".lib",
+     "unknown": "",
+     "wasi": "",
+     "windows": ".lib",
+@@ -148,6 +153,7 @@ _SYSTEM_TO_DYLIB_EXT = {
+     "ios": ".dylib",
+     "linux": ".so",
+     "none": ".so",
++    "uefi": "", # UEFI doesn't have dynamic linking
+     "unknown": ".wasm",
+     "wasi": ".wasm",
+     "windows": ".dll",
+@@ -191,6 +197,7 @@ _SYSTEM_TO_STDLIB_LINKFLAGS = {
+     "none": [],
+     "openbsd": ["-lpthread"],
+     "solaris": ["-lsocket", "-lposix4", "-lpthread", "-lresolv"],
++    "uefi": [],
+     "unknown": [],
+     "uwp": ["ws2_32.lib"],
+     "wasi": [],
+-- 
+2.41.0
+