From e6c9982f166ffef37976abfb058a91fc623706b0 Mon Sep 17 00:00:00 2001 From: Milen Dzhumerov Date: Thu, 19 Sep 2024 01:37:35 -0700 Subject: [PATCH] Add test host app transition marker Summary: If `embed_xctest_frameworks_in_test_host_app` is set on an `apple_test()`, the `config//marker/apple/constraints:embed_xctest_frameworks_enabled` constraint is then added to the `test_host_app` via a transition. This constraint is then used in `apple_bundle()` to embed XCTest frameworks. Reviewed By: manicaesar Differential Revision: D62936576 fbshipit-source-id: 62f95834503eee2dfac8ec71286b0f54c4dd5293 --- apple/apple_test_host_app_transition.bzl | 38 ++++++++++++++++++++++++ decls/ios_rules.bzl | 6 +++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 apple/apple_test_host_app_transition.bzl diff --git a/apple/apple_test_host_app_transition.bzl b/apple/apple_test_host_app_transition.bzl new file mode 100644 index 000000000..d239b018a --- /dev/null +++ b/apple/apple_test_host_app_transition.bzl @@ -0,0 +1,38 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under both the MIT license found in the +# LICENSE-MIT file in the root directory of this source tree and the Apache +# License, Version 2.0 found in the LICENSE-APACHE file in the root directory +# of this source tree. + +def _apple_test_host_app_transition_impl(platform: PlatformInfo, refs: struct, attrs: struct) -> PlatformInfo: + if not attrs.embed_xctest_frameworks_in_test_host_app: + return platform + + updated_constraints = dict(platform.configuration.constraints) + + test_host_marker_setting_label = refs.embed_xctest_frameworks_constraint_setting[ConstraintSettingInfo].label + if test_host_marker_setting_label in updated_constraints: + return platform + + test_host_marker_value_info = refs.embed_xctest_frameworks_marker_constraint_value[ConstraintValueInfo] + updated_constraints[test_host_marker_setting_label] = test_host_marker_value_info + + return PlatformInfo( + label = platform.label + "-test-host-app", + configuration = ConfigurationInfo( + constraints = updated_constraints, + values = platform.configuration.values, + ), + ) + +apple_test_host_app_transition = transition( + impl = _apple_test_host_app_transition_impl, + refs = { + "embed_xctest_frameworks_constraint_setting": "config//marker/apple/constraints:embed_xctest_frameworks", + "embed_xctest_frameworks_marker_constraint_value": "config//marker/apple/constraints:embed_xctest_frameworks_enabled", + }, + attrs = [ + "embed_xctest_frameworks_in_test_host_app", + ], +) diff --git a/decls/ios_rules.bzl b/decls/ios_rules.bzl index e2fda6354..1bf4a3aef 100644 --- a/decls/ios_rules.bzl +++ b/decls/ios_rules.bzl @@ -12,6 +12,7 @@ load("@prelude//apple:apple_common.bzl", "apple_common") load("@prelude//apple:apple_rules_impl_utility.bzl", "apple_dsymutil_attrs", "get_apple_toolchain_attr") +load("@prelude//apple:apple_test_host_app_transition.bzl", "apple_test_host_app_transition") load("@prelude//apple:apple_toolchain_types.bzl", "AppleToolsInfo") load("@prelude//apple:apple_universal_executable.bzl", "apple_universal_executable_impl") load("@prelude//apple:cxx_universal_executable.bzl", "cxx_universal_executable_impl") @@ -673,7 +674,7 @@ apple_test = prelude_rule( apple_common.info_plist_arg() | apple_common.info_plist_substitutions_arg() | { - "test_host_app": attrs.option(attrs.dep(), default = None, doc = """ + "test_host_app": attrs.option(attrs.transition_dep(cfg = apple_test_host_app_transition), default = None, doc = """ A build target identifying an `apple_bundle()` rule that builds an application bundle. Output of the specified rule will be used as a test host of this test. This @@ -684,6 +685,9 @@ apple_test = prelude_rule( reference errors during compilation, but if the symbols do not exist, it might result in runtime crashes). """), + "embed_xctest_frameworks_in_test_host_app": attrs.option(attrs.bool(), default = None, doc = """ + Controls whether a marker constraint is added to the `test_host_app`. + """), } | cxx_common.srcs_arg() | cxx_common.platform_srcs_arg() |