@@ -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
712721def _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 ,
0 commit comments