Skip to content

Commit a1d6eed

Browse files
committed
Incorporate improvements from #1428
Incorporates key improvements from PR #1428 into the system library implementation to create a more robust and complete solution: - Add linker settings support to Swift targets via `linkopts` - Add standard C/Objc compilation flags - Support both explicit headers from `clang_src_info` and glob patterns - Handle modulemap files when provided, otherwise auto-generate - Add comprehensive unit tests for system library targets The enhanced implementation now supports both system libraries with explicit modulemaps (like in #1428) and those without (like `GRDBSQLite`).
1 parent 9981570 commit a1d6eed

File tree

2 files changed

+85
-21
lines changed

2 files changed

+85
-21
lines changed

swiftpkg/internal/swiftpkg_build_files.bzl

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _new_for_target(repository_ctx, pkg_ctx, target, artifact_infos = []):
2424
if target.module_type == module_types.clang:
2525
return _clang_target_build_file(repository_ctx, pkg_ctx, target)
2626
elif target.module_type == module_types.swift:
27-
return _swift_target_build_file(pkg_ctx, target)
27+
return _swift_target_build_file(repository_ctx, pkg_ctx, target)
2828
elif target.module_type == module_types.system_library:
2929
return _system_library_build_file(target)
3030
elif target.module_type == module_types.binary:
@@ -41,7 +41,7 @@ def _new_for_target(repository_ctx, pkg_ctx, target, artifact_infos = []):
4141

4242
# MARK: - Swift Target
4343

44-
def _swift_target_build_file(pkg_ctx, target):
44+
def _swift_target_build_file(repository_ctx, pkg_ctx, target):
4545
if target.swift_src_info == None:
4646
fail("Expected a `swift_src_info`. name: ", target.name)
4747

@@ -188,6 +188,15 @@ def _swift_target_build_file(pkg_ctx, target):
188188
if len(copts) > 0:
189189
attrs["copts"] = bzl_selects.to_starlark(copts, mutually_inclusive = True)
190190

191+
linkopts = []
192+
if target.linker_settings != None:
193+
linkopts.extend(lists.flatten([
194+
bzl_selects.new_from_build_setting(bs)
195+
for bs in target.linker_settings.linked_libraries
196+
]))
197+
if linkopts:
198+
attrs["linkopts"] = _starlarkify_clang_attrs(repository_ctx, {"linkopts": linkopts})["linkopts"]
199+
191200
if target.resources:
192201
swift_apple_res_bundle_info = _apple_resource_bundle_for_swift(
193202
pkg_ctx,
@@ -710,47 +719,52 @@ def _starlarkify_clang_attrs(repository_ctx, attrs):
710719
# MARK: - System Library Targets
711720

712721
def _system_library_build_file(target):
713-
# System libraries are typically C/Objc libraries that need to be exposed to Swift.
714-
# We generate a cc_library with a swift_interop_hint for proper Swift interop.
715-
716722
bzl_target_name = target.label.name
717723

718-
# Build the cc_library attributes
719-
attrs = {}
724+
attrs = {
725+
"visibility": ["//visibility:public"],
726+
}
727+
728+
# Standard C/Objc compilation flags from SPM
729+
copts = [
730+
"-fblocks",
731+
"-fobjc-arc",
732+
"-fPIC",
733+
"-DSWIFT_PACKAGE=1",
734+
]
735+
attrs["copts"] = copts
720736

721-
# For system libraries, sources is always empty. We need to use glob to find files.
722-
# Look for all .h files in the target path
723-
hdrs_glob = paths.join(target.path, "**/*.h")
737+
# Determine headers: prefer explicit headers from clang_src_info, fallback to glob
738+
if target.clang_src_info and target.clang_src_info.hdrs:
739+
attrs["hdrs"] = target.clang_src_info.hdrs
740+
else:
741+
hdrs_glob = paths.join(target.path, "**/*.h")
742+
attrs["hdrs"] = scg.new_fn_call("glob", [hdrs_glob])
724743

725-
# System libraries typically have headers and a module.modulemap
726-
# We'll use glob patterns to find them
727-
attrs["hdrs"] = scg.new_fn_call("glob", [hdrs_glob])
744+
# Determine module map: use explicit if provided, otherwise let rules_swift auto-generate
745+
module_map_file = None
746+
if target.clang_src_info and target.clang_src_info.modulemap_path:
747+
module_map_file = target.clang_src_info.modulemap_path
728748

729-
# Create swift_interop_hint for C/Swift interop
749+
# Create swift_interop_hint for Swift interop
730750
aspect_hint_target_name = pkginfo_targets.swift_hint_label_name(bzl_target_name)
731751

732752
load_stmts = [swift_interop_hint_load_stmt]
733753
decls = []
734754

735-
# Create the swift_interop_hint
736755
decls.append(
737756
build_decls.new(
738757
kind = swift_kinds.interop_hint,
739758
name = aspect_hint_target_name,
740759
attrs = {
741-
# For system libraries, we often don't have a module map file
742-
# Set to None and let rules_swift generate one if needed
743-
"module_map": None,
760+
"module_map": module_map_file,
744761
"module_name": target.c99name,
745762
},
746763
),
747764
)
748765

749-
# Add aspect_hints to cc_library
750766
attrs["aspect_hints"] = [":{}".format(aspect_hint_target_name)]
751-
attrs["visibility"] = ["//visibility:public"]
752767

753-
# Create the cc_library
754768
decls.append(
755769
build_decls.new(
756770
kind = clang_kinds.library,

swiftpkg/tests/swiftpkg_build_files_tests.bzl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ def _pkg_info(
8484
type = pkginfos.new_product_type(executable = True),
8585
targets = ["SwiftExecutableTarget"],
8686
),
87+
pkginfos.new_product(
88+
name = "SystemLibraryTarget",
89+
type = pkginfos.new_product_type(
90+
library = pkginfos.new_library_type(
91+
library_type_kinds.automatic,
92+
),
93+
),
94+
targets = ["SystemLibraryTarget"],
95+
),
8796
],
8897
targets = [
8998
# Old-style regular library that is used to create a binary from an
@@ -508,6 +517,20 @@ def _pkg_info(
508517
],
509518
),
510519
),
520+
pkginfos.new_target(
521+
name = "SystemLibraryTarget",
522+
type = "system",
523+
c99name = "SystemLibraryTarget",
524+
module_type = "SystemLibraryTarget",
525+
path = "Sources/SystemLibraryTarget",
526+
sources = [],
527+
dependencies = [],
528+
repo_name = _repo_name,
529+
clang_src_info = pkginfos.new_clang_src_info(
530+
hdrs = ["someHeader.h"],
531+
modulemap_path = "module.modulemap",
532+
),
533+
),
511534
],
512535
expose_build_targets = expose_build_targets,
513536
)
@@ -1161,6 +1184,33 @@ swift_interop_hint(
11611184
module_map = "ObjcLibraryWithResources.rspm_modulemap",
11621185
module_name = "ObjcLibraryWithResources",
11631186
)
1187+
""",
1188+
),
1189+
struct(
1190+
msg = "System library target",
1191+
name = "SystemLibraryTarget",
1192+
pkg_info = _pkg_info(),
1193+
exp = """\
1194+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_interop_hint")
1195+
1196+
swift_interop_hint(
1197+
name = "SystemLibraryTarget.rspm_swift_hint",
1198+
module_map = "module.modulemap",
1199+
module_name = "SystemLibraryTarget",
1200+
)
1201+
1202+
cc_library(
1203+
name = "SystemLibraryTarget.rspm",
1204+
aspect_hints = [":SystemLibraryTarget.rspm_swift_hint"],
1205+
copts = [
1206+
"-fblocks",
1207+
"-fobjc-arc",
1208+
"-fPIC",
1209+
"-DSWIFT_PACKAGE=1",
1210+
],
1211+
hdrs = ["someHeader.h"],
1212+
visibility = ["//visibility:public"],
1213+
)
11641214
""",
11651215
),
11661216
struct(

0 commit comments

Comments
 (0)