blob: 59f411376b799fe81e4734f8f387994d6972ede5 [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):
vladmos20a042f2018-06-01 04:51:21 -070039 return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]
jcater949145a2019-01-29 07:24:57 -080040
jcatere2fcb952019-02-04 09:02:37 -080041 if Label("@bazel_tools//tools/cpp:toolchain_type") in ctx.fragments.platform.enabled_toolchain_types:
jcater949145a2019-01-29 07:24:57 -080042 return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]
43
44 # Fall back to the legacy implicit attribute lookup.
45 if hasattr(ctx.attr, "_cc_toolchain"):
vladmos20a042f2018-06-01 04:51:21 -070046 return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]
jcater949145a2019-01-29 07:24:57 -080047
48 # We didn't find anything.
49 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.")