xref: /aosp_15_r20/external/bazelbuild-rules_cc/cc/find_cc_toolchain.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1*eed53cd4SHONG Yifan# pylint: disable=g-bad-file-header
2*eed53cd4SHONG Yifan# Copyright 2016 The Bazel Authors. All rights reserved.
3*eed53cd4SHONG Yifan#
4*eed53cd4SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License");
5*eed53cd4SHONG Yifan# you may not use this file except in compliance with the License.
6*eed53cd4SHONG Yifan# You may obtain a copy of the License at
7*eed53cd4SHONG Yifan#
8*eed53cd4SHONG Yifan#    http://www.apache.org/licenses/LICENSE-2.0
9*eed53cd4SHONG Yifan#
10*eed53cd4SHONG Yifan# Unless required by applicable law or agreed to in writing, software
11*eed53cd4SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS,
12*eed53cd4SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*eed53cd4SHONG Yifan# See the License for the specific language governing permissions and
14*eed53cd4SHONG Yifan# limitations under the License.
15*eed53cd4SHONG Yifan
16*eed53cd4SHONG Yifan"""
17*eed53cd4SHONG YifanHelpers for CC Toolchains.
18*eed53cd4SHONG Yifan
19*eed53cd4SHONG YifanRules that require a CC toolchain should call `use_cc_toolchain` and `find_cc_toolchain`
20*eed53cd4SHONG Yifanto depend on and find a cc toolchain.
21*eed53cd4SHONG Yifan
22*eed53cd4SHONG Yifan* When https://github.com/bazelbuild/bazel/issues/7260 is **not** flipped, current
23*eed53cd4SHONG Yifan  C++ toolchain is selected using the legacy mechanism (`--crosstool_top`,
24*eed53cd4SHONG Yifan  `--cpu`, `--compiler`). For that to work the rule needs to declare an
25*eed53cd4SHONG Yifan  `_cc_toolchain` attribute, e.g.
26*eed53cd4SHONG Yifan
27*eed53cd4SHONG Yifan    foo = rule(
28*eed53cd4SHONG Yifan        implementation = _foo_impl,
29*eed53cd4SHONG Yifan        attrs = {
30*eed53cd4SHONG Yifan            "_cc_toolchain": attr.label(
31*eed53cd4SHONG Yifan                default = Label(
32*eed53cd4SHONG Yifan                    "@rules_cc//cc:current_cc_toolchain", # copybara-use-repo-external-label
33*eed53cd4SHONG Yifan                ),
34*eed53cd4SHONG Yifan            ),
35*eed53cd4SHONG Yifan        },
36*eed53cd4SHONG Yifan    )
37*eed53cd4SHONG Yifan
38*eed53cd4SHONG Yifan* When https://github.com/bazelbuild/bazel/issues/7260 **is** flipped, current
39*eed53cd4SHONG Yifan  C++ toolchain is selected using the toolchain resolution mechanism
40*eed53cd4SHONG Yifan  (`--platforms`). For that to work the rule needs to declare a dependency on
41*eed53cd4SHONG Yifan  C++ toolchain type:
42*eed53cd4SHONG Yifan
43*eed53cd4SHONG Yifan    load(":find_cc_toolchain/bzl", "use_cc_toolchain")
44*eed53cd4SHONG Yifan
45*eed53cd4SHONG Yifan    foo = rule(
46*eed53cd4SHONG Yifan        implementation = _foo_impl,
47*eed53cd4SHONG Yifan        toolchains = use_cc_toolchain(),
48*eed53cd4SHONG Yifan    )
49*eed53cd4SHONG Yifan
50*eed53cd4SHONG YifanWe advise to depend on both `_cc_toolchain` attr and on the toolchain type for
51*eed53cd4SHONG Yifanthe duration of the migration. After
52*eed53cd4SHONG Yifanhttps://github.com/bazelbuild/bazel/issues/7260 is flipped (and support for old
53*eed53cd4SHONG YifanBazel version is not needed), it's enough to only keep the toolchain type.
54*eed53cd4SHONG Yifan"""
55*eed53cd4SHONG Yifan
56*eed53cd4SHONG YifanCC_TOOLCHAIN_TYPE = "@bazel_tools//tools/cpp:toolchain_type"  # copybara-use-repo-external-label
57*eed53cd4SHONG Yifan
58*eed53cd4SHONG Yifandef find_cc_toolchain(ctx):
59*eed53cd4SHONG Yifan    """
60*eed53cd4SHONG YifanReturns the current `CcToolchainInfo`.
61*eed53cd4SHONG Yifan
62*eed53cd4SHONG Yifan    Args:
63*eed53cd4SHONG Yifan      ctx: The rule context for which to find a toolchain.
64*eed53cd4SHONG Yifan
65*eed53cd4SHONG Yifan    Returns:
66*eed53cd4SHONG Yifan      A CcToolchainInfo.
67*eed53cd4SHONG Yifan    """
68*eed53cd4SHONG Yifan
69*eed53cd4SHONG Yifan    # Check the incompatible flag for toolchain resolution.
70*eed53cd4SHONG Yifan    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):
71*eed53cd4SHONG Yifan        if not CC_TOOLCHAIN_TYPE in ctx.toolchains:
72*eed53cd4SHONG Yifan            fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.")
73*eed53cd4SHONG Yifan        toolchain_info = ctx.toolchains[CC_TOOLCHAIN_TYPE]
74*eed53cd4SHONG Yifan        if toolchain_info == None:
75*eed53cd4SHONG Yifan            # No cpp toolchain was found, so report an error.
76*eed53cd4SHONG Yifan            fail("Unable to find a CC toolchain using toolchain resolution. Target: %s, Platform: %s, Exec platform: %s" %
77*eed53cd4SHONG Yifan                 (ctx.label, ctx.fragments.platform.platform, ctx.fragments.platform.host_platform))
78*eed53cd4SHONG Yifan        if hasattr(toolchain_info, "cc_provider_in_toolchain") and hasattr(toolchain_info, "cc"):
79*eed53cd4SHONG Yifan            return toolchain_info.cc
80*eed53cd4SHONG Yifan        return toolchain_info
81*eed53cd4SHONG Yifan
82*eed53cd4SHONG Yifan    # Fall back to the legacy implicit attribute lookup.
83*eed53cd4SHONG Yifan    if hasattr(ctx.attr, "_cc_toolchain"):
84*eed53cd4SHONG Yifan        return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]
85*eed53cd4SHONG Yifan
86*eed53cd4SHONG Yifan    # We didn't find anything.
87*eed53cd4SHONG Yifan    fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.")
88*eed53cd4SHONG Yifan
89*eed53cd4SHONG Yifandef find_cpp_toolchain(ctx):
90*eed53cd4SHONG Yifan    """Deprecated, use `find_cc_toolchain` instead.
91*eed53cd4SHONG Yifan
92*eed53cd4SHONG Yifan    Args:
93*eed53cd4SHONG Yifan      ctx: See `find_cc_toolchain`.
94*eed53cd4SHONG Yifan
95*eed53cd4SHONG Yifan    Returns:
96*eed53cd4SHONG Yifan      A CcToolchainInfo.
97*eed53cd4SHONG Yifan    """
98*eed53cd4SHONG Yifan    return find_cc_toolchain(ctx)
99*eed53cd4SHONG Yifan
100*eed53cd4SHONG Yifandef use_cc_toolchain(mandatory = False):
101*eed53cd4SHONG Yifan    """
102*eed53cd4SHONG Yifan    Helper to depend on the cc toolchain.
103*eed53cd4SHONG Yifan
104*eed53cd4SHONG Yifan    Usage:
105*eed53cd4SHONG Yifan    ```
106*eed53cd4SHONG Yifan    my_rule = rule(
107*eed53cd4SHONG Yifan        toolchains = [other toolchain types] + use_cc_toolchain(),
108*eed53cd4SHONG Yifan    )
109*eed53cd4SHONG Yifan    ```
110*eed53cd4SHONG Yifan
111*eed53cd4SHONG Yifan    Args:
112*eed53cd4SHONG Yifan      mandatory: Whether or not it should be an error if the toolchain cannot be resolved.
113*eed53cd4SHONG Yifan
114*eed53cd4SHONG Yifan    Returns:
115*eed53cd4SHONG Yifan      A list that can be used as the value for `rule.toolchains`.
116*eed53cd4SHONG Yifan    """
117*eed53cd4SHONG Yifan    return [config_common.toolchain_type(CC_TOOLCHAIN_TYPE, mandatory = mandatory)]
118