Skip to content

Commit

Permalink
Add support for swiftinterface files from (xc)frameworks file process…
Browse files Browse the repository at this point in the history
…ing (bazelbuild#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 6c85e96)

Co-authored-by: Mauricio Garcia <[email protected]>
  • Loading branch information
2 people authored and chiragramani committed Aug 11, 2023
1 parent 55723e1 commit b4ed2c1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 3 additions & 1 deletion apple/internal/apple_framework_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
15 changes: 14 additions & 1 deletion apple/internal/apple_xcframework_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = []
Expand All @@ -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(
Expand Down Expand Up @@ -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 = [],
)

Expand Down Expand Up @@ -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())

Expand Down
16 changes: 14 additions & 2 deletions apple/internal/framework_import_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ 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 = []
binary_imports = []
header_imports = []
module_map_imports = []
swift_module_imports = []
swift_interface_imports = []
for file in import_files:
# Extension matching
file_extension = file.extension
Expand All @@ -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)
Expand All @@ -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,
)
Expand All @@ -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)

Expand Down Expand Up @@ -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,
)

Expand Down
14 changes: 6 additions & 8 deletions test/testdata/fmwk/generate_framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b4ed2c1

Please sign in to comment.