1# pylint: disable=g-bad-file-header 2# Copyright 2016 The Bazel Authors. All rights reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16""" 17Returns the current `CcToolchainInfo`. 18 19* When https://github.com/bazelbuild/bazel/issues/7260 is **not** flipped, current 20 C++ toolchain is selected using the legacy mechanism (`--crosstool_top`, 21 `--cpu`, `--compiler`). For that to work the rule needs to declare an 22 `_cc_toolchain` attribute, e.g. 23 24 foo = rule( 25 implementation = _foo_impl, 26 attrs = { 27 "_cc_toolchain": attr.label( 28 default = Label( 29 "@rules_cc//cc:current_cc_toolchain", # copybara-use-repo-external-label 30 ), 31 ), 32 }, 33 ) 34 35* When https://github.com/bazelbuild/bazel/issues/7260 **is** flipped, current 36 C++ toolchain is selected using the toolchain resolution mechanism 37 (`--platforms`). For that to work the rule needs to declare a dependency on 38 C++ toolchain type: 39 40 foo = rule( 41 implementation = _foo_impl, 42 toolchains = [ 43 "@rules_cc//cc:toolchain_type", # copybara-use-repo-external-label 44 ], 45 ) 46 47We advise to depend on both `_cc_toolchain` attr and on the toolchain type for 48the duration of the migration. After 49https://github.com/bazelbuild/bazel/issues/7260 is flipped (and support for old 50Bazel version is not needed), it's enough to only keep the toolchain type. 51""" 52 53def find_cc_toolchain(ctx): 54 """ 55Returns the current `CcToolchainInfo`. 56 57 Args: 58 ctx: The rule context for which to find a toolchain. 59 60 Returns: 61 A CcToolchainInfo. 62 """ 63 64 # Check the incompatible flag for toolchain resolution. 65 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): 66 if not "@bazel_tools//tools/cpp:toolchain_type" in ctx.toolchains: 67 fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.") 68 toolchain_info = ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"] 69 if hasattr(toolchain_info, "cc_provider_in_toolchain") and hasattr(toolchain_info, "cc"): 70 return toolchain_info.cc 71 return toolchain_info 72 73 # Fall back to the legacy implicit attribute lookup. 74 if hasattr(ctx.attr, "_cc_toolchain"): 75 return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] 76 77 # We didn't find anything. 78 fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.") 79 80def find_cpp_toolchain(ctx): 81 """Deprecated, use `find_cc_toolchain` instead. 82 83 Args: 84 ctx: See `find_cc_toolchain`. 85 86 Returns: 87 A CcToolchainInfo. 88 """ 89 return find_cc_toolchain(ctx) 90