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
  • Loading branch information
allevato authored and swiple-rules-gardener committed Nov 7, 2022
1 parent 6b56b01 commit a80cea9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
15 changes: 15 additions & 0 deletions swift/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load(
"per_module_swiftcopt_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 @@ -242,6 +243,20 @@ filegroup(
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"],
)

# Consumed by Bazel integration tests.
filegroup(
name = "for_bazel_tests",
Expand Down
4 changes: 4 additions & 0 deletions swift/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,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 @@ -12,6 +12,7 @@ bzl_library(
"//swift/internal:action_names",
"//swift/internal:feature_names",
"//swift/internal:features",
"//swift/internal:providers",
"//swift/internal:target_triples",
"//swift/internal:utils",
"//swift/internal:wmo",
Expand All @@ -36,6 +37,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
18 changes: 18 additions & 0 deletions swift/toolchains/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ load(
"@build_bazel_rules_swift//swift/internal:features.bzl",
"features_for_build_modes",
)
load(
"@build_bazel_rules_swift//swift/internal:providers.bzl",
"SwiftModuleAliasesInfo",
)
load(
"@build_bazel_rules_swift//swift/internal:target_triples.bzl",
"target_triples",
Expand Down Expand Up @@ -791,6 +795,13 @@ def _xcode_swift_toolchain_impl(ctx):
xcode_config = xcode_config,
)

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 @@ -818,6 +829,7 @@ def _xcode_swift_toolchain_impl(ctx):
swift_linkopts_providers.objc_info,
],
),
module_aliases = module_aliases,
package_configurations = [
target[SwiftPackageConfigurationInfo]
for target in ctx.attr.package_configurations
Expand Down Expand Up @@ -919,6 +931,12 @@ The C++ toolchain from which linking flags and other tools needed by the Swift
toolchain (such as `clang`) will be retrieved.
""",
),
"_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

1 comment on commit a80cea9

@brentleyjones
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.