cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 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 | |
hlopko | 05fef18 | 2018-05-30 23:50:40 -0700 | [diff] [blame] | 16 | """ |
| 17 | Finds the c++ toolchain. |
cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 18 | |
hlopko | 05fef18 | 2018-05-30 23:50:40 -0700 | [diff] [blame] | 19 | Returns the toolchain if enabled, and falls back to a toolchain constructed from |
| 20 | the CppConfiguration. |
cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 21 | """ |
| 22 | |
cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 23 | def find_cpp_toolchain(ctx): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 24 | """ |
| 25 | Finds the c++ toolchain. |
cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 26 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 27 | If the c++ toolchain is in use, returns it. Otherwise, returns a c++ |
| 28 | toolchain derived from legacy toolchain selection. |
cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 29 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 30 | Args: |
| 31 | ctx: The rule context for which to find a toolchain. |
cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 32 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 33 | Returns: |
| 34 | A CcToolchainProvider. |
| 35 | """ |
juliexxia | 45f1da1 | 2018-07-10 08:02:39 -0700 | [diff] [blame] | 36 | 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.") |
cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 38 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 39 | 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] |