Skip to content

Commit

Permalink
Ensure cc_test sets 'requires-darwin' in execution requirements i…
Browse files Browse the repository at this point in the history
…f targeting an Apple platform (this is logic in native code)

PiperOrigin-RevId: 440392380
  • Loading branch information
trybka authored and pull[bot] committed Dec 18, 2023
1 parent 30971dd commit 5650f9b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CcInfo = _builtins.toplevel.CcInfo
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",
Expand Down Expand Up @@ -818,6 +819,14 @@ def _get_copts(ctx, common, feature_configuration, additional_make_variable_subs
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,
Expand Down Expand Up @@ -853,4 +862,5 @@ cc_helper = struct(
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,
)
15 changes: 15 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,31 @@
"""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)

# 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(
Expand All @@ -49,6 +61,9 @@ def _cc_test_impl(ctx):
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(
Expand Down
4 changes: 4 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def _get_stl():
def _get_repo():
return "bazel_tools"

def _get_platforms_root():
return "platforms//"

def _additional_fragments():
return []

Expand Down Expand Up @@ -93,6 +96,7 @@ semantics = struct(
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,
Expand Down

0 comments on commit 5650f9b

Please sign in to comment.