Skip to content

Commit ad19faa

Browse files
allevatoluispadron
authored andcommitted
Add a build setting to the Apple transitions that dependencies can be used to determine if they're being built into an Apple bundle or not.
Cherry-pick: d810748
1 parent 0e509be commit ad19faa

6 files changed

Lines changed: 113 additions & 0 deletions

File tree

apple/build_settings/build_settings.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ Controls the output format for dSYM debug symbols. Valid values are:
7171
# List of all registered build settings without command line flags at
7272
# `rules_apple/apple/build_settings/BUILD`.
7373
build_settings = {
74+
"building_apple_bundle": struct(
75+
doc = """
76+
This is set to True if the target configuration is building a bundled Apple
77+
binary. For example, an `ios_application`, `watchos_extension`, or
78+
`macos_unit_test`, but *not* a `macos_command_line_application`.
79+
80+
This can be tested by clients to provide conditional behavior depending on
81+
whether or not a library is being built as a dependency of a bundled executable.
82+
""",
83+
default = False,
84+
),
7485
"enable_wip_features": struct(
7586
doc = """
7687
Enables functionality that is still a work in progress, with interfaces and output that can change

apple/internal/rule_attrs.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ appropriate resources location within the bundle.
545545
doc = """
546546
An `apple_bundle_version` target that represents the version for this target. See
547547
[`apple_bundle_version`](https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-general.md?cl=head#apple_bundle_version).
548+
""",
549+
),
550+
"_building_apple_bundle": attr.bool(
551+
default = True,
552+
doc = """
553+
Internal attribute read by Apple rule transitions to set the
554+
`building_apple_bundle` build setting.
548555
""",
549556
),
550557
}

apple/internal/transition_support.bzl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ def _is_arch_supported_for_target_tuple(*, environment_arch, minimum_os_version,
304304
def _command_line_options(
305305
*,
306306
apple_platforms = [],
307+
building_apple_bundle = False,
307308
environment_arch = None,
308309
force_bundle_outputs = False,
309310
minimum_os_version,
@@ -318,6 +319,8 @@ def _command_line_options(
318319
first element will be applied to `platforms` as that will be what is resolved by the
319320
underlying rule. Defaults to an empty list, which will signal to Bazel that platform
320321
mapping can take place as a fallback measure.
322+
building_apple_bundle: Indicates if the rule is building a bundle (rather than a
323+
standalone executable or library).
321324
environment_arch: A valid Apple environment when applicable with its architecture as a
322325
string (for example `sim_arm64` from `ios_sim_arm64`, or `arm64` from `ios_arm64`), or
323326
None to infer a value from command line options passed through settings.
@@ -343,6 +346,7 @@ def _command_line_options(
343346

344347
default_platforms = [settings[_CPU_TO_DEFAULT_PLATFORM_FLAG[cpu]]] if _is_bazel_7 else []
345348
return {
349+
build_settings_labels.building_apple_bundle: building_apple_bundle,
346350
build_settings_labels.use_tree_artifacts_outputs: force_bundle_outputs if force_bundle_outputs else settings[build_settings_labels.use_tree_artifacts_outputs],
347351
"//command_line_option:apple_platform_type": platform_type,
348352
"//command_line_option:apple_platforms": apple_platforms,
@@ -411,6 +415,7 @@ def _resolved_environment_arch_for_arch(*, arch, environment, platform_type):
411415

412416
def _command_line_options_for_xcframework_platform(
413417
*,
418+
building_apple_bundle,
414419
minimum_os_version,
415420
platform_attr,
416421
platform_type,
@@ -419,6 +424,8 @@ def _command_line_options_for_xcframework_platform(
419424
"""Generates a dictionary of command line options keyed by 1:2+ transition for this platform.
420425
421426
Args:
427+
building_apple_bundle: Indicates if the rule is building a bundle (rather than a
428+
standalone executable or library).
422429
minimum_os_version: A string representing the minimum OS version specified for this
423430
platform, represented as a dotted version number (for example, `"9.0"`).
424431
platform_attr: The attribute for the apple platform specifying in dictionary form which
@@ -453,6 +460,7 @@ def _command_line_options_for_xcframework_platform(
453460
environment = target_environment,
454461
platform_type = platform_type,
455462
): _command_line_options(
463+
building_apple_bundle = building_apple_bundle,
456464
environment_arch = resolved_environment_arch,
457465
minimum_os_version = minimum_os_version,
458466
platform_type = platform_type,
@@ -467,7 +475,9 @@ def _apple_rule_base_transition_impl(settings, attr):
467475
"""Rule transition for Apple rules using Bazel CPUs and a valid Apple split transition."""
468476
minimum_os_version = attr.minimum_os_version
469477
platform_type = attr.platform_type
478+
building_apple_bundle = getattr(attr, "_building_apple_bundle", False)
470479
return _command_line_options(
480+
building_apple_bundle = building_apple_bundle,
471481
environment_arch = _environment_archs(platform_type, minimum_os_version, settings)[0],
472482
minimum_os_version = minimum_os_version,
473483
platform_type = platform_type,
@@ -496,6 +506,7 @@ _apple_platform_transition_inputs = _apple_platforms_rule_base_transition_inputs
496506
"//command_line_option:platforms",
497507
]
498508
_apple_rule_base_transition_outputs = [
509+
build_settings_labels.building_apple_bundle,
499510
build_settings_labels.use_tree_artifacts_outputs,
500511
"//command_line_option:apple_platform_type",
501512
"//command_line_option:apple_platforms",
@@ -528,12 +539,14 @@ def _apple_platforms_rule_base_transition_impl(settings, attr):
528539
"""Rule transition for Apple rules using Bazel platforms and the Starlark split transition."""
529540
minimum_os_version = attr.minimum_os_version
530541
platform_type = attr.platform_type
542+
building_apple_bundle = getattr(attr, "_building_apple_bundle", False)
531543
environment_arch = None
532544
if not settings["//command_line_option:incompatible_enable_apple_toolchain_resolution"]:
533545
# Add fallback to match an anticipated split of Apple cpu-based resolution
534546
environment_arch = _environment_archs(platform_type, minimum_os_version, settings)[0]
535547
return _command_line_options(
536548
apple_platforms = settings["//command_line_option:apple_platforms"],
549+
building_apple_bundle = building_apple_bundle,
537550
environment_arch = environment_arch,
538551
minimum_os_version = minimum_os_version,
539552
platform_type = platform_type,
@@ -550,6 +563,7 @@ def _apple_platforms_rule_bundle_output_base_transition_impl(settings, attr):
550563
"""Rule transition for Apple rules using Bazel platforms which force bundle outputs."""
551564
minimum_os_version = attr.minimum_os_version
552565
platform_type = attr.platform_type
566+
building_apple_bundle = getattr(attr, "_building_apple_bundle", False)
553567
environment_arch = None
554568
if not settings["//command_line_option:incompatible_enable_apple_toolchain_resolution"]:
555569
# Add fallback to match an anticipated split of Apple cpu-based resolution
@@ -560,6 +574,7 @@ def _apple_platforms_rule_bundle_output_base_transition_impl(settings, attr):
560574
)
561575
return _command_line_options(
562576
apple_platforms = settings["//command_line_option:apple_platforms"],
577+
building_apple_bundle = building_apple_bundle,
563578
environment_arch = environment_arch[0],
564579
force_bundle_outputs = True,
565580
minimum_os_version = minimum_os_version,
@@ -656,6 +671,7 @@ def _apple_platform_split_transition_impl(settings, attr):
656671
if str(platform) not in output_dictionary:
657672
output_dictionary[str(platform)] = _command_line_options(
658673
apple_platforms = apple_platforms,
674+
building_apple_bundle = getattr(attr, "_building_apple_bundle", False),
659675
minimum_os_version = attr.minimum_os_version,
660676
platform_type = attr.platform_type,
661677
settings = settings,
@@ -664,6 +680,7 @@ def _apple_platform_split_transition_impl(settings, attr):
664680
else:
665681
minimum_os_version = attr.minimum_os_version
666682
platform_type = attr.platform_type
683+
building_apple_bundle = getattr(attr, "_building_apple_bundle", False)
667684
for environment_arch in _environment_archs(platform_type, minimum_os_version, settings):
668685
found_cpu = _cpu_string(
669686
environment_arch = environment_arch,
@@ -704,6 +721,7 @@ def _apple_platform_split_transition_impl(settings, attr):
704721
continue
705722

706723
output_dictionary[found_cpu] = _command_line_options(
724+
building_apple_bundle = building_apple_bundle,
707725
environment_arch = environment_arch,
708726
minimum_os_version = minimum_os_version,
709727
platform_type = platform_type,
@@ -772,6 +790,7 @@ def _xcframework_split_transition_impl(settings, attr):
772790
target_environments.append("simulator")
773791

774792
command_line_options = _command_line_options_for_xcframework_platform(
793+
building_apple_bundle = getattr(attr, "_building_apple_bundle", False),
775794
minimum_os_version = attr.minimum_os_versions.get(platform_type),
776795
platform_attr = platform_attr,
777796
platform_type = platform_type,

test/starlark_tests/macos_application_tests.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,17 @@ def macos_application_test_suite(name):
443443
tags = [name],
444444
)
445445

446+
archive_contents_test(
447+
name = "{}_is_building_apple_bundle_test".format(name),
448+
build_type = "simulator",
449+
target_under_test = "//test/starlark_tests/targets_under_test/macos:bundled_app_to_test_building_apple_bundle",
450+
binary_test_file = "$CONTENT_ROOT/MacOS/bundled_app_to_test_building_apple_bundle",
451+
binary_test_architecture = "x86_64",
452+
binary_contains_symbols = ["_IsBuildingAppleBundle"],
453+
binary_not_contains_symbols = ["_IsNotBuildingAppleBundle"],
454+
tags = [name],
455+
)
456+
446457
archive_contents_test(
447458
name = "{}_archive_contains_ccinfo_deps_dylibs_test".format(name),
448459
build_type = "simulator",

test/starlark_tests/macos_command_line_application_tests.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,17 @@ def macos_command_line_application_test_suite(name):
294294
tags = [name],
295295
)
296296

297+
binary_contents_test(
298+
name = "{}_is_building_apple_bundle_test".format(name),
299+
build_type = "device",
300+
target_under_test = "//test/starlark_tests/targets_under_test/macos:cmd_app_to_test_building_apple_bundle",
301+
binary_test_file = "$BINARY",
302+
binary_test_architecture = "x86_64",
303+
binary_contains_symbols = ["_IsNotBuildingAppleBundle"],
304+
binary_not_contains_symbols = ["_IsBuildingAppleBundle"],
305+
tags = [name],
306+
)
307+
297308
native.test_suite(
298309
name = name,
299310
tags = [name],

test/starlark_tests/targets_under_test/macos/BUILD

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,3 +3039,57 @@ apple_static_xcframework(
30393039
tags = common.fixture_tags,
30403040
deps = [":fmwk_lib"],
30413041
)
3042+
3043+
# ---------------------------------------------------------------------------------------
3044+
# Tests for the `building_apple_bundle` build setting.
3045+
3046+
config_setting(
3047+
name = "is_building_apple_bundle",
3048+
flag_values = {
3049+
"//apple/build_settings:building_apple_bundle": "True",
3050+
},
3051+
)
3052+
3053+
genrule(
3054+
name = "is_building_apple_bundle_src",
3055+
outs = ["IsBuildingAppleBundle.m"],
3056+
cmd = """
3057+
cat > $@ <<'EOF'
3058+
__attribute__((used)) void IS_BUILDING_APPLE_BUNDLE_SYMBOL() {}
3059+
int main() { return 0; }
3060+
EOF
3061+
""",
3062+
tags = common.fixture_tags,
3063+
)
3064+
3065+
objc_library(
3066+
name = "is_building_apple_bundle_lib",
3067+
srcs = ["IsBuildingAppleBundle.m"],
3068+
copts = select({
3069+
":is_building_apple_bundle": ["-DIS_BUILDING_APPLE_BUNDLE_SYMBOL=IsBuildingAppleBundle"],
3070+
"//conditions:default": ["-DIS_BUILDING_APPLE_BUNDLE_SYMBOL=IsNotBuildingAppleBundle"],
3071+
}),
3072+
tags = common.fixture_tags,
3073+
)
3074+
3075+
macos_command_line_application(
3076+
name = "cmd_app_to_test_building_apple_bundle",
3077+
minimum_os_version = common.min_os_macos.baseline,
3078+
tags = common.fixture_tags,
3079+
deps = [
3080+
":is_building_apple_bundle_lib",
3081+
],
3082+
)
3083+
3084+
macos_application(
3085+
name = "bundled_app_to_test_building_apple_bundle",
3086+
bundle_id = "com.google.example",
3087+
infoplists = [
3088+
"//test/starlark_tests/resources:Info.plist",
3089+
],
3090+
minimum_os_version = common.min_os_macos.baseline,
3091+
tags = common.fixture_tags,
3092+
deps = [
3093+
":is_building_apple_bundle_lib",
3094+
],
3095+
)

0 commit comments

Comments
 (0)