Skip to content

Commit ece6d2a

Browse files
nglevincopybara-github
authored andcommitted
Add fail_on_missing_constraint parameter to apple_support.platform_info_from_rule_ctx.
This allows callers to control whether a missing Apple platform constraint should result in a failure or a warning, or if a fallback should be silently applied, without the need to be on an allowlist. Generally this should be assumed "True" for rules-level logic as it can identify correctness issues quickly. However, users who want top level builds to default to Apple Silicon macOS as previous Apple fragment APIs allowed (albeit for Intel macOS) can still use the same API with this argument set to "False". PiperOrigin-RevId: 936570577
1 parent 186d544 commit ece6d2a

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

lib/apple_support.bzl

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,34 @@ Internal Error: Found unrecognized target environment of {target_environment} fo
649649
),
650650
)
651651

652-
def _apple_platform_info_from_rule_ctx(ctx):
653-
"""Returns an ApplePlatformInfo provider from a rule context, needed to resolve constraints."""
654-
target_os = _target_os_from_rule_ctx(ctx, fail_on_missing_constraint = False)
652+
def _platform_info_from_rule_ctx(ctx, fail_on_missing_constraint = True):
653+
"""Returns `ApplePlatformInfo` from a rule context, required for Apple platform information.
654+
655+
In order to use `apple_support.platform_info_from_rule_ctx()`, you'll need to modify your rule
656+
definition to add the following:
657+
658+
* Add the `apple_support.platform_constraint_attrs()` attributes to the `attrs` dictionary.
659+
This can be done using the `dicts.add()` method from Skylib or via the `|` operator.
660+
661+
Args:
662+
ctx: The context of the rule that has Apple platform constraint attributes.
663+
fail_on_missing_constraint: Whether to fail if no constraint is found. If `False`, a
664+
fallback platform of Apple Silicon macOS will be used if no Apple platform was found
665+
from the current target configuration. If `True`, the function will fail if no Apple
666+
platform is found and the target is not in the
667+
`ALLOWED_USERS_OF_MISSING_PLATFORM_FALLBACK` allowlist.
668+
669+
Returns:
670+
An `ApplePlatformInfo` representing the resolved Apple platform.
671+
"""
672+
target_os = _target_os_from_rule_ctx(
673+
ctx,
674+
fail_on_missing_constraint = False,
675+
)
655676
if not target_os:
656677
full_label = "//{package}:{name}".format(package = ctx.label.package, name = ctx.label.name)
657-
if full_label not in ALLOWED_USERS_OF_MISSING_PLATFORM_FALLBACK:
678+
if (fail_on_missing_constraint and
679+
full_label not in ALLOWED_USERS_OF_MISSING_PLATFORM_FALLBACK):
658680
fail("""
659681
ERROR: A valid Apple platform constraint could not be found for target {full_label}
660682
@@ -668,9 +690,10 @@ through `--platforms` rather than relying on the defaults.
668690
full_label = full_label,
669691
))
670692

671-
# Starlark-only default fallback when Apple constraints are missing (e.g. Linux host analysis)
672-
# buildifier: disable=print
673-
print("Warning: Target {} is analyzed without Apple platform constraints. Applying temporary macos fallback.".format(full_label))
693+
if fail_on_missing_constraint:
694+
# Starlark-only default fallback when Apple constraints are missing (e.g. Linux host analysis)
695+
# buildifier: disable=print
696+
print("Warning: Target {} is analyzed without Apple platform constraints. Applying temporary macos fallback.".format(full_label))
674697
target_os = "macos"
675698
target_env = "device"
676699
target_arch = "arm64"
@@ -698,7 +721,7 @@ apple_support = struct(
698721
xcode = _xcode_path_placeholder,
699722
),
700723
platform_constraint_attrs = _platform_constraint_attrs,
701-
platform_info_from_rule_ctx = _apple_platform_info_from_rule_ctx,
724+
platform_info_from_rule_ctx = _platform_info_from_rule_ctx,
702725
run = _run,
703726
run_shell = _run_shell,
704727
target_arch_from_rule_ctx = _target_arch_from_rule_ctx,

0 commit comments

Comments
 (0)