Skip to content

Commit 86e2db7

Browse files
katrecopybara-github
authored andcommitted
Add a helper method for rules to depend on the cpp toolchain type.
Fixes bazelbuild#14728, part of bazelbuild#14727. Rules can begin using this as soon as it is released (hopefully in Bazel 5.1), and then future versions of Bazel will update the functionality as optional toolchains are added and C++ rules are updated to use them. Closes bazelbuild#14742. PiperOrigin-RevId: 427931123
1 parent ca58b64 commit 86e2db7

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

tools/cpp/cc_flags_supplier.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "CC_FLAGS_MAKE_VARIABLE_ACTION_NAME")
1717
load("@bazel_tools//tools/cpp:cc_flags_supplier_lib.bzl", "build_cc_flags")
18-
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
18+
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
1919

2020
def _cc_flags_supplier_impl(ctx):
2121
cc_toolchain = find_cpp_toolchain(ctx)
@@ -30,7 +30,7 @@ cc_flags_supplier = rule(
3030
attrs = {
3131
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
3232
},
33-
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
33+
toolchains = use_cpp_toolchain(),
3434
incompatible_use_toolchain_transition = True,
3535
fragments = ["cpp"],
3636
)

tools/cpp/compiler_flag.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Rule that allows select() to differentiate between compilers."""
1616

17-
load("//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
17+
load("//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
1818

1919
def _compiler_flag_impl(ctx):
2020
toolchain = find_cpp_toolchain(ctx)
@@ -25,6 +25,6 @@ compiler_flag = rule(
2525
attrs = {
2626
"_cc_toolchain": attr.label(default = Label("//tools/cpp:current_cc_toolchain")),
2727
},
28-
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
28+
toolchains = use_cpp_toolchain(),
2929
incompatible_use_toolchain_transition = True,
3030
)

tools/cpp/toolchain_utils.bzl

+28-7
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
# limitations under the License.
1515

1616
"""
17-
Finds the c++ toolchain.
17+
Utilities to help work with c++ toolchains.
1818
19-
Returns the toolchain if enabled, and falls back to a toolchain constructed from
20-
the CppConfiguration.
2119
"""
2220

21+
CPP_TOOLCHAIN_TYPE = "@bazel_tools//tools/cpp:toolchain_type"
22+
2323
def find_cpp_toolchain(ctx):
2424
"""
2525
Finds the c++ toolchain.
2626
2727
If the c++ toolchain is in use, returns it. Otherwise, returns a c++
28-
toolchain derived from legacy toolchain selection.
28+
toolchain derived from legacy toolchain selection, constructed from
29+
the CppConfiguration.
2930
3031
Args:
3132
ctx: The rule context for which to find a toolchain.
@@ -36,9 +37,9 @@ def find_cpp_toolchain(ctx):
3637

3738
# Check the incompatible flag for toolchain resolution.
3839
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):
39-
if not "@bazel_tools//tools/cpp:toolchain_type" in ctx.toolchains:
40-
fail("In order to use find_cpp_toolchain, you must include the '@bazel_tools//tools/cpp:toolchain_type' in the toolchains argument to your rule.")
41-
toolchain_info = ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]
40+
if not CPP_TOOLCHAIN_TYPE in ctx.toolchains:
41+
fail("In order to use find_cpp_toolchain, you must include the '%s' in the toolchains argument to your rule." % CPP_TOOLCHAIN_TYPE)
42+
toolchain_info = ctx.toolchains[CPP_TOOLCHAIN_TYPE]
4243
if hasattr(toolchain_info, "cc_provider_in_toolchain") and hasattr(toolchain_info, "cc"):
4344
return toolchain_info.cc
4445
return toolchain_info
@@ -49,3 +50,23 @@ def find_cpp_toolchain(ctx):
4950

5051
# We didn't find anything.
5152
fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.")
53+
54+
def use_cpp_toolchain(mandatory = True):
55+
"""
56+
Helper to depend on the c++ toolchain.
57+
58+
Usage:
59+
```
60+
my_rule = rule(
61+
toolchains = [other toolchain types] + use_cpp_toolchain(),
62+
)
63+
```
64+
65+
Args:
66+
mandatory: Whether or not it should be an error if the toolchain cannot be resolved.
67+
Currently ignored, this will be enabled when optional toolchain types are added.
68+
69+
Returns:
70+
A list that can be used as the value for `rule.toolchains`.
71+
"""
72+
return [CPP_TOOLCHAIN_TYPE]

0 commit comments

Comments
 (0)