How to compile certain cc_binary's with a different toolchain #25810
Replies: 5 comments 5 replies
-
Check out this video: https://www.youtube.com/watch?v=CrfQaZjuj_Y The issues you are observing are due to you either trying (1) to compile for the host platform, that your target is incompatible with; or (2) you try to compile everything for your target, for which you don't have the appropriate python toolchain configured. Also note that, for example, And, as always, a minimal repro example would have been helpful, so take the above with a grain of salt. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick response!
Hmm...it definitely covers similar ground that the platforms documentation covers. But I think my usage is slightly more advanced; I'm trying to combine the ideas from platforms and toolchains such that specific toolchains are used when cross compiling.
Is it not possible to have one build where BOTH are built in one invocation of bazel? For example, I have cases where I need to cross compile a binary for a target architecture different than the host, but then test it on the host by feeding the target binary into another program compiled for the host to "test" the target binary. Also, I'm not trying to compile everything; I'm invoking
Whether I use Also forgot to mention another thing I tried; cc_library(
...
toolchains = [
"//bazel/toolchain/my_toolchain:my_toolchain",
], $ bazel build //example:foo
ERROR: example/BUILD.bazel:3:11: in toolchains attribute of cc_library rule //example:foo: '//bazel/toolchain/my_toolchain:my_toolchain' does not have mandatory providers: 'TemplateVariableInfo' I guess what I'm looking for, what would be ideal is something like: cc_binary(
name = "foo",
...
target_compatible_with = [
"@platforms//cpu:arm",
],
)
cc_binary(
name = "bar",
...
target_compatible_with = [
"@platforms//cpu:riscv",
],
) Then have BOTH foo and bar build from one invocation of bazel, via distinct toolchains. I can do exactly that by hacking something up using rules, but I'm basically re-implementing a poor man's cc_binary all over again. |
Beta Was this translation helpful? Give feedback.
-
If you want to build the same target for multiple platforms in a single build invocation, I think you want to look into "platform transitions". They allow you to build a binary This comes up in embedded development for example, as you may be aware - you have a library that you want to build for an embedded platform, but for testing you can also build it for use on the exec host. Unfortunately good examples are few and far between, but I think this may get you somewhere: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/transitions.md. I do have a repo where I use this approach, but sadly it's not publishable. If you find yourself building for the same set of platforms all the time, someone cooked this up: https://stackoverflow.com/questions/72681255/how-to-properly-use-bazel-transitions-for-multiarch-build |
Beta Was this translation helpful? Give feedback.
-
Ah, thanks for the tip. I was just reading the docs on transitions this morning, wondering if that's possibly what I'd need to use here. I had trouble wrapping my head around the syntax, but based on your confirmation it sounds like I should give them another shot. Thanks for confirming!
So I'd need to add aspect_bazel_lib as a dependency? I guess I can infer the usage from their tests, lol. I don't think they have an equivalent for cc_library, so I filed a feature request: bazel-contrib/bazel-lib#1065. Worst case, I could try to code up the rule locally, then go about upstreaming it.
Yeah, I can kind of see how that might work for me. Let me give it a shot. Thank you so much for the links and discussion! |
Beta Was this translation helpful? Give feedback.
-
Ah, this might be an issue for the ergonomics I seek:
So I'm guessing then I will still need a rule vs being able to just use
Thanks @fmeum , let me give that a shot.
I wired up exactly this, but I still hit that error relating to python.
it's as if as soon as I specify a non-host platform (explicilty, or via transitions), bazel errors because rules_python (which should be irrelevant for a cc_library) don't recognice my custom platform. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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
, andcc_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. I'm also trying to mix that with the platforms concept.
In my BUILD.bazel, I tried adding:
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:But then I observe failures due to Python:
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 thecc_library
rule to use a specific toolchain always?target_compatible_with
smells like that maybe might do what I want. Adding:But then building without
--platforms=//bazel/toolchain/my_toolchain:my_platform
fails since the host compiler is selected: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!
Beta Was this translation helpful? Give feedback.
All reactions