Skip to content

Commit e819495

Browse files
fmeumcopybara-github
authored andcommitted
Replace --define=EXECUTOR=remote with toolchains and selects
This allows for cross-platform builds to work seamlessly without a need to manually toggle between prebuilt and from source toolchains. As a positive side effect, this change also paves the way for dropping a dependency on an exec-configured C++ toolchain of Java and Python targets built for non-Windows platforms. Rulesets will need to migrate to using the launcher maker toolchain instead of the `launcher_maker` target. rules_shell wasn't affected by this as it already defined its own launcher toolchain. Work towards #19587 Closes #26019. PiperOrigin-RevId: 758697189 Change-Id: Ibfa272390056963ef35301614520fd38d018210e
1 parent df5d96f commit e819495

File tree

13 files changed

+188
-52
lines changed

13 files changed

+188
-52
lines changed

site/en/remote/sandbox.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ build:docker-sandbox --javabase=<...>
7171
build:docker-sandbox --crosstool_top=<...>
7272
build:docker-sandbox --experimental_docker_image=<...>
7373
build:docker-sandbox --spawn_strategy=docker --strategy=Javac=docker --genrule_strategy=docker
74-
build:docker-sandbox --define=EXECUTOR=remote
7574
build:docker-sandbox --experimental_docker_verbose
7675
build:docker-sandbox --experimental_enable_docker_sandbox
7776
```

src/MODULE.tools

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ bazel_dep(name = "rules_shell", version = "0.3.0")
4545
# add rules_android
4646
# add apple_support
4747

48-
register_toolchains("//tools/test:all")
48+
register_toolchains(
49+
"//tools/launcher:all",
50+
"//tools/test:all",
51+
)

src/conditions/BUILD

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,6 @@ config_setting(
220220
visibility = ["//visibility:public"],
221221
)
222222

223-
config_setting(
224-
name = "remote",
225-
values = {"define": "EXECUTOR=remote"},
226-
visibility = ["//visibility:public"],
227-
)
228-
229223
config_setting(
230224
name = "debian_build",
231225
values = {

src/conditions/BUILD.tools

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ config_setting(
145145

146146
config_setting(
147147
name = "host_windows_x64_constraint",
148+
deprecation = "No longer used by Bazel and will be removed in the future. Migrate to toolchains or define your own version of this setting.",
148149
values = {"host_cpu": "x64_windows"},
149150
)
150151

151152
config_setting(
152153
name = "host_windows_arm64_constraint",
154+
deprecation = "No longer used by Bazel and will be removed in the future. Migrate to toolchains or define your own version of this setting.",
153155
values = {"host_cpu": "arm64_windows"},
154156
)
155157

@@ -159,11 +161,13 @@ alias(
159161
":host_windows_arm64_constraint": ":host_windows_arm64_constraint",
160162
"//conditions:default": ":host_windows_x64_constraint",
161163
}),
164+
deprecation = "No longer used by Bazel and will be removed in the future. Migrate to toolchains or define your own version of this setting.",
162165
visibility = ["//visibility:public"],
163166
)
164167

165168
config_setting(
166169
name = "remote",
170+
deprecation = "No longer used by Bazel and will be removed in the future. Migrate to toolchains or define your own version of this setting.",
167171
values = {"define": "EXECUTOR=remote"},
168172
visibility = ["//visibility:public"],
169173
)

src/test/py/bazel/py_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ def _RunRemoteBazel(self, args):
165165
'--strategy=Javac=remote',
166166
'--strategy=Closure=remote',
167167
'--genrule_strategy=remote',
168-
'--define=EXECUTOR=remote',
169168
'--remote_executor=grpc://localhost:' + str(self._worker_port),
170169
'--remote_cache=grpc://localhost:' + str(self._worker_port),
171170
'--remote_timeout=3600',

src/test/py/bazel/windows_remote_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def _RunRemoteBazel(self, args, env_remove=None, env_add=None):
3030
'--strategy=Javac=remote',
3131
'--strategy=Closure=remote',
3232
'--genrule_strategy=remote',
33-
'--define=EXECUTOR=remote',
3433
'--remote_executor=grpc://localhost:' + str(self._worker_port),
3534
'--remote_cache=grpc://localhost:' + str(self._worker_port),
3635
'--remote_timeout=3600',

src/test/shell/bazel/bazel_java_test.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,8 +1582,7 @@ EOF
15821582
function test_build_hello_world_with_remote_embedded_tool_targets() {
15831583
write_hello_library_files
15841584

1585-
bazel build //java/main:main_deploy.jar --define EXECUTOR=remote \
1586-
&> $TEST_log || fail "build failed"
1585+
bazel build //java/main:main_deploy.jar &> $TEST_log || fail "build failed"
15871586
}
15881587

15891588

src/test/shell/integration/ui_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ EOF
747747
expect_log "ERROR: 'run' only works with tests with one shard"
748748
# If we would print this again after the run failed, we would overwrite the
749749
# error message above.
750-
expect_log_n "INFO: Build completed successfully, [4567] total actions" 1
750+
expect_log_n "INFO: Build completed successfully, [4-9] total actions" 1
751751
}
752752

753753
function test_exit_code_reported() {

tools/build_defs.bzl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
"""Utility for compiling at Java 8."""
1616

17+
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
18+
19+
visibility("//tools/...")
20+
1721
_java_language_version_8_transition = transition(
1822
implementation = lambda settings, attr: {
1923
"//command_line_option:java_language_version": "8",
@@ -48,3 +52,65 @@ def transition_java_language_8_filegroup(name, files, visibility):
4852
)
4953

5054
BZLMOD_ENABLED = str(Label("@bazel_tools//:foo")).startswith("@@")
55+
56+
IS_HOST_WINDOWS = Label("@platforms//os:windows") in [Label(label) for label in HOST_CONSTRAINTS]
57+
58+
def _single_binary_toolchain_rule_impl(ctx):
59+
return platform_common.ToolchainInfo(
60+
binary = ctx.file.binary,
61+
)
62+
63+
_single_binary_toolchain_rule = rule(
64+
implementation = _single_binary_toolchain_rule_impl,
65+
attrs = {
66+
"binary": attr.label(
67+
allow_single_file = True,
68+
mandatory = True,
69+
),
70+
},
71+
)
72+
73+
def single_binary_toolchain(
74+
*,
75+
name,
76+
toolchain_type,
77+
binary = None,
78+
target_compatible_with = [],
79+
exec_compatible_with = []):
80+
"""Declares a toolchain together with its implementation for an optional single binary."""
81+
impl_name = name + "_impl"
82+
83+
_single_binary_toolchain_rule(
84+
name = impl_name,
85+
binary = binary,
86+
# Avoid eager loading of the binary, which may come from a remote
87+
# repository, in wildcard builds.
88+
tags = ["manual"],
89+
visibility = ["//visibility:private"],
90+
)
91+
92+
native.toolchain(
93+
name = name,
94+
toolchain_type = toolchain_type,
95+
toolchain = ":" + impl_name,
96+
target_compatible_with = target_compatible_with,
97+
exec_compatible_with = exec_compatible_with,
98+
visibility = ["//visibility:private"],
99+
)
100+
101+
def _current_toolchain_base_impl(ctx, *, toolchain_type):
102+
executable = ctx.actions.declare_file(ctx.label.name)
103+
ctx.actions.symlink(
104+
output = executable,
105+
target_file = ctx.toolchains[toolchain_type].binary,
106+
)
107+
return DefaultInfo(executable = executable)
108+
109+
def _make_current_toolchain_rule(toolchain_type):
110+
return rule(
111+
implementation = lambda ctx: _current_toolchain_base_impl(ctx, toolchain_type = toolchain_type),
112+
toolchains = [toolchain_type],
113+
executable = True,
114+
)
115+
116+
current_launcher_binary = _make_current_toolchain_rule("//tools/launcher:launcher_toolchain_type")

tools/def_parser/BUILD.tools

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
package(default_visibility = ["//visibility:public"])
1+
load("//tools:build_defs.bzl", "IS_HOST_WINDOWS")
22

3-
filegroup(
4-
name = "def_parser_windows",
5-
srcs = select({
6-
"//src/conditions:remote": ["//third_party/def_parser:def_parser"],
7-
"//conditions:default": ["def_parser.exe"],
8-
}),
9-
)
3+
package(default_visibility = ["//visibility:public"])
104

5+
# This should use a toolchain setup like the one for //tools/launcher, but that
6+
# doesn't provide a good way to bootstrap the def_parser from source build and
7+
# targets such as `--malloc`, which must not depend on def_parser. Instead,
8+
# the cc_* rules use a computed default and exclude tags. Since def_parser is
9+
# only used when targeting Windows and itself only runs on Windows, we only
10+
# match on the target platform here (which is the exec platform after the exec
11+
# transition applied by the implicit dep edge from cc_* rules).
12+
# TODO: Consider improving the situation by wrapping the targets with exclusions
13+
# with transitions.
1114
filegroup(
1215
name = "def_parser",
1316
srcs = select({
14-
"//src/conditions:host_windows": ["def_parser_windows"],
15-
"//conditions:default": [
16-
"no_op.bat",
17-
],
17+
"@platforms//os:windows": ["def_parser.exe"] if IS_HOST_WINDOWS else ["//third_party/def_parser:def_parser"],
18+
"//conditions:default": ["no_op.bat"],
1819
}),
1920
)

tools/launcher/BUILD.tools

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,83 @@
1+
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
2+
load(
3+
"//tools:build_defs.bzl",
4+
"IS_HOST_WINDOWS",
5+
"current_launcher_binary",
6+
"single_binary_toolchain",
7+
)
8+
19
package(default_visibility = ["//visibility:public"])
210

11+
# WARNING: These targets and toolchain types only exist for the purposes of
12+
# rulesets formerly included in Bazel itself and may change or be removed at any
13+
# time.
14+
15+
current_launcher_binary(name = "launcher")
16+
17+
# DEPRECATED: Use the `:launcher_maker_toolchain_type` toolchain instead to
18+
# avoid an unnecessary dependency on a C++ toolchain when building for a
19+
# non-Windows platform.
320
filegroup(
4-
name = "launcher_windows",
21+
name = "launcher_maker",
522
srcs = select({
6-
"//src/conditions:remote": ["//src/tools/launcher:launcher"],
7-
"//conditions:default": ["launcher.exe"],
23+
":is_host": ["launcher_maker.exe" if IS_HOST_WINDOWS else "//src/tools/launcher:launcher_maker"],
24+
"//conditions:default": ["//src/tools/launcher:launcher_maker"],
825
}),
926
)
1027

11-
filegroup(
12-
name = "launcher",
13-
srcs = select({
14-
"//src/conditions:host_windows": [":launcher_windows"],
15-
"//conditions:default": [
16-
"//src/tools/launcher:launcher",
17-
],
18-
}),
28+
config_setting(
29+
name = "is_host",
30+
constraint_values = HOST_CONSTRAINTS,
31+
visibility = ["//visibility:private"],
1932
)
2033

21-
filegroup(
22-
name = "launcher_maker",
23-
srcs = select({
24-
"//src/conditions:host_windows": [":launcher_maker_windows"],
25-
"//conditions:default": [
26-
"//src/tools/launcher:launcher_maker",
27-
],
28-
}),
34+
toolchain_type(name = "launcher_toolchain_type")
35+
36+
toolchain_type(name = "launcher_maker_toolchain_type")
37+
38+
# Toolchains are prefixed with a number to ensure that their order of definition
39+
# matches their precedence in the toolchain resolution process when registered
40+
# with a wildcard pattern (which sorts by name).
41+
# TODO(#19587): Make all prebuilt binaries available in external repos and add
42+
# toolchains for them below so that cross-platform builds can use them without
43+
# needing to build from source.
44+
IS_HOST_WINDOWS and single_binary_toolchain(
45+
name = "1_prebuilt_launcher",
46+
binary = "launcher.exe",
47+
target_compatible_with = HOST_CONSTRAINTS,
48+
toolchain_type = ":launcher_toolchain_type",
2949
)
3050

31-
filegroup(
32-
name = "launcher_maker_windows",
33-
srcs = select({
34-
"//src/conditions:remote": ["//src/tools/launcher:launcher_maker"],
35-
"//conditions:default": ["launcher_maker.exe"],
36-
}),
51+
single_binary_toolchain(
52+
name = "2_source_launcher_toolchain",
53+
binary = "//src/tools/launcher",
54+
target_compatible_with = ["@platforms//os:windows"],
55+
toolchain_type = ":launcher_toolchain_type",
56+
)
57+
58+
single_binary_toolchain(
59+
name = "3_no_launcher_toolchain",
60+
binary = "empty.sh",
61+
toolchain_type = ":launcher_toolchain_type",
62+
)
63+
64+
IS_HOST_WINDOWS and single_binary_toolchain(
65+
name = "1_prebuilt_launcher_maker",
66+
binary = "launcher_maker.exe",
67+
exec_compatible_with = HOST_CONSTRAINTS,
68+
target_compatible_with = ["@platforms//os:windows"],
69+
toolchain_type = ":launcher_maker_toolchain_type",
70+
)
71+
72+
single_binary_toolchain(
73+
name = "2_source_launcher_maker_toolchain",
74+
binary = "//src/tools/launcher:launcher_maker",
75+
target_compatible_with = ["@platforms//os:windows"],
76+
toolchain_type = ":launcher_maker_toolchain_type",
77+
)
78+
79+
single_binary_toolchain(
80+
name = "3_no_launcher_maker_toolchain",
81+
binary = "empty.sh",
82+
toolchain_type = ":launcher_maker_toolchain_type",
3783
)

tools/launcher/empty.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright 2016 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# Tests the examples provided in Bazel
18+
#

tools/zip/BUILD.tools

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
2+
13
package(default_visibility = ["//visibility:public"])
24

35
filegroup(
46
name = "zipper",
57
srcs = select({
6-
"//src/conditions:remote": ["//third_party/ijar:zipper"],
7-
"//conditions:default": glob(["zipper/*"]),
8+
":matches_host_constraints": glob(["zipper/*"]),
9+
"//conditions:default": ["//third_party/ijar:zipper"],
810
}),
911
)
1012

1113
alias(
1214
name = "unzip_fdo",
1315
actual = "zipper",
1416
)
17+
18+
config_setting(
19+
name = "matches_host_constraints",
20+
constraint_values = HOST_CONSTRAINTS,
21+
visibility = ["//visibility:private"],
22+
)

0 commit comments

Comments
 (0)