xref: /aosp_15_r20/external/bazelbuild-rules_java/toolchains/java_toolchain_alias.bzl (revision abe8e1b943c923005d847f1e3cf6637de4ed1a1f)
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
17load("//java/common:java_common.bzl", "java_common")
18
19def _java_runtime_alias(ctx):
20    """An experimental implementation of java_runtime_alias using toolchain resolution."""
21    toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
22    toolchain = toolchain_info.java_runtime
23    return [
24        toolchain_info,
25        toolchain,
26        platform_common.TemplateVariableInfo({
27            "JAVA": str(toolchain.java_executable_exec_path),
28            "JAVABASE": str(toolchain.java_home),
29        }),
30        # See b/65239471 for related discussion of handling toolchain runfiles/data.
31        DefaultInfo(
32            runfiles = ctx.runfiles(transitive_files = toolchain.files),
33            files = toolchain.files,
34        ),
35    ]
36
37java_runtime_alias = rule(
38    implementation = _java_runtime_alias,
39    toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"],
40)
41
42def _java_host_runtime_alias(ctx):
43    """An experimental implementation of java_host_runtime_alias using toolchain resolution."""
44    runtime = ctx.attr._runtime
45    java_runtime = runtime[java_common.JavaRuntimeInfo]
46    template_variable_info = runtime[platform_common.TemplateVariableInfo]
47    toolchain_info = platform_common.ToolchainInfo(java_runtime = java_runtime)
48    return [
49        java_runtime,
50        template_variable_info,
51        toolchain_info,
52        runtime[DefaultInfo],
53    ]
54
55java_host_runtime_alias = rule(
56    implementation = _java_host_runtime_alias,
57    attrs = {
58        "_runtime": attr.label(
59            default = Label("//toolchains:current_java_runtime"),
60            providers = [
61                java_common.JavaRuntimeInfo,
62                platform_common.TemplateVariableInfo,
63            ],
64            cfg = "exec",
65        ),
66    },
67    provides = [
68        java_common.JavaRuntimeInfo,
69        platform_common.TemplateVariableInfo,
70        platform_common.ToolchainInfo,
71    ],
72)
73
74def _java_runtime_transition_impl(_settings, attr):
75    return {"//command_line_option:java_runtime_version": attr.runtime_version}
76
77_java_runtime_transition = transition(
78    implementation = _java_runtime_transition_impl,
79    inputs = [],
80    outputs = ["//command_line_option:java_runtime_version"],
81)
82
83java_runtime_version_alias = rule(
84    implementation = _java_runtime_alias,
85    toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"],
86    attrs = {
87        "runtime_version": attr.string(mandatory = True),
88    },
89    cfg = _java_runtime_transition,
90)
91
92def _java_toolchain_alias(ctx):
93    """An experimental implementation of java_toolchain_alias using toolchain resolution."""
94    toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"]
95    toolchain = toolchain_info.java
96
97    return [
98        toolchain_info,
99        toolchain,
100    ]
101
102java_toolchain_alias = rule(
103    implementation = _java_toolchain_alias,
104    toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
105)
106