Ensure `cc_test` sets `'requires-darwin'` in execution requirements if targeting an Apple platform (this is logic in native code)

PiperOrigin-RevId: 440392380
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
index 01a669c..6cdc593 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
@@ -20,6 +20,7 @@
 cc_common = _builtins.toplevel.cc_common
 cc_internal = _builtins.internal.cc_internal
 CcNativeLibraryInfo = _builtins.internal.CcNativeLibraryInfo
+platform_common = _builtins.toplevel.platform_common
 
 artifact_category = struct(
     STATIC_LIBRARY = "STATIC_LIBRARY",
@@ -818,6 +819,14 @@
     expanded_attribute_copts = _expand_make_variables_for_copts(ctx, tokenization, attribute_copts, additional_make_variable_substitutions)
     return expanded_package_copts + expanded_attribute_copts
 
+def _has_target_constraints(ctx, constraints):
+    # Constraints is a label_list
+    for constraint in constraints:
+        constraint_value = constraint[platform_common.ConstraintValueInfo]
+        if ctx.target_platform_has_constraint(constraint_value):
+            return True
+    return False
+
 cc_helper = struct(
     merge_cc_debug_contexts = _merge_cc_debug_contexts,
     is_code_coverage_enabled = _is_code_coverage_enabled,
@@ -853,4 +862,5 @@
     get_toolchain_global_make_variables = _get_toolchain_global_make_variables,
     get_cc_flags_make_variable = _get_cc_flags_make_variable,
     get_copts = _get_copts,
+    has_target_constraints = _has_target_constraints,
 )
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
index 5af3f27..c0c724f 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
@@ -15,12 +15,16 @@
 """cc_test Starlark implementation."""
 
 load(":common/cc/cc_binary.bzl", "cc_binary_impl")
+load(":common/paths.bzl", "paths")
 
 # TODO(b/198254254): We need to do a wrapper around cc_test like for
 # cc_binary, but for now it should work.
 load(":common/cc/cc_binary_attrs.bzl", "cc_binary_attrs_with_aspects")
+load(":common/cc/cc_helper.bzl", "cc_helper")
+load(":common/cc/semantics.bzl", "semantics")
 
 cc_internal = _builtins.internal.cc_internal
+platform_common = _builtins.toplevel.platform_common
 testing = _builtins.toplevel.testing
 
 _cc_test_attrs = dict(cc_binary_attrs_with_aspects)
@@ -28,6 +32,14 @@
 # Update cc_test defaults:
 _cc_test_attrs.update(
     _is_test = attr.bool(default = True),
+    _apple_constraints = attr.label_list(
+        default = [
+            "@" + paths.join(semantics.get_platforms_root(), "os:ios"),
+            "@" + paths.join(semantics.get_platforms_root(), "os:macos"),
+            "@" + paths.join(semantics.get_platforms_root(), "os:tvos"),
+            "@" + paths.join(semantics.get_platforms_root(), "os:watchos"),
+        ],
+    ),
     stamp = attr.int(values = [-1, 0, 1], default = 0),
     linkstatic = attr.bool(default = False),
     malloc = attr.label(
@@ -49,6 +61,9 @@
     return _handle_legacy_return(ctx, cc_info, providers)
 
 def _handle_legacy_return(ctx, cc_info, providers):
+    if cc_helper.has_target_constraints(ctx, ctx.attr._apple_constraints):
+        # When built for Apple platforms, require the execution to be on a Mac.
+        providers.append(testing.ExecutionInfo({"requires-darwin": ""}))
     if ctx.fragments.cpp.enable_legacy_cc_provider():
         # buildifier: disable=rule-impl-return
         return struct(
diff --git a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl
index 46579ee..4e8cb51 100644
--- a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl
@@ -37,6 +37,9 @@
 def _get_repo():
     return "bazel_tools"
 
+def _get_platforms_root():
+    return "platforms//"
+
 def _additional_fragments():
     return []
 
@@ -93,6 +96,7 @@
     determine_headers_checking_mode = _determine_headers_checking_mode,
     get_semantics = _get_semantics,
     get_repo = _get_repo,
+    get_platforms_root = _get_platforms_root,
     additional_fragments = _additional_fragments,
     get_distribs_attr = _get_distribs_attr,
     get_licenses_attr = _get_licenses_attr,