diff --git a/swiftpkg/internal/pkginfos.bzl b/swiftpkg/internal/pkginfos.bzl index 08aa9a413..e4bca13d9 100644 --- a/swiftpkg/internal/pkginfos.bzl +++ b/swiftpkg/internal/pkginfos.bzl @@ -375,6 +375,20 @@ def _new_target_from_json_maps( pkg_path = pkg_path, sources = clang_src_info.explicit_srcs + clang_src_info.hdrs, ) + elif module_type == module_types.system_library: + # System libraries have their headers/modulemaps in the library root path, so the root path is the public path + public_hdrs_path = "" + + clang_src_info = _new_clang_src_info_from_sources( + repository_ctx = repository_ctx, + pkg_path = pkg_path, + c99name = c99name, + target_path = target_path, + source_paths = source_paths, + public_hdrs_path = public_hdrs_path, + exclude_paths = exclude_paths, + other_hdr_srch_paths = [], + ) return _new_target( name = target_name, diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index f16a12560..fcae4ad4e 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -19,7 +19,7 @@ def _new_for_target(repository_ctx, pkg_ctx, target, artifact_infos = []): if target.module_type == module_types.clang: return _clang_target_build_file(repository_ctx, pkg_ctx, target) elif target.module_type == module_types.swift: - return _swift_target_build_file(pkg_ctx, target) + return _swift_target_build_file(repository_ctx, pkg_ctx, target) elif target.module_type == module_types.system_library: return _system_library_build_file(target) elif target.module_type == module_types.binary: @@ -36,7 +36,7 @@ def _new_for_target(repository_ctx, pkg_ctx, target, artifact_infos = []): # MARK: - Swift Target -def _swift_target_build_file(pkg_ctx, target): +def _swift_target_build_file(repository_ctx, pkg_ctx, target): if target.swift_src_info == None: fail("Expected a `swift_src_info`. name: ", target.name) @@ -80,10 +80,11 @@ def _swift_target_build_file(pkg_ctx, target): fail_if_not_found = False, ) - if not dep_target or dep_target.type != target_types.macro: + if dep_target and dep_target.type == target_types.macro: + macro_targets.append(dep_target) + else: target_deps.append(target_dep) continue - macro_targets.append(dep_target) if macro_targets: attrs["plugins"] = [ @@ -108,6 +109,15 @@ def _swift_target_build_file(pkg_ctx, target): "-DSWIFT_PACKAGE", ] + linkopts = [] + if target.linker_settings != None: + linkopts.extend(lists.flatten([ + bzl_selects.new_from_build_setting(bs) + for bs in target.linker_settings.linked_libraries + ])) + if linkopts: + attrs["linkopts"] = _starlarkify_clang_attrs(repository_ctx, {"linkopts": linkopts})["linkopts"] + # GH046: Support plugins. is_library_target = lists.contains([target_types.library, target_types.regular], target.type) @@ -647,12 +657,49 @@ def _starlarkify_clang_attrs(repository_ctx, attrs): # MARK: - System Library Targets -# GH009(chuck): Remove unused-variable directives - -# buildifier: disable=unused-variable def _system_library_build_file(target): - # GH009(chuck): Implement _system_library_build_file - return None + attrs = { + "visibility": ["//:__subpackages__"], + } + + # These flags are used by SPM when compiling clang modules. + copts = [ + # Enable 'blocks' language feature + "-fblocks", + # Synthesize retain and release calls for Objective-C pointers + "-fobjc-arc", + # Enable support for PIC macros + "-fPIC", + # The SWIFT_PACKAGE define is a magical value that SPM uses when it + # builds clang libraries that will be used as Swift modules. + "-DSWIFT_PACKAGE=1", + ] + attrs["copts"] = copts + + module_map_file = target.clang_src_info.modulemap_path + attrs["module_map"] = module_map_file + + # System library targets must include a modulemap file. + # https://github.com/swiftlang/swift-package-manager/blob/12c14222fdde2ffd8303a2c805fed1b1eb802e5c/Sources/PackageLoading/PackageBuilder.swift#L853 + if not module_map_file: + fail("Expected a modulemap file for a system library target. name: ", target.name) + + header_files = target.clang_src_info.hdrs + attrs["hdrs"] = header_files + + bzl_target_name = pkginfo_targets.bazel_label_name(target) + + decls = [ + build_decls.new( + kind = objc_kinds.library, + name = bzl_target_name, + attrs = attrs, + ), + ] + + return build_files.new( + decls = decls, + ) # MARK: - Apple xcframework Targets diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index de7c889b7..f8fc8144d 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -68,6 +68,15 @@ _pkg_info = pkginfos.new( ), targets = ["RegularSwiftTargetAsLibrary"], ), + pkginfos.new_product( + name = "SystemLibraryTarget", + type = pkginfos.new_product_type( + library = pkginfos.new_library_type( + library_type_kinds.automatic, + ), + ), + targets = ["SystemLibraryTarget"], + ), pkginfos.new_product( name = "ObjcLibraryWithModulemap", type = pkginfos.new_product_type( @@ -118,6 +127,20 @@ _pkg_info = pkginfos.new( repo_name = _repo_name, swift_src_info = pkginfos.new_swift_src_info(), ), + pkginfos.new_target( + name = "SystemLibraryTarget", + type = "regular", + c99name = "SystemLibraryTarget", + module_type = "SystemLibraryTarget", + path = "Source/SystemLibraryTarget", + sources = [], + dependencies = [], + repo_name = _repo_name, + clang_src_info = pkginfos.new_clang_src_info( + hdrs = ["someHeader.h"], + modulemap_path = "module.modulemap", + ), + ), pkginfos.new_target( name = "RegularSwiftTargetAsLibraryTests", type = "test", @@ -759,6 +782,25 @@ swift_interop_hint( module_map = "include/module.modulemap", module_name = "ObjcLibraryWithModulemap", ) +""", + ), + struct( + msg = "System library Target declaration", + name = "SystemLibraryTarget", + exp = """\ + +objc_library( + name = "SystemLibraryTarget.rspm", + copts = [ + "-fblocks", + "-fobjc-arc", + "-fPIC", + "-DSWIFT_PACKAGE=1", + ], + hdrs = ["someHeader.h"], + module_map = "module.modulemap", + visibility = ["//:__subpackages__"], +) """, ), struct(