1313# limitations under the License.
1414"""Bazel java_binary rule"""
1515
16+ load ("@bazel_features//:features.bzl" , "bazel_features" )
1617load ("@bazel_skylib//lib:paths.bzl" , "paths" )
1718load ("@rules_cc//cc:find_cc_toolchain.bzl" , "use_cc_toolchain" )
1819load ("//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+
205214def _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
355368def make_java_binary (executable ):
0 commit comments