From 99d8e627958f0ed2a596710b9b09ef95e2c7689f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Marc=CC=A7al?= Date: Tue, 5 Nov 2024 01:28:37 -0300 Subject: [PATCH] add support to SPM SystemLibrary --- swiftpkg/internal/pkginfos.bzl | 12 ++++++ swiftpkg/internal/swiftpkg_build_files.bzl | 44 +++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/swiftpkg/internal/pkginfos.bzl b/swiftpkg/internal/pkginfos.bzl index 08aa9a413..77f7ca6fc 100644 --- a/swiftpkg/internal/pkginfos.bzl +++ b/swiftpkg/internal/pkginfos.bzl @@ -375,6 +375,18 @@ 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: + 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 3075f1889..82cc80b6e 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -641,8 +641,48 @@ def _starlarkify_clang_attrs(repository_ctx, attrs): # 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