Auto-selecting the correct C++ toolchain for a target

419 views
Skip to first unread message

Filip Filmar

unread,
Mar 10, 2023, 3:26:19 AM3/10/23
to bazel-discuss
Hi folks.

Suppose I have the following:

```
cc_library(
    name = "hello",
    srcs = ["hello.cc"],
    target_compatible_with = [
        "@platforms//cpu:foo", # imagine here that :foo exists
        "@platforms//os:bar", # imagine here that :bar exists
    ],
)
```

Is it possible for the correct compiler to be chosen automatically by bazel if I do `bazel build //:hello`? (that is, no added .bazelrc or flag magic)

I know it is possible for a target to error out if the user selects an incompatible toolchain. But, is it possible to have bazel select the correct toolchain automatically, assuming that the correct toolchain is registered?

Thanks,
F

Gregg Reynolds

unread,
Mar 10, 2023, 12:54:49 PM3/10/23
to Filip Filmar, bazel-discuss
On Fri, Mar 10, 2023 at 2:26 AM Filip Filmar <fil...@gmail.com> wrote:
Hi folks.

Suppose I have the following:

```
cc_library(
    name = "hello",
    srcs = ["hello.cc"],
    target_compatible_with = [
        "@platforms//cpu:foo", # imagine here that :foo exists
        "@platforms//os:bar", # imagine here that :bar exists
    ],
)
```

Is it possible for the correct compiler to be chosen automatically by bazel if I do `bazel build //:hello`? (that is, no added .bazelrc or flag magic)

Bazel always selects the correct compiler, by definition: it selects the compiler that matches your configuration.  If that's not the one you're expecting then your config needs fixing.  What is it you want to accomplish?

Gregg

Filip Filmar

unread,
Mar 10, 2023, 1:52:43 PM3/10/23
to Gregg Reynolds, bazel-discuss
An use case is a simulator of a specific machine: the simulator itself is written in C++ for the host and compiled by bazel; another program I want to compile is written in C++, but needs to be compiled for the target. But then, I need to feed the target's binary into the host's simulator.

This is I think different from the use case where you can compile the entire code base for different platforms.

F



Austin Schuh

unread,
Mar 10, 2023, 2:11:16 PM3/10/23
to Filip Filmar, Gregg Reynolds, bazel-discuss
You want a configuration transition.
https://bazel.build/extending/config That will let you switch
platforms, which will trigger Bazel to pick a new compiler/toolchain
suitable for the new platform.

Austin

Filip Filmar

unread,
Mar 10, 2023, 2:45:17 PM3/10/23
to Austin Schuh, Gregg Reynolds, bazel-discuss
On Fri, Mar 10, 2023 at 11:11 AM Austin Schuh <austin...@gmail.com> wrote:
On Fri, Mar 10, 2023 at 10:52 AM Filip Filmar <fil...@gmail.com> wrote:
[...] the simulator itself is written in C++ for the host and compiled by bazel; another program I want to compile is written in C++, but needs to be compiled for the target. But then, I need to feed the target's binary into the host's simulator.


You want a configuration transition.
https://bazel.build/extending/config  That will let you switch
platforms, which will trigger Bazel to pick a new compiler/toolchain
suitable for the new platform.

I can see how a transition seems to achieve what I want. I can see how I could apply one to a rule that I wrote. 

But I don't see how I can apply one to a built-in rule such as cc_binary or cc_library. What am I missing?

Thanks,
F


Austin Schuh

unread,
Mar 10, 2023, 4:06:27 PM3/10/23
to Filip Filmar, Gregg Reynolds, bazel-discuss
Check out https://github.com/aspect-build/bazel-lib/blob/main/lib/transitions.bzl.
You need a rule which does the transition, and you point it to your
binary. And then depend on the transition rule to pick up the
transioned binary.

We ended up hacking that target up a bit to do what we want, but you
might be able to use it as is. There's some good stuff in that repo.

Austin

Filip Filmar

unread,
Mar 11, 2023, 1:23:01 AM3/11/23
to Austin Schuh, Gregg Reynolds, bazel-discuss
On Fri, Mar 10, 2023 at 1:06 PM Austin Schuh <austin...@gmail.com> wrote:
Check out https://github.com/aspect-build/bazel-lib/blob/main/lib/transitions.bzl.
You need a rule which does the transition, and you point it to your
binary.  And then depend on the transition rule to pick up the
transioned binary.

We ended up hacking that target up a bit to do what we want, but you
might be able to use it as is.  There's some good stuff in that repo.

Hm, I tried, but I only get various different errors.  bazel 5.1.0 won't even consider a transition, 
and bazel 6.1.0 is OK with the transition, but does not actually transition.

I think the results exemplify my dilemma from the original post - I can't seem to be able to tell bazel that this one binary needs to be for a different target system.

Any ideas?

F

Filip Filmar

unread,
Mar 12, 2023, 7:36:01 AM3/12/23
to Austin Schuh, Gregg Reynolds, bazel-discuss
I finally pinpointed the correct set of incantations to make this happen.

Just as in https://groups.google.com/g/bazel-discuss/c/LulrD2m-Ao4, I was tripped up by the two different methods of toolchain declaration. The insights there, especially that last message that
established the correct invocation sequence from cc_toolchain_config to register_toolchain, was the key.

A working example is here: https://github.com/filmil/bazel-experiments/tree/main/rv5-tools. It seems to work with bazel 5.1.0, as in that repo.
I have not yet tested how it plays together with `--crosstool_top`. (while I might not want to use `--crosstool_top`, my upstreams happen to use it. Proceeding, fingers crossed.)

F

Reply all
Reply to author
Forward
0 new messages