Skip to content

Commit

Permalink
Introduce the swift_common.get_toolchain helper function
Browse files Browse the repository at this point in the history
This is change 1 of _N_ to migrate the Swift build rules to use new-style Bazel toolchains. This change:

*   Updates the toolchain configuration rules to wrap the `SwiftToolchainInfo` provider in `platform_common.ToolchainInfo`, for future use by `toolchain()` rules.
*   Funnels all toolchain access through a new `swift_toolchain.get_toolchain()` function that looks up the toolchain using `ctx.toolchains`, falling back to the implicit attribute.

Since `ctx.toolchains` isn't defined yet, the second change is a no-op but it simplifies future parts of the migration.

PiperOrigin-RevId: 439638938
(cherry picked from commit 73b248d)
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Feb 4, 2025
1 parent 4f54ce1 commit 49065f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 3 additions & 4 deletions apple/internal/apple_framework_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down Expand Up @@ -255,7 +254,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, attr = "_swift_toolchain")
swiftinterface_files = framework_import_support.get_swift_module_files_with_target_triplet(
swift_module_files = framework.swift_interface_imports,
target_triplet = target_triplet,
Expand Down Expand Up @@ -321,7 +320,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, attr = "_swift_toolchain")
providers.append(SwiftUsageInfo())

# The Swift toolchain propagates Swift-specific linker flags (e.g.,
Expand Down Expand Up @@ -390,7 +389,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, attr = "_swift_toolchain")
swiftinterface_files = framework_import_support.get_swift_module_files_with_target_triplet(
swift_module_files = framework.swift_interface_imports,
target_triplet = target_triplet,
Expand Down
12 changes: 8 additions & 4 deletions apple/internal/apple_xcframework_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -557,7 +561,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, attr = "_swift_toolchain")
providers.append(
framework_import_support.swift_info_from_module_interface(
actions = actions,
Expand Down Expand Up @@ -634,7 +638,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, attr = "_swift_toolchain")
providers.append(SwiftUsageInfo())

# The Swift toolchain propagates Swift-specific linker flags (e.g.,
Expand Down Expand Up @@ -688,7 +692,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, attr = "_swift_toolchain")
providers.append(
framework_import_support.swift_info_from_module_interface(
actions = actions,
Expand Down

0 comments on commit 49065f7

Please sign in to comment.