Skip to content

Commit 7c1ce31

Browse files
committed
Use the launcher_maker toolchain if available
This avoids an unnecessary dependency on a C++ toolchain matching the target platform when not building for Windows.
1 parent 4ce6ca9 commit 7c1ce31

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

.bazelci/presubmit.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ tasks:
6060
build_targets: *build_targets
6161
test_targets: *test_targets
6262
test_flags:
63-
- "--test_tag_filters=-min_bazel_8"
63+
- "--test_tag_filters=-min_bazel_8,-min_bazel_9"
6464
# Bazel 8+
6565
build_and_test:
6666
name: "Bazel {modern_bazel}"
@@ -76,7 +76,7 @@ tasks:
7676
build_targets: *build_targets_bazel6
7777
test_targets: *test_targets_bazel6
7878
test_flags:
79-
- "--test_tag_filters=-min_bazel_7,-min_bazel_8"
79+
- "--test_tag_filters=-min_bazel_7,-min_bazel_8,-min_bazel_9"
8080
ubuntu2004_integration_bazel6:
8181
name: "Integration w/ Bazel 6.5.0"
8282
bazel: 6.5.0

MODULE.bazel

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ module(
77

88
bazel_dep(name = "platforms", version = "0.0.11")
99
bazel_dep(name = "rules_cc", version = "0.0.15")
10-
bazel_dep(name = "bazel_features", version = "1.28.0")
11-
archive_override(
12-
module_name = "bazel_features",
13-
integrity = "sha256-SOPLvKDy+RN7GHKN8eFjQ+58Wx4Isj+vcXoUluBqxLo=",
14-
strip_prefix = "bazel_features-59915eb2ca215c7b2266c83c49bb7522a5b6737f",
15-
urls = ["https://github.com/bazel-contrib/bazel_features/archive/59915eb2ca215c7b2266c83c49bb7522a5b6737f.zip"],
16-
)
10+
bazel_dep(name = "bazel_features", version = "1.30.0")
1711

1812
bazel_dep(name = "bazel_skylib", version = "1.6.1")
1913
bazel_dep(name = "protobuf", version = "27.0", repo_name = "com_google_protobuf")

java/bazel/rules/bazel_java_binary.bzl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Bazel java_binary rule"""
1515

16+
load("@bazel_features//:features.bzl", "bazel_features")
1617
load("@bazel_skylib//lib:paths.bzl", "paths")
1718
load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain")
1819
load("//java/common:java_semantics.bzl", "semantics")
@@ -202,6 +203,14 @@ def _get_executable(ctx):
202203

203204
return ctx.actions.declare_file(executable_name)
204205

206+
_LAUNCHER_MAKER_TOOLCHAIN_TYPE = "@bazel_tools//tools/launcher:launcher_maker_toolchain_type"
207+
_LAUNCHER_MAKER_TOOLCHAIN = config_common.toolchain_type(_LAUNCHER_MAKER_TOOLCHAIN_TYPE, mandatory = True)
208+
209+
def _find_launcher_maker(ctx):
210+
if bazel_features.rules._has_launcher_maker_toolchain:
211+
return ctx.toolchains[_LAUNCHER_MAKER_TOOLCHAIN_TYPE].binary
212+
return ctx.attr._windows_launcher_maker
213+
205214
def _create_stub(ctx, java_attrs, launcher, executable, jvm_flags, main_class, coverage_main_class):
206215
java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
207216
java_executable = helper.get_java_executable(ctx, java_runtime_toolchain, launcher)
@@ -282,7 +291,7 @@ def _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jv
282291
# TODO(b/295221112): Change to use the "launcher" attribute (only windows use a fixed _launcher attribute)
283292
launcher_artifact = ctx.executable._launcher
284293
ctx.actions.run(
285-
executable = ctx.executable._windows_launcher_maker,
294+
executable = _find_launcher_maker(ctx),
286295
inputs = [launcher_artifact],
287296
outputs = [executable],
288297
arguments = [launcher_artifact.path, launch_info, executable.path],
@@ -308,6 +317,8 @@ def make_binary_rule(implementation, *, doc, attrs, executable = False, test = F
308317
provides = [JavaInfo],
309318
toolchains = [semantics.JAVA_TOOLCHAIN] + use_cc_toolchain() + (
310319
[semantics.JAVA_RUNTIME_TOOLCHAIN] if executable or test else []
320+
) + (
321+
[_LAUNCHER_MAKER_TOOLCHAIN] if bazel_features.rules._has_launcher_maker_toolchain else []
311322
),
312323
# TODO(hvd): replace with filegroups?
313324
outputs = {
@@ -340,16 +351,18 @@ logic as the Java package of source files. For example, a source file at
340351
),
341352
"_test_support": attr.label(default = _compute_test_support),
342353
"_launcher": attr.label(
343-
cfg = "exec",
354+
cfg = "target",
344355
executable = True,
345356
default = "@bazel_tools//tools/launcher:launcher",
346357
),
358+
},
359+
{
347360
"_windows_launcher_maker": attr.label(
348361
default = "@bazel_tools//tools/launcher:launcher_maker",
349362
cfg = "exec",
350363
executable = True,
351364
),
352-
},
365+
} if not bazel_features.rules._has_launcher_maker_toolchain else {},
353366
)
354367

355368
def make_java_binary(executable):

test/java/common/rules/java_binary_tests.bzl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,45 @@ def _test_java_binary_propagates_direct_native_libraries_impl(env, target):
171171
matching.str_matches("-Djava.library.path=${JAVA_RUNFILES}/*/test_java_binary_propagates_direct_native_libraries"),
172172
)
173173

174+
def _test_java_binary_cross_compilation_to_unix(name):
175+
# A Unix platform that:
176+
# - has a JDK
177+
# - does not require a launcher
178+
# - is not supported by the default C++ toolchain
179+
util.helper_target(
180+
native.platform,
181+
name = name + "/platform",
182+
constraint_values = [
183+
"@platforms//os:linux",
184+
"@platforms//cpu:s390x",
185+
],
186+
)
187+
188+
util.helper_target(
189+
java_binary,
190+
name = name + "/bin",
191+
srcs = ["java/C.java"],
192+
main_class = "C",
193+
)
194+
195+
analysis_test(
196+
name = name,
197+
impl = _test_java_binary_cross_compilation_to_unix_impl,
198+
target = name + "/bin",
199+
config_settings = {
200+
"//command_line_option:platforms": [Label(name + "/platform")],
201+
},
202+
# Requires the launcher_maker toolchain.
203+
attr_values = {"tags": ["min_bazel_9"]},
204+
)
205+
206+
def _test_java_binary_cross_compilation_to_unix_impl(env, target):
207+
# The main assertion is that analysis succeeds, but verify the absence of a
208+
# binary launcher for good measure.
209+
executable = target[DefaultInfo].files_to_run.executable.short_path
210+
assert_action = env.expect.that_target(target).action_generating(executable)
211+
assert_action.substitutions().keys().contains("%jvm_flags%")
212+
174213
def java_binary_tests(name):
175214
test_suite(
176215
name = name,
@@ -179,5 +218,6 @@ def java_binary_tests(name):
179218
_test_stamp_conversion_does_not_override_int,
180219
_test_java_binary_attributes,
181220
_test_java_binary_propagates_direct_native_libraries,
221+
_test_java_binary_cross_compilation_to_unix,
182222
],
183223
)

0 commit comments

Comments
 (0)