xref: /aosp_15_r20/external/bazelbuild-rules_java/toolchains/remote_java_repository.bzl (revision abe8e1b943c923005d847f1e3cf6637de4ed1a1f)
1# Copyright 2020 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"""Rules for importing JDKs from http archive.
16
17Rule remote_java_repository imports a JDK and creates toolchain definitions for it.
18"""
19
20load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
21load("//toolchains:jdk_build_file.bzl", "JDK_BUILD_TEMPLATE")
22
23def _toolchain_config_impl(ctx):
24    ctx.file("WORKSPACE", "workspace(name = \"{name}\")\n".format(name = ctx.name))
25    ctx.file("BUILD.bazel", ctx.attr.build_file)
26
27_toolchain_config = repository_rule(
28    local = True,
29    implementation = _toolchain_config_impl,
30    attrs = {
31        "build_file": attr.string(),
32    },
33)
34
35def remote_java_repository(name, version, target_compatible_with = None, prefix = "remotejdk", **kwargs):
36    """Imports a JDK from a http archive and creates runtime toolchain definitions for it.
37
38    Register the toolchains defined by this macro via `register_toolchains("@<name>//:all")`, where
39    `<name>` is the value of the `name` parameter.
40
41    Toolchain resolution is determined with target_compatible_with
42    parameter and constrained with --java_runtime_version flag either having value
43    of "version" or "{prefix}_{version}" parameters.
44
45    Args:
46      name: A unique name for this rule.
47      version: Version of the JDK imported.
48      target_compatible_with: Target platform constraints (CPU and OS) for this JDK.
49      prefix: Optional alternative prefix for configuration flag value used to determine this JDK.
50      **kwargs: Refer to http_archive documentation
51    """
52    http_archive(
53        name = name,
54        build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = version),
55        **kwargs
56    )
57    _toolchain_config(
58        name = name + "_toolchain_config_repo",
59        build_file = """
60config_setting(
61    name = "prefix_version_setting",
62    values = {{"java_runtime_version": "{prefix}_{version}"}},
63    visibility = ["//visibility:private"],
64)
65config_setting(
66    name = "version_setting",
67    values = {{"java_runtime_version": "{version}"}},
68    visibility = ["//visibility:private"],
69)
70alias(
71    name = "version_or_prefix_version_setting",
72    actual = select({{
73        ":version_setting": ":version_setting",
74        "//conditions:default": ":prefix_version_setting",
75    }}),
76    visibility = ["//visibility:private"],
77)
78toolchain(
79    name = "toolchain",
80    target_compatible_with = {target_compatible_with},
81    target_settings = [":version_or_prefix_version_setting"],
82    toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type",
83    toolchain = "{toolchain}",
84)
85toolchain(
86    name = "bootstrap_runtime_toolchain",
87    # These constraints are not required for correctness, but prevent fetches of remote JDK for
88    # different architectures. As every Java compilation toolchain depends on a bootstrap runtime in
89    # the same configuration, this constraint will not result in toolchain resolution failures.
90    exec_compatible_with = {target_compatible_with},
91    target_settings = [":version_or_prefix_version_setting"],
92    toolchain_type = "@bazel_tools//tools/jdk:bootstrap_runtime_toolchain_type",
93    toolchain = "{toolchain}",
94)
95""".format(
96            prefix = prefix,
97            version = version,
98            target_compatible_with = target_compatible_with,
99            toolchain = "@{repo}//:jdk".format(repo = name),
100        ),
101    )
102