jcater | cc4fbe9 | 2022-02-02 09:58:20 -0800 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
| 3 | title: Toolchain Resolution Implementation Details |
| 4 | --- |
| 5 | |
fwe | 8934b2b | 2022-02-18 03:08:03 -0800 | [diff] [blame^] | 6 | <div style="background-color: #EFCBCB; color: #AE2B2B; border: 1px solid #AE2B2B; border-radius: 5px; border-left: 10px solid #AE2B2B; padding: 0.5em;"> |
| 7 | <b>IMPORTANT:</b> The Bazel docs have moved! Please update your bookmark to <a href="https://bazel.build/docs/toolchain_resolution_implementation" style="color: #0000EE;">https://bazel.build/docs/toolchain_resolution_implementation</a> |
| 8 | <p/> |
| 9 | You can <a href="https://blog.bazel.build/2022/02/17/Launching-new-Bazel-site.html" style="color: #0000EE;">read about</a> the migration, and let us <a href="https://forms.gle/onkAkr2ZwBmcbWXj7" style="color: #0000EE;">know what you think</a>. |
| 10 | </div> |
| 11 | |
| 12 | |
jcater | cc4fbe9 | 2022-02-02 09:58:20 -0800 | [diff] [blame] | 13 | # Toolchain Resolution Implementation Details |
| 14 | |
| 15 | |
| 16 | **Note:** This section is intended for Bazel developers, and is not needed by |
| 17 | rule authors. |
| 18 | |
| 19 | Several SkyFunction classes implement the [toolchain resolution][Toolchains] process: |
| 20 | |
| 21 | 1. [`RegisteredToolchainsFunction`][RegisteredToolchainsFunction] and |
| 22 | [`RegisteredExecutionPlatformsFunction`][RegisteredExecutionPlatformsFunction] |
| 23 | find available toolchains and execution platforms, based on the current |
| 24 | configuration and WORKSPACE file. |
| 25 | |
| 26 | 1. [`SingleToolchainResolutionFunction`][SingleToolchainResolutionFunction] |
| 27 | resolves a single toolchain type for every execution platform. That is, for |
| 28 | every execution platform it finds the best registered toolchain to use based |
| 29 | on the following criteria: |
| 30 | |
| 31 | 1. Make sure the toolchain and target platform are compatible, by checking |
| 32 | the `target_compatible_with` attribute. |
| 33 | 1. Make sure the toolchain and execution platform are compatible, by |
| 34 | checking the `exec_compatible_with` attribute. |
| 35 | 1. If multiple toolchains are left, choose the highest-priority one (the |
| 36 | one that was registered first). |
| 37 | |
| 38 | 1. [`ToolchainResolutionFunction`][ToolchainResolutionFunction] calls |
| 39 | `SingleToolchainResolutionFunction` for each requested toolchain type, and |
| 40 | then determines the best execution platform to use. |
| 41 | |
| 42 | 1. First, remove any execution platform that does not have a valid |
| 43 | toolchain for each requested toolchain type. |
| 44 | 2. If multiple execution platforms are left, choose the highest-priority |
| 45 | one (the one that was registered first). |
| 46 | 1. If the execution platform is already set by the toolchain |
| 47 | transition, it will be selected first as described below. |
| 48 | |
| 49 | As discussed in [Toolchains and Configurations][Toolchains and Configurations], |
| 50 | the dependency from a target to a toolchain uses a special configuration that |
| 51 | forces the execution platform to be the same for both. Despite the name |
| 52 | "toolchain transition", this is not implemented as a configuration |
| 53 | transition, but instead as a special subclass of |
| 54 | [`ConfiguredTargetKey`][ConfiguredTargetKey], called |
| 55 | [`ToolchainDependencyConfiguredTargetKey`][ToolchainDependencyConfiguredTargetKey]. |
| 56 | In addition to the other data in `ConfiguredTargetKey`, this subclass also holds |
| 57 | the label of the execution platform. When `ToolchainResolutionFunction` is |
| 58 | considering which execution platform to use, if the forced execution platform |
| 59 | from the `ToolchainDependencyConfiguredTargetKey` is valid, it will be used even |
| 60 | if it is not the highest-priority. |
| 61 | |
| 62 | **Note:** If the forced execution platform is not valid (because there are no |
| 63 | valid toolchains, or because of execution constraints from the rule or target), |
| 64 | then the highest-priority valid execution platform will be used instead. |
| 65 | |
| 66 | [ConfiguredTargetKey]: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/skyframe/ConfiguredTargetKey.java |
| 67 | [RegisteredExecutionPlatformsFunction]: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/skyframe/RegisteredExecutionPlatformsFunction.java |
| 68 | [RegisteredToolchainsFunction]: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/skyframe/RegisteredToolchainsFunction.java |
| 69 | [SingleToolchainResolutionFunction]: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/skyframe/SingleToolchainResolutionFunction.java |
| 70 | [ToolchainDependencyConfiguredTargetKey]: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/skyframe/ConfiguredTargetKey.java;bpv=1;bpt=1;l=164?ss=bazel&q=ConfiguredTargetKey&gsn=ToolchainDependencyConfiguredTargetKey&gs=kythe%3A%2F%2Fgithub.com%2Fbazelbuild%2Fbazel%3Flang%3Djava%3Fpath%3Dcom.google.devtools.build.lib.skyframe.ConfiguredTargetKey.ToolchainDependencyConfiguredTargetKey%2336c7e68f8cd5ea0b5a21b3769e63e6b2d489b9ca8c6f79798839e7f40cf2a19e |
| 71 | [ToolchainResolutionFunction]: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/skyframe/ToolchainResolutionFunction.java |
| 72 | [Toolchains]: toolchains.html |
| 73 | [Toolchains and Configurations]: toolchains.html#toolchains-and-configurations |