Skip to content

Commit

Permalink
Use apple_platforms_split_transition for static_framework rules (#1604)
Browse files Browse the repository at this point in the history
This change addresses removal of workaround introduced on <unknown
commit>
to map apple_common.multi_arch_split split attribute keys based on Bazel
Apple static library linking API for iOS and tvOS static framework
rules.

- Removes `_apple_common_multi_arch_split_key` from transition_support
lib.
- Use `cc_toolchain_forwarder` for `_child_configuration_dummy`
attribute.
- Use rule descriptor `deps_cfg` for j2objc and Bazel's binary linking
API attributes.
- Introduce a new rule base transition that propagates
`apple_platforms`.

(cherry picked from commit 15a38a1)
  • Loading branch information
keith authored Aug 11, 2023
1 parent a12d1df commit 53d8764
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 91 deletions.
1 change: 1 addition & 0 deletions apple/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ bzl_library(
deps = [
":apple_product_type",
":bundle_package_type",
":transition_support",
],
)

Expand Down
26 changes: 11 additions & 15 deletions apple/internal/ios_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

"""Implementation of iOS rules."""

load(
"@build_bazel_rules_apple//apple/internal/utils:clang_rt_dylibs.bzl",
"clang_rt_dylibs",
)
load(
"@build_bazel_rules_apple//apple/internal:apple_product_type.bzl",
"apple_product_type",
Expand Down Expand Up @@ -87,10 +91,6 @@ load(
"@build_bazel_rules_apple//apple/internal:swift_support.bzl",
"swift_support",
)
load(
"@build_bazel_rules_swift//swift:swift.bzl",
"SwiftInfo",
)
load(
"@build_bazel_rules_apple//apple/internal:transition_support.bzl",
"transition_support",
Expand All @@ -99,17 +99,14 @@ load(
"@build_bazel_rules_apple//apple/internal/aspects:swift_dynamic_framework_aspect.bzl",
"SwiftDynamicFrameworkInfo",
)
load(
"@build_bazel_rules_apple//apple/internal/utils:clang_rt_dylibs.bzl",
"clang_rt_dylibs",
)
load(
"@build_bazel_rules_apple//apple/internal:framework_import_support.bzl",
"libraries_to_link_for_dynamic_framework",
)
load(
"@build_bazel_rules_apple//apple:providers.bzl",
"AppleFrameworkBundleInfo",
"ApplePlatformInfo",
"IosAppClipBundleInfo",
"IosApplicationBundleInfo",
"IosExtensionBundleInfo",
Expand All @@ -119,6 +116,7 @@ load(
"IosStaticFrameworkBundleInfo",
"IosStickerPackExtensionBundleInfo",
)
load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftInfo")
load("@bazel_skylib//lib:collections.bzl", "collections")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")

Expand Down Expand Up @@ -1392,6 +1390,7 @@ def _ios_static_framework_impl(ctx):
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
avoid_deps = ctx.attr.avoid_deps
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
deps = ctx.attr.deps
label = ctx.label
predeclared_outputs = ctx.outputs
Expand Down Expand Up @@ -1431,15 +1430,11 @@ def _ios_static_framework_impl(ctx):

swift_infos = {}
if swift_support.uses_swift(deps):
for link_output in link_result.outputs:
split_attr_key = transition_support.apple_common_multi_arch_split_key(
cpu = link_output.architecture,
environment = link_output.environment,
platform_type = link_output.platform,
)
for split_attr_key, cc_toolchain in cc_toolchain_forwarder.items():
apple_platform_info = cc_toolchain[ApplePlatformInfo]
for dep in split_deps[split_attr_key]:
if SwiftInfo in dep:
swift_infos[link_output.architecture] = dep[SwiftInfo]
swift_infos[apple_platform_info.target_arch] = dep[SwiftInfo]

# If there's any Swift dependencies on the static framework rule, treat it as a Swift static
# framework.
Expand Down Expand Up @@ -2147,6 +2142,7 @@ target. Finally, it also bundles a `module.modulemap` file pointing to the
umbrella header for Objetive-C module compatibility. This umbrella header and
modulemap can be skipped by disabling the `swift.no_generated_header` feature (
i.e. `--features=-swift.no_generated_header`).""",
cfg = transition_support.apple_platforms_rule_base_transition,
)

ios_imessage_application = rule_factory.create_apple_bundling_rule(
Expand Down
13 changes: 6 additions & 7 deletions apple/internal/macos_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ load(
"AppleBinaryInfo",
"AppleBinaryInfoplistInfo",
"AppleFrameworkBundleInfo",
"ApplePlatformInfo",
"MacosApplicationBundleInfo",
"MacosBundleBundleInfo",
"MacosExtensionBundleInfo",
Expand Down Expand Up @@ -2369,6 +2370,7 @@ def _macos_static_framework_impl(ctx):
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
avoid_deps = ctx.attr.avoid_deps
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
deps = ctx.attr.deps
label = ctx.label
predeclared_outputs = ctx.outputs
Expand Down Expand Up @@ -2408,15 +2410,11 @@ def _macos_static_framework_impl(ctx):

swift_infos = {}
if swift_support.uses_swift(deps):
for link_output in link_result.outputs:
split_attr_key = transition_support.apple_common_multi_arch_split_key(
cpu = link_output.architecture,
environment = link_output.environment,
platform_type = link_output.platform,
)
for split_attr_key, cc_toolchain in cc_toolchain_forwarder.items():
apple_platform_info = cc_toolchain[ApplePlatformInfo]
for dep in split_deps[split_attr_key]:
if SwiftInfo in dep:
swift_infos[link_output.architecture] = dep[SwiftInfo]
swift_infos[apple_platform_info.target_arch] = dep[SwiftInfo]

# If there's any Swift dependencies on the static framework rule, treat it as a Swift static
# framework.
Expand Down Expand Up @@ -2505,6 +2503,7 @@ macos_static_framework = rule_factory.create_apple_bundling_rule(
implementation = _macos_static_framework_impl,
platform_type = "macos",
product_type = apple_product_type.static_framework,
cfg = transition_support.apple_platforms_rule_base_transition,
doc = """Builds and bundles a macOS static framework for third-party distribution.
A static framework is bundled like a dynamic framework except that the embedded
Expand Down
59 changes: 48 additions & 11 deletions apple/internal/rule_factory.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ load(
"@build_bazel_rules_apple//apple:providers.bzl",
"AppleBundleInfo",
"AppleBundleVersionInfo",
"ApplePlatformInfo",
"AppleResourceBundleInfo",
"AppleTestRunnerInfo",
"IosAppClipBundleInfo",
Expand Down Expand Up @@ -125,7 +126,9 @@ def _common_linking_api_attrs(*, cfg = apple_common.multi_arch_split):
return {
"_child_configuration_dummy": attr.label(
cfg = cfg,
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
providers = [cc_common.CcToolchainInfo, ApplePlatformInfo],
default =
"@build_bazel_rules_apple//apple:default_cc_toolchain_forwarder",
),
}

Expand Down Expand Up @@ -162,12 +165,22 @@ def _link_multi_arch_binary_attrs(*, cfg = apple_common.multi_arch_split):

# Needed for the J2ObjC processing code that already exists in the implementation of
# apple_common.link_multi_arch_binary.
_J2OBJC_BINARY_LINKING_ATTRS = {
"_dummy_lib": attr.label(
cfg = apple_common.multi_arch_split,
default = Label("@bazel_tools//tools/objc:dummy_lib"),
),
}
def _j2objc_binary_linking_attrs(*, cfg):
return {
"_dummy_lib": attr.label(
cfg = cfg,
default = Label("@bazel_tools//tools/objc:dummy_lib"),
),
"_j2objc_dead_code_pruner": attr.label(
executable = True,
# Setting `allow_single_file=True` would be more correct. Unfortunately,
# doing so prevents using py_binary as the underlying target because py_binary
# produces at least _two_ output files (the executable plus any files in srcs)
allow_files = True,
cfg = "exec",
default = Label("@bazel_tools//tools/objc:j2objc_dead_code_pruner_binary"),
),
}

_COMMON_TEST_ATTRS = {
"data": attr.label_list(
Expand Down Expand Up @@ -251,8 +264,8 @@ def _common_binary_linking_attrs(deps_cfg, product_type):

return dicts.add(
_COMMON_ATTRS,
_J2OBJC_BINARY_LINKING_ATTRS,
_link_multi_arch_binary_attrs(),
_j2objc_binary_linking_attrs(cfg = deps_cfg),
_link_multi_arch_binary_attrs(cfg = deps_cfg),
{
# This attribute is required by the Clang runtime libraries processing partial.
# See utils/clang_rt_dylibs.bzl and partials/clang_rt_dylibs.bzl
Expand Down Expand Up @@ -601,6 +614,12 @@ to manually dlopen the framework at runtime.
default = True,
doc = "Private attribute to generate Swift interfaces for static frameworks.",
),
"_cc_toolchain_forwarder": attr.label(
cfg = transition_support.apple_platform_split_transition,
providers = [cc_common.CcToolchainInfo, ApplePlatformInfo],
default =
"@build_bazel_rules_apple//apple:default_cc_toolchain_forwarder",
),
"hdrs": attr.label_list(
allow_files = [".h"],
doc = """
Expand All @@ -619,7 +638,7 @@ umbrella header will be generated under the same name as this target.
""",
),
"avoid_deps": attr.label_list(
cfg = apple_common.multi_arch_split,
cfg = transition_support.apple_platform_split_transition,
doc = """
A list of library targets on which this framework depends in order to compile, but the transitive
closure of which will not be linked into the framework's binary.
Expand Down Expand Up @@ -859,6 +878,12 @@ to manually dlopen the framework at runtime.
default = True,
doc = "Private attribute to generate Swift interfaces for static frameworks.",
),
"_cc_toolchain_forwarder": attr.label(
cfg = transition_support.apple_platform_split_transition,
providers = [cc_common.CcToolchainInfo, ApplePlatformInfo],
default =
"@build_bazel_rules_apple//apple:default_cc_toolchain_forwarder",
),
"hdrs": attr.label_list(
allow_files = [".h"],
doc = """
Expand Down Expand Up @@ -981,6 +1006,12 @@ to manually dlopen the framework at runtime.
default = True,
doc = "Private attribute to generate Swift interfaces for static frameworks.",
),
"_cc_toolchain_forwarder": attr.label(
cfg = transition_support.apple_platform_split_transition,
providers = [cc_common.CcToolchainInfo, ApplePlatformInfo],
default =
"@build_bazel_rules_apple//apple:default_cc_toolchain_forwarder",
),
"hdrs": attr.label_list(
allow_files = [".h"],
doc = """
Expand All @@ -999,7 +1030,7 @@ umbrella header will be generated under the same name as this target.
""",
),
"avoid_deps": attr.label_list(
cfg = apple_common.multi_arch_split,
cfg = transition_support.apple_platform_split_transition,
doc = """
A list of library targets on which this framework depends in order to compile, but the transitive
closure of which will not be linked into the framework's binary.
Expand Down Expand Up @@ -1118,6 +1149,12 @@ to manually dlopen the framework at runtime.
default = True,
doc = "Private attribute to generate Swift interfaces for static frameworks.",
),
"_cc_toolchain_forwarder": attr.label(
cfg = transition_support.apple_platform_split_transition,
providers = [cc_common.CcToolchainInfo, ApplePlatformInfo],
default =
"@build_bazel_rules_apple//apple:default_cc_toolchain_forwarder",
),
"hdrs": attr.label_list(
allow_files = [".h"],
doc = """
Expand Down
8 changes: 6 additions & 2 deletions apple/internal/rule_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ load(
"@build_bazel_rules_apple//apple/internal:bundle_package_type.bzl",
"bundle_package_type",
)
load(
"@build_bazel_rules_apple//apple/internal:transition_support.bzl",
"transition_support",
)

# Options to declare signing behavior and exceptions.
#
Expand Down Expand Up @@ -352,7 +356,7 @@ _RULE_TYPE_DESCRIPTORS = {
allowed_device_families = ["iphone", "ipad"],
bundle_extension = ".framework",
codesigning_exceptions = _CODESIGNING_EXCEPTIONS.skip_signing,
deps_cfg = apple_common.multi_arch_split,
deps_cfg = transition_support.apple_platform_split_transition,
has_infoplist = False,
product_type = apple_product_type.static_framework,
requires_bundle_id = False,
Expand Down Expand Up @@ -705,7 +709,7 @@ _RULE_TYPE_DESCRIPTORS = {
allowed_device_families = ["tv"],
bundle_extension = ".framework",
codesigning_exceptions = _CODESIGNING_EXCEPTIONS.skip_signing,
deps_cfg = apple_common.multi_arch_split,
deps_cfg = transition_support.apple_platform_split_transition,
has_infoplist = False,
product_type = apple_product_type.static_framework,
requires_bundle_id = False,
Expand Down
Loading

0 comments on commit 53d8764

Please sign in to comment.