Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the module_mapping flag to the Swift rules #1332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ SwiftToolchainInfo(<a href="#SwiftToolchainInfo-action_configs">action_configs</
<a href="#SwiftToolchainInfo-const_protocols_to_gather">const_protocols_to_gather</a>, <a href="#SwiftToolchainInfo-debug_outputs_provider">debug_outputs_provider</a>, <a href="#SwiftToolchainInfo-developer_dirs">developer_dirs</a>,
<a href="#SwiftToolchainInfo-entry_point_linkopts_provider">entry_point_linkopts_provider</a>, <a href="#SwiftToolchainInfo-feature_allowlists">feature_allowlists</a>,
<a href="#SwiftToolchainInfo-generated_header_module_implicit_deps_providers">generated_header_module_implicit_deps_providers</a>, <a href="#SwiftToolchainInfo-implicit_deps_providers">implicit_deps_providers</a>,
<a href="#SwiftToolchainInfo-package_configurations">package_configurations</a>, <a href="#SwiftToolchainInfo-requested_features">requested_features</a>, <a href="#SwiftToolchainInfo-root_dir">root_dir</a>, <a href="#SwiftToolchainInfo-swift_worker">swift_worker</a>,
<a href="#SwiftToolchainInfo-module_aliases">module_aliases</a>, <a href="#SwiftToolchainInfo-package_configurations">package_configurations</a>, <a href="#SwiftToolchainInfo-requested_features">requested_features</a>, <a href="#SwiftToolchainInfo-root_dir">root_dir</a>, <a href="#SwiftToolchainInfo-swift_worker">swift_worker</a>,
<a href="#SwiftToolchainInfo-test_configuration">test_configuration</a>, <a href="#SwiftToolchainInfo-tool_configs">tool_configs</a>, <a href="#SwiftToolchainInfo-unsupported_features">unsupported_features</a>)
</pre>

Expand All @@ -109,6 +109,7 @@ that use the toolchain.
| <a id="SwiftToolchainInfo-feature_allowlists"></a>feature_allowlists | A list of `SwiftFeatureAllowlistInfo` providers that allow or prohibit packages from requesting or disabling features. |
| <a id="SwiftToolchainInfo-generated_header_module_implicit_deps_providers"></a>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:<br><br>* `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.<br><br>This is used to provide modular dependencies for the fixed inclusions (Darwin, Foundation) that are unconditionally emitted in those files.<br><br>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. |
| <a id="SwiftToolchainInfo-implicit_deps_providers"></a>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):<br><br>* `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.<br><br>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. |
| <a id="SwiftToolchainInfo-module_aliases"></a>module_aliases | A `SwiftModuleAliasesInfo` provider that defines the module aliases to use during compilation. |
| <a id="SwiftToolchainInfo-package_configurations"></a>package_configurations | A list of `SwiftPackageConfigurationInfo` providers that specify additional compilation configuration options that are applied to targets on a per-package basis. |
| <a id="SwiftToolchainInfo-requested_features"></a>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.<br><br>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). |
| <a id="SwiftToolchainInfo-root_dir"></a>root_dir | `String`. The workspace-relative root directory of the toolchain. |
Expand Down
15 changes: 15 additions & 0 deletions swift/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down Expand Up @@ -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"],
)
4 changes: 4 additions & 0 deletions swift/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion swift/swift_module_mapping.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand Down
2 changes: 2 additions & 0 deletions swift/toolchains/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions swift/toolchains/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions swift/toolchains/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down