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 .executable ._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,11 +291,13 @@ 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 ],
289298 use_default_shell_env = True ,
299+ toolchain = _LAUNCHER_MAKER_TOOLCHAIN_TYPE if bazel_features .rules ._has_launcher_maker_toolchain else None ,
300+ mnemonic = "JavaLauncherMaker" ,
290301 )
291302 return executable
292303
@@ -308,6 +319,8 @@ def make_binary_rule(implementation, *, doc, attrs, executable = False, test = F
308319 provides = [JavaInfo ],
309320 toolchains = [semantics .JAVA_TOOLCHAIN ] + use_cc_toolchain () + (
310321 [semantics .JAVA_RUNTIME_TOOLCHAIN ] if executable or test else []
322+ ) + (
323+ [_LAUNCHER_MAKER_TOOLCHAIN ] if bazel_features .rules ._has_launcher_maker_toolchain else []
311324 ),
312325 # TODO(hvd): replace with filegroups?
313326 outputs = {
@@ -340,16 +353,18 @@ logic as the Java package of source files. For example, a source file at
340353 ),
341354 "_test_support" : attr .label (default = _compute_test_support ),
342355 "_launcher" : attr .label (
343- cfg = "exec " ,
356+ cfg = "target " ,
344357 executable = True ,
345358 default = "@bazel_tools//tools/launcher:launcher" ,
346359 ),
360+ },
361+ {
347362 "_windows_launcher_maker" : attr .label (
348363 default = "@bazel_tools//tools/launcher:launcher_maker" ,
349364 cfg = "exec" ,
350365 executable = True ,
351366 ),
352- },
367+ } if not bazel_features . rules . _has_launcher_maker_toolchain else {} ,
353368)
354369
355370def make_java_binary (executable ):
0 commit comments