[apple] support tvos_sim_arm64 in toolchain

Closes #14439.

PiperOrigin-RevId: 427721738
diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java
index 2de9906..1faccff 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java
@@ -77,8 +77,8 @@
   private static final String MACOS_CPU_PREFIX = "darwin_";
 
   // TODO(b/180572694): Remove after platforms based toolchain resolution supported.
-  /** Prefix for forced iOS simulator cpu values */
-  public static final String IOS_FORCED_SIMULATOR_CPU_PREFIX = "sim_";
+  /** Prefix for forced iOS and tvOS simulator cpu values */
+  public static final String FORCED_SIMULATOR_CPU_PREFIX = "sim_";
 
   /** Default cpu for iOS builds. */
   @VisibleForTesting
@@ -229,25 +229,24 @@
     // The removeSimPrefix argument is necessary due to a simulator and device both using arm64
     // architecture. In the case of Starlark asking for the architecture, we should return the
     // actual architecture (arm64) but in other cases in this class what we actually want is the
-    // CPU without the ios prefix (e.g. sim_arm64). This parameter is provided in the private method
-    // so that internal to this class we are able to use both without duplicating retrieval logic.
+    // CPU without the ios/tvos prefix (e.g. sim_arm64). This parameter is provided in the private
+    // method so that internal to this class we are able to use both without duplicating retrieval
+    // logic.
     // TODO(b/180572694): Remove removeSimPrefix parameter once platforms are used instead of CPU
+    String cpu = getPrefixedAppleCpu(applePlatformType, appleCpus);
+    if (removeSimPrefix && cpu.startsWith(FORCED_SIMULATOR_CPU_PREFIX)) {
+      cpu = cpu.substring(FORCED_SIMULATOR_CPU_PREFIX.length());
+    }
+    return cpu;
+  }
+
+  private static String getPrefixedAppleCpu(PlatformType applePlatformType, AppleCpus appleCpus) {
     if (!Strings.isNullOrEmpty(appleCpus.appleSplitCpu())) {
-      String cpu = appleCpus.appleSplitCpu();
-      if (removeSimPrefix && cpu.startsWith(IOS_FORCED_SIMULATOR_CPU_PREFIX)) {
-        cpu = cpu.substring(IOS_FORCED_SIMULATOR_CPU_PREFIX.length());
-      }
-      return cpu;
+      return appleCpus.appleSplitCpu();
     }
     switch (applePlatformType) {
       case IOS:
-        {
-          String cpu = Iterables.getFirst(appleCpus.iosMultiCpus(), appleCpus.iosCpu());
-          if (removeSimPrefix && cpu.startsWith(IOS_FORCED_SIMULATOR_CPU_PREFIX)) {
-            cpu = cpu.substring(IOS_FORCED_SIMULATOR_CPU_PREFIX.length());
-          }
-          return cpu;
-        }
+        return Iterables.getFirst(appleCpus.iosMultiCpus(), appleCpus.iosCpu());
       case WATCHOS:
         return appleCpus.watchosCpus().get(0);
       case TVOS:
diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java b/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java
index 68eb162..e90d844 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java
@@ -51,7 +51,7 @@
   private static final ImmutableSet<String> WATCHOS_DEVICE_TARGET_CPUS =
       ImmutableSet.of("watchos_armv7k", "watchos_arm64_32");
   private static final ImmutableSet<String> TVOS_SIMULATOR_TARGET_CPUS =
-      ImmutableSet.of("tvos_x86_64");
+      ImmutableSet.of("tvos_x86_64", "tvos_sim_arm64");
   private static final ImmutableSet<String> TVOS_DEVICE_TARGET_CPUS =
       ImmutableSet.of("tvos_arm64");
   private static final ImmutableSet<String> CATALYST_TARGET_CPUS =
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
index aa3f498..0f1d869 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
@@ -34,7 +34,7 @@
 _IOS_DEVICE_TARGET_CPUS = ["ios_armv6", "ios_arm64", "ios_armv7", "ios_armv7s", "ios_arm64e"]
 _WATCHOS_SIMULATOR_TARGET_CPUS = ["watchos_i386", "watchos_x86_64", "watchos_arm64"]
 _WATCHOS_DEVICE_TARGET_CPUS = ["watchos_armv7k", "watchos_arm64_32"]
-_TVOS_SIMULATOR_TARGET_CPUS = ["tvos_x86_64"]
+_TVOS_SIMULATOR_TARGET_CPUS = ["tvos_x86_64", "tvos_sim_arm64"]
 _TVOS_DEVICE_TARGET_CPUS = ["tvos_arm64"]
 _CATALYST_TARGET_CPUS = ["catalyst_x86_64"]
 _MACOS_TARGET_CPUS = ["darwin_x86_64", "darwin_arm64", "darwin_arm64e", "darwin"]
diff --git a/tools/osx/crosstool/BUILD.toolchains b/tools/osx/crosstool/BUILD.toolchains
index 2abbfec..12aa6a2 100644
--- a/tools/osx/crosstool/BUILD.toolchains
+++ b/tools/osx/crosstool/BUILD.toolchains
@@ -52,6 +52,10 @@
         "@platforms//os:ios",
         "@platforms//cpu:x86_64",
     ],
+    "tvos_sim_arm64": [
+        "@platforms//os:ios",
+        "@platforms//cpu:aarch64",
+    ],
     "watchos_arm64": [
         "@platforms//os:ios",
         "@platforms//cpu:aarch64",
diff --git a/tools/osx/crosstool/cc_toolchain_config.bzl b/tools/osx/crosstool/cc_toolchain_config.bzl
index 3a280d7..d85c3ed 100644
--- a/tools/osx/crosstool/cc_toolchain_config.bzl
+++ b/tools/osx/crosstool/cc_toolchain_config.bzl
@@ -76,6 +76,8 @@
         target_system_name = "x86_64-apple-ios"
     elif (ctx.attr.cpu == "ios_sim_arm64"):
         target_system_name = "arm64-apple-ios-simulator"
+    elif (ctx.attr.cpu == "tvos_sim_arm64"):
+        target_system_name = "arm64-apple-tvos-simulator"
     elif (ctx.attr.cpu == "watchos_arm64"):
         target_system_name = "arm64-apple-watchos-simulator"
     elif (ctx.attr.cpu == "darwin_x86_64"):
@@ -105,7 +107,7 @@
 
     host_system_name = "x86_64-apple-macosx"
     arch = ctx.attr.cpu.split("_", 1)[-1]
-    if ctx.attr.cpu == "ios_sim_arm64":
+    if ctx.attr.cpu in ["ios_sim_arm64", "tvos_sim_arm64", "watchos_arm64"]:
         arch = "arm64"
 
     all_compile_actions = [
@@ -770,7 +772,8 @@
             ],
         )
     elif (ctx.attr.cpu == "tvos_arm64" or
-          ctx.attr.cpu == "tvos_x86_64"):
+          ctx.attr.cpu == "tvos_x86_64" or
+          ctx.attr.cpu == "tvos_sim_arm64"):
         apply_default_compiler_flags_feature = feature(
             name = "apply_default_compiler_flags",
             flag_sets = [
@@ -930,6 +933,7 @@
         ctx.attr.cpu == "ios_x86_64" or
         ctx.attr.cpu == "ios_sim_arm64" or
         ctx.attr.cpu == "tvos_x86_64" or
+        ctx.attr.cpu == "tvos_sim_arm64" or
         ctx.attr.cpu == "watchos_i386" or
         ctx.attr.cpu == "watchos_x86_64" or
         ctx.attr.cpu == "watchos_arm64"):
@@ -1001,6 +1005,7 @@
         ctx.attr.cpu == "ios_sim_arm64" or
         ctx.attr.cpu == "tvos_arm64" or
         ctx.attr.cpu == "tvos_x86_64" or
+        ctx.attr.cpu == "tvos_sim_arm64" or
         ctx.attr.cpu == "watchos_arm64_32" or
         ctx.attr.cpu == "watchos_armv7k" or
         ctx.attr.cpu == "watchos_i386" or
@@ -1290,7 +1295,8 @@
                 ),
             ],
         )
-    elif (ctx.attr.cpu == "tvos_x86_64"):
+    elif (ctx.attr.cpu == "tvos_x86_64" or
+          ctx.attr.cpu == "tvos_sim_arm64"):
         version_min_feature = feature(
             name = "version_min",
             flag_sets = [
@@ -1766,6 +1772,7 @@
         ctx.attr.cpu == "ios_sim_arm64" or
         ctx.attr.cpu == "tvos_arm64" or
         ctx.attr.cpu == "tvos_x86_64" or
+        ctx.attr.cpu == "tvos_sim_arm64" or
         ctx.attr.cpu == "watchos_arm64_32" or
         ctx.attr.cpu == "watchos_armv7k" or
         ctx.attr.cpu == "watchos_i386" or
@@ -2851,6 +2858,7 @@
         ctx.attr.cpu == "ios_sim_arm64" or
         ctx.attr.cpu == "tvos_arm64" or
         ctx.attr.cpu == "tvos_x86_64" or
+        ctx.attr.cpu == "tvos_sim_arm64" or
         ctx.attr.cpu == "watchos_arm64_32" or
         ctx.attr.cpu == "watchos_armv7k" or
         ctx.attr.cpu == "watchos_i386" or
diff --git a/tools/osx/crosstool/osx_archs.bzl b/tools/osx/crosstool/osx_archs.bzl
index ec684eb..aeb82ac 100644
--- a/tools/osx/crosstool/osx_archs.bzl
+++ b/tools/osx/crosstool/osx_archs.bzl
@@ -25,6 +25,7 @@
     "watchos_i386",
     "watchos_x86_64",
     "tvos_x86_64",
+    "tvos_sim_arm64",
 ]
 
 OSX_TOOLS_ARCHS = [