blob: be6f665877ea276c409e29f5ecee0fdd4918ba7b [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 """
juliexxia45f1da12018-07-10 08:02:39 -070036 if not hasattr(ctx.attr, "_cc_toolchain"):
37 fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.")
cpeyser8e9b2892017-12-04 07:40:48 -080038
vladmos20a042f2018-06-01 04:51:21 -070039 if Label("@bazel_tools//tools/cpp:toolchain_type") in ctx.fragments.platform.enabled_toolchain_types:
40 return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]
41 else:
42 return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]