Is there a way to skip platform incompatible targets during the build?

1,707 views
Skip to first unread message

Konstantin

unread,
Feb 9, 2022, 9:24:50 PM2/9/22
to bazel-discuss

We have Bazel build with several top level targets representing separate products which can be requested like “bazel build ProductA”.

Different targets have different compatibility requirements, so we (plan to) assign “target_compatible_with” attribute to the targets which have platform limitations.

The problem we hit is that “bazel build ProductA” does not combine with “target_compatible_with” attribute. As soon as in the transitive closure of the product there is a target incompatible with the requested platform it fails the build.

We see no way to skip incompatible targets in the transitive closure of the requested top level target.

Any ideas?

Thank you!

Konstantin

P.S I know incompatible targets can be skipped like this, but it only works for cquery while we need it for the build. Unfortunately provider IncompatiblePlatformProvider is not available in Starlark and “providers” function is only available in cquery, but not in build. 

Brian Silverman

unread,
Feb 9, 2022, 9:42:28 PM2/9/22
to Konstantin, bazel-discuss
It should "just work" if you set target_compatible_with. If you explicitly mention incompatible targets on the command line, it will fail with an error (because you probably wanted it built if you wrote it). If wildcards include incompatible targets, they will just be skipped, and all the compatible ones built/tested/etc. https://docs.bazel.build/versions/main/platforms.html#skipping-incompatible-targets has details.

That feature currently has one limitation I've run into: if no toolchains are available for a platform, that will cause an error before the incompatible target skipping takes effect. https://github.com/bazelbuild/bazel/pull/14096 is in-progress work to skip those targets before failing the build.

--
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 on the web visit https://groups.google.com/d/msgid/bazel-discuss/2cb79c40-0f2c-48bc-89dc-c9ceb933f57dn%40googlegroups.com.

Brian Silverman

unread,
Feb 9, 2022, 9:44:23 PM2/9/22
to Konstantin, bazel-discuss
(Sorry, sent too soon)

If some parts of ProductA are incompatible with some platforms, but you want the rest built, then use select to skip those dependencies on the appropriate platforms. Incompatible target skipping evaluates select statements for the appropriate platform.

Konstantin

unread,
Feb 9, 2022, 10:03:41 PM2/9/22
to bazel-discuss
Brian, thank you for the prompt response!
Yes I understand that we can use "select" to trim out platform incompatible branches of the product, that is what we already doing.
Unfortunate consequence of it is that platform compatibility information must be manually kept in sync between each target "target_compatible_with" attribute and the select statement in the consuming rule. This is obviously suboptimal and I am looking for the better solution.

Philipp Schrader

unread,
Feb 10, 2022, 2:44:57 AM2/10/22
to bazel-discuss
I suspect that the best option you have today is to use the cquery feature you linked in the original message.
Something like:

$ bazel build $(bazel cquery --output=starlark --starlark:file=example.cquery //path/to:ProductA)

where example.cquery is the sample from the page you linked.

The analysis graph should be shared between the query and the build.
The only awkward thing is that the command is very wordy.
A helper script would help with that.

Konstantin

unread,
Feb 10, 2022, 7:08:04 AM2/10/22
to bazel-discuss
Yes, Philipp, we considered approach with cquery filtering out incompatible targets before the build command, but it requires separate Bazel invocation for each target platform and unfortunately kills the chances of building multiple platforms in parallel in the future.

Greg Estren

unread,
Feb 10, 2022, 12:30:33 PM2/10/22
to bazel-discuss
As Philipp and Brian mentioned, is this about requesting ProductA explicitly vs. implicitly via a "bazel build //:all" call?

i..e is this a feature request to also make it possible to skip, not error on, explicitly requested incompatible targets?

Konstantin

unread,
Feb 16, 2022, 1:52:33 PM2/16/22
to bazel-discuss
Sorry, Greg, I just noticed your response. Yes, it looks like are all on the same page and the conclusion is - it is not possible with the current implementation and therefore it is a feature request  to skip, not error on, explicitly requested incompatible targets. For our purposes it would be sufficient to just do it globally (like with the command line flag), but I can imagine more elaborate cases when people may want to make a decision in their rule implementation functions how they want to handle particular incompatible dependencies. And it does not look hard - making  IncompatiblePlatformProvider accessible from Starlark is nearly all which is needed for it, as long as the rule depending on incompatible target is given a chance to execute at all. 

Konstantin 

Reply all
Reply to author
Forward
0 new messages