Skip to content

Commit e3c8cfb

Browse files
sebastianv1cgrindelmergify[bot]
authored
fix: precompiled resource bundle warnings (#1566)
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. --------- Co-authored-by: Chuck Grindel <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 9299323 commit e3c8cfb

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

swiftpkg/internal/swiftpkg_build_files.bzl

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,9 @@ expected: {expected}\
724724

725725
# MARK: - Apple Resource Group
726726

727+
def _sanitized_bundle_file_name(bundle_path):
728+
return bundle_path.replace(" ", "_").replace(".", "_").replace("(", "_").replace(")", "_")
729+
727730
def _apple_resource_bundle(target, package_name, default_localization, expose_build_targets):
728731
bzl_target_name = pkginfo_targets.bazel_label_name(target)
729732
bundle_label_name = pkginfo_targets.resource_bundle_label_name(bzl_target_name)
@@ -732,10 +735,21 @@ def _apple_resource_bundle(target, package_name, default_localization, expose_bu
732735
bzl_target_name,
733736
)
734737

735-
resources = sorted([
738+
sorted_resources = sorted([
736739
r.path
737740
for r in target.resources
738741
])
742+
resources = [
743+
r
744+
for r in sorted_resources
745+
if not r.endswith(".bundle")
746+
]
747+
precompiled_bundles_and_labels = [
748+
(r, "{}_{}".format(bundle_label_name, _sanitized_bundle_file_name(r.split("/")[-1])))
749+
for r in sorted_resources
750+
if r.endswith(".bundle")
751+
]
752+
precompiled_bundle_resource_labels = [bundle_pair[1] for bundle_pair in precompiled_bundles_and_labels]
739753

740754
load_stmts = [
741755
apple_resource_bundle_load_stmt,
@@ -757,11 +771,28 @@ def _apple_resource_bundle(target, package_name, default_localization, expose_bu
757771
"infoplists": [":{}".format(infoplist_name)],
758772
# Based upon the code in SPM, it looks like they only support unstructured resources.
759773
# https://github.com/apple/swift-package-manager/blob/main/Sources/PackageModel/Resource.swift#L25-L33
760-
"resources": resources,
774+
"resources": resources + [":{}".format(bundle_label) for bundle_label in precompiled_bundle_resource_labels],
761775
"visibility": _target_visibility(expose_build_targets),
762776
},
763777
),
764778
]
779+
780+
if len(precompiled_bundles_and_labels) > 0:
781+
load_stmts.append(apple_resource_bundle_import_load_stmt)
782+
for precompiled_bundle, precompiled_bundle_label_name in precompiled_bundles_and_labels:
783+
decls.append(
784+
build_decls.new(
785+
kind = apple_kinds.apple_bundle_import,
786+
name = precompiled_bundle_label_name,
787+
attrs = {
788+
"bundle_imports": scg.new_fn_call(
789+
"glob",
790+
["{precompiled_bundle}/**/*".format(precompiled_bundle = precompiled_bundle)],
791+
),
792+
},
793+
),
794+
)
795+
765796
return struct(
766797
bundle_name = bundle_name,
767798
bundle_label_name = bundle_label_name,
@@ -1132,6 +1163,7 @@ apple_kinds = struct(
11321163
static_xcframework_import = "apple_static_xcframework_import",
11331164
dynamic_xcframework_import = "apple_dynamic_xcframework_import",
11341165
resource_bundle = "apple_resource_bundle",
1166+
apple_bundle_import = "apple_bundle_import",
11351167
)
11361168

11371169
apple_apple_location = "@build_bazel_rules_apple//apple:apple.bzl"
@@ -1153,6 +1185,11 @@ apple_resource_bundle_load_stmt = load_statements.new(
11531185
apple_kinds.resource_bundle,
11541186
)
11551187

1188+
apple_resource_bundle_import_load_stmt = load_statements.new(
1189+
apple_resources_location,
1190+
apple_kinds.apple_bundle_import,
1191+
)
1192+
11561193
swiftpkg_kinds = struct(
11571194
generate_modulemap = "generate_modulemap",
11581195
objc_resource_bundle_accessor_hdr = "objc_resource_bundle_accessor_hdr",

swiftpkg/tests/swiftpkg_build_files_tests.bzl

Lines changed: 83 additions & 0 deletions
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)