Skip to content

Commit 07fcaf0

Browse files
authored
chore: refactor resource accessors for Swift and ObjC (#1184)
Refactor the resource bundle code so that the accessor generation is not buried in a shared function. This is a precursor for implementing #1079.
1 parent 34b0098 commit 07fcaf0

File tree

1 file changed

+132
-105
lines changed

1 file changed

+132
-105
lines changed

swiftpkg/internal/swiftpkg_build_files.bzl

Lines changed: 132 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ def _swift_target_build_file(pkg_ctx, target):
4747
"visibility": ["//:__subpackages__"],
4848
}
4949

50+
def _update_attr_list(name, value):
51+
# We need to create a new list, because the retrieved list could be
52+
# frozen.
53+
attr_list = list(attrs.get(name, []))
54+
attr_list.append(value)
55+
attrs[name] = attr_list
56+
5057
# Naively parse the tools semver.
5158
tools_version = pkg_ctx.pkg_info.tools_version or "0.0.0"
5259
tools_version_components = tools_version.split(".") + ["0", "0"]
@@ -132,15 +139,19 @@ def _swift_target_build_file(pkg_ctx, target):
132139
if len(copts) > 0:
133140
attrs["copts"] = bzl_selects.to_starlark(copts)
134141

135-
res_build_file = _handle_target_resources(
136-
pkg_ctx,
137-
target,
138-
attrs,
139-
include_swift_accessor = True,
140-
include_objc_accessor = False,
141-
)
142-
if res_build_file:
143-
all_build_files.append(res_build_file)
142+
if target.resources:
143+
swift_apple_res_bundle_info = _apple_resource_bundle_for_swift(
144+
pkg_ctx,
145+
target,
146+
)
147+
all_build_files.append(swift_apple_res_bundle_info.build_file)
148+
_update_attr_list("data", ":{}".format(
149+
swift_apple_res_bundle_info.bundle_label_name,
150+
))
151+
_update_attr_list("srcs", ":{}".format(
152+
swift_apple_res_bundle_info.accessor_label_name,
153+
))
154+
144155
if is_library_target:
145156
load_stmts = [swift_library_load_stmt]
146157
decls = [_swift_library_from_target(target, attrs)]
@@ -238,22 +249,43 @@ def _clang_target_build_file(repository_ctx, pkg_ctx, target):
238249
if len(list) > 0:
239250
attrs[attr] = transform_fn(list) if transform_fn else list
240251

252+
def _update_attr_list(name, value):
253+
# We need to create a new list, because the retrieved list could be
254+
# frozen.
255+
attr_list = list(attrs.get(name, []))
256+
attr_list.append(value)
257+
attrs[name] = attr_list
258+
241259
_set_if_not_empty("deps", deps, bzl_selects.to_starlark)
242260
_set_if_not_empty("hdrs", clang_src_info.hdrs)
243261
_set_if_not_empty("srcs", clang_src_info.srcs)
244262
_set_if_not_empty("includes", clang_src_info.public_includes)
245263
_set_if_not_empty("textual_hdrs", clang_src_info.textual_hdrs)
246264

247-
res_build_file = _handle_target_resources(
248-
pkg_ctx,
249-
target,
250-
attrs,
251-
include_swift_accessor = False,
252-
include_objc_accessor = (target.objc_src_info != None),
253-
)
254-
255-
if res_build_file:
256-
all_build_files.append(res_build_file)
265+
if target.resources:
266+
clang_apple_res_bundle_info = _apple_resource_bundle_for_clang(
267+
pkg_ctx,
268+
target,
269+
)
270+
all_build_files.append(clang_apple_res_bundle_info.build_file)
271+
_update_attr_list("data", ":{}".format(
272+
clang_apple_res_bundle_info.bundle_label_name,
273+
))
274+
if clang_apple_res_bundle_info.objc_accessor_hdr_label_name:
275+
# SPM provides a SWIFTPM_MODULE_BUNDLE macro to access the bundle for
276+
# ObjC code. The header file contains the macro definition. It needs
277+
# to be available in every Objc source file. So, we specify the
278+
# -include flag specifying the header path.
279+
# https://github.com/apple/swift-package-manager/blob/8387798811c6cc43761c5e1b48df2d3412dc5de4/Sources/Build/BuildDescription/ClangTargetBuildDescription.swift#L390
280+
_update_attr_list("srcs", ":{}".format(
281+
clang_apple_res_bundle_info.objc_accessor_hdr_label_name,
282+
))
283+
_update_attr_list("copts", "-include$(location :{})".format(
284+
clang_apple_res_bundle_info.objc_accessor_hdr_label_name,
285+
))
286+
_update_attr_list("srcs", ":{}".format(
287+
clang_apple_res_bundle_info.objc_accessor_impl_label_name,
288+
))
257289

258290
# The copts may be updated by functions that were executed before this
259291
# point. Use whatever has been set.
@@ -499,57 +531,7 @@ expected: {expected}\
499531

500532
# MARK: - Apple Resource Group
501533

502-
def _handle_target_resources(
503-
pkg_ctx,
504-
target,
505-
attrs,
506-
include_swift_accessor,
507-
include_objc_accessor):
508-
if len(target.resources) == 0:
509-
return None
510-
511-
def _update_attr_list(name, value):
512-
# We need to create a new list, because the retrieved list could be
513-
# frozen.
514-
attr_list = list(attrs.get(name, []))
515-
attr_list.append(value)
516-
attrs[name] = attr_list
517-
518-
bzl_target_name = pkginfo_targets.bazel_label_name(target)
519-
_update_attr_list("data", ":{}".format(
520-
pkginfo_targets.resource_bundle_label_name(bzl_target_name),
521-
))
522-
if include_swift_accessor:
523-
# Apparently, SPM provides a `Bundle.module` accessor. So, we do too.
524-
# https://stackoverflow.com/questions/63237395/generating-resource-bundle-accessor-type-bundle-has-no-member-module
525-
_update_attr_list("srcs", ":{}".format(
526-
pkginfo_targets.resource_bundle_accessor_label_name(bzl_target_name),
527-
))
528-
if include_objc_accessor:
529-
# SPM provides a SWIFTPM_MODULE_BUNDLE macro to access the bundle for
530-
# ObjC code. The header file contains the macro definition. It needs
531-
# to be available in every Objc source file. So, we specify the
532-
# -include flag specifying the header path.
533-
# https://github.com/apple/swift-package-manager/blob/8387798811c6cc43761c5e1b48df2d3412dc5de4/Sources/Build/BuildDescription/ClangTargetBuildDescription.swift#L390
534-
_update_attr_list("srcs", ":{}".format(
535-
pkginfo_targets.objc_resource_bundle_accessor_hdr_label_name(bzl_target_name),
536-
))
537-
_update_attr_list("copts", "-include$(location :{})".format(
538-
pkginfo_targets.objc_resource_bundle_accessor_hdr_label_name(bzl_target_name),
539-
))
540-
_update_attr_list("srcs", ":{}".format(
541-
pkginfo_targets.objc_resource_bundle_accessor_impl_label_name(bzl_target_name),
542-
))
543-
544-
return _apple_resource_bundle(
545-
target,
546-
pkg_ctx.pkg_info.name,
547-
pkg_ctx.pkg_info.default_localization,
548-
include_swift_accessor = include_swift_accessor,
549-
include_objc_accessor = include_objc_accessor,
550-
)
551-
552-
def _apple_resource_bundle(target, package_name, default_localization, include_swift_accessor, include_objc_accessor):
534+
def _apple_resource_bundle(target, package_name, default_localization):
553535
bzl_target_name = pkginfo_targets.bazel_label_name(target)
554536
bundle_label_name = pkginfo_targets.resource_bundle_label_name(bzl_target_name)
555537
bundle_name = pkginfo_targets.resource_bundle_name(package_name, target.c99name)
@@ -587,47 +569,92 @@ def _apple_resource_bundle(target, package_name, default_localization, include_s
587569
},
588570
),
589571
]
590-
if include_swift_accessor:
591-
load_stmts.append(swiftpkg_resource_bundle_accessor_load_stmt)
592-
decls.append(
593-
build_decls.new(
594-
kind = swiftpkg_kinds.resource_bundle_accessor,
595-
name = pkginfo_targets.resource_bundle_accessor_label_name(
596-
bzl_target_name,
597-
),
598-
attrs = {
599-
"bundle_name": bundle_name,
600-
},
572+
return struct(
573+
bundle_name = bundle_name,
574+
bundle_label_name = bundle_label_name,
575+
build_file = build_files.new(load_stmts = load_stmts, decls = decls),
576+
)
577+
578+
def _apple_resource_bundle_for_swift(pkg_ctx, target):
579+
apple_res_bundle_info = _apple_resource_bundle(
580+
target,
581+
pkg_ctx.pkg_info.name,
582+
pkg_ctx.pkg_info.default_localization,
583+
)
584+
585+
# Apparently, SPM provides a `Bundle.module` accessor. So, we do too.
586+
# https://stackoverflow.com/questions/63237395/generating-resource-bundle-accessor-type-bundle-has-no-member-module
587+
accessor_label_name = pkginfo_targets.resource_bundle_accessor_label_name(
588+
pkginfo_targets.bazel_label_name(target),
589+
)
590+
return struct(
591+
bundle_label_name = apple_res_bundle_info.bundle_label_name,
592+
accessor_label_name = accessor_label_name,
593+
build_file = build_files.merge(
594+
apple_res_bundle_info.build_file,
595+
build_files.new(
596+
load_stmts = [swiftpkg_resource_bundle_accessor_load_stmt],
597+
decls = [
598+
build_decls.new(
599+
kind = swiftpkg_kinds.resource_bundle_accessor,
600+
name = accessor_label_name,
601+
attrs = {
602+
"bundle_name": apple_res_bundle_info.bundle_name,
603+
},
604+
),
605+
],
601606
),
602-
)
603-
if include_objc_accessor:
604-
load_stmts.append(swiftpkg_objc_resource_bundle_accessor_hdr_load_stmt)
605-
load_stmts.append(swiftpkg_objc_resource_bundle_accessor_impl_load_stmt)
606-
hdr_label_name = pkginfo_targets.objc_resource_bundle_accessor_hdr_label_name(
607+
),
608+
)
609+
610+
def _apple_resource_bundle_for_clang(pkg_ctx, target):
611+
apple_res_bundle_info = _apple_resource_bundle(
612+
target,
613+
pkg_ctx.pkg_info.name,
614+
pkg_ctx.pkg_info.default_localization,
615+
)
616+
all_build_files = [apple_res_bundle_info.build_file]
617+
objc_accessor_hdr_label_name = None
618+
objc_accessor_impl_label_name = None
619+
if target.objc_src_info:
620+
bzl_target_name = pkginfo_targets.bazel_label_name(target)
621+
objc_accessor_hdr_label_name = pkginfo_targets.objc_resource_bundle_accessor_hdr_label_name(
607622
bzl_target_name,
608623
)
609-
decls.append(
610-
build_decls.new(
611-
kind = swiftpkg_kinds.objc_resource_bundle_accessor_hdr,
612-
name = hdr_label_name,
613-
attrs = {
614-
"module_name": target.c99name,
615-
},
616-
),
624+
objc_accessor_impl_label_name = pkginfo_targets.objc_resource_bundle_accessor_impl_label_name(
625+
bzl_target_name,
617626
)
618-
decls.append(
619-
build_decls.new(
620-
kind = swiftpkg_kinds.objc_resource_bundle_accessor_impl,
621-
name = pkginfo_targets.objc_resource_bundle_accessor_impl_label_name(
622-
bzl_target_name,
623-
),
624-
attrs = {
625-
"bundle_name": bundle_name,
626-
"module_name": target.c99name,
627-
},
627+
all_build_files.append(
628+
build_files.new(
629+
load_stmts = [
630+
swiftpkg_objc_resource_bundle_accessor_hdr_load_stmt,
631+
swiftpkg_objc_resource_bundle_accessor_impl_load_stmt,
632+
],
633+
decls = [
634+
build_decls.new(
635+
kind = swiftpkg_kinds.objc_resource_bundle_accessor_hdr,
636+
name = objc_accessor_hdr_label_name,
637+
attrs = {
638+
"module_name": target.c99name,
639+
},
640+
),
641+
build_decls.new(
642+
kind = swiftpkg_kinds.objc_resource_bundle_accessor_impl,
643+
name = objc_accessor_impl_label_name,
644+
attrs = {
645+
"bundle_name": apple_res_bundle_info.bundle_name,
646+
"module_name": target.c99name,
647+
},
648+
),
649+
],
628650
),
629651
)
630-
return build_files.new(load_stmts = load_stmts, decls = decls)
652+
return struct(
653+
bundle_label_name = apple_res_bundle_info.bundle_label_name,
654+
objc_accessor_hdr_label_name = objc_accessor_hdr_label_name,
655+
objc_accessor_impl_label_name = objc_accessor_impl_label_name,
656+
build_file = build_files.merge(*all_build_files),
657+
)
631658

632659
# MARK: - Modulemap Generation
633660

0 commit comments

Comments
 (0)