Skip to content

Commit 4e01cab

Browse files
committed
Fix precompiled resource bundle warnings
For SPM modules that use precompiled resource bundles (i.e `.bundle` files), Bazel throws the warning ``` WARNING: input 'Sources/Down/Resources/DownView (macOS).bundle' is a directory; dependency checking of directories is unsound ``` Instead of just referencing the `.bundle` path directly we should wrap it in a `apple_bundle_import` for the prebuilt bundle and then add it into the `resources` attr.
1 parent c1b29f1 commit 4e01cab

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

swiftpkg/internal/swiftpkg_build_files.bzl

+39-2
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,9 @@ expected: {expected}\
727727

728728
# MARK: - Apple Resource Group
729729

730+
def _sanitized_bundle_file_name(bundle_path):
731+
return bundle_path.replace(" ", "_").replace(".", "_").replace("(", "_").replace(")", "_")
732+
730733
def _apple_resource_bundle(target, package_name, default_localization, expose_build_targets):
731734
bzl_target_name = pkginfo_targets.bazel_label_name(target)
732735
bundle_label_name = pkginfo_targets.resource_bundle_label_name(bzl_target_name)
@@ -735,10 +738,21 @@ def _apple_resource_bundle(target, package_name, default_localization, expose_bu
735738
bzl_target_name,
736739
)
737740

738-
resources = sorted([
741+
sorted_resources = sorted([
739742
r.path
740743
for r in target.resources
741744
])
745+
resources = [
746+
r
747+
for r in sorted_resources
748+
if not r.endswith(".bundle")
749+
]
750+
precompiled_bundles_and_labels = [
751+
(r, "{}_{}".format(bundle_label_name, _sanitized_bundle_file_name(r.split("/")[-1])))
752+
for r in sorted_resources
753+
if r.endswith(".bundle")
754+
]
755+
precompiled_bundle_resource_labels = [bundle_pair[1] for bundle_pair in precompiled_bundles_and_labels]
742756

743757
load_stmts = [
744758
apple_resource_bundle_load_stmt,
@@ -760,11 +774,28 @@ def _apple_resource_bundle(target, package_name, default_localization, expose_bu
760774
"infoplists": [":{}".format(infoplist_name)],
761775
# Based upon the code in SPM, it looks like they only support unstructured resources.
762776
# https://github.com/apple/swift-package-manager/blob/main/Sources/PackageModel/Resource.swift#L25-L33
763-
"resources": resources,
777+
"resources": resources + [":{}".format(bundle_label) for bundle_label in precompiled_bundle_resource_labels],
764778
"visibility": _target_visibility(expose_build_targets),
765779
},
766780
),
767781
]
782+
783+
if len(precompiled_bundles_and_labels) > 0:
784+
load_stmts.append(apple_resource_bundle_import_load_stmt)
785+
for precompiled_bundle, precompiled_bundle_label_name in precompiled_bundles_and_labels:
786+
decls.append(
787+
build_decls.new(
788+
kind = apple_kinds.apple_bundle_import,
789+
name = precompiled_bundle_label_name,
790+
attrs = {
791+
"bundle_imports": scg.new_fn_call(
792+
"glob",
793+
["{precompiled_bundle}/**/*".format(precompiled_bundle = precompiled_bundle)],
794+
),
795+
},
796+
),
797+
)
798+
768799
return struct(
769800
bundle_name = bundle_name,
770801
bundle_label_name = bundle_label_name,
@@ -1135,6 +1166,7 @@ apple_kinds = struct(
11351166
static_xcframework_import = "apple_static_xcframework_import",
11361167
dynamic_xcframework_import = "apple_dynamic_xcframework_import",
11371168
resource_bundle = "apple_resource_bundle",
1169+
apple_bundle_import = "apple_bundle_import",
11381170
)
11391171

11401172
apple_apple_location = "@build_bazel_rules_apple//apple:apple.bzl"
@@ -1156,6 +1188,11 @@ apple_resource_bundle_load_stmt = load_statements.new(
11561188
apple_kinds.resource_bundle,
11571189
)
11581190

1191+
apple_resource_bundle_import_load_stmt = load_statements.new(
1192+
apple_resources_location,
1193+
apple_kinds.apple_bundle_import,
1194+
)
1195+
11591196
swiftpkg_kinds = struct(
11601197
generate_modulemap = "generate_modulemap",
11611198
objc_resource_bundle_accessor_hdr = "objc_resource_bundle_accessor_hdr",

swiftpkg/tests/swiftpkg_build_files_tests.bzl

+83
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,33 @@ def _pkg_info(
437437
repo_name = _repo_name,
438438
swift_src_info = pkginfos.new_swift_src_info(),
439439
),
440+
pkginfos.new_target(
441+
name = "SwiftLibraryWithPrecompiledBundleResource",
442+
type = "regular",
443+
c99name = "SwiftLibraryWithPrecompiledBundleResource",
444+
module_type = "SwiftTarget",
445+
path = "Source/SwiftLibraryWithPrecompiledBundleResource",
446+
sources = [
447+
"SwiftLibraryWithPrecompiledBundleResource.swift",
448+
],
449+
resources = [
450+
pkginfos.new_resource(
451+
path = "Source/SwiftLibraryWithPrecompiledBundleResource/Resources/PrecompiledResources.bundle",
452+
rule = pkginfos.new_resource_rule(
453+
process = pkginfos.new_resource_rule_process(),
454+
),
455+
),
456+
pkginfos.new_resource(
457+
path = "Source/SwiftLibraryWithPrecompiledBundleResource/Resources/chicken.json",
458+
rule = pkginfos.new_resource_rule(
459+
process = pkginfos.new_resource_rule_process(),
460+
),
461+
),
462+
],
463+
dependencies = [],
464+
repo_name = _repo_name,
465+
swift_src_info = pkginfos.new_swift_src_info(),
466+
),
440467
pkginfos.new_target(
441468
name = "ObjcLibraryWithResources",
442469
type = "regular",
@@ -958,6 +985,62 @@ swift_library(
958985
tags = ["manual"],
959986
visibility = ["//:__subpackages__"],
960987
)
988+
""",
989+
),
990+
struct(
991+
msg = "Swift library target with precompiled bundle resource.",
992+
name = "SwiftLibraryWithPrecompiledBundleResource",
993+
pkg_info = _pkg_info(),
994+
exp = """\
995+
load("@build_bazel_rules_apple//apple:resources.bzl", "apple_bundle_import", "apple_resource_bundle")
996+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
997+
load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "resource_bundle_accessor", "resource_bundle_infoplist")
998+
999+
apple_bundle_import(
1000+
name = "SwiftLibraryWithPrecompiledBundleResource.rspm_resource_bundle_PrecompiledResources_bundle",
1001+
bundle_imports = glob(["Source/SwiftLibraryWithPrecompiledBundleResource/Resources/PrecompiledResources.bundle/**/*"]),
1002+
)
1003+
1004+
apple_resource_bundle(
1005+
name = "SwiftLibraryWithPrecompiledBundleResource.rspm_resource_bundle",
1006+
bundle_name = "MyPackage_SwiftLibraryWithPrecompiledBundleResource",
1007+
infoplists = [":SwiftLibraryWithPrecompiledBundleResource.rspm_resource_bundle_infoplist"],
1008+
resources = [
1009+
"Source/SwiftLibraryWithPrecompiledBundleResource/Resources/chicken.json",
1010+
":SwiftLibraryWithPrecompiledBundleResource.rspm_resource_bundle_PrecompiledResources_bundle",
1011+
],
1012+
visibility = ["//:__subpackages__"],
1013+
)
1014+
1015+
resource_bundle_accessor(
1016+
name = "SwiftLibraryWithPrecompiledBundleResource.rspm_resource_bundle_accessor",
1017+
bundle_name = "MyPackage_SwiftLibraryWithPrecompiledBundleResource",
1018+
)
1019+
1020+
resource_bundle_infoplist(
1021+
name = "SwiftLibraryWithPrecompiledBundleResource.rspm_resource_bundle_infoplist",
1022+
region = "en",
1023+
)
1024+
1025+
swift_library(
1026+
name = "SwiftLibraryWithPrecompiledBundleResource.rspm",
1027+
always_include_developer_search_paths = True,
1028+
alwayslink = True,
1029+
copts = [
1030+
"-DSWIFT_PACKAGE",
1031+
"-Xcc",
1032+
"-DSWIFT_PACKAGE",
1033+
],
1034+
data = [":SwiftLibraryWithPrecompiledBundleResource.rspm_resource_bundle"],
1035+
module_name = "SwiftLibraryWithPrecompiledBundleResource",
1036+
package_name = "MyPackage",
1037+
srcs = [
1038+
"Source/SwiftLibraryWithPrecompiledBundleResource/SwiftLibraryWithPrecompiledBundleResource.swift",
1039+
":SwiftLibraryWithPrecompiledBundleResource.rspm_resource_bundle_accessor",
1040+
],
1041+
tags = ["manual"],
1042+
visibility = ["//:__subpackages__"],
1043+
)
9611044
""",
9621045
),
9631046
struct(

0 commit comments

Comments
 (0)