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 884e544)
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Jun 19, 2024
1 parent 555f08c commit 3c645d6
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 106 deletions.
23 changes: 23 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,29 @@ Extracts the symbol graph from a Swift module.
| <a id="swift_common.extract_symbol_graph-swift_toolchain"></a>swift_toolchain | The `SwiftToolchainInfo` provider of the toolchain. | none |


<a id="swift_common.get_toolchain"></a>

## swift_common.get_toolchain

<pre>
swift_common.get_toolchain(<a href="#swift_common.get_toolchain-ctx">ctx</a>, <a href="#swift_common.get_toolchain-attr">attr</a>)
</pre>

Gets the Swift toolchain associated with the rule or aspect.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="swift_common.get_toolchain-ctx"></a>ctx | The rule or aspect context. | none |
| <a id="swift_common.get_toolchain-attr"></a>attr | The name of the attribute on the calling rule or aspect that should be used to retrieve the toolchain if it is not provided by the `toolchains` argument of the rule/aspect. Note that this is only supported for legacy/migration purposes and will be removed once migration to toolchains is complete. | `"_toolchain"` |

**RETURNS**

A `SwiftToolchainInfo` provider.


<a id="swift_common.is_enabled"></a>

## swift_common.is_enabled
Expand Down
5 changes: 2 additions & 3 deletions proto/swift_proto_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ load(
"SwiftInfo",
"SwiftProtoCompilerInfo",
"SwiftProtoInfo",
"SwiftToolchainInfo",
"swift_common",
)

Expand Down Expand Up @@ -258,7 +257,7 @@ def compile_swift_protos_for_target(
))

# Extract the swift toolchain and configure the features:
swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = swift_common.get_toolchain(ctx)
feature_configuration = swift_common.configure_features(
ctx = ctx,
requested_features = ctx.features,
Expand Down Expand Up @@ -303,7 +302,7 @@ def compile_swift_protos_for_target(
)

# Extract the swift toolchain and configure the features:
swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = swift_common.get_toolchain(ctx)
feature_configuration = swift_common.configure_features(
ctx = ctx,
requested_features = ctx.features,
Expand Down
9 changes: 9 additions & 0 deletions swift/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ bzl_library(
":features",
":module_maps",
":providers",
":toolchain_utils",
":utils",
],
)
Expand All @@ -193,6 +194,7 @@ bzl_library(
":providers",
":swift_clang_module_aspect",
":symbol_graph_extracting",
":toolchain_utils",
],
)

Expand All @@ -206,6 +208,7 @@ bzl_library(
":features",
":providers",
":symbol_graph_extracting",
":toolchain_utils",
":utils",
"@bazel_skylib//lib:dicts",
],
Expand Down Expand Up @@ -354,6 +357,12 @@ bzl_library(
],
)

bzl_library(
name = "toolchain_utils",
srcs = ["toolchain_utils.bzl"],
visibility = ["//swift:__subpackages__"],
)

bzl_library(
name = "utils",
srcs = ["utils.bzl"],
Expand Down
3 changes: 1 addition & 2 deletions swift/internal/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""Common attributes used by multiple Swift build rules."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo", "SwiftToolchainInfo")
load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo")

def swift_common_rule_attrs(
additional_deps_aspects = [],
Expand Down Expand Up @@ -390,7 +390,6 @@ def swift_toolchain_attrs(toolchain_attr_name = "_toolchain"):
return {
toolchain_attr_name: attr.label(
default = Label("@build_bazel_rules_swift_local_config//:toolchain"),
providers = [[SwiftToolchainInfo]],
),
}

Expand Down
9 changes: 2 additions & 7 deletions swift/internal/swift_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ load(
"register_link_binary_action",
)
load(":output_groups.bzl", "supplemental_compilation_output_groups")
load(
":providers.bzl",
"SwiftCompilerPluginInfo",
"SwiftInfo",
"SwiftToolchainInfo",
)
load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo")
load(":swift_common.bzl", "swift_common")
load(
":utils.bzl",
Expand Down Expand Up @@ -58,7 +53,7 @@ def _maybe_parse_as_library_copts(srcs):
return ["-parse-as-library"] if use_parse_as_library else []

def _swift_binary_impl(ctx):
swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = swift_common.get_toolchain(ctx)

feature_configuration = configure_features_for_binary(
ctx = ctx,
Expand Down
7 changes: 5 additions & 2 deletions swift/internal/swift_clang_module_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ load(":module_maps.bzl", "write_module_map")
load(
":providers.bzl",
"SwiftInfo",
"SwiftToolchainInfo",
"create_clang_module",
"create_module",
"create_swift_info",
)
load(":toolchain_utils.bzl", "get_swift_toolchain")
load(":utils.bzl", "compilation_context_for_explicit_module_compilation")

_MULTIPLE_TARGET_ASPECT_ATTRS = [
Expand Down Expand Up @@ -838,7 +838,10 @@ def _swift_clang_module_aspect_impl(target, aspect_ctx):
])
unsupported_features.append(SWIFT_FEATURE_MODULE_MAP_NO_PRIVATE_HEADERS)

swift_toolchain = aspect_ctx.attr._toolchain_for_aspect[SwiftToolchainInfo]
swift_toolchain = get_swift_toolchain(
aspect_ctx,
attr = "_toolchain_for_aspect",
)
feature_configuration = configure_features(
ctx = aspect_ctx,
requested_features = requested_features,
Expand Down
2 changes: 2 additions & 0 deletions swift/internal/swift_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ load(
)
load(":swift_clang_module_aspect.bzl", "create_swift_interop_info")
load(":symbol_graph_extracting.bzl", "extract_symbol_graph")
load(":toolchain_utils.bzl", "get_swift_toolchain")

# The exported `swift_common` module, which defines the public API for directly
# invoking actions that compile Swift code from other rules.
Expand All @@ -71,6 +72,7 @@ swift_common = struct(
create_swift_module = create_swift_module,
derive_module_name = derive_module_name,
extract_symbol_graph = extract_symbol_graph,
get_toolchain = get_swift_toolchain,
is_enabled = is_feature_enabled,
library_rule_attrs = swift_library_rule_attrs,
precompile_clang_module = precompile_clang_module,
Expand Down
4 changes: 2 additions & 2 deletions swift/internal/swift_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":attrs.bzl", "swift_common_rule_attrs", "swift_toolchain_attrs")
load(":linking.bzl", "new_objc_provider")
load(":providers.bzl", "SwiftInfo", "SwiftToolchainInfo")
load(":providers.bzl", "SwiftInfo")
load(":swift_common.bzl", "swift_common")
load(":utils.bzl", "compact", "get_compilation_contexts", "get_providers")

Expand All @@ -43,7 +43,7 @@ def _swift_import_impl(ctx):
fail("One or both of 'swiftinterface' and 'swiftmodule' must be " +
"specified.")

swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = swift_common.get_toolchain(ctx)
feature_configuration = swift_common.configure_features(
ctx = ctx,
swift_toolchain = swift_toolchain,
Expand Down
4 changes: 2 additions & 2 deletions swift/internal/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ load(
)
load(":linking.bzl", "new_objc_provider")
load(":output_groups.bzl", "supplemental_compilation_output_groups")
load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo", "SwiftToolchainInfo")
load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo")
load(":swift_clang_module_aspect.bzl", "swift_clang_module_aspect")
load(":swift_common.bzl", "swift_common")
load(
Expand Down Expand Up @@ -130,7 +130,7 @@ def _swift_library_impl(ctx):
if not module_name:
module_name = swift_common.derive_module_name(ctx.label)

swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = swift_common.get_toolchain(ctx)
feature_configuration = swift_common.configure_features(
ctx = ctx,
requested_features = ctx.features + extra_features,
Expand Down
4 changes: 2 additions & 2 deletions swift/internal/swift_library_group.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":attrs.bzl", "swift_deps_attr", "swift_toolchain_attrs")
load(":providers.bzl", "SwiftInfo", "SwiftToolchainInfo")
load(":providers.bzl", "SwiftInfo")
load(":swift_clang_module_aspect.bzl", "swift_clang_module_aspect")
load(":swift_common.bzl", "swift_common")
load(":utils.bzl", "get_providers")
Expand All @@ -28,7 +28,7 @@ def _swift_library_group_impl(ctx):
for dep in deps
if CcInfo in dep
]
swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = swift_common.get_toolchain(ctx)

return [
DefaultInfo(),
Expand Down
4 changes: 2 additions & 2 deletions swift/internal/swift_module_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":derived_files.bzl", "derived_files")
load(":linking.bzl", "new_objc_provider")
load(":output_groups.bzl", "supplemental_compilation_output_groups")
load(":providers.bzl", "SwiftInfo", "SwiftToolchainInfo")
load(":providers.bzl", "SwiftInfo")
load(":swift_common.bzl", "swift_common")
load(":utils.bzl", "compact", "get_providers")

Expand Down Expand Up @@ -47,7 +47,7 @@ def _swift_module_alias_impl(ctx):
output = reexport_src,
)

swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = swift_common.get_toolchain(ctx)
feature_configuration = swift_common.configure_features(
ctx = ctx,
requested_features = ctx.features,
Expand Down
10 changes: 3 additions & 7 deletions swift/internal/swift_symbol_graph_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":attrs.bzl", "swift_toolchain_attrs")
load(":derived_files.bzl", "derived_files")
load(":features.bzl", "configure_features")
load(
":providers.bzl",
"SwiftInfo",
"SwiftSymbolGraphInfo",
"SwiftToolchainInfo",
)
load(":providers.bzl", "SwiftInfo", "SwiftSymbolGraphInfo")
load(":symbol_graph_extracting.bzl", "extract_symbol_graph")
load(":toolchain_utils.bzl", "get_swift_toolchain")
load(":utils.bzl", "include_developer_search_paths")

def _swift_symbol_graph_aspect_impl(target, aspect_ctx):
symbol_graphs = []

if SwiftInfo in target:
swift_toolchain = aspect_ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = get_swift_toolchain(aspect_ctx)
feature_configuration = configure_features(
ctx = aspect_ctx,
swift_toolchain = swift_toolchain,
Expand Down
3 changes: 1 addition & 2 deletions swift/internal/swift_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ load(
"SwiftCompilerPluginInfo",
"SwiftInfo",
"SwiftSymbolGraphInfo",
"SwiftToolchainInfo",
)
load(":swift_common.bzl", "swift_common")
load(":swift_symbol_graph_aspect.bzl", "test_discovery_symbol_graph_aspect")
Expand Down Expand Up @@ -170,7 +169,7 @@ def _generate_test_discovery_srcs(*, actions, deps, name, test_discoverer):
return outputs

def _swift_test_impl(ctx):
swift_toolchain = ctx.attr._toolchain[SwiftToolchainInfo]
swift_toolchain = swift_common.get_toolchain(ctx)

feature_configuration = configure_features_for_binary(
ctx = ctx,
Expand Down
79 changes: 43 additions & 36 deletions swift/internal/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -424,44 +424,51 @@ def _swift_toolchain_impl(ctx):
# TODO(allevato): Move some of the remaining hardcoded values, like object
# format and Obj-C interop support, to attributes so that we can remove the
# assumptions that are only valid on Linux.
swift_toolchain_info = SwiftToolchainInfo(
action_configs = all_action_configs,
cc_toolchain_info = cc_toolchain,
clang_implicit_deps_providers = (
collect_implicit_deps_providers([])
),
developer_dirs = [],
entry_point_linkopts_provider = _entry_point_linkopts_provider,
feature_allowlists = [
target[SwiftFeatureAllowlistInfo]
for target in ctx.attr.feature_allowlists
],
generated_header_module_implicit_deps_providers = (
collect_implicit_deps_providers([])
),
implicit_deps_providers = collect_implicit_deps_providers(
[],
additional_cc_infos = [swift_linkopts_cc_info],
),
package_configurations = [
target[SwiftPackageConfigurationInfo]
for target in ctx.attr.package_configurations
],
requested_features = requested_features,
root_dir = toolchain_root,
swift_worker = ctx.attr._worker[DefaultInfo].files_to_run,
const_protocols_to_gather = ctx.file.const_protocols_to_gather,
test_configuration = struct(
env = env,
execution_requirements = {},
),
tool_configs = all_tool_configs,
unsupported_features = ctx.disabled_features + [
SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD,
SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE,
],
)

return [
SwiftToolchainInfo(
action_configs = all_action_configs,
cc_toolchain_info = cc_toolchain,
clang_implicit_deps_providers = (
collect_implicit_deps_providers([])
),
developer_dirs = [],
entry_point_linkopts_provider = _entry_point_linkopts_provider,
feature_allowlists = [
target[SwiftFeatureAllowlistInfo]
for target in ctx.attr.feature_allowlists
],
generated_header_module_implicit_deps_providers = (
collect_implicit_deps_providers([])
),
implicit_deps_providers = collect_implicit_deps_providers(
[],
additional_cc_infos = [swift_linkopts_cc_info],
),
package_configurations = [
target[SwiftPackageConfigurationInfo]
for target in ctx.attr.package_configurations
],
requested_features = requested_features,
root_dir = toolchain_root,
swift_worker = ctx.attr._worker[DefaultInfo].files_to_run,
const_protocols_to_gather = ctx.file.const_protocols_to_gather,
test_configuration = struct(
env = env,
execution_requirements = {},
),
tool_configs = all_tool_configs,
unsupported_features = ctx.disabled_features + [
SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD,
SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE,
],
platform_common.ToolchainInfo(
swift_toolchain = swift_toolchain_info,
),
# TODO(b/205018581): Remove this legacy propagation when everything is
# migrated over to new-style toolchains.
swift_toolchain_info,
]

swift_toolchain = rule(
Expand Down
Loading

0 comments on commit 3c645d6

Please sign in to comment.