Skip to content

Commit 91be02a

Browse files
gkoremanluispadron
andauthored
Add use_runfiles aspect_hint to include runfiles for specific cc_libr… (#2479)
Fixes #2477 Include all data from cc_libraries as runfiles or resources, depending on aspect_hints. Provide aspect_hints for resource collection for all resource targets (eg, swift_library, cc_library, etc) By default all data is now included from cc_libraries as runfiles and follows the expected runfiles folder structure, with files retaining their nested folders and being placed in /external/pkg_name/some/file.txt when included from a http_archive or local_repository. The default behavior for other resource collecting targets like swift_library has not changed, and will continue to collect and process data as resources. To allow modification of this default behavior, users may add aspect_hints to the target cc_library/swift_library/etc. There are three supported aspect_hints: @build_bazel_rules_apple//apple:use_runfiles @build_bazel_rules_apple//apple:use_resources @build_bazel_rules_apple//apple:suppress_resources ##### Example Here is an example of modifying the default behavior to bundle data.txt as a resource instead of a runfile. ``` cc_library( name = "libapp", srcs = ["main.cpp",], data = [":data.txt"], aspect_hints = ["@build_bazel_rules_apple//apple:use_runfiles"], ) macos_application( name = "app_macos", deps = [":libapp"], ) ``` data.txt is bundled in Contents/Resources/data.txt #### Note Hints apply only to the target and do not affect transitive deps, however if a target includes runfiles then all runfiles are bundled (including transitive runfiles) regardless of the hints applied to transitive targets. --------- Co-authored-by: Luis Padron <heyluispadron@gmail.com>
1 parent 4b40a88 commit 91be02a

14 files changed

Lines changed: 389 additions & 28 deletions

File tree

apple/BUILD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load("//apple/internal/aspects:resource_aspect_hint.bzl", "apple_resource_hint", "apple_resource_hint_action")
23
load(":cc_toolchain_forwarder.bzl", "cc_toolchain_forwarder")
34

45
package(default_visibility = ["//visibility:public"])
@@ -278,3 +279,20 @@ filegroup(
278279
],
279280
visibility = ["//:__pkg__"],
280281
)
282+
283+
# An aspect hint that enables runfile inclusion as AppleResources
284+
# for cc_libraries. Runfiles keep their folder structure.
285+
apple_resource_hint(
286+
name = "use_resources",
287+
action = apple_resource_hint_action.resources,
288+
)
289+
290+
apple_resource_hint(
291+
name = "use_runfiles",
292+
action = apple_resource_hint_action.runfiles,
293+
)
294+
295+
apple_resource_hint(
296+
name = "suppress_resources",
297+
action = apple_resource_hint_action.suppress,
298+
)

apple/internal/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ bzl_library(
319319
"//apple:providers",
320320
"//apple/internal/aspects:framework_provider_aspect",
321321
"//apple/internal/aspects:resource_aspect",
322+
"//apple/internal/aspects:resource_aspect_hint",
322323
"//apple/internal/utils:clang_rt_dylibs",
323324
"//apple/internal/utils:main_thread_checker_dylibs",
324325
"@bazel_skylib//lib:collections",
@@ -394,6 +395,7 @@ bzl_library(
394395
"//apple:providers",
395396
"//apple/internal/aspects:framework_provider_aspect",
396397
"//apple/internal/aspects:resource_aspect",
398+
"//apple/internal/aspects:resource_aspect_hint",
397399
"//apple/internal/utils:clang_rt_dylibs",
398400
"//apple/internal/utils:main_thread_checker_dylibs",
399401
],
@@ -533,6 +535,7 @@ bzl_library(
533535
"//apple/internal/aspects:app_intents_aspect",
534536
"//apple/internal/aspects:framework_provider_aspect",
535537
"//apple/internal/aspects:resource_aspect",
538+
"//apple/internal/aspects:resource_aspect_hint",
536539
"//apple/internal/aspects:swift_usage_aspect",
537540
"//apple/internal/testing:apple_test_bundle_support",
538541
"@bazel_skylib//lib:dicts",
@@ -556,6 +559,7 @@ bzl_library(
556559
"//apple:providers",
557560
"//apple/internal/aspects:framework_provider_aspect",
558561
"//apple/internal/aspects:resource_aspect",
562+
"//apple/internal/aspects:resource_aspect_hint",
559563
"//apple/internal/aspects:swift_dynamic_framework_aspect",
560564
"//apple/internal/aspects:swift_usage_aspect",
561565
"//apple/internal/testing:apple_test_bundle_support",
@@ -667,6 +671,7 @@ bzl_library(
667671
"//apple:providers",
668672
"//apple/internal/aspects:framework_provider_aspect",
669673
"//apple/internal/aspects:resource_aspect",
674+
"//apple/internal/aspects:resource_aspect_hint",
670675
"//apple/internal/utils:clang_rt_dylibs",
671676
"//apple/internal/utils:main_thread_checker_dylibs",
672677
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
@@ -703,6 +708,7 @@ bzl_library(
703708
"//apple:providers",
704709
"//apple/internal/aspects:framework_provider_aspect",
705710
"//apple/internal/aspects:resource_aspect",
711+
"//apple/internal/aspects:resource_aspect_hint",
706712
"//apple/internal/utils:clang_rt_dylibs",
707713
"//apple/internal/utils:main_thread_checker_dylibs",
708714
"@bazel_skylib//lib:sets",
@@ -738,6 +744,7 @@ bzl_library(
738744
"//apple:providers",
739745
"//apple/internal/aspects:framework_provider_aspect",
740746
"//apple/internal/aspects:resource_aspect",
747+
"//apple/internal/aspects:resource_aspect_hint",
741748
"//apple/internal/utils:clang_rt_dylibs",
742749
"//apple/internal/utils:main_thread_checker_dylibs",
743750
"@bazel_skylib//lib:sets",
@@ -771,6 +778,7 @@ bzl_library(
771778
":transition_support",
772779
"//apple:providers",
773780
"//apple/internal/aspects:resource_aspect",
781+
"//apple/internal/aspects:resource_aspect_hint",
774782
"//apple/internal/aspects:swift_usage_aspect",
775783
"//apple/internal/utils:files",
776784
"@bazel_skylib//lib:partial",

apple/internal/aspects/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ bzl_library(
5858
],
5959
)
6060

61+
bzl_library(
62+
name = "resource_aspect_hint",
63+
srcs = ["resource_aspect_hint.bzl"],
64+
visibility = [
65+
"//apple/internal:__pkg__",
66+
"//apple/internal/testing:__pkg__",
67+
],
68+
deps = [
69+
"@build_bazel_rules_swift//swift",
70+
],
71+
)
72+
6173
bzl_library(
6274
name = "swift_dynamic_framework_aspect",
6375
srcs = ["swift_dynamic_framework_aspect.bzl"],

apple/internal/aspects/resource_aspect.bzl

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ load(
5858
"@build_bazel_rules_apple//apple/internal:swift_support.bzl",
5959
"swift_support",
6060
)
61+
load(
62+
"@build_bazel_rules_apple//apple/internal/aspects:resource_aspect_hint.bzl",
63+
"AppleResourceHintInfo",
64+
"apple_resource_hint_action",
65+
)
6166
load(
6267
"@build_bazel_rules_apple//apple/internal/providers:apple_debug_info.bzl",
6368
"AppleDebugInfo",
@@ -125,48 +130,85 @@ def _apple_resource_aspect_impl(target, ctx):
125130
collect_structured_args = {}
126131
collect_framework_import_bundle_files = None
127132

133+
hint_action = None
134+
default_action = None
135+
136+
# TODO: remove usage of `getattr` and use `aspect_ctx.rule.attr.aspect_hints` directly when we drop Bazel 6.
137+
aspect_hint = None
138+
for hint in getattr(ctx.rule.attr, "aspect_hints", []):
139+
if AppleResourceHintInfo in hint:
140+
if aspect_hint:
141+
fail(("Conflicting AppleResourceHintInfo from aspect hints " +
142+
"'{hint1}' and '{hint2}'. Only one is " +
143+
"allowed.").format(
144+
hint1 = str(aspect_hint.label),
145+
hint2 = str(hint.label),
146+
))
147+
aspect_hint = hint
148+
hint_action = hint[AppleResourceHintInfo].action
149+
128150
# Owner to attach to the resources as they're being bucketed.
129151
owner = None
130152

131153
# The name of the bundle directory to place resources within, if required.
132154
bundle_name = None
133155

134156
if ctx.rule.kind == "objc_library":
157+
default_action = apple_resource_hint_action.resources
135158
collect_args["res_attrs"] = ["data"]
136159

137160
# Only set objc_library targets as owners if they have srcs, non_arc_srcs or deps. This
138161
# treats objc_library targets without sources as resource aggregators.
139162
if ctx.rule.attr.srcs or ctx.rule.attr.non_arc_srcs or ctx.rule.attr.deps:
140163
owner = str(ctx.label)
141164

165+
elif ctx.rule.kind == "cc_library":
166+
default_action = apple_resource_hint_action.runfiles
167+
collect_args["res_attrs"] = ["data"]
168+
142169
elif ctx.rule.kind == "objc_import":
170+
default_action = apple_resource_hint_action.resources
171+
collect_args["res_attrs"] = ["data"]
172+
173+
elif ctx.rule.kind == "cc_import":
174+
default_action = apple_resource_hint_action.runfiles
143175
collect_args["res_attrs"] = ["data"]
144176

145177
elif ctx.rule.kind == "swift_library":
178+
default_action = apple_resource_hint_action.resources
146179
module_names = [x.name for x in target[SwiftInfo].direct_modules if x.swift]
147180
bucketize_args["swift_module"] = module_names[0] if module_names else None
148181
collect_args["res_attrs"] = ["data"]
149182
owner = str(ctx.label)
150183

151184
elif ctx.rule.kind in ["apple_static_framework_import", "apple_static_xcframework_import"]:
185+
default_action = apple_resource_hint_action.resources
152186
if AppleFrameworkImportBundleInfo in target:
153187
collect_framework_import_bundle_files = target[AppleFrameworkImportBundleInfo].bundle_files
154188
collect_args["res_attrs"] = ["data"]
155189
owner = str(ctx.label)
156190

157191
elif ctx.rule.kind == "apple_resource_group":
192+
default_action = apple_resource_hint_action.resources
158193
collect_args["res_attrs"] = ["resources"]
159194
collect_structured_args["res_attrs"] = ["structured_resources"]
160195

161196
elif ctx.rule.kind == "apple_resource_bundle":
197+
default_action = apple_resource_hint_action.resources
162198
collect_infoplists_args["res_attrs"] = ["infoplists"]
163199
collect_args["res_attrs"] = ["resources"]
164200
collect_structured_args["res_attrs"] = ["structured_resources"]
165201
process_args["bundle_id"] = ctx.rule.attr.bundle_id or None
166202
bundle_name = "{}.bundle".format(ctx.rule.attr.bundle_name or ctx.label.name)
167203

204+
if hint_action:
205+
default_action = hint_action
206+
207+
is_resource_action = default_action == apple_resource_hint_action.resources
208+
is_runfiles_action = default_action == apple_resource_hint_action.runfiles
209+
168210
# Collect all resource files related to this target.
169-
if collect_infoplists_args:
211+
if collect_infoplists_args and is_resource_action:
170212
infoplists = resources.collect(
171213
attr = ctx.rule.attr,
172214
**collect_infoplists_args
@@ -190,7 +232,7 @@ def _apple_resource_aspect_impl(target, ctx):
190232
),
191233
)
192234

193-
if collect_args:
235+
if collect_args and is_resource_action:
194236
resource_files = resources.collect(
195237
attr = ctx.rule.attr,
196238
**collect_args
@@ -213,7 +255,7 @@ def _apple_resource_aspect_impl(target, ctx):
213255
),
214256
)
215257

216-
if collect_structured_args:
258+
if collect_structured_args and is_resource_action:
217259
# `structured_resources` requires an explicit parent directory, requiring them to be
218260
# processed differently from `resources` and resources inherited from other fields.
219261
#
@@ -264,7 +306,7 @@ def _apple_resource_aspect_impl(target, ctx):
264306
)
265307

266308
# Collect .bundle/ files from framework_import rules
267-
if collect_framework_import_bundle_files:
309+
if collect_framework_import_bundle_files and is_resource_action:
268310
parent_dir_param = partial.make(
269311
resources.bundle_relative_parent_dir,
270312
extension = "bundle",
@@ -278,6 +320,18 @@ def _apple_resource_aspect_impl(target, ctx):
278320
),
279321
)
280322

323+
if is_runfiles_action:
324+
# Gather the runfiles and mark them as pre-processed/unprocessed
325+
# dylibs are excluded from runfile packaging because they are included in the Content/Resources directory instead.
326+
apple_resource_infos.append(
327+
resources.bucketize_typed(
328+
[x for x in target[DefaultInfo].default_runfiles.files.to_list() if x.extension != "dylib"],
329+
owner = None,
330+
bucket_type = "unprocessed",
331+
parent_dir_param = partial.make(resources.runfiles_resources_parent_dir),
332+
),
333+
)
334+
281335
# Get the providers from dependencies, referenced by deps and locations for resources.
282336
apple_debug_infos = []
283337
apple_dsym_bundle_infos = []

0 commit comments

Comments
 (0)