Skip to content

Commit

Permalink
Use the host_platform from @platforms in Bazel
Browse files Browse the repository at this point in the history
* Upgrade to `platforms` 0.0.9
* `--host_platform` now defaults to `@bazel_tools//tools:host_platform`, which is an alias of `@platforms//host`
* `local_config_platform` (the repo rule) now just outputs a thin wrapper; `@local_config_platform//:host` is an alias of `@platforms//host`, and `@local_config_platform//:constraints.bzl` re-exports `@platforms//host:constraints.bzl`
* Removed all test mocks of `local_config_platform`. A follow-up will guard prod usage behind a flag.

Work towards #8766 and #18285.

PiperOrigin-RevId: 620316973
Change-Id: I6593f62569f31faee69e88a520a0f7f42009e05d
  • Loading branch information
Wyverald authored and copybara-github committed Mar 29, 2024
1 parent bb2e118 commit 74c8ca4
Show file tree
Hide file tree
Showing 35 changed files with 285 additions and 376 deletions.
2 changes: 1 addition & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ platform(
constraint_values = [
":highcpu_machine",
],
parents = ["@local_config_platform//:host"],
parents = ["@platforms//host"],
)

platform(
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bazel_dep(name = "rules_license", version = "0.0.7")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf")
bazel_dep(name = "grpc", version = "1.48.1.bcr.1", repo_name = "com_github_grpc_grpc")
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "rules_pkg", version = "0.9.1")
bazel_dep(name = "stardoc", version = "0.5.6", repo_name = "io_bazel_skydoc")
bazel_dep(name = "zstd-jni", version = "1.5.2-3.bcr.1")
Expand Down
86 changes: 60 additions & 26 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ log "Building output/bazel"
# host.
bazel_build "src:bazel_nojdk${EXE_EXT}" \
--action_env=PATH \
--host_platform=@local_config_platform//:host \
--platforms=@local_config_platform//:host \
--host_platform=@platforms//host \
--platforms=@platforms//host \
|| fail "Could not build Bazel"
bazel_bin_path="$(get_bazel_bin_path)/src/bazel_nojdk${EXE_EXT}"
[ -e "$bazel_bin_path" ] \
Expand Down
3 changes: 2 additions & 1 deletion site/en/concepts/platforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ Project owners should define explicit
they want to build for. These are then triggered with `--platforms`.

When `--platforms` isn't set, Bazel defaults to a `platform` representing the
local build machine. This is auto-generated at `@local_config_platform//:host`
local build machine. This is auto-generated at `@platforms//host` (aliased as
`@bazel_tools//tools:host_platform`)
so there's no need to explicitly define it. It maps the local machine's `OS`
and `CPU` with `constraint_value`s declared in
[`@platforms`](https://github.com/bazelbuild/platforms){: .external}.
Expand Down
13 changes: 7 additions & 6 deletions site/en/extending/platforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,24 @@ systems. These are all located in
[https://github.com/bazelbuild/platforms](https://github.com/bazelbuild/platforms){: .external}.

Bazel ships with the following special platform definition:
`@local_config_platform//:host`. This is the autodetected host platform value -
`@platforms//host` (aliased as `@bazel_tools//tools:host_platform`). This is the
autodetected host platform value -
represents autodetected platform for the system Bazel is running on.

## Specifying a platform for a build {:#specifying-build-platform}

You can specify the host and target platforms for a build using the following
command-line flags:

* `--host_platform` - defaults to `@local_config_platform//:host`
* `@local_config_platform` is a repo backed by a repo rule that detects the
host OS and CPU and writes the platform target.
* It also creates `@local_config_platform//:constraints.bzl`, which exposes
* `--host_platform` - defaults to `@bazel_tools//tools:host_platform`
* This target is aliased to `@platforms//host`, which is backed by a repo
rule that detects the host OS and CPU and writes the platform target.
* There's also `@platforms//host:constraints.bzl`, which exposes
an array called `HOST_CONSTRAINTS`, which can be used in other BUILD and
Starlark files.
* `--platforms` - defaults to the host platform
* This means that when no other flags are set,
`@local_config_platform//:host` is the target platform.
`@platforms//host` is the target platform.
* If `--host_platform` is set and not `--platforms`, the value of
`--host_platform` is both the host and target platform.

Expand Down
2 changes: 1 addition & 1 deletion src/MODULE.tools
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bazel_dep(name = "rules_proto", version = "4.0.0")
bazel_dep(name = "rules_python", version = "0.22.1")

bazel_dep(name = "buildozer", version = "6.4.0.2")
bazel_dep(name = "platforms", version = "0.0.7")
bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "protobuf", version = "3.19.6", repo_name = "com_google_protobuf")
bazel_dep(name = "zlib", version = "1.3")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static boolean platformIsDefault(Label platform) {
name = "host_platform",
oldName = "experimental_host_platform",
converter = EmptyToNullLabelConverter.class,
defaultValue = "@local_config_platform//:host",
defaultValue = "@bazel_tools//tools:host_platform",
documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
effectTags = {
OptionEffectTag.AFFECTS_OUTPUTS,
Expand Down
Loading

0 comments on commit 74c8ca4

Please sign in to comment.