1# Copyright 2019 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Experimental re-implementations of Java toolchain aliases using toolchain resolution.""" 16 17def _java_runtime_alias(ctx): 18 """An experimental implementation of java_runtime_alias using toolchain resolution.""" 19 toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"] 20 toolchain = toolchain_info.java_runtime 21 return [ 22 toolchain_info, 23 toolchain, 24 platform_common.TemplateVariableInfo({ 25 "JAVA": str(toolchain.java_executable_exec_path), 26 "JAVABASE": str(toolchain.java_home), 27 }), 28 # See b/65239471 for related discussion of handling toolchain runfiles/data. 29 DefaultInfo( 30 runfiles = ctx.runfiles(transitive_files = toolchain.files), 31 files = toolchain.files, 32 ), 33 ] 34 35java_runtime_alias = rule( 36 implementation = _java_runtime_alias, 37 toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"], 38 incompatible_use_toolchain_transition = True, 39) 40 41def _java_host_runtime_alias(ctx): 42 """An experimental implementation of java_host_runtime_alias using toolchain resolution.""" 43 runtime = ctx.attr._runtime 44 java_runtime = runtime[java_common.JavaRuntimeInfo] 45 template_variable_info = runtime[platform_common.TemplateVariableInfo] 46 toolchain_info = platform_common.ToolchainInfo(java_runtime = java_runtime) 47 return [ 48 java_runtime, 49 template_variable_info, 50 toolchain_info, 51 runtime[DefaultInfo], 52 ] 53 54java_host_runtime_alias = rule( 55 implementation = _java_host_runtime_alias, 56 attrs = { 57 "_runtime": attr.label( 58 default = Label("//toolchains:current_java_runtime"), 59 providers = [ 60 java_common.JavaRuntimeInfo, 61 platform_common.TemplateVariableInfo, 62 ], 63 cfg = "exec", 64 ), 65 }, 66 provides = [ 67 java_common.JavaRuntimeInfo, 68 platform_common.TemplateVariableInfo, 69 platform_common.ToolchainInfo, 70 ], 71) 72 73def _java_runtime_transition_impl(_settings, attr): 74 return {"//command_line_option:java_runtime_version": attr.runtime_version} 75 76_java_runtime_transition = transition( 77 implementation = _java_runtime_transition_impl, 78 inputs = [], 79 outputs = ["//command_line_option:java_runtime_version"], 80) 81 82java_runtime_version_alias = rule( 83 implementation = _java_runtime_alias, 84 toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"], 85 incompatible_use_toolchain_transition = True, 86 attrs = { 87 "runtime_version": attr.string(mandatory = True), 88 "_allowlist_function_transition": attr.label( 89 default = "@bazel_tools//tools/allowlists/function_transition_allowlist", 90 ), 91 }, 92 cfg = _java_runtime_transition, 93) 94 95def _java_toolchain_alias(ctx): 96 """An experimental implementation of java_toolchain_alias using toolchain resolution.""" 97 toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"] 98 toolchain = toolchain_info.java 99 100 # buildifier: disable=rule-impl-return 101 return struct( 102 providers = [ 103 toolchain_info, 104 toolchain, 105 ], 106 # Use the legacy provider syntax for compatibility with the native rules. 107 java_toolchain = toolchain, 108 ) 109 110java_toolchain_alias = rule( 111 implementation = _java_toolchain_alias, 112 toolchains = ["@bazel_tools//tools/jdk:toolchain_type"], 113 incompatible_use_toolchain_transition = True, 114) 115