bazel: incompatible_enable_cc_toolchain_resolution: Turn on toolchain resolution for cc rules
Flag: --incompatible_enable_cc_toolchain_resolution
Available since: 0.23
Will be flipped in: 7.0.0
Description
C++ toolchain resolution is an improved mechanism that selects a proper C++ toolchain (compiler) based on a pair of target and execution platforms (think cross compilation from linux to mac). It’s an improvement over the old mechanism that sets the --crosstool_top
flag. More information about platforms and toolchain resolution on https://bazel.build/reference/be/platforms-and-toolchains
Current status
- C++ toolchain resolution has been in use at large repository at Google for a year without significant problems.
- rules_apple and rules_swift lack toolchainization, however they work using
platform_mapping
- Bazel’s C++ toolchain autoconfiguration already supports toolchain resolution.
Migration
If you’re not using C++, or custom --crosstool_top
, --cpu
, or --compiler
,
you don’t use select
on these options, you can stop reading now, there is
nothing to migrate for you.
Migration before the flag is flipped (only in case you’re building for custom platforms, using custom C++ toolchains or using custom rules invoking cc_common.compile
and cc_common.link
):
- Add C++ platform definitions, register C++ toolchains
- Add missing C++ toolchain requirements to rules (Already added to OSS rules)
- Update blazerc configuration to use custom platforms
- Update Starlark configuration transitions to use platforms (ones affecting --cpu and --crostool_top, in cases
platform_mappings
isn’t used) - Update or fix configurations (
bazelrc
) to use--platforms
(where--cpu
or--crosstool_top
is used)
Migration after the flag is flipped:
- Replace cpu and os based selects with platform based selects (for example selects
cc_target_os
,target_cpu
and other) - Remove
--crosstool_top
,--cpu
flags frombazelrcs
and from Starlark configuration transitions - Remove part of
platform_mappings
configuration needed for C++ - Remove
_cc_toolchain
implicit dependency
Additional details
1. Add C++ platform definitions, register C++ toolchains
Please use constraint_settings
and constraint_values
from
the canonical Bazel Platforms
Repository (don’t hesitate to propose
missing targets!). If there is a need for C++ specific constraints, feel free to
upload a PR to the rules_cc
repository. It is extremely important that the whole ecosystem uses the same
constraints, we can only reuse libraries and toolchains when we speak the same
language.
2. Add missing C++ toolchain requirements to rules
For Starlark rules owners who depend on C++ toolchain it will be necessary to declare dependency on C++ toolchain type.
Before:
foo = rule(
implementation = _foo_impl,
attrs = {
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
)
After:
foo = rule(
implementation = _foo_impl,
attrs = {
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
toolchains = use_cpp_toolchain(),
)
See the docs and use @rules_cc//cc:find_cc_toolchain.bzl
(if using Bazel >= 0.27) or @bazel_tools//tools/cpp:toolchain_utils.bzl
to locate current C++ toolchain (otherwise). Also see examples for general usage.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 2
- Comments: 66 (55 by maintainers)
Links to this issue
Commits related to this issue
- Add incompatible flag to enable cc rules to use toolchain resolution. New flag: --incompatible_enable_cc_toolchain_resolution This deprecates and replaces the previous flag, --enabled_toolchain_type... — committed to bazelbuild/bazel by katre 5 years ago
- Expose the --incompatible_enable_cc_toolchain_resolution flag to Starlark and update find_cpp_toolchain to use it. Part of #7260. PiperOrigin-RevId: 231400764 — committed to bazelbuild/bazel by katre 5 years ago
- Add incompatible flag to enable cc rules to use toolchain resolution. New flag: --incompatible_enable_cc_toolchain_resolution This deprecates and replaces the previous flag, --enabled_toolchain_type... — committed to weixiao-huang/bazel by katre 5 years ago
- Expose the --incompatible_enable_cc_toolchain_resolution flag to Starlark and update find_cpp_toolchain to use it. Part of #7260. PiperOrigin-RevId: 231400764 — committed to weixiao-huang/bazel by katre 5 years ago
- Add toolchains attribute to compiler_flag This is needed to build Bazel when --incompatible_enable_cc_toolchain_resolution is set. #6516 #7260 RELNOTES: None. PiperOrigin-RevId: 238209238 — committed to bazelbuild/bazel by hlopko 5 years ago
- Add Bazel bootstrap test for C++ rules using platforms #6516 #7260. RELNOTES: None. PiperOrigin-RevId: 238409948 — committed to bazelbuild/bazel by hlopko 5 years ago
- Do not use CppConfiguration from CcToolchainProvider for --force_pic In order to implement this correctly (without changing the existing API) we need to thread CppConfiguration passed in cc_common.co... — committed to bazelbuild/bazel by hlopko 5 years ago
- Remove the use of enabled_toolchain_types, prior to it being removed entirely. Part of the work on #7260. PiperOrigin-RevId: 242451270 — committed to bazelbuild/bazel by katre 5 years ago
- Update rules_cc for --incompatible_enable_cc_toolchain_resolution https://github.com/bazelbuild/bazel/issues/7260 — committed to bazelbuild/rules_cc by hlopko 5 years ago
- Update rules_swift for --incompatible_enable_cc_toolchain_resolution bazelbuild/bazel#7260 — committed to hlopko/rules_swift by hlopko 5 years ago
- Migrate for --incompatible_enable_cc_toolchain_resolution https://github.com/bazelbuild/bazel/issues/7260 — committed to hlopko/rules_foreign_cc by hlopko 5 years ago
- Update rules_cc for --incompatible_enable_cc_toolchain_resolution [Copybara import of https://github.com/bazelbuild/rules_cc/pull/14] https://github.com/bazelbuild/bazel/issues/7260 Merge 3e6d3b9a4a... — committed to bazelbuild/rules_cc by a-googler 5 years ago
- Migrate for --incompatible_enable_cc_toolchain_resolution (#262) https://github.com/bazelbuild/bazel/issues/7260 — committed to bazelbuild/rules_foreign_cc by hlopko 5 years ago
- Update upb to be forward compatible with Bazel 0.27 Specifically, this PR prepares up for: https://github.com/bazelbuild/bazel/issues/7793 https://github.com/bazelbuild/bazel/issues/7260 — committed to hlopko/upb by hlopko 5 years ago
- Add find_cc_toolchain.bzl to help accessing current C++ toolchain This is to help migrating for: * https://github.com/bazelbuild/bazel/issues/7260 * https://github.com/bazelbuild/bazel/issues/7643 ... — committed to hlopko/rules_cc by hlopko 5 years ago
- Split toolchains from cc_toolchains in cc_configure This change modifies which part of cc_configure is executed unconditionally and which only on demand. The root of the problem was the call `regist... — committed to bazelbuild/bazel by hlopko 5 years ago
- Migrate kythe for Bazel 0.27 This PR migrates Kythe for https://github.com/bazelbuild/bazel/issues/7260. — committed to hlopko/kythe by hlopko 5 years ago
- Migrate kythe for Bazel 0.27 (#3769) This PR migrates Kythe for https://github.com/bazelbuild/bazel/issues/7260. — committed to kythe/kythe by hlopko 5 years ago
- Fix tulsi for --incompatible_enable_cc_toolchain_resolution https://github.com/bazelbuild/bazel/issues/7260 PiperOrigin-RevId: 250642318 — committed to bazelbuild/tulsi by hlopko 5 years ago
- Flip `--incompatible_enable_cc_toolchain_resolution` RELNOTES: Incompatible flag `--incompatible_enable_cc_toolchain_resolution` has been flipped. See https://github.com/bazelbuild/bazel/issues/7260 ... — committed to hlopko/bazel by hlopko 5 years ago
The best approach would be to continue to use the
--compiler
flag, with the newtarget_settings
attribute on thetoolchain
rule, like this:This will allow you to pass
--compiler=msvc
to select the msvc toolchain, etc, while still respecting your other constraints for the execution and target platform.Hi all, confirming if we still plan to flip this flag in 7.0?
The rules_foreign_cc breakage is due to the rules_android_ndk breakage as it is just one of the examples that use rules_android_ndk that is broken.
No, we don’t have any plans to fix this short of enabling toolchain resolution and removing the legacy support. You can file a feature request for this and the C++ team can prioritize it, if you’d like.
@katre in https://github.com/bazelbuild/bazel/issues/7260#issuecomment-813381150 you wrote about using target_settings to select the right compiler. How would this work when different compilers should be used on the host/exec and target platform?
Example:
You said you provided your own toolchains? Try using
--toolchain_resolution_debug
to see what toolchains are being considered and why they aren’t being used.Thanks @hlopko for the precision. Managed to turn on the incompatible_enable_cc_toolchain_resolution flag and have everything compiling without any issues (given I have a platform_mappings file).
Hello everyone. I’ve observed the toolchain resolution mechanism ignores the
--platforms
and--host_platform
command line options. The case is the following: I wish to selectMSVC
orclang-cl
on Windows from the command line. I’ve defined two platforms in myBUILD
:And registered both in
WORKSPACE
:The order in
register_execution_platforms
does matter: Bazel just takes the first and looks for a toolchain that satisfies all the constraints defined by the platform. If there is a such toolchain, Bazel just uses it. And If I wish to use the second one platform and do:Bazel just ignores it and still uses
x64_windows-msvc
as the execution platform.Maybe it is because the toolchain resolution depends on execution platform, not on a host or target one while the
--host_platform
and '–platforms` command line options are for host and target platforms respectively.Now I see only one workaround: not to register execution platforms in
WORKSPACE
but to use--extra_execution_platforms
option and when one wish to set aconfig_setting
for the platform, the--platform
option should also been used.Should I open a new issue about this?
SGTM. As long as the code path is being tested regularly to prevent rot, I am fine with this.
Yay for surprises 😃 So the osx C++ toolchain doesn’t yet work with platforms because of this TODO. @katre kindly volunteered to fix it 😃