xref: /aosp_15_r20/build/bazel/rules/java/BUILD (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1"""
2Copyright (C) 2023 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17load("@bazel_skylib//rules:common_settings.bzl", "string_setting")
18load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "DEFAULT_JAVACOPTS", "default_java_toolchain")
19load("@soong_injection//java_toolchain:constants.bzl", "constants")
20load("//build/bazel/rules/java/errorprone:errorprone.bzl", "errorprone_global_flags")
21load(":bootclasspath.bzl", "bootclasspath")
22load(":bootclasspath_test.bzl", "bootclasspath_test_suite")
23load(":host_for_device_test.bzl", "host_for_device_test_suite")
24load(":java_aconfig_library_test.bzl", "java_aconfig_library_rt_test_suite", "java_aconfig_library_test_suite")
25load(":java_resources_test.bzl", "java_resources_test_suite")
26load(":java_sysprop_library_test.bzl", "java_sysprop_library_test_suite")
27load(":java_system_modules_test.bzl", "java_system_modules_test_suite")
28load(":java_xsd_config_library_test.bzl", "java_xsd_config_library_test_suite")
29load(":library_test.bzl", "java_library_test_suite")
30load(":merged_txts_test.bzl", "merged_txts_test_suite")
31load(":platform_compat_config_test.bzl", "platform_compat_config_test_suite")
32load(":sdk_library_test.bzl", "java_sdk_library_test_suite")
33load(":sdk_transition_test.bzl", "sdk_transition_test_suite")
34load(":versions.bzl", "java_versions")
35load(":versions_test.bzl", "versions_test_suite")
36
37package(
38    default_visibility = ["//visibility:public"],
39)
40
41java_sdk_library_test_suite(name = "java_sdk_library_tests")
42
43merged_txts_test_suite(name = "merged_txts_tests")
44
45java_system_modules_test_suite(name = "java_system_modules_tests")
46
47java_library_test_suite(name = "java_library_tests")
48
49bootclasspath_test_suite(name = "bootclasspath_tests")
50
51java_resources_test_suite(name = "java_resources_test_suite")
52
53versions_test_suite(name = "versions_tests")
54
55sdk_transition_test_suite(name = "sdk_transition_tests")
56
57host_for_device_test_suite(name = "host_for_device_test_suite")
58
59java_xsd_config_library_test_suite(name = "java_xsd_config_library_tests")
60
61platform_compat_config_test_suite(name = "platform_compat_config_tests")
62
63java_aconfig_library_test_suite(name = "java_aconfig_library_test")
64
65java_aconfig_library_rt_test_suite(name = "java_aconfig_library_rt_test")
66
67java_sysprop_library_test_suite(name = "java_sysprop_library_tests")
68
69string_setting(
70    name = "version",
71    build_setting_default = str(java_versions.get_version()),
72    values = [str(v) for v in java_versions.ALL_VERSIONS],
73)
74
75[
76    config_setting(
77        name = setting,
78        flag_values = {
79            "//build/bazel/rules/java:version": str(java_version),
80        },
81    )
82    for java_version, setting in java_versions.VERSION_TO_CONFIG_SETTING.items()
83]
84
85# There is no need for both host and device java version build settings in a
86# world where every java_*/android_*/kt_* target uses the AOSP-specific
87# wrappers. However, there are targets defined by BUILD.tools files within the
88# Bazel binary that do not use the wrapper. These would inherit their java
89# version from their reverse dependency, which can cause build failures (e.g. an
90# android_library_import with java_version=7 has a tools dependency on a
91# non-wrapped Bazel java_library that uses lambdas). By using a separate host
92# version, we can reset it to its default when in the device configuration, so
93# that a subsequent exec transition will use the default java version.
94string_setting(
95    name = "host_version",
96    build_setting_default = str(java_versions.get_version()),
97    values = [str(v) for v in java_versions.ALL_VERSIONS],
98)
99
100[
101    config_setting(
102        name = "host_" + setting,
103        flag_values = {
104            "//build/bazel/rules/java:host_version": str(java_version),
105        },
106    )
107    for java_version, setting in java_versions.VERSION_TO_CONFIG_SETTING.items()
108]
109
110java_version_select_dict = {
111    "host_" + setting: str(version)
112    for version, setting in java_versions.VERSION_TO_CONFIG_SETTING.items()
113} | {
114    "//conditions:default": str(java_versions.get_version()),
115}
116
117# Give host-side tools a version of OpenJDK's standard libraries
118# close to what they're targeting. As of Dec 2017, AOSP is only
119# bundling OpenJDK 8 and 9, so nothing < 8 is available.
120#
121# When building with OpenJDK 8, the following should have no
122# effect since those jars would be available by default.
123#
124# When building with OpenJDK 9 but targeting a version < 1.8,
125# putting them on the bootclasspath means that:
126# a) code can't (accidentally) refer to OpenJDK 9 specific APIs
127# b) references to existing APIs are not reinterpreted in an
128#    OpenJDK 9-specific way, eg. calls to subclasses of
129#    java.nio.Buffer as in http://b/70862583
130
131bootclasspath(
132    name = "pre_java9_bootclasspath",
133    auxiliary = ["//prebuilts/jdk/jdk8:pre_java9_bootclasspath_jars"],
134    bootclasspath = ["//prebuilts/jdk/jdk8:pre_java9_bootclasspath_jars"],
135    system = None,
136)
137
138default_java_toolchain(
139    name = "jdk21_host_toolchain_java",
140    bootclasspath = select({
141        "host_config_setting_java_7": [":pre_java9_bootclasspath"],
142        "host_config_setting_java_8": [":pre_java9_bootclasspath"],
143        "//conditions:default": ["@rules_java_builtin//toolchains:platformclasspath"],
144    }),
145    # TODO(b/218720643): Support switching between multiple JDKs.
146    java_runtime = "//prebuilts/jdk/jdk21:jdk21_runtime",
147    misc = errorprone_global_flags + DEFAULT_JAVACOPTS + constants.CommonJdkFlags,
148    source_version = select(java_version_select_dict),
149    target_version = select(java_version_select_dict),
150    toolchain_definition = False,
151)
152
153toolchain(
154    name = "jdk21_host_toolchain_java_definition",
155    exec_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
156    target_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
157    target_settings = [],
158    toolchain = ":jdk21_host_toolchain_java",
159    toolchain_type = "@bazel_tools//tools/jdk:toolchain_type",
160)
161