From 6815737b10490b2eab4f946a947593ee677a6fe9 Mon Sep 17 00:00:00 2001 From: Brentley Jones Date: Tue, 4 Feb 2025 13:27:07 -0600 Subject: [PATCH] Cherry-pick Swift toolchain changes from upstream (#2643) --- apple/internal/apple_framework_import.bzl | 18 ++++++++----- apple/internal/apple_xcframework_import.bzl | 28 ++++++++++++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/apple/internal/apple_framework_import.bzl b/apple/internal/apple_framework_import.bzl index d9423efb7..a486d26f9 100644 --- a/apple/internal/apple_framework_import.bzl +++ b/apple/internal/apple_framework_import.bzl @@ -33,7 +33,6 @@ load( load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain") load( "@build_bazel_rules_swift//swift:swift.bzl", - "SwiftToolchainInfo", "swift_clang_module_aspect", "swift_common", ) @@ -78,6 +77,10 @@ load( "AppleFrameworkImportBundleInfo", ) +# The name of the execution group that houses the Swift toolchain and is used to +# run Swift actions. +_SWIFT_EXEC_GROUP = "swift" + def _swiftmodule_for_cpu(swiftmodule_files, cpu): """Select the cpu specific swiftmodule.""" @@ -255,7 +258,7 @@ def _apple_dynamic_framework_import_impl(ctx): if "apple._import_framework_via_swiftinterface" in features and framework.swift_interface_imports: # Create SwiftInfo provider - swift_toolchain = ctx.attr._swift_toolchain[SwiftToolchainInfo] + swift_toolchain = swift_common.get_toolchain(ctx, exec_group = _SWIFT_EXEC_GROUP) swiftinterface_files = framework_import_support.get_swift_module_files_with_target_triplet( swift_module_files = framework.swift_interface_imports, target_triplet = target_triplet, @@ -321,7 +324,7 @@ def _apple_static_framework_import_impl(ctx): additional_objc_providers = [] additional_objc_provider_fields = {} if framework.swift_interface_imports or framework.swift_module_imports or has_swift: - toolchain = ctx.attr._swift_toolchain[SwiftToolchainInfo] + toolchain = swift_common.get_toolchain(ctx, exec_group = _SWIFT_EXEC_GROUP) providers.append(SwiftUsageInfo()) # The Swift toolchain propagates Swift-specific linker flags (e.g., @@ -390,7 +393,7 @@ def _apple_static_framework_import_impl(ctx): if "apple._import_framework_via_swiftinterface" in features and framework.swift_interface_imports: # Create SwiftInfo provider - swift_toolchain = ctx.attr._swift_toolchain[SwiftToolchainInfo] + swift_toolchain = swift_common.get_toolchain(ctx, exec_group = _SWIFT_EXEC_GROUP) swiftinterface_files = framework_import_support.get_swift_module_files_with_target_triplet( swift_module_files = framework.swift_interface_imports, target_triplet = target_triplet, @@ -429,7 +432,6 @@ apple_dynamic_framework_import = rule( fragments = ["cpp"], attrs = dicts.add( rule_attrs.common_tool_attrs(), - swift_common.toolchain_attrs(toolchain_attr_name = "_swift_toolchain"), { "framework_imports": attr.label_list( allow_empty = False, @@ -499,7 +501,6 @@ apple_static_framework_import = rule( fragments = ["cpp", "objc"], attrs = dicts.add( rule_attrs.common_tool_attrs(), - swift_common.toolchain_attrs(toolchain_attr_name = "_swift_toolchain"), { "framework_imports": attr.label_list( allow_empty = False, @@ -576,6 +577,11 @@ not include Swift interface or Swift module files. ), }, ), + exec_groups = { + _SWIFT_EXEC_GROUP: exec_group( + toolchains = swift_common.use_toolchain(), + ), + }, toolchains = use_cpp_toolchain(), doc = """ This rule encapsulates an already-built static framework. It is defined by a list of diff --git a/apple/internal/apple_xcframework_import.bzl b/apple/internal/apple_xcframework_import.bzl index 7ac356644..5b631f1f0 100644 --- a/apple/internal/apple_xcframework_import.bzl +++ b/apple/internal/apple_xcframework_import.bzl @@ -18,7 +18,11 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts") load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain") load("@build_bazel_apple_support//lib:apple_support.bzl", "apple_support") -load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftToolchainInfo", "swift_clang_module_aspect", "swift_common") +load( + "@build_bazel_rules_swift//swift:swift.bzl", + "swift_clang_module_aspect", + "swift_common", +) load("//apple:providers.bzl", "AppleFrameworkImportInfo") load( "//apple/internal:apple_toolchains.bzl", @@ -57,6 +61,10 @@ load( # This defines an _enum_ to identify an imported XCFramework bundle type. _BUNDLE_TYPE = struct(frameworks = 1, libraries = 2) +# The name of the execution group that houses the Swift toolchain and is used to +# run Swift actions. +_SWIFT_EXEC_GROUP = "swift" + def _classify_xcframework_imports(config_vars, xcframework_imports): """Classifies XCFramework files for later processing. @@ -557,7 +565,7 @@ def _apple_dynamic_xcframework_import_impl(ctx): if "apple._import_framework_via_swiftinterface" in features and xcframework_library.swift_module_interface: # Create SwiftInfo provider - swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo] + swift_toolchain = swift_common.get_toolchain(ctx, exec_group = _SWIFT_EXEC_GROUP) providers.append( framework_import_support.swift_info_from_module_interface( actions = actions, @@ -634,7 +642,7 @@ def _apple_static_xcframework_import_impl(ctx): 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] + swift_toolchain = swift_common.get_toolchain(ctx, exec_group = _SWIFT_EXEC_GROUP) providers.append(SwiftUsageInfo()) # The Swift toolchain propagates Swift-specific linker flags (e.g., @@ -688,7 +696,7 @@ def _apple_static_xcframework_import_impl(ctx): if "apple._import_framework_via_swiftinterface" in features and xcframework_library.swift_module_interface: # Create SwiftInfo provider - swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo] + swift_toolchain = swift_common.get_toolchain(ctx, exec_group = _SWIFT_EXEC_GROUP) providers.append( framework_import_support.swift_info_from_module_interface( actions = actions, @@ -744,7 +752,6 @@ objc_library( implementation = _apple_dynamic_xcframework_import_impl, attrs = dicts.add( rule_attrs.common_tool_attrs(), - swift_common.toolchain_attrs(), { "xcframework_imports": attr.label_list( allow_empty = False, @@ -784,6 +791,11 @@ Unnecssary and ignored, will be removed in the future. ), }, ), + exec_groups = { + _SWIFT_EXEC_GROUP: exec_group( + toolchains = swift_common.use_toolchain(), + ), + }, fragments = ["apple", "cpp"], provides = [ AppleFrameworkImportInfo, @@ -818,7 +830,6 @@ objc_library( implementation = _apple_static_xcframework_import_impl, attrs = dicts.add( rule_attrs.common_tool_attrs(), - swift_common.toolchain_attrs(), { "alwayslink": attr.bool( default = False, @@ -920,6 +931,11 @@ Unnecssary and ignored, will be removed in the future. ), }, ), + exec_groups = { + _SWIFT_EXEC_GROUP: exec_group( + toolchains = swift_common.use_toolchain(), + ), + }, fragments = ["apple", "cpp", "objc"], toolchains = use_cpp_toolchain(), )