diff --git a/doc/providers.md b/doc/providers.md
index 7ea144ffd..e27cd89fb 100644
--- a/doc/providers.md
+++ b/doc/providers.md
@@ -87,7 +87,7 @@ SwiftToolchainInfo(action_configs
const_protocols_to_gather, debug_outputs_provider, developer_dirs,
entry_point_linkopts_provider, feature_allowlists,
generated_header_module_implicit_deps_providers, implicit_deps_providers,
- package_configurations, requested_features, root_dir, swift_worker,
+ module_aliases, package_configurations, requested_features, root_dir, swift_worker,
test_configuration, tool_configs, unsupported_features)
@@ -109,6 +109,7 @@ that use the toolchain.
| feature_allowlists | A list of `SwiftFeatureAllowlistInfo` providers that allow or prohibit packages from requesting or disabling features. |
| generated_header_module_implicit_deps_providers | A `struct` with the following fields, which are providers from targets that should be treated as compile-time inputs to actions that precompile the explicit module for the generated Objective-C header of a Swift module:
* `cc_infos`: A list of `CcInfo` providers from targets specified as the toolchain's implicit dependencies. * `objc_infos`: A list of `apple_common.Objc` providers from targets specified as the toolchain's implicit dependencies. * `swift_infos`: A list of `SwiftInfo` providers from targets specified as the toolchain's implicit dependencies.
This is used to provide modular dependencies for the fixed inclusions (Darwin, Foundation) that are unconditionally emitted in those files.
For ease of use, this field is never `None`; it will always be a valid `struct` containing the fields described above, even if those lists are empty. |
| implicit_deps_providers | A `struct` with the following fields, which represent providers from targets that should be added as implicit dependencies of any Swift compilation or linking target (but not to precompiled explicit C/Objective-C modules):
* `cc_infos`: A list of `CcInfo` providers from targets specified as the toolchain's implicit dependencies. * `objc_infos`: A list of `apple_common.Objc` providers from targets specified as the toolchain's implicit dependencies. * `swift_infos`: A list of `SwiftInfo` providers from targets specified as the toolchain's implicit dependencies.
For ease of use, this field is never `None`; it will always be a valid `struct` containing the fields described above, even if those lists are empty. |
+| module_aliases | A `SwiftModuleAliasesInfo` provider that defines the module aliases to use during compilation. |
| package_configurations | A list of `SwiftPackageConfigurationInfo` providers that specify additional compilation configuration options that are applied to targets on a per-package basis. |
| requested_features | `List` of `string`s. Features that should be implicitly enabled by default for targets built using this toolchain, unless overridden by the user by listing their negation in the `features` attribute of a target/package or in the `--features` command line flag.
These features determine various compilation and debugging behaviors of the Swift build rules, and they are also passed to the C++ APIs used when linking (so features defined in CROSSTOOL may be used here). |
| root_dir | `String`. The workspace-relative root directory of the toolchain. |
diff --git a/swift/BUILD b/swift/BUILD
index 4dfa2bf4a..12a1cef1f 100644
--- a/swift/BUILD
+++ b/swift/BUILD
@@ -6,6 +6,7 @@ load(
"repeatable_string_flag",
)
load(":swift_interop_hint.bzl", "swift_interop_hint")
+load(":swift_module_mapping.bzl", "swift_module_mapping")
package(default_visibility = ["//visibility:public"])
@@ -368,3 +369,17 @@ filegroup(
name = "empty",
visibility = ["//visibility:private"],
)
+
+# Provides the mapping that will be passed to the compiler as module aliases.
+label_flag(
+ name = "module_mapping",
+ build_setting_default = ":empty_module_mapping",
+)
+
+# The default empty module mapping used when the `:module_mapping` flag is not
+# set.
+swift_module_mapping(
+ name = "empty_module_mapping",
+ aliases = {},
+ visibility = ["//visibility:private"],
+)
diff --git a/swift/providers.bzl b/swift/providers.bzl
index e7f97f6f7..91f48a82a 100644
--- a/swift/providers.bzl
+++ b/swift/providers.bzl
@@ -286,6 +286,10 @@ linking target (but not to precompiled explicit C/Objective-C modules):
For ease of use, this field is never `None`; it will always be a valid `struct`
containing the fields described above, even if those lists are empty.
+""",
+ "module_aliases": """\
+A `SwiftModuleAliasesInfo` provider that defines the module aliases to use
+during compilation.
""",
"package_configurations": """\
A list of `SwiftPackageConfigurationInfo` providers that specify additional
diff --git a/swift/swift_module_mapping.bzl b/swift/swift_module_mapping.bzl
index 795632366..abcf87b40 100644
--- a/swift/swift_module_mapping.bzl
+++ b/swift/swift_module_mapping.bzl
@@ -29,7 +29,7 @@ def _swift_module_mapping_impl(ctx):
aliases = ctx.attr.aliases
new_names_seen = dict()
- for original_name, new_name in aliases:
+ for original_name, new_name in aliases.items():
previous_new_name = new_names_seen.get(new_name, None)
if previous_new_name:
fail((
diff --git a/swift/toolchains/BUILD b/swift/toolchains/BUILD
index 9701bf261..25921f4b9 100644
--- a/swift/toolchains/BUILD
+++ b/swift/toolchains/BUILD
@@ -14,6 +14,7 @@ bzl_library(
"//swift/internal:debugging",
"//swift/internal:feature_names",
"//swift/internal:features",
+ "//swift/internal:providers",
"//swift/internal:target_triples",
"//swift/internal:utils",
"//swift/internal:wmo",
@@ -39,6 +40,7 @@ bzl_library(
"//swift/internal:attrs",
"//swift/internal:feature_names",
"//swift/internal:features",
+ "//swift/internal:providers",
"//swift/internal:target_triples",
"//swift/internal:utils",
"//swift/internal:wmo",
diff --git a/swift/toolchains/swift_toolchain.bzl b/swift/toolchains/swift_toolchain.bzl
index 9f20d4a8c..1655c4d1e 100644
--- a/swift/toolchains/swift_toolchain.bzl
+++ b/swift/toolchains/swift_toolchain.bzl
@@ -63,6 +63,7 @@ load(
"SWIFT_FEATURE_USE_MODULE_WRAP",
)
load("//swift/internal:features.bzl", "features_for_build_modes")
+load("//swift/internal:providers.bzl", "SwiftModuleAliasesInfo")
load("//swift/internal:target_triples.bzl", "target_triples")
load(
"//swift/internal:utils.bzl",
@@ -529,6 +530,7 @@ def _swift_toolchain_impl(ctx):
generated_header_module_implicit_deps_providers = (
collect_implicit_deps_providers([])
),
+ module_aliases = ctx.attr._module_mapping[SwiftModuleAliasesInfo].aliases,
implicit_deps_providers = collect_implicit_deps_providers(
[],
additional_cc_infos = [swift_linkopts_cc_info],
@@ -626,6 +628,12 @@ The label of the `string_list` containing additional flags that should be passed
to the compiler for exec transition builds.
""",
),
+ "_module_mapping": attr.label(
+ default = Label(
+ "@build_bazel_rules_swift//swift:module_mapping",
+ ),
+ providers = [[SwiftModuleAliasesInfo]],
+ ),
"_worker": attr.label(
cfg = "exec",
allow_files = True,
diff --git a/swift/toolchains/xcode_swift_toolchain.bzl b/swift/toolchains/xcode_swift_toolchain.bzl
index 0f91f48f2..8e0139816 100644
--- a/swift/toolchains/xcode_swift_toolchain.bzl
+++ b/swift/toolchains/xcode_swift_toolchain.bzl
@@ -67,6 +67,7 @@ load(
"SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE",
)
load("//swift/internal:features.bzl", "features_for_build_modes")
+load("//swift/internal:providers.bzl", "SwiftModuleAliasesInfo")
load("//swift/internal:target_triples.bzl", "target_triples")
load(
"//swift/internal:utils.bzl",
@@ -785,6 +786,13 @@ def _xcode_swift_toolchain_impl(ctx):
),
)
+ module_aliases = ctx.attr._module_mapping[SwiftModuleAliasesInfo].aliases
+ if module_aliases and not _is_xcode_at_least_version(xcode_config, "14.0"):
+ fail(
+ "Module mappings are only supported by Swift 5.7 (Xcode 14.0) " +
+ "and higher.",
+ )
+
swift_toolchain_info = SwiftToolchainInfo(
action_configs = all_action_configs,
cc_toolchain_info = cc_toolchain,
@@ -812,6 +820,7 @@ def _xcode_swift_toolchain_impl(ctx):
additional_cc_infos = [swift_linkopts_providers.cc_info],
additional_objc_infos = [swift_linkopts_providers.objc_info],
),
+ module_aliases = module_aliases,
package_configurations = [
target[SwiftPackageConfigurationInfo]
for target in ctx.attr.package_configurations
@@ -936,6 +945,12 @@ The label of the `string_list` containing additional flags that should be passed
to the compiler for exec transition builds.
""",
),
+ "_module_mapping": attr.label(
+ default = Label(
+ "@build_bazel_rules_swift//swift:module_mapping",
+ ),
+ providers = [[SwiftModuleAliasesInfo]],
+ ),
"_worker": attr.label(
cfg = "exec",
allow_files = True,