Platforms, Constraints & select()
A build runs on one machine and may produce code for another. Bazel models both with platforms:
| Platform | Meaning | Flag |
|---|---|---|
| host | the machine where you typed bazel |
(detected) |
| execution | where actions run: your laptop, or a remote worker | --extra_execution_platforms |
| target | where the built code will run | --platforms |
A platform is just a named set of constraint values. Constraints are defined as data, not hard-coded in Bazel: the platforms module provides the common ones (@platforms//os:linux, @platforms//cpu:aarch64…), and you can define your own.
constraint_setting(name = "gpu") # a dimension
constraint_value(name = "cuda", constraint_setting = ":gpu") # its possible values
constraint_value(name = "no_gpu", constraint_setting = ":gpu")
platform(
name = "linux_arm64_cuda",
constraint_values = ["@platforms//os:linux", "@platforms//cpu:aarch64", ":cuda"],
)A custom constraint, select() and incompatible targets
This repository defines a tier constraint (free or pro edition), two platforms that differ only in that constraint, a target whose command depends on the platform through select(), and a target that only makes sense on the pro tier:
mkdir ws && cd ws
cat > MODULE.bazel <<'EOF'
bazel_dep(name = "rules_cc", version = "0.2.27")
bazel_dep(name = "platforms", version = "1.0.0")
EOF
mkdir -p plat app
cat > plat/BUILD.bazel <<'EOF'
constraint_setting(name = "tier", default_constraint_value = ":free")
constraint_value(name = "free", constraint_setting = ":tier", visibility = ["//visibility:public"])
constraint_value(name = "pro", constraint_setting = ":tier", visibility = ["//visibility:public"])
platform(name = "linux_free", parents = ["@platforms//host"], constraint_values = [":free"])
platform(name = "linux_pro", parents = ["@platforms//host"], constraint_values = [":pro"])
EOF
cat > app/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "app",
srcs = ["main.cc"],
local_defines = select({
"//plat:pro": ["EDITION=\\\"pro\\\"", "MAX_USERS=1000"],
"//conditions:default": ["EDITION=\\\"free\\\"", "MAX_USERS=5"],
}),
)
cc_binary(
name = "analytics",
srcs = ["analytics.cc"],
target_compatible_with = ["//plat:pro"],
)
EOF
printf '#include <cstdio>\nint main() { std::printf("edition %%s, up to %%d users\\n", EDITION, MAX_USERS); }\n' > app/main.cc
printf '#include <cstdio>\nint main() { std::puts("analytics ready"); }\n' > app/analytics.cc
echo "== host platform"
bazel run //app 2>/dev/null
echo "== --platforms=//plat:linux_pro"
bazel run --platforms=//plat:linux_pro //app 2>/dev/null
echo "== build everything on the free tier"
bazel build --platforms=//plat:linux_free --show_result=5 //app/... 2>&1 | grep -E 'skipped|up-to-date|bazel-bin|Build'
echo "== ask for the pro-only target explicitly"
bazel build --platforms=//plat:linux_free //app:analytics 2>&1 | grep -A2 'is incompatible'== host platform
edition free, up to 5 users
== --platforms=//plat:linux_pro
edition pro, up to 1000 users
== build everything on the free tier
WARNING: Build option --platforms has changed, discarding analysis cache (this can be expensive, see https://bazel.build/advanced/performance/iteration-speed).
Target //app:analytics was skipped
Target //app:app up-to-date:
bazel-bin/app/app
INFO: Build completed successfully, 4 total actions
== ask for the pro-only target explicitly
ERROR: Analysis of target '//app:analytics' failed; build aborted: Target //app:analytics is incompatible and cannot be built, but was explicitly requested.
Dependency chain:
//app:analytics (4e329e) <-- target platform (//plat:linux_free) didn't satisfy constraint //plat:pro
What happened:
parents = ["@platforms//host"]copies every constraint of the host platform (Linux, x86-64…) and adds or overrides ours.--platformsselects the target platform; nothing else about the build machine changed.select()keys can beconstraint_values directly. The default edition came fromdefault_constraint_value = ":free", which also applies to the host platform.target_compatible_withmarksanalyticsas incompatible with the free tier. Wildcard builds (//app/...) skip it (--show_result=5makes Bazel print the per-target result lines, including "was skipped"; by default it only lists results when one target was requested); asking for it explicitly is an error that shows the dependency chain. Anything depending on an incompatible target is incompatible too, so CI can build//...for every platform without hand-written exclusion lists.
config_setting: select() on flags
select() can also branch on build settings through config_setting: built-in flags (values = {"compilation_mode": "opt"}) or your own user-defined flags (Starlark build settings, usually from bazel_skylib):
mkdir ws && cd ws
cat > MODULE.bazel <<'EOF'
bazel_dep(name = "rules_cc", version = "0.2.27")
bazel_dep(name = "bazel_skylib", version = "1.9.0")
EOF
mkdir -p flags app
cat > flags/BUILD.bazel <<'EOF'
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
bool_flag(name = "telemetry", build_setting_default = False)
string_flag(name = "region", build_setting_default = "eu", values = ["eu", "us", "apac"])
config_setting(name = "telemetry_on", flag_values = {":telemetry": "True"}, visibility = ["//visibility:public"])
config_setting(name = "region_us", flag_values = {":region": "us"}, visibility = ["//visibility:public"])
config_setting(name = "optimized", values = {"compilation_mode": "opt"}, visibility = ["//visibility:public"])
EOF
cat > app/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "app",
srcs = ["main.cc"],
local_defines = select({"//flags:telemetry_on": ["TELEMETRY=1"], "//conditions:default": ["TELEMETRY=0"]})
+ select({"//flags:region_us": ["REGION=\\\"us\\\""], "//conditions:default": ["REGION=\\\"eu/apac\\\""]})
+ select({"//flags:optimized": ["BUILD=\\\"opt\\\""], "//conditions:default": ["BUILD=\\\"dev\\\""]}),
)
EOF
printf '#include <cstdio>\nint main() { std::printf("telemetry=%%d region=%%s build=%%s\\n", TELEMETRY, REGION, BUILD); }\n' > app/main.cc
bazel run //app 2>/dev/null
bazel run --//flags:telemetry --//flags:region=us -c opt //app 2>/dev/null
bazel run --//flags:region=mars //app 2>&1 | grep -o "Error setting //flags:region: .*"telemetry=0 region=eu/apac build=dev
telemetry=1 region=us build=opt
Error setting //flags:region: invalid value 'mars'. Allowed values are ["eu", "us", "apac"]
User-defined flags are passed as --//package:name=value (booleans as --//flags:telemetry / --no//flags:telemetry), are validated (values = [...]), and, like every option, become part of the configuration: each combination gets its own output directory and cache keys. Put commonly used combinations in .bazelrc configs (build:us --//flags:region=us).
Note: Use
select()for real variation (platforms, editions, opt vs dbg). Don't use it to toggle tests or debugging code that could simply be separate targets: every flag doubles the configurations CI might have to build.
Test yourself
What does --platforms=//plat:linux_pro change?
- The machine Bazel runs on
- The target platform the code is built for
- The execution platform for actions
- Nothing unless a toolchain is registered
A target has target_compatible_with = ["//plat:pro"]. What happens with bazel build //... on the free platform?
- The build fails
- The target is skipped as incompatible
- It builds anyway
- Bazel asks interactively
How do you pass a Starlark string flag //flags:region on the command line?
- --region=us
- --//flags:region=us
- --define region=us only
- In MODULE.bazel