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 d1b9974 commit a39ee60
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 70 deletions.
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
44 changes: 44 additions & 0 deletions swift/internal/toolchain_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helpers used to depend on and access the Swift toolchain."""

SWIFT_TOOLCHAIN_TYPE = "@build_bazel_rules_swift//toolchains:toolchain_type"

def get_swift_toolchain(ctx, attr = "_toolchain"):
"""Gets the Swift toolchain associated with the rule or aspect.
Args:
ctx: The rule or aspect context.
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.
Returns:
A `SwiftToolchainInfo` provider.
"""
if SWIFT_TOOLCHAIN_TYPE in ctx.toolchains:
return ctx.toolchains[SWIFT_TOOLCHAIN_TYPE].swift_toolchain

# TODO(b/205018581): Delete this code path when migration to the new
# toolchain APIs is complete.
toolchain_target = getattr(ctx.attr, attr, None)
if toolchain_target and platform_common.ToolchainInfo in toolchain_target:
return toolchain_target[platform_common.ToolchainInfo].swift_toolchain

fail("To use `swift_common.get_toolchain`, you must declare the " +
"toolchain in your rule using " +
"`toolchains = [swift_common.toolchain_type()]`.")
81 changes: 44 additions & 37 deletions swift/internal/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -706,45 +706,52 @@ def _xcode_swift_toolchain_impl(ctx):
),
)

swift_toolchain_info = SwiftToolchainInfo(
action_configs = all_action_configs,
cc_toolchain_info = cc_toolchain,
clang_implicit_deps_providers = collect_implicit_deps_providers(
ctx.attr.clang_implicit_deps,
),
developer_dirs = swift_toolchain_developer_paths,
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(
ctx.attr.generated_header_module_implicit_deps,
)
),
implicit_deps_providers = collect_implicit_deps_providers(
ctx.attr.implicit_deps + ctx.attr.clang_implicit_deps,
additional_cc_infos = [swift_linkopts_providers.cc_info],
additional_objc_infos = [swift_linkopts_providers.objc_info],
),
package_configurations = [
target[SwiftPackageConfigurationInfo]
for target in ctx.attr.package_configurations
],
requested_features = requested_features,
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 = execution_requirements,
),
tool_configs = all_tool_configs,
unsupported_features = ctx.disabled_features + [
SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD,
],
)

return [
SwiftToolchainInfo(
action_configs = all_action_configs,
cc_toolchain_info = cc_toolchain,
clang_implicit_deps_providers = collect_implicit_deps_providers(
ctx.attr.clang_implicit_deps,
),
developer_dirs = swift_toolchain_developer_paths,
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(
ctx.attr.generated_header_module_implicit_deps,
)
),
implicit_deps_providers = collect_implicit_deps_providers(
ctx.attr.implicit_deps + ctx.attr.clang_implicit_deps,
additional_cc_infos = [swift_linkopts_providers.cc_info],
additional_objc_infos = [swift_linkopts_providers.objc_info],
),
package_configurations = [
target[SwiftPackageConfigurationInfo]
for target in ctx.attr.package_configurations
],
requested_features = requested_features,
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 = execution_requirements,
),
tool_configs = all_tool_configs,
unsupported_features = ctx.disabled_features + [
SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD,
],
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,
]

xcode_swift_toolchain = rule(
Expand Down
Loading

0 comments on commit a39ee60

Please sign in to comment.