Skip to content

Commit

Permalink
Add the apple.no_legacy_swiftinterface feature to disable the autom…
Browse files Browse the repository at this point in the history
…atic library evolution transition on framework rules.

We are migrating users to a model where a new attribute, `library_evolution`, must be set on `swift_library` targets that vend the user-facing module in a framework, instead of automatically applying library evolution to the whole subgraph. In order to migrate, users can add this feature to their framework target and at the same time set `library_evolution = True` on their library.

Once everyone is migrated, library evolution should be removed entirely from the Apple transitions and the feature can be removed from BUILD files.

PiperOrigin-RevId: 487022298
  • Loading branch information
allevato authored and swiple-rules-gardener committed Nov 8, 2022
1 parent 6fa02bf commit cb9a1de
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 7 deletions.
36 changes: 29 additions & 7 deletions apple/internal/transition_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ def _resolved_environment_arch_for_arch(*, arch, environment):

def _command_line_options_for_xcframework_platform(
*,
attr,
minimum_os_version,
platform_attr,
platform_type,
Expand All @@ -293,6 +294,7 @@ def _command_line_options_for_xcframework_platform(
"""Generates a dictionary of command line options keyed by 1:2+ transition for this platform.
Args:
attr: The attributes passed to the transition function.
minimum_os_version: A string representing the minimum OS version specified for this
platform, represented as a dotted version number (for example, `"9.0"`).
platform_attr: The attribute for the apple platform specifying in dictionary form which
Expand Down Expand Up @@ -325,7 +327,10 @@ def _command_line_options_for_xcframework_platform(
environment = target_environment,
platform_type = platform_type,
): _command_line_options(
emit_swiftinterface = True,
emit_swiftinterface = _should_emit_swiftinterface(
attr,
is_xcframework = True,
),
environment_arch = resolved_environment_arch,
minimum_os_version = minimum_os_version,
platform_type = platform_type,
Expand All @@ -336,10 +341,29 @@ def _command_line_options_for_xcframework_platform(

return output_dictionary

def _should_emit_swiftinterface(attr, is_xcframework = False):
"""Whether a .swiftinterface file should be emitted for Swift dependencies.
This is temporary while we migrate users of the framework rules to enable
library evolution on specific targets instead of having it automatically
applied to the entire dependency subgraph.
"""

# TODO(b/239957001): Delete all this when everyone is migrated to the new
# `library_evolution` attribute on `swift_library`.
features = getattr(attr, "features", [])
if "apple.no_legacy_swiftinterface" in features:
return False

# iOS and tvOS static frameworks require underlying swift_library targets generate a Swift
# interface file. These rules define a private attribute called `_emitswiftinterface` that
# let's this transition flip rules_swift config down the build graph.
return is_xcframework or hasattr(attr, "_emitswiftinterface")

def _apple_rule_base_transition_impl(settings, attr):
"""Rule transition for Apple rules using Bazel CPUs and apple_common split transition."""
return _command_line_options(
emit_swiftinterface = hasattr(attr, "_emitswiftinterface"),
emit_swiftinterface = _should_emit_swiftinterface(attr),
minimum_os_version = attr.minimum_os_version,
platform_type = attr.platform_type,
settings = settings,
Expand Down Expand Up @@ -402,7 +426,7 @@ def _apple_platforms_rule_base_transition_impl(settings, attr):
"""Rule transition for Apple rules using Bazel platforms and Starlark split transition."""
return _command_line_options(
apple_platforms = settings["//command_line_option:apple_platforms"],
emit_swiftinterface = hasattr(attr, "_emitswiftinterface"),
emit_swiftinterface = _should_emit_swiftinterface(attr),
minimum_os_version = attr.minimum_os_version,
platform_type = attr.platform_type,
settings = settings,
Expand Down Expand Up @@ -474,10 +498,7 @@ def _apple_platform_split_transition_impl(settings, attr):
"""Starlark 1:2+ transition for Apple platform-aware rules"""
output_dictionary = {}

# iOS and tvOS static frameworks require underlying swift_library targets generate a Swift
# interface file. These rules define a private attribute called `_emitswiftinterface` that
# let's this transition flip rules_swift config down the build graph.
emit_swiftinterface = hasattr(attr, "_emitswiftinterface")
emit_swiftinterface = _should_emit_swiftinterface(attr)

invalid_requested_archs = []

Expand Down Expand Up @@ -603,6 +624,7 @@ def _xcframework_transition_impl(settings, attr):
target_environments.append("simulator")

command_line_options = _command_line_options_for_xcframework_platform(
attr = attr,
minimum_os_version = attr.minimum_os_versions.get(platform_type),
platform_attr = getattr(attr, platform_type),
platform_type = platform_type,
Expand Down
65 changes: 65 additions & 0 deletions test/starlark_tests/targets_under_test/apple/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ objc_library(
swift_library(
name = "swift_lib_for_static_xcfmwk",
srcs = ["DummyFmwk.swift"],
library_evolution = True,
module_name = "ios_static_xcfmwk_with_swift",
tags = common.fixture_tags,
)
Expand All @@ -272,6 +273,7 @@ swift_library(
name = "swift_lib_for_static_xcfmwk_with_headers",
srcs = ["DummyFmwk.swift"],
generates_header = True,
library_evolution = True,
module_name = "ios_static_xcfmwk_with_swift_generated_headers",
tags = common.fixture_tags,
)
Expand All @@ -280,6 +282,7 @@ swift_library(
name = "swift_lib_for_static_xcfmwk_custom_bundle_name",
srcs = ["DummyFmwk.swift"],
generates_header = True,
library_evolution = True,
module_name = "ios_static_xcfmwk_with_custom_bundle_name",
tags = common.fixture_tags,
)
Expand All @@ -289,6 +292,7 @@ swift_library(
srcs = [
"DummyFmwk.swift",
],
library_evolution = True,
module_name = "ios_dynamic_lipoed_swift_xcframework",
tags = common.fixture_tags,
)
Expand All @@ -300,6 +304,9 @@ dummy_test_runner(
apple_xcframework(
name = "ios_dynamic_xcframework",
bundle_id = "com.google.example",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -320,6 +327,9 @@ apple_xcframework(
apple_xcframework(
name = "tvos_dynamic_xcframework",
bundle_id = "com.google.example",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand Down Expand Up @@ -348,6 +358,9 @@ apple_xcframework(
apple_xcframework(
name = "ios_dynamic_lipoed_xcframework",
bundle_id = "com.google.example",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -374,6 +387,9 @@ apple_xcframework(
apple_xcframework(
name = "ios_dynamic_lipoed_swift_xcframework",
bundle_id = "com.google.example",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -399,6 +415,9 @@ apple_xcframework(
families_required = {
"ios": ["ipad"],
},
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
"//test/starlark_tests/resources:Another.plist",
Expand All @@ -420,6 +439,9 @@ apple_xcframework(
apple_xcframework(
name = "ios_dynamic_xcframework_multiple_infoplists",
bundle_id = "com.google.example",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
"//test/starlark_tests/resources:Another.plist",
Expand All @@ -445,6 +467,9 @@ apple_xcframework(
"//test/starlark_tests/resources:Another.plist",
"//test/starlark_tests/resources:resource_bundle",
],
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -465,6 +490,9 @@ apple_xcframework(
apple_xcframework(
name = "ios_dynamic_xcframework_with_deps_resource_bundle",
bundle_id = "com.google.example",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -491,6 +519,9 @@ apple_xcframework(
exported_symbols_lists = [
"//test/starlark_tests/resources:ExportAnotherFunctionShared.exp",
],
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -516,6 +547,9 @@ apple_xcframework(
"//test/starlark_tests/resources:ExportAnotherFunctionShared.exp",
"//test/starlark_tests/resources:ExportDontCallMeShared.exp",
],
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -540,6 +574,9 @@ apple_xcframework(
exported_symbols_lists = [
"//test/starlark_tests/resources:ExportAnotherFunctionShared.exp",
],
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -562,6 +599,9 @@ apple_xcframework(
name = "ios_swift_xcframework_with_generated_header",
bundle_id = "com.google.example",
bundle_name = "SwiftFmwkWithGenHeader",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -588,6 +628,9 @@ apple_xcframework(
name = "ios_dynamic_xcframework_with_umbrella_header_conflict",
bundle_id = "com.google.example",
bundle_name = "UmbrellaHeaderConflict",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
Expand All @@ -608,6 +651,9 @@ apple_xcframework(

apple_static_xcframework(
name = "ios_static_xcframework_oldest_supported",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
ios = {
"simulator": [
"x86_64",
Expand All @@ -628,6 +674,9 @@ apple_static_xcframework(
apple_static_xcframework(
name = "ios_static_xcfmwk_with_avoid_deps",
avoid_deps = [":StaticFmwkLowerLib"],
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
ios = {
"simulator": [
"x86_64",
Expand All @@ -644,6 +693,9 @@ apple_static_xcframework(

apple_static_xcframework(
name = "ios_static_xcfmwk_with_objc_sdk_dylibs_and_and_sdk_frameworks",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
ios = {
"device": ["arm64"],
},
Expand All @@ -659,6 +711,9 @@ apple_static_xcframework(

apple_static_xcframework(
name = "ios_static_xcfmwk_with_swift",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
ios = {
"device": ["arm64"],
"simulator": [
Expand All @@ -676,6 +731,9 @@ apple_static_xcframework(
apple_static_xcframework(
name = "ios_static_xcfmwk_with_swift_and_bundle_name",
bundle_name = "ios_static_xcfmwk_with_custom_bundle_name",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
ios = {
"device": ["arm64"],
"simulator": [
Expand All @@ -692,6 +750,9 @@ apple_static_xcframework(

apple_static_xcframework(
name = "ios_static_xcfmwk_with_swift_generated_headers",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
ios = {
"device": ["arm64"],
"simulator": [
Expand All @@ -711,6 +772,9 @@ apple_static_xcframework(

apple_static_xcframework(
name = "ios_static_xcframework",
# TODO(b/239957001): Remove this when the rule no longer forces library
# evolution.
features = ["apple.no_legacy_swiftinterface"],
ios = {
"simulator": [
"arm64",
Expand Down Expand Up @@ -803,6 +867,7 @@ swift_library(
name = "SwiftFmwkWithGenHeader",
srcs = ["DummyFmwk.swift"],
generates_header = True,
library_evolution = True,
module_name = "SwiftFmwkWithGenHeader",
tags = common.fixture_tags,
)
Expand Down
Loading

1 comment on commit cb9a1de

@keith
Copy link
Member

@keith keith commented on cb9a1de Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.