Skip to content

Commit 431a39b

Browse files
committed
Replace --define=EXECUTOR=remote with toolchains and select
This allows for cross-platform builds to work seamlessly without a need to manually toggle between prebuilt and from source toolchains.
1 parent d986163 commit 431a39b

File tree

12 files changed

+156
-60
lines changed

12 files changed

+156
-60
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ alias(
164164

165165
config_setting(
166166
name = "remote",
167+
deprecation = "No longer used by Bazel and may be removed in the future. Migrate to toolchains or define your own version of this setting.",
167168
values = {"define": "EXECUTOR=remote"},
168169
visibility = ["//visibility:public"],
169170
)

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

tools/BUILD.tools

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
12
load(":build_defs.bzl", "BZLMOD_ENABLED")
23

34
package(default_visibility = ["//visibility:public"])
@@ -13,6 +14,12 @@ platform(
1314
visibility = ["//visibility:private"],
1415
)
1516

17+
config_setting(
18+
name = "matches_host_constraints",
19+
constraint_values = HOST_CONSTRAINTS,
20+
visibility = ["//tools:__subpackages__"],
21+
)
22+
1623
# All bzl files in the built in repo '@bazel_tools'.
1724
filegroup(
1825
name = "bzl_srcs",

tools/build_defs.bzl

Lines changed: 58 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,57 @@ 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(l) for l in HOST_CONSTRAINTS]
57+
58+
def _single_binary_toolchain_rule_impl(ctx):
59+
return platform_common.ToolchainInfo(
60+
binary = ctx.attr.binary,
61+
)
62+
63+
_single_binary_toolchain_rule = rule(
64+
implementation = _single_binary_toolchain_rule_impl,
65+
attrs = {
66+
"binary": attr.label(allow_single_file = True),
67+
},
68+
)
69+
70+
def single_binary_toolchain(
71+
*,
72+
name,
73+
toolchain_type,
74+
binary = None,
75+
target_compatible_with = [],
76+
exec_compatible_with = []):
77+
"""Declares a toolchain together with its implementation for an optional single binary."""
78+
impl_name = name + "_impl"
79+
80+
_single_binary_toolchain_rule(
81+
name = impl_name,
82+
binary = binary,
83+
# Avoid eager loading of the binary, which may come from a remote
84+
# repository, in wildcard builds.
85+
tags = ["manual"],
86+
visibility = ["//visibility:private"],
87+
)
88+
89+
native.toolchain(
90+
name = name,
91+
toolchain_type = toolchain_type,
92+
toolchain = ":" + impl_name,
93+
target_compatible_with = target_compatible_with,
94+
exec_compatible_with = exec_compatible_with,
95+
visibility = ["//visibility:private"],
96+
)
97+
98+
def _current_toolchain_base_impl(ctx, *, toolchain_type):
99+
return DefaultInfo(files = depset([ctx.toolchains[toolchain_type].binary]))
100+
101+
def _make_current_toolchain_rule(toolchain_type):
102+
return rule(
103+
implementation = lambda ctx: _current_toolchain_base_impl(ctx, toolchain_type = toolchain_type),
104+
toolchains = [toolchain_type],
105+
)
106+
107+
current_launcher_binary = _make_current_toolchain_rule("//tools/launcher:launcher_toolchain_type")
108+
current_launcher_maker_binary = _make_current_toolchain_rule("//tools/launcher:launcher_maker_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: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,73 @@
1+
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
2+
load(
3+
"//tools:build_defs.bzl",
4+
"IS_HOST_WINDOWS",
5+
"current_launcher_binary",
6+
"current_launcher_maker_binary",
7+
"single_binary_toolchain",
8+
)
9+
110
package(default_visibility = ["//visibility:public"])
211

3-
filegroup(
4-
name = "launcher_windows",
5-
srcs = select({
6-
"//src/conditions:remote": ["//src/tools/launcher:launcher"],
7-
"//conditions:default": ["launcher.exe"],
8-
}),
9-
)
10-
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-
}),
19-
)
20-
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-
}),
29-
)
30-
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-
}),
12+
current_launcher_binary(name = "launcher")
13+
14+
current_launcher_maker_binary(name = "launcher_maker")
15+
16+
# WARNING: These toolchain types and their associated toolchains only exist for
17+
# the purposes of rulesets formerly included in Bazel itself and may change or
18+
# be removed at any time.
19+
20+
toolchain_type(
21+
name = "launcher_toolchain_type",
22+
visibility = ["//visibility:private"],
23+
)
24+
25+
toolchain_type(
26+
name = "launcher_maker_toolchain_type",
27+
visibility = ["//visibility:private"],
28+
)
29+
30+
# Toolchains are prefixed with a number to ensure that their order of definition
31+
# matches their precedence in the toolchain resolution process when registered
32+
# with a wildcard pattern (which sorts by name).
33+
# TODO(#19587): Make all prebuilt binaries available in external repos and add
34+
# toolchains for them below so that cross-platform builds can use them without
35+
# needing to build from source.
36+
IS_HOST_WINDOWS and single_binary_toolchain(
37+
name = "1_prebuilt_launcher",
38+
binary = "launcher.exe",
39+
target_compatible_with = HOST_CONSTRAINTS,
40+
toolchain_type = ":launcher_toolchain_type",
41+
)
42+
43+
single_binary_toolchain(
44+
name = "2_source_launcher_toolchain",
45+
binary = "//src/tools/launcher",
46+
target_compatible_with = ["@platforms//os:windows"],
47+
toolchain_type = ":launcher_toolchain_type",
48+
)
49+
50+
single_binary_toolchain(
51+
name = "3_no_launcher_toolchain",
52+
toolchain_type = ":launcher_toolchain_type",
53+
)
54+
55+
IS_HOST_WINDOWS and single_binary_toolchain(
56+
name = "1_prebuilt_launcher_maker",
57+
binary = "launcher_maker.exe",
58+
exec_compatible_with = HOST_CONSTRAINTS,
59+
target_compatible_with = ["@platforms//os:windows"],
60+
toolchain_type = ":launcher_maker_toolchain_type",
61+
)
62+
63+
single_binary_toolchain(
64+
name = "2_source_launcher_maker_toolchain",
65+
binary = "//src/tools/launcher:launcher_maker",
66+
target_compatible_with = ["@platforms//os:windows"],
67+
toolchain_type = ":launcher_maker_toolchain_type",
68+
)
69+
70+
single_binary_toolchain(
71+
name = "3_no_launcher_maker_toolchain",
72+
toolchain_type = ":launcher_maker_toolchain_type",
3773
)

tools/zip/BUILD.tools

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package(default_visibility = ["//visibility:public"])
33
filegroup(
44
name = "zipper",
55
srcs = select({
6-
"//src/conditions:remote": ["//third_party/ijar:zipper"],
7-
"//conditions:default": glob(["zipper/*"]),
6+
"//tools:matches_host_constraints": glob(["zipper/*"]),
7+
"//conditions:default": ["//third_party/ijar:zipper"],
88
}),
99
)
1010

0 commit comments

Comments
 (0)