@@ -24,7 +24,7 @@ def _new_for_target(repository_ctx, pkg_ctx, target, artifact_infos = []):
24
24
if target .module_type == module_types .clang :
25
25
return _clang_target_build_file (repository_ctx , pkg_ctx , target )
26
26
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 )
28
28
elif target .module_type == module_types .system_library :
29
29
return _system_library_build_file (target )
30
30
elif target .module_type == module_types .binary :
@@ -41,7 +41,7 @@ def _new_for_target(repository_ctx, pkg_ctx, target, artifact_infos = []):
41
41
42
42
# MARK: - Swift Target
43
43
44
- def _swift_target_build_file (pkg_ctx , target ):
44
+ def _swift_target_build_file (repository_ctx , pkg_ctx , target ):
45
45
if target .swift_src_info == None :
46
46
fail ("Expected a `swift_src_info`. name: " , target .name )
47
47
@@ -188,6 +188,15 @@ def _swift_target_build_file(pkg_ctx, target):
188
188
if len (copts ) > 0 :
189
189
attrs ["copts" ] = bzl_selects .to_starlark (copts , mutually_inclusive = True )
190
190
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
+
191
200
if target .resources :
192
201
swift_apple_res_bundle_info = _apple_resource_bundle_for_swift (
193
202
pkg_ctx ,
@@ -710,47 +719,52 @@ def _starlarkify_clang_attrs(repository_ctx, attrs):
710
719
# MARK: - System Library Targets
711
720
712
721
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
-
716
722
bzl_target_name = target .label .name
717
723
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
720
736
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 ])
724
743
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
728
748
729
- # Create swift_interop_hint for C/ Swift interop
749
+ # Create swift_interop_hint for Swift interop
730
750
aspect_hint_target_name = pkginfo_targets .swift_hint_label_name (bzl_target_name )
731
751
732
752
load_stmts = [swift_interop_hint_load_stmt ]
733
753
decls = []
734
754
735
- # Create the swift_interop_hint
736
755
decls .append (
737
756
build_decls .new (
738
757
kind = swift_kinds .interop_hint ,
739
758
name = aspect_hint_target_name ,
740
759
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 ,
744
761
"module_name" : target .c99name ,
745
762
},
746
763
),
747
764
)
748
765
749
- # Add aspect_hints to cc_library
750
766
attrs ["aspect_hints" ] = [":{}" .format (aspect_hint_target_name )]
751
- attrs ["visibility" ] = ["//visibility:public" ]
752
767
753
- # Create the cc_library
754
768
decls .append (
755
769
build_decls .new (
756
770
kind = clang_kinds .library ,
0 commit comments