Skip to content

Commit

Permalink
Add a use_cc_toolchain helper to rules_cc.
Browse files Browse the repository at this point in the history
Part of github.com/bazelbuild/bazel/issues/14727.

PiperOrigin-RevId: 449499388
Change-Id: I9fb5fecd1059b0d48a26e89d1c25d54fd2348b55
  • Loading branch information
katre authored and copybara-github committed May 18, 2022
1 parent ab0be67 commit 8bb0eb5
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions cc/find_cc_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
# limitations under the License.

"""
Returns the current `CcToolchainInfo`.
Helpers for CC Toolchains.
Rules that require a CC toolchain should call `use_cc_toolchain` and `find_cc_toolchain`
to depend on and find a cc toolchain.
* When https://github.com/bazelbuild/bazel/issues/7260 is **not** flipped, current
C++ toolchain is selected using the legacy mechanism (`--crosstool_top`,
Expand All @@ -37,11 +40,11 @@ Returns the current `CcToolchainInfo`.
(`--platforms`). For that to work the rule needs to declare a dependency on
C++ toolchain type:
load(":find_cc_toolchain/bzl", "use_cc_toolchain")
foo = rule(
implementation = _foo_impl,
toolchains = [
"@bazel_tools//tools/cpp:toolchain_type", # copybara-use-repo-external-label
],
toolchains = use_cc_toolchain(),
)
We advise to depend on both `_cc_toolchain` attr and on the toolchain type for
Expand All @@ -50,6 +53,8 @@ https://github.com/bazelbuild/bazel/issues/7260 is flipped (and support for old
Bazel version is not needed), it's enough to only keep the toolchain type.
"""

CC_TOOLCHAIN_TYPE = "@bazel_tools//tools/cpp:toolchain_type" # copybara-use-repo-external-label

def find_cc_toolchain(ctx):
"""
Returns the current `CcToolchainInfo`.
Expand All @@ -63,9 +68,9 @@ Returns the current `CcToolchainInfo`.

# Check the incompatible flag for toolchain resolution.
if hasattr(cc_common, "is_cc_toolchain_resolution_enabled_do_not_use") and cc_common.is_cc_toolchain_resolution_enabled_do_not_use(ctx = ctx):
if not "@bazel_tools//tools/cpp:toolchain_type" in ctx.toolchains: # copybara-use-repo-external-label
if not CC_TOOLCHAIN_TYPE in ctx.toolchains:
fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.")
toolchain_info = ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"] # copybara-use-repo-external-label
toolchain_info = ctx.toolchains[CC_TOOLCHAIN_TYPE]
if hasattr(toolchain_info, "cc_provider_in_toolchain") and hasattr(toolchain_info, "cc"):
return toolchain_info.cc
return toolchain_info
Expand All @@ -87,3 +92,24 @@ def find_cpp_toolchain(ctx):
A CcToolchainInfo.
"""
return find_cc_toolchain(ctx)

# buildifier: disable=unused-variable
def use_cc_toolchain(mandatory = True):
"""
Helper to depend on the cc toolchain.
Usage:
```
my_rule = rule(
toolchains = [other toolchain types] + use_cc_toolchain(),
)
```
Args:
mandatory: Whether or not it should be an error if the toolchain cannot be resolved.
Currently ignored, this will be enabled when optional toolchain types are added.
Returns:
A list that can be used as the value for `rule.toolchains`.
"""
return [CC_TOOLCHAIN_TYPE]

0 comments on commit 8bb0eb5

Please sign in to comment.