blob: 6b124b91a85415ca48455c1aa848ee45cf190c37 [file] [log] [blame]
cpeyser8e9b2892017-12-04 07:40:48 -08001# 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
hlopko05fef182018-05-30 23:50:40 -070016"""
17Finds the c++ toolchain.
cpeyser8e9b2892017-12-04 07:40:48 -080018
hlopko05fef182018-05-30 23:50:40 -070019Returns the toolchain if enabled, and falls back to a toolchain constructed from
20the CppConfiguration.
cpeyser8e9b2892017-12-04 07:40:48 -080021"""
22
cpeyser8e9b2892017-12-04 07:40:48 -080023def find_cpp_toolchain(ctx):
vladmos20a042f2018-06-01 04:51:21 -070024 """
25 Finds the c++ toolchain.
cpeyser8e9b2892017-12-04 07:40:48 -080026
vladmos20a042f2018-06-01 04:51:21 -070027 If the c++ toolchain is in use, returns it. Otherwise, returns a c++
28 toolchain derived from legacy toolchain selection.
cpeyser8e9b2892017-12-04 07:40:48 -080029
vladmos20a042f2018-06-01 04:51:21 -070030 Args:
31 ctx: The rule context for which to find a toolchain.
cpeyser8e9b2892017-12-04 07:40:48 -080032
vladmos20a042f2018-06-01 04:51:21 -070033 Returns:
34 A CcToolchainProvider.
35 """
cpeyser8e9b2892017-12-04 07:40:48 -080036
jcater949145a2019-01-29 07:24:57 -080037 # Check the incompatible flag for toolchain resolution.
38 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):
jcater25562422021-02-25 14:12:49 +010039 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"]
42 if hasattr(toolchain_info, "cc_provider_in_toolchain") and hasattr(toolchain_info, "cc"):
43 return toolchain_info.cc
44 return toolchain_info
jcater949145a2019-01-29 07:24:57 -080045
46 # Fall back to the legacy implicit attribute lookup.
47 if hasattr(ctx.attr, "_cc_toolchain"):
vladmos20a042f2018-06-01 04:51:21 -070048 return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]
jcater949145a2019-01-29 07:24:57 -080049
50 # We didn't find anything.
jcater0581d192019-04-08 06:21:58 -070051 fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.")