From b4ed2c1816ce1ca29069a12213e2a65b5cf9d363 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 11 Aug 2023 12:02:56 -0700 Subject: [PATCH] Add support for swiftinterface files from (xc)frameworks file processing (#2043) Originally we chose not to take this upstream commit because we want to maintain support for pre-compiling your own internal frameworks without library evolution. Due to future commits on the upstream branch that we do want this commit takes swiftinterface only support while still maintaining swiftmodule support (cherry picked from commit 6c85e96dda26495f72ad77c041c750b53400314f) Co-authored-by: Mauricio Garcia --- apple/internal/apple_framework_import.bzl | 4 +++- apple/internal/apple_xcframework_import.bzl | 15 ++++++++++++++- apple/internal/framework_import_support.bzl | 16 ++++++++++++++-- test/testdata/fmwk/generate_framework.bzl | 14 ++++++-------- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/apple/internal/apple_framework_import.bzl b/apple/internal/apple_framework_import.bzl index a6590d645..5eca3723c 100644 --- a/apple/internal/apple_framework_import.bzl +++ b/apple/internal/apple_framework_import.bzl @@ -327,7 +327,9 @@ def _apple_static_framework_import_impl(ctx): additional_cc_infos = [] additional_objc_providers = [] additional_objc_provider_fields = {} - if framework_imports_by_category.swift_module_imports or has_swift: + if framework_imports_by_category.swift_interface_imports or \ + framework_imports_by_category.swift_module_imports or \ + has_swift: toolchain = ctx.attr._toolchain[SwiftToolchainInfo] providers.append(SwiftUsageInfo()) diff --git a/apple/internal/apple_xcframework_import.bzl b/apple/internal/apple_xcframework_import.bzl index 03419027c..e39a9699b 100644 --- a/apple/internal/apple_xcframework_import.bzl +++ b/apple/internal/apple_xcframework_import.bzl @@ -140,6 +140,8 @@ def _get_xcframework_library( headers: List of File referencing XCFramework library header files. This can be either a single tree artifact or a list of regular artifacts. clang_module_map: File referencing the XCFramework library Clang modulemap file. + swift_module_interface: File referencing the XCFramework library Swift module interface + file (`.swiftinterface`). """ xcframework_library = None if not parse_xcframework_info_plist: @@ -200,6 +202,13 @@ def _get_xcframework_library_from_paths(*, target_triplet, xcframework): f.basename.startswith(target_triplet.architecture) ] + swift_module_interfaces = [ + f + for f in files_by_category.swift_interface_imports + if _matches_library(f) and + f.basename.startswith(target_triplet.architecture) + ] + framework_files = filter_by_library_identifier(xcframework.files) includes = [] @@ -218,6 +227,7 @@ def _get_xcframework_library_from_paths(*, target_triplet, xcframework): includes = includes, clang_module_map = module_maps[0] if module_maps else None, swiftmodule = swiftmodules, + swift_module_interface = swift_module_interfaces[0] if swift_module_interfaces else None, ) def _get_xcframework_library_with_xcframework_processor( @@ -337,6 +347,7 @@ def _get_xcframework_library_with_xcframework_processor( includes = includes, clang_module_map = module_map_file, swiftmodule = [], + swift_module_interface = None, framework_files = [], ) @@ -520,7 +531,9 @@ def _apple_static_xcframework_import_impl(ctx): additional_cc_infos = [] additional_objc_providers = [] - if xcframework.files_by_category.swift_module_imports or has_swift: + if xcframework.files_by_category.swift_interface_imports or \ + xcframework.files_by_category.swift_module_imports or \ + has_swift: swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo] providers.append(SwiftUsageInfo()) diff --git a/apple/internal/framework_import_support.bzl b/apple/internal/framework_import_support.bzl index 590193dba..36a54bc6b 100644 --- a/apple/internal/framework_import_support.bzl +++ b/apple/internal/framework_import_support.bzl @@ -145,6 +145,7 @@ def _classify_file_imports(config_vars, import_files): - header_imports: Objective-C(++) header imports. - module_map_imports: Clang modulemap imports. - swift_module_imports: Swift module imports. + - swift_interface_imports: Swift module interface imports. - bundling_imports: Unclassified imports. """ bundling_imports = [] @@ -152,6 +153,7 @@ def _classify_file_imports(config_vars, import_files): header_imports = [] module_map_imports = [] swift_module_imports = [] + swift_interface_imports = [] for file in import_files: # Extension matching file_extension = file.extension @@ -175,15 +177,22 @@ def _classify_file_imports(config_vars, import_files): header_imports.append(file) module_map_imports.append(file) continue - if file_extension in ["swiftmodule", "swiftinterface"]: + if file_extension == "swiftmodule": # Add Swift's module files to header_imports so # that they are correctly included in the build # by Bazel but they aren't processed in any way header_imports.append(file) swift_module_imports.append(file) continue + if file_extension == "swiftinterface": + # Add Swift's interface files to header_imports so + # that they are correctly included in the build + # by Bazel but they aren't processed in any way + header_imports.append(file) + swift_interface_imports.append(file) + continue if file_extension in ["swiftdoc", "swiftsourceinfo"]: - # Ignore swiftdoc files, they don't matter in the build, only for IDEs + # Ignore swiftdoc files, they don't matter in the build, only for IDEs. continue if file_extension == "a": binary_imports.append(file) @@ -202,6 +211,7 @@ def _classify_file_imports(config_vars, import_files): binary_imports = binary_imports, header_imports = header_imports, module_map_imports = module_map_imports, + swift_interface_imports = swift_interface_imports, swift_module_imports = swift_module_imports, bundling_imports = bundling_imports, ) @@ -220,6 +230,7 @@ def _classify_framework_imports(config_vars, framework_imports): - header_imports: Apple framework header imports. - module_map_imports: Apple framework modulemap imports. - swift_module_imports: Apple framework swiftmodule imports. + - swift_interface_imports: Apple framework Swift module interface imports. """ framework_imports_by_category = _classify_file_imports(config_vars, framework_imports) @@ -250,6 +261,7 @@ def _classify_framework_imports(config_vars, framework_imports): bundling_imports = bundling_imports, header_imports = framework_imports_by_category.header_imports, module_map_imports = framework_imports_by_category.module_map_imports, + swift_interface_imports = framework_imports_by_category.swift_interface_imports, swift_module_imports = framework_imports_by_category.swift_module_imports, ) diff --git a/test/testdata/fmwk/generate_framework.bzl b/test/testdata/fmwk/generate_framework.bzl index e03414acc..d98340bb1 100644 --- a/test/testdata/fmwk/generate_framework.bzl +++ b/test/testdata/fmwk/generate_framework.bzl @@ -98,14 +98,12 @@ def _generate_import_framework_impl(ctx): ), ) - # Mock swiftmodule file. Imported `.swiftmodule` files are not supported due to Swift - # toolchain compatibility and Swift's ABI stability through `.swiftinterface` files. - # However, Apple framework import rules use Swift interface files to flag an imported - # framework contains Swift, and thus propagate Swift toolchain specific flags up the - # build graph. + # Apple framework and XCFrameworks import rules use Swift interface files to flag an + # imported framework contains Swift, and thus propagate Swift toolchain specific flags up + # the build graph. if include_module_interface_files: - module_interface = actions.declare_file(architectures[0] + ".swiftmodule") - actions.write(output = module_interface, content = "I'm a mock .swiftmodule file") + module_interface = actions.declare_file(architectures[0] + ".swiftinterface") + actions.write(output = module_interface, content = "I'm a mock .swiftinterface file") module_interfaces.append(module_interface) # Create framework bundle @@ -163,7 +161,7 @@ Info.plist file to test resource propagation. ), "swift_library": attr.label( allow_files = True, - doc = "Label for a Swift library target to source archive and swiftmodule files from.", + doc = "Label for a Swift library target to source archive and module files from.", providers = [SwiftInfo], ), "libtype": attr.string(