'How to select a platform from the command line in Bazel

I'm messing around with Bazel example files. This one in particular on github, seems to select a tool by checking which platform is detected (specified?). If I execute:

bazel build :sh

it uses Linux. I deleted the default rule, and it still picked up Linux, so it looks like it autodetects the platform.

However, I'd like to know how to force it to use windows/something else. I know bazel has a --platforms command line argument, but I cannot for the life of me figure out how to pick the windows one. So, is it possible and if so, how do you select it?



Solution 1:[1]

Bazel effectively defaults to the target, host, and exec platforms all matching the local build system. There's some additional indirection to support migrating from older ways of specifying the platforms (--cpu, --crosstool_cop, etc), but that is being moved away from so I would not start with that system for a new project. Building with Platforms talks more about the migration.

If you have a platform and a toolchain for your desired target platform, you should be able to build by just passing --incompatible_enable_cc_toolchain_resolution --platforms=//my:platform. The Status section of that docs page lists flags for other languages.

You'll have to find a toolchain and find/write a platform for your target. The Configure C++ Toolchains tutorial walks through creating a toolchain. Writing a platform is pretty simple, something like this:

platform(
    name = "linux_armv7",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:armv7",
    ],
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brian Silverman