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
(cherry picked from commit cb9a1de)
  • Loading branch information
allevato authored and keith committed Aug 18, 2023
1 parent 7c5fab9 commit 8bdaeaf
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 @@ -291,6 +291,7 @@ def _resolved_environment_arch_for_arch(*, arch, environment, platform_type):

def _command_line_options_for_xcframework_platform(
*,
attr,
minimum_os_version,
platform_attr,
platform_type,
Expand All @@ -299,6 +300,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 @@ -332,7 +334,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 @@ -343,10 +348,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 @@ -408,7 +432,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 @@ -481,10 +505,7 @@ def _apple_platform_split_transition_impl(settings, attr):
output_dictionary = {}
invalid_requested_archs = []

# 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)

if settings["//command_line_option:incompatible_enable_apple_toolchain_resolution"]:
platforms = (
Expand Down Expand Up @@ -608,6 +629,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 @@ -265,6 +265,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 @@ -273,6 +274,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 @@ -281,6 +283,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 @@ -290,6 +293,7 @@ swift_library(
srcs = [
"DummyFmwk.swift",
],
library_evolution = True,
module_name = "ios_dynamic_lipoed_swift_xcframework",
tags = common.fixture_tags,
)
Expand All @@ -301,6 +305,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 Down Expand Up @@ -580,6 +587,9 @@ apple_static_xcframework_import(
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 @@ -608,6 +618,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 @@ -634,6 +647,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 @@ -659,6 +675,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 @@ -680,6 +699,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 @@ -705,6 +727,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 @@ -725,6 +750,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 @@ -751,6 +779,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 @@ -776,6 +807,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 @@ -800,6 +834,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 @@ -822,6 +859,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 @@ -848,6 +888,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 @@ -868,6 +911,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 Down Expand Up @@ -1004,6 +1050,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 @@ -1020,6 +1069,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 @@ -1035,6 +1087,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 @@ -1052,6 +1107,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 @@ -1068,6 +1126,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 @@ -1087,6 +1148,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 @@ -1179,6 +1243,7 @@ swift_library(
name = "SwiftFmwkWithGenHeader",
srcs = ["DummyFmwk.swift"],
generates_header = True,
library_evolution = True,
module_name = "SwiftFmwkWithGenHeader",
tags = common.fixture_tags,
)
Expand Down
Loading

0 comments on commit 8bdaeaf

Please sign in to comment.