-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the
swift_common.get_toolchain
helper function
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
1 parent
d1b9974
commit a39ee60
Showing
15 changed files
with
122 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]`.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.