cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 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 | |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 17 | def _java_runtime_alias(ctx): |
| 18 | """An experimental implementation of java_runtime_alias using toolchain resolution.""" |
Ivo List | e1c404e | 2021-08-18 05:14:32 -0700 | [diff] [blame] | 19 | toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"] |
| 20 | toolchain = toolchain_info.java_runtime |
jcater | d3da9aa | 2021-04-08 06:12:49 -0700 | [diff] [blame] | 21 | return [ |
| 22 | toolchain_info, |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 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 | |
| 35 | java_runtime_alias = rule( |
| 36 | implementation = _java_runtime_alias, |
| 37 | toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"], |
jcater | cbd5938 | 2021-03-15 05:52:53 -0700 | [diff] [blame] | 38 | incompatible_use_toolchain_transition = True, |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | def _java_host_runtime_alias(ctx): |
| 42 | """An experimental implementation of java_host_runtime_alias using toolchain resolution.""" |
| 43 | runtime = ctx.attr._runtime |
jcater | 30041cc | 2021-03-25 05:33:50 -0700 | [diff] [blame] | 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) |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 47 | return [ |
jcater | 30041cc | 2021-03-25 05:33:50 -0700 | [diff] [blame] | 48 | java_runtime, |
| 49 | template_variable_info, |
| 50 | toolchain_info, |
Fabian Meumertzheim | 1828563 | 2021-10-25 02:22:31 -0700 | [diff] [blame] | 51 | runtime[DefaultInfo], |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 52 | ] |
| 53 | |
| 54 | java_host_runtime_alias = rule( |
| 55 | implementation = _java_host_runtime_alias, |
| 56 | attrs = { |
| 57 | "_runtime": attr.label( |
| 58 | default = Label("@bazel_tools//tools/jdk:current_java_runtime"), |
| 59 | providers = [ |
| 60 | java_common.JavaRuntimeInfo, |
| 61 | platform_common.TemplateVariableInfo, |
| 62 | ], |
| 63 | cfg = "host", |
| 64 | ), |
| 65 | }, |
jcater | 30041cc | 2021-03-25 05:33:50 -0700 | [diff] [blame] | 66 | provides = [ |
| 67 | java_common.JavaRuntimeInfo, |
| 68 | platform_common.TemplateVariableInfo, |
| 69 | platform_common.ToolchainInfo, |
| 70 | ], |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 71 | ) |
| 72 | |
ilist | d14fcf4 | 2020-12-14 05:15:06 -0800 | [diff] [blame] | 73 | def _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 | |
| 82 | java_runtime_version_alias = rule( |
Ivo List | e1c404e | 2021-08-18 05:14:32 -0700 | [diff] [blame] | 83 | implementation = _java_runtime_alias, |
ilist | d14fcf4 | 2020-12-14 05:15:06 -0800 | [diff] [blame] | 84 | toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"], |
jcater | cbd5938 | 2021-03-15 05:52:53 -0700 | [diff] [blame] | 85 | incompatible_use_toolchain_transition = True, |
ilist | d14fcf4 | 2020-12-14 05:15:06 -0800 | [diff] [blame] | 86 | attrs = { |
| 87 | "runtime_version": attr.string(mandatory = True), |
ilist | d14fcf4 | 2020-12-14 05:15:06 -0800 | [diff] [blame] | 88 | "_allowlist_function_transition": attr.label( |
| 89 | default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| 90 | ), |
| 91 | }, |
| 92 | cfg = _java_runtime_transition, |
| 93 | ) |
| 94 | |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 95 | def _java_toolchain_alias(ctx): |
| 96 | """An experimental implementation of java_toolchain_alias using toolchain resolution.""" |
Ivo List | e1c404e | 2021-08-18 05:14:32 -0700 | [diff] [blame] | 97 | toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"] |
| 98 | toolchain = toolchain_info.java |
cushon | 5a43041 | 2019-02-16 19:24:48 -0800 | [diff] [blame] | 99 | return struct( |
jcater | d3da9aa | 2021-04-08 06:12:49 -0700 | [diff] [blame] | 100 | providers = [ |
| 101 | toolchain_info, |
| 102 | toolchain, |
| 103 | ], |
cushon | 5a43041 | 2019-02-16 19:24:48 -0800 | [diff] [blame] | 104 | # Use the legacy provider syntax for compatibility with the native rules. |
| 105 | java_toolchain = toolchain, |
| 106 | ) |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 107 | |
| 108 | java_toolchain_alias = rule( |
| 109 | implementation = _java_toolchain_alias, |
| 110 | toolchains = ["@bazel_tools//tools/jdk:toolchain_type"], |
jcater | ed3bcf2 | 2020-10-21 11:21:59 -0700 | [diff] [blame] | 111 | incompatible_use_toolchain_transition = True, |
cushon | 5a5149b | 2019-02-12 22:30:34 -0800 | [diff] [blame] | 112 | ) |