Custom Platform dependency without specifying --platform option

61 views
Skip to first unread message

Tushar Sharma

unread,
Dec 16, 2024, 2:05:05 AM (10 days ago) Dec 16
to bazel-discuss
Hi All,

I am trying to create a custom toolchain for gcc and clang and have created platforms for the same as well. So in my command line if I use

bazel build ... --platforms=@toochains//clang:clang_platform

bazel build ... --platforms=@toochains//gcc:gcc_platform

It uses clang and gcc respectively.

Now I want to specify this dependency in target itself So that if I have two targets 1 should be built with gcc and second should be built with clang without specifying --platforms option.
Is there a way to achieve the same.
I have tried specifying
target_compatible_with = [
        "@toolchains//clang:clang_platform",
        ]

But it gives me an error 
in target_compatible_with attribute of cc_binary rule //:hello-world: '@@toolchains//clang:clang_platform' does not have mandatory providers: 'ConstraintValueInfo'

Is there a way in bazel that we can achieve this

Best,
Tushar

Brian Silverman

unread,
Dec 18, 2024, 12:49:43 AM (8 days ago) Dec 18
to Tushar Sharma, bazel-discuss
You're looking for a user-defined transition, specially on //command_line_option:platforms. https://github.com/bazel-contrib/bazel-lib/blob/main/docs/transitions.md provides some simple general-purpose rules which do this for you. If you don't have an existing custom rule to integrate with, using one of those is the best option.

If you want to implement it yourself, the transition is trivial:

def _gcc_transition_impl(settings, attr):
    return {"//command_line_option:platforms": "@toochains//gcc:gcc_platform"}

gcc_transition = transition(
    implementation = _gcc_transition_impl,
    inputs = [],
    outputs = ["//command_line_option:platforms"],
)

Then you need to attach it to a rule. How you do that depends on what rule you end up attaching it to.

For reference (not the solution to your immediate question), target_compatible_with is indicating that the target can only be built for that platform, and it's either ignored (in command line wildcards) or an error if you try to build it for a different platform. Also you have to use the constraints instead of the platform itself. https://bazel.build/extending/platforms#skipping-incompatible-targets has a bit more detail.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/bazel-discuss/6769d0eb-0750-443e-8f58-59deeb92200cn%40googlegroups.com.

John Cater

unread,
Dec 18, 2024, 10:28:17 AM (8 days ago) Dec 18
to Brian Silverman, Tushar Sharma, bazel-discuss
Brian is correct about how to implement the transition for your rule. You should also look at (and consider using) the `platform_data` rule from rules_platform, which encapsulates this as a rule all ready for you to drop in.

I'd also want to consider why you want this to be a platform? There is an existing --compiler flag for use when selecting specific compilers: you can fairly easily hook it up to your custom cc_toolchain definitions (if there's not an example in rules_cc, I can help provide one).

Tushar Sharma

unread,
Dec 19, 2024, 5:36:57 AM (7 days ago) Dec 19
to bazel-discuss
Hi Brian and John,

Thanks for your prompt response.

Since I am new to bazel I am trying to explore different options that bazel provides and trying to understand the underlying design of bazel.
As Brian mentioned about user defined transitions, I was not aware of that and now I am trying to explore bazel transitions.

Also regarding the usage of platforms, I was not sure how to invoke different toolchains based on requirement in the same workspace and I came across some documentation or thread that with platform I can achieve this So I tried using the same. I will definitely try out the platform_data rule as John mentioned.

Also John It will be a great help if you can provide an example for --compiler flag.

My main objective here is lets say I have two targets If I trigger bazel build ... command it should automatically use specific toolchain for each target.

Best,
Tushar 

Reply all
Reply to author
Forward
0 new messages