blob: ddf6d3fca092e706b52a9fadcccc18dae3551bda [file] [log] [blame]
cushon5a5149b2019-02-12 22:30:34 -08001# 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
cushon5a5149b2019-02-12 22:30:34 -080017def _java_runtime_alias(ctx):
18 """An experimental implementation of java_runtime_alias using toolchain resolution."""
Ivo Liste1c404e2021-08-18 05:14:32 -070019 toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
20 toolchain = toolchain_info.java_runtime
jcaterd3da9aa2021-04-08 06:12:49 -070021 return [
22 toolchain_info,
cushon5a5149b2019-02-12 22:30:34 -080023 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"],
jcatercbd59382021-03-15 05:52:53 -070038 incompatible_use_toolchain_transition = True,
cushon5a5149b2019-02-12 22:30:34 -080039)
40
41def _java_host_runtime_alias(ctx):
42 """An experimental implementation of java_host_runtime_alias using toolchain resolution."""
43 runtime = ctx.attr._runtime
jcater30041cc2021-03-25 05:33:50 -070044 java_runtime = runtime[java_common.JavaRuntimeInfo]
45 template_variable_info = runtime[platform_common.TemplateVariableInfo]
46 toolchain_info = platform_common.ToolchainInfo(java_runtime = java_runtime)
cushon5a5149b2019-02-12 22:30:34 -080047 return [
jcater30041cc2021-03-25 05:33:50 -070048 java_runtime,
49 template_variable_info,
50 toolchain_info,
Fabian Meumertzheim18285632021-10-25 02:22:31 -070051 runtime[DefaultInfo],
cushon5a5149b2019-02-12 22:30:34 -080052 ]
53
54java_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 },
jcater30041cc2021-03-25 05:33:50 -070066 provides = [
67 java_common.JavaRuntimeInfo,
68 platform_common.TemplateVariableInfo,
69 platform_common.ToolchainInfo,
70 ],
cushon5a5149b2019-02-12 22:30:34 -080071)
72
ilistd14fcf42020-12-14 05:15:06 -080073def _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(
Ivo Liste1c404e2021-08-18 05:14:32 -070083 implementation = _java_runtime_alias,
ilistd14fcf42020-12-14 05:15:06 -080084 toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"],
jcatercbd59382021-03-15 05:52:53 -070085 incompatible_use_toolchain_transition = True,
ilistd14fcf42020-12-14 05:15:06 -080086 attrs = {
87 "runtime_version": attr.string(mandatory = True),
ilistd14fcf42020-12-14 05:15:06 -080088 "_allowlist_function_transition": attr.label(
89 default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
90 ),
91 },
92 cfg = _java_runtime_transition,
93)
94
cushon5a5149b2019-02-12 22:30:34 -080095def _java_toolchain_alias(ctx):
96 """An experimental implementation of java_toolchain_alias using toolchain resolution."""
Ivo Liste1c404e2021-08-18 05:14:32 -070097 toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"]
98 toolchain = toolchain_info.java
cushon5a430412019-02-16 19:24:48 -080099 return struct(
jcaterd3da9aa2021-04-08 06:12:49 -0700100 providers = [
101 toolchain_info,
102 toolchain,
103 ],
cushon5a430412019-02-16 19:24:48 -0800104 # Use the legacy provider syntax for compatibility with the native rules.
105 java_toolchain = toolchain,
106 )
cushon5a5149b2019-02-12 22:30:34 -0800107
108java_toolchain_alias = rule(
109 implementation = _java_toolchain_alias,
110 toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
jcatered3bcf22020-10-21 11:21:59 -0700111 incompatible_use_toolchain_transition = True,
cushon5a5149b2019-02-12 22:30:34 -0800112)