Skip to content

Let targets set what platforms they build for #6519

Closed

Description

@gregestren

Tracking issue on Bazel Configurability Roadmap

Goal:

  • Something like this works:
$ cat a/BUILD
cc_binary(name = "app_for_mac", platforms = ["//platforms:mac"])

$ bazel build //a:app_for_mac # Automatically builds for a Mac, no command line flags!

This syntax is theoretical; it may not look exactly like this. The point is rules can embed which platforms they target and all the correct toolchains get linked in without having to specify anything at the command line.

Furthermore, different rules needing different toolchains (e.g. $ bazel build //a:all) will all independently do the right thing.

This is v0 because it may have performance consequences. This also doesn't cover non-platform settings (like app version, enable/disable feature X), which will be covered elsewhere in the roadmap.

Activity

  1. added
    P3
    team-Configurability
    on Oct 25, 2018
  2. gregestren commented on Oct 25, 2018

    @gregestren
    ContributorAuthor

    Prerequisite work is still happening, so marking as a P3 now. This means we're not actively working on this yet. Will upgrade priority once the prereqs are out of the way.

  3. gregestren commented on Apr 26, 2019

    @gregestren
    ContributorAuthor

    April '19 update:

    Still blocked on prerequisites of basic platform & toolchain support. I'd still estimate a "second half of the year" time frame for starting to experiment with this, unless there's enough enthusiastic interest to jump on board even without concern about the scaling & caching concerns.

    Technically I think really interested users will be able to explore this in the next month or two by manually combining together platform/toolchain support and Starlark transitions.

  4. steel-exodus commented on May 5, 2019

    @steel-exodus

    @gregestren Hi Greg, can you elaborate on how we can achieve multi-platform builds via transitions?

    I'm building a small kernel using Bazel as the build engine. The boot-loader is being written targeting UEFI, which uses a PE COFF toolchain (basically targeting Windows via CLang/LLVM), and I want the kernel executables itself to be in ELF format (x86_64-none-elf).

    At the moment, I have no clear path to go about it, was thinking more in the lines of having two separate Bazel build steps, and then combining the data into a boot disk, but if I could do everything inside Bazel it will be so much easier.

  5. gregestren commented on May 6, 2019

    @gregestren
    ContributorAuthor

    Hey @wpieterse2825,

    Building in separate steps is indeed an option if it comes to that. A major inspiration of these new APIs is to not have to resort to that, so we're on the same page there.

    What BUILD rules are you using to represent the boot-loader and kernel executables and what flags are you using to switch the toolchain?

    I suspect we can get a clear path for your use case.

  6. steel-exodus commented on May 7, 2019

    @steel-exodus

    @gregestren Thanks for the reply Greg.

    At the moment they are just plain cc_binary and cc_library rules. You can have a look at the repository.

    I'm not doing anything fancy at the moment, just settings the --crosstool-top switch to point to my custom toolchain that can build UEFI applications. I still need to separate out the various pieces, I'm just not sure what is the correct direction forward. What would be you're opinion on how I should handle this? I saw the configuration and build road-map on the Bazel website, and I'm quite excited for the option to use platforms inside the cc_binary and cc_library rules.

    Also, this is just a pet project to build a small "DOS" like operating system, nothing serious.

  7. steel-exodus commented on May 7, 2019

    @steel-exodus

    Also, to add to the above, the uefi part needs to be built with the PE COFF toolchain, while the kernel and architecture parts I need in ELF format.

    Thinking of it, I’m using LLVM and LLD, maybe I should try playing with creating a macro wrapping the cc rules for the UEFI and Kernel parts, where the macro itselfs sets copts to specify what linker to use.

  8. katre commented on May 7, 2019

    @katre
    Collaborator

    @wpieterse2825: Have you taken a look at #7260? Once you write toolchain() targets for your existing cc_toolchain() declarations, you can use the flag to enable platform-based cc toolchain resolution now, which might be simpler than using copts and crosstool_top. All of the missing features of full multiplatform builds are still present, of course, but it moves you closer to that world.

  9. steel-exodus commented on May 7, 2019

    @steel-exodus

    Hi @katre, thanks for mentioning it.

    I did try and do that, defining the various architectures and firmware targets. You can have a look here. But when I try and build only with platforms, without specifying the --cpu parameter, it always tries to build with the MSVC toolchain, which obviously causes problems when linking the binary. I could not figure it out at the time, will give it a try again and let you know my results.

  10. steel-exodus commented on May 7, 2019

    @steel-exodus

    @katre I did what you suggested and it worked. Could finally remove the --cpu flag from my .bazelrc file as everything is now compiled with platforms. Thanks.

  11. katre commented on May 7, 2019

    @katre
    Collaborator

    Excellent, I'm glad it's working, we're trying hard to get this ready enough to enable by default.

  12. 21 remaining items

  13. sluongng commented on Nov 27, 2023

    @sluongng
    Contributor

    Hey folks, any update on support for multiple target platforms?
    Today we are seeing this warning

    WARNING: --platforms only supports a single target platform: using the first option //platforms:linux_x86_64
    

    and the code for this warning points back to this issue.

  14. katre commented on Nov 27, 2023

    @katre
    Collaborator

    Trying to get Bazel to directly support multiple values for --platforms is semantically very confusing, so we've made the decision to leave the specific semantics up to rule authors, who know their users' needs and ecosystems better.

    To that end, my concrete recommendations are:

    1. Rule authors should add platform and/or platforms attributes to appropriate rules, using the features from https://github.com/bazelbuild/proposals/blob/main/designs/2023-06-08-standard-platform-transitions.md, once we get those released (we're setting up the release process for that now, please imagine the sound of bureaucracy happening in the background).
    2. Rule authors can also add custom flags for multi-platform support, like --android_platforms, where that makes sense (for example, Android uses this to create a fat APK from a single android_binary target).
    3. Bazel should fix the --platforms flag to be less confusing (The platforms flag should be single-valued #19807):
      1. Make the existing multi-valued flag single-valued, but leave the name.
      2. Actually rename the flag to --platform (flag migrations are hard, sorry), leaving --platforms as a deprecated legacy name.
      3. Actually remove --platforms.
  15. Silic0nS0ldier commented on Nov 7, 2024

    @Silic0nS0ldier
    Contributor

    I think I spied a regression in the --platforms invalid options handling due to 81acd2a#diff-a437b5091032394bcb6af037a2c2ffc063f330a9092144f498edb2feda14d782R252.

    The check is performed in PlatformConfiguration.reportInvalidOptions which uses PlatformOptions, however the instance it receives has .platforms already normalised so extraneous platforms are silently ignored (previously a warning was emitted).

  16. katre commented on Nov 7, 2024

    @katre
    Collaborator

    Yes, there's a lot of confusion about the right way to handle this.

  17. github-actions commented on Jan 12, 2026

    @github-actions

    Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.

  18. added
    stale
    on Jan 12, 2026
  19. hisener commented on Jan 12, 2026

    @hisener
    Contributor

    I think we should keep this feature request open.

  20. sluongng commented on Jan 12, 2026

    @sluongng
    Contributor

    @hisener could you please elaborate more on your use case?

    This issue is very old and mostly out of date? The community is now in favor of using Bazel Transitions to set the target platform for each individual target.

    So let's say if you have a cc_binary that you want to build for a specific target, you can do something like this

    cc_binary(name = "foo", ..., tags = ["manual"])
    
    a_platform_transition_rule(src = ":foo")
    

    Popular rules, such as rules_go, offer platform transitions as a direct attribute for more convenience. There are also things like with_cfg to scope down the transitions easier.

    So I don't see a reason to keep this one open.

  21. hisener commented on Jan 12, 2026

    @hisener
    Contributor

    When I subscribed to this issue, our use case was using different JDKs for some targets. It's not possible out of the box. We ended up using with_cfg as you mentioned. However, there are a bunch of gotchas, e.g., env_inherit doesn't work in some cases.

    Thus, I think having something built-in to define a target's intended platform is still valuable.

  22. fmeum commented on Jan 12, 2026

    @fmeum
    Collaborator

    However, there are a bunch of gotchas, e.g., env_inherit doesn't work in some cases.

    with_cfg should support env_inherit and generally aims to shield you from such transition gotchas. Could you file an issue for this over on the with_cfg.bzl repo?

  23. hisener commented on Jan 12, 2026

    @hisener
    Contributor

    with_cfg should support env_inherit and generally aims to shield you from such transition gotchas. Could you file an issue for this over on the with_cfg.bzl repo?

    Yes, it's on my list for a while. 😞 I'll try to find some time.

    This is what I wrote in October:

    They have a special treatment for env_inherit, but it doesn’t work in our usage because we wrap a function not the java_test native rule (kind:<function _dd_java_test from //rules/java:defs.bzl>). See also https://github.com/fmeum/with_cfg.bzl/blob/178f719f9ffdab3849cdbda259e8debf46c61dda/with_cfg/private/wrapper.bzl#L115-L121.

  24. fmeum commented on Jan 12, 2026

    @fmeum
    Collaborator

    env_inherit on java_test is backed by RunEnvironmentInfo in Bazel 7 and higher and with_cfg forwards this provider, so I would expect that to work without the hack necessary for native java_test.

  25. sluongng commented on Jan 12, 2026

    @sluongng
    Contributor

    I think since the direction for configuring targeted platforms has been decided to be using transitions, and viable transition machinery do exist today, it's safe to close this issue to reduce noise (some information from 2018-2019 is no longer applicable to 2026). We can iron out the UX in newer issues that are more focused on the exact problem rather than this broad topic.

    There are some remaining TODOs from this issue, though:

    • Actually rename the flag to --platform (flag migrations are hard, sorry), leaving --platforms as a deprecated legacy name.

    • Actually remove --platforms.

    But other than that, we should move the with_cfg conversation to a different place and close this one.

  26. gregestren commented on Jan 12, 2026

    @gregestren
    ContributorAuthor

    Agreed on closing for more focused requests. (and thanks for supporting that conclusion @hisener ).

    Acknowledged that there are various ways to kind of achieve this functionality and a canonical best practice API remains elusive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3staleteam-Configurabilitytype: feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions