From ab29ef850417b78c682060bed173b9b0b03ea7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Seweryn=20Pla=C5=BCuk?= Date: Tue, 24 Sep 2024 21:49:31 +0200 Subject: [PATCH] Fix apple_static_xcframework rule to exclude bundles of dependencies marked as avoid_deps (#2541) Fixes https://github.com/bazelbuild/rules_apple/issues/2540. ~Alternatively, I thought about not including resources from the dependencies passed in `avoid_deps`, but I'm not sure that this would not be a breaking change.~ --- apple/internal/xcframework_rules.bzl | 9 ++++++- doc/rules-apple.md | 2 +- .../apple_static_xcframework_tests.bzl | 17 ++++++++++++ .../targets_under_test/apple/BUILD | 27 +++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/apple/internal/xcframework_rules.bzl b/apple/internal/xcframework_rules.bzl index 6c965f73da..e862c1e220 100644 --- a/apple/internal/xcframework_rules.bzl +++ b/apple/internal/xcframework_rules.bzl @@ -1046,6 +1046,11 @@ def _apple_static_xcframework_impl(ctx): split_attr = ctx.split_attr, split_attr_keys = link_output.split_attr_keys, ) + targets_to_avoid = _unioned_attrs( + attr_names = ["avoid_deps"], + split_attr = ctx.split_attr, + split_attr_keys = link_output.split_attr_keys, + ) partial_output = partial.call(partials.resources_partial( actions = actions, apple_mac_toolchain_info = apple_mac_toolchain_info, @@ -1060,6 +1065,7 @@ def _apple_static_xcframework_impl(ctx): resource_deps = resource_deps, rule_descriptor = rule_descriptor, rule_label = label, + targets_to_avoid = targets_to_avoid, version = None, )) @@ -1147,12 +1153,13 @@ the target will be used instead. default = "@build_bazel_rules_apple//apple/internal:environment_plist_ios", ), "avoid_deps": attr.label_list( + aspects = [apple_resource_aspect], allow_files = True, cfg = transition_support.xcframework_transition, mandatory = False, 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. +closure of which will not be linked into the framework's binary, nor bundled into final XCFramework. """, ), "bundle_name": attr.string( diff --git a/doc/rules-apple.md b/doc/rules-apple.md index 17c3b625ab..81a87e7222 100755 --- a/doc/rules-apple.md +++ b/doc/rules-apple.md @@ -187,7 +187,7 @@ Generates an XCFramework with static libraries for third-party distribution. | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | | deps | A list of files directly referencing libraries to be represented for each given platform split in the XCFramework. These libraries will be embedded within each platform split. | List of labels | required | | -| avoid_deps | 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. | List of labels | optional | `[]` | +| avoid_deps | 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, nor bundled into final XCFramework. | List of labels | optional | `[]` | | bundle_name | The desired name of the XCFramework bundle (without the extension) and the binaries for all embedded static libraries. If this attribute is not set, then the name of the target will be used instead. | String | optional | `""` | | executable_name | The desired name of the executable, if the bundle has an executable. If this attribute is not set, then the name of the `bundle_name` attribute will be used if it is set; if not, then the name of the target will be used instead. | String | optional | `""` | | families_required | A list of device families supported by this extension, with platforms such as `ios` as keys. Valid values are `iphone` and `ipad` for `ios`; at least one must be specified if a platform is defined. Currently, this only affects processing of `ios` resources. | Dictionary: String -> List of strings | optional | `{}` | diff --git a/test/starlark_tests/apple_static_xcframework_tests.bzl b/test/starlark_tests/apple_static_xcframework_tests.bzl index b631bd9c93..50b86eb514 100644 --- a/test/starlark_tests/apple_static_xcframework_tests.bzl +++ b/test/starlark_tests/apple_static_xcframework_tests.bzl @@ -89,6 +89,23 @@ def apple_static_xcframework_test_suite(name): tags = [name], ) + archive_contents_test( + name = "{}_ios_avoid_deps_bundles_test".format(name), + build_type = "device", + compilation_mode = "opt", + target_under_test = "//test/starlark_tests/targets_under_test/apple:ios_xcframework_bundling_static_fmwks_with_avoid_deps", + contains = [ + "$BUNDLE_ROOT/ios-arm64/ios_xcframework_bundling_static_fmwks_with_avoid_deps.framework/ios_xcframework_bundling_static_fmwks_with_avoid_deps", + "$BUNDLE_ROOT/ios-arm64_x86_64-simulator/ios_xcframework_bundling_static_fmwks_with_avoid_deps.framework/ios_xcframework_bundling_static_fmwks_with_avoid_deps", + "$BUNDLE_ROOT/Info.plist", + ], + not_contains = [ + "$BUNDLE_ROOT/ios-arm64/ios_xcframework_bundling_static_fmwks_with_avoid_deps.framework/resource_bundle.bundle", + "$BUNDLE_ROOT/ios-arm64_x86_64-simulator/ios_xcframework_bundling_static_fmwks_with_avoid_deps.framework/resource_bundle.bundle", + ], + tags = [name], + ) + archive_contents_test( name = "{}_objc_generated_modulemap_file_content_test".format(name), build_type = "device", diff --git a/test/starlark_tests/targets_under_test/apple/BUILD b/test/starlark_tests/targets_under_test/apple/BUILD index b472041f97..a4b5ad477e 100644 --- a/test/starlark_tests/targets_under_test/apple/BUILD +++ b/test/starlark_tests/targets_under_test/apple/BUILD @@ -939,6 +939,16 @@ objc_library( tags = common.fixture_tags, ) +objc_library( + name = "fmwk_lib_with_resources_upper_lib", + srcs = [ + "//test/starlark_tests/resources:shared.h", + "//test/starlark_tests/resources:shared.m", + ], + tags = common.fixture_tags, + deps = [":fmwk_lib_with_resources"], +) + apple_static_xcframework( name = "ios_static_xcframework_with_resources", ios = { @@ -1007,6 +1017,23 @@ apple_static_xcframework_import( xcframework_imports = [":generated_ios_static_xcframework_with_resources"], ) +apple_static_xcframework( + name = "ios_xcframework_bundling_static_fmwks_with_avoid_deps", + avoid_deps = [":fmwk_lib_with_resources"], + ios = { + "simulator": [ + "x86_64", + "arm64", + ], + "device": ["arm64"], + }, + minimum_os_versions = { + "ios": common.min_os_ios.baseline, + }, + tags = common.fixture_tags, + deps = [":fmwk_lib_with_resources_upper_lib"], +) + apple_static_xcframework( name = "ios_xcframework_bundling_static_fmwks", ios = {