Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to compile certain cc_binary's with a different toolchain #25810

24 views
Skip to first unread message

Nick Desaulniers

unread,
Apr 10, 2025, 1:20:00 PMApr 10
to bazel-discuss
Reposting from https://github.com/bazelbuild/bazel/discussions/25810, maybe a discussion there is more appropriate?

I have a codebase that's a mix of C++ for the host and target machines of different architectures (and some python just for the host).

Ideally, I'd like to continue using `cc_library`, `cc_binary`, and `cc_test` for target specific programs, but I'm looking for the most ergonomic way to say something to the effect of "this cc_binary should only be built for the target, and never the host."

I've mostly followed [this tutorial](https://bazel.build/tutorials/ccp-toolchain-config).  I'm also trying to mix that with the [platforms](https://bazel.build/extending/platforms) concept.

In my BUILD.bazel, I tried adding:
```starlark
constraint_setting(name = "mcpu_setting")
constraint_value(name = "my_cpu", constraint_setting = ":mcpu_setting")
platform(
    name = "my_platform",
    constraint_values = [":my_cpu"],
)
toolchain(
  ...
  toolchain = ":cc_my_toolchain",
  target_compatible_with = [
        ":my_cpu",
    ],
)
```
Building with `bazel build //example:foo --toolchain_resolution_debug='@bazel_tools//tools/cpp:toolchain_type' --platforms=//bazel/toolchain/my_toolchain:my_platform`, I can see my toolchain getting selected:
```
**ToolchainResolution:   Selected //bazel/toolchain/my_toolchain:cc_my_toolchain to run on execution platform //bazel/toolchain:clang**
```
But then I observe failures due to Python:
```
ERROR: ...external/rules_python+/python/BUILD.bazel:363:21: While resolving toolchains for target @@rules_python+//python:current_py_toolchain (c38586b): No matching toolchains found for types:
  @@bazel_tools//tools/python:toolchain_type
```
and adding `--toolchain_resolution_debug='@@bazel_tools//tools/python:toolchain_type'` shows that python rules know about lots of different platforms, but not my platform.  At this point, I'm flabbergasted; I'm trying to build a cc_library here with no deps, who cares about python?

---
Building with `--platforms=//bazel/toolchain/my_toolchain:my_platform` is already way way too verbose to inflict on our developers. Surely there's a way to just specify in the `cc_library` rule to use a specific toolchain always?

[`target_compatible_with`](https://bazel.build/reference/be/common-definitions#common.target_compatible_with) smells like that maybe might do what I want. Adding:
```starlark
cc_libary(
  ...
  target_compatible_with = [
        "//bazel/toolchain/my_toolchain:my_cpu",
    ],
```
But then building without `--platforms=//bazel/toolchain/my_toolchain:my_platform` fails since the _host_ compiler is selected:
```
ERROR: Analysis of target '//example:foo' failed; build aborted: Target //example:foo is incompatible and cannot be built, but was explicitly requested.
Dependency chain:
    //example:foo (be84d8)   <-- target platform (//bazel/toolchain:clang) didn't satisfy constraint //bazel/toolchain/my_toolchain:my_cpu
```
Rebuilding with `--platforms=...` causes the python failure above.

---
Is what I'm trying to do even possible with bazel? Is platforms the right way to do this, or something else that I'm missing?  Thanks for taking a look!  Consider replying on https://github.com/bazelbuild/bazel/discussions/25810.

Filip Filmar

unread,
Apr 10, 2025, 2:02:42 PMApr 10
to Nick Desaulniers, bazel-discuss
On Thu, Apr 10, 2025 at 8:19 PM 'Nick Desaulniers' via bazel-discuss <bazel-...@googlegroups.com> wrote:
Is what I'm trying to do even possible with bazel? Is platforms the right way to do this, or something else that I'm missing?  Thanks for taking a look!  Consider replying on https://github.com/bazelbuild/bazel/discussions/25810.

 Added a reply to your github issue. I think platforms are the right way to do this, and what you want to do should be doable.

F

Reply all
Reply to author
Forward
0 new messages