Remove current_cc_toolchain and all uses
The special cc toolchain alias @bazel_tools//tools/cpp:current_cc_toolchain is incorrect and unsafe, specifically in an environment with multiple execution platforms.
Please read this document for details.
Much of this work has already been completed inside google's monorepo, this is a tracking issue for public changes needed.
Based on the doc it looks like genrules can put the toolchain type in toolchains, but what about custom rules? It seems like with bazel @ 7319e5e75650af6959d6bfe19a7e11fee8185205 and this example:
py_binary(
name = "foo",
srcs = ["main.py"],
main = "main.py",
env = {
"FOO": "$(CC)",
},
toolchains = [
"@bazel_tools//tools/cpp:toolchain_type",
],
)
it fails with:
ERROR: /tmp/repro/BUILD.bazel:3:10: in toolchains attribute of py_binary rule //:foo: '@@bazel_tools//tools/cpp:toolchain_type' does not have mandatory providers: 'TemplateVariableInfo'. Since this rule was created by the macro 'py_binary', the error might have been caused by the macro implementation
To me this seems very similar to the genrule case, but I've seen this pattern in use for many toolchain types. Example project I used:
You're correct: the current case in ToolchainContextUtil only works for genrule. I've mentioned to @gregestren and the Configurability team that they should explore opening this up. I'm re-assigning this issue to them since I won't be tackling it further.
I think this would generally be a great improvement, because the pattern of having to define some current_FOO_toolchain is quite common. so if it also solves this case that seems like a win win!
Is it possible to have more than 1 cc_toolchain during the compilation?
For example, I want to build CUDA libraries use nvcc while I need to build ordinary libraries with gcc. The rules_cuda creates a new type cuda_toolchain to play with nvcc, but it is really complex! It will be helpful to treat nvcc as a side cc_toolchain. (In a pratical case I'm writing rules_sycl)