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 | """ |
cpeyser | 8e9b289 | 2017-12-04 07:40:48 -0800 | [diff] [blame] | 36 | |
jcater | 949145a | 2019-01-29 07:24:57 -0800 | [diff] [blame] | 37 | # 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): |
jcater | 2556242 | 2021-02-25 14:12:49 +0100 | [diff] [blame] | 39 | 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 |
jcater | 949145a | 2019-01-29 07:24:57 -0800 | [diff] [blame] | 45 | |
| 46 | # Fall back to the legacy implicit attribute lookup. |
| 47 | if hasattr(ctx.attr, "_cc_toolchain"): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 48 | return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] |
jcater | 949145a | 2019-01-29 07:24:57 -0800 | [diff] [blame] | 49 | |
| 50 | # We didn't find anything. |
jcater | 0581d19 | 2019-04-08 06:21:58 -0700 | [diff] [blame] | 51 | fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.") |