Skip to content

Commit

Permalink
Add the module_mapping flag to the Swift rules
Browse files Browse the repository at this point in the history
The value of this flag should be the label a `swift_module_mapping` target, which provides the aliases to use during the build.

At this time, module aliases are only being supported as a globally-applied build flag. This can be revisited in the future if aliases are needed for individual subgraphs, but this is much more complex to represent in BUILD.

This change only adds the flag and hooks it into the toolchain; the compilation changes will be added in a subsequent change.

PiperOrigin-RevId: 486659250
(cherry picked from commit a80cea9)
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Oct 3, 2024
1 parent 7c2dd0f commit aeed2a8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
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

0 comments on commit aeed2a8

Please sign in to comment.