xref: /aosp_15_r20/build/bazel/rules/java/versions.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2023 The Android Open Source Project
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"""Constants and utility functions relating to Java versions and how they map to SDK versions.
15"""
16
17load("//build/bazel/rules/common:api.bzl", "api")
18
19# The default java version used absent any java_version or sdk_version specification.
20_DEFAULT_VERSION = 17
21
22# All available java versions
23_ALL_VERSIONS = [
24    7,
25    8,
26    9,
27    11,
28    17,
29]
30
31_VERSION_TO_CONFIG_SETTING = {
32    java_version: "config_setting_java_%s" % java_version
33    for java_version in _ALL_VERSIONS
34}
35
36def _compatible_versions_for_api_level(api_level):
37    """Returns all possible java versions that can be used at the given api level."""
38    if api_level in (api.FUTURE_API_LEVEL, api.NONE_API_LEVEL):
39        return _ALL_VERSIONS
40    if api_level <= 23:
41        return [7]
42    if api_level <= 29:
43        return [
44            7,
45            8,
46        ]
47    if api_level <= 31:
48        return [
49            7,
50            8,
51            9,
52        ]
53    if api_level <= 33:
54        return [
55            7,
56            8,
57            9,
58            11,
59        ]
60    return _ALL_VERSIONS
61
62def _supports_pre_java_9(api_level):
63    return any([
64        version < 9
65        for version in _compatible_versions_for_api_level(api_level)
66    ])
67
68def _supports_post_java_9(api_level):
69    return any([
70        version >= 9
71        for version in _compatible_versions_for_api_level(api_level)
72    ])
73
74_NORMALIZED_VERSIONS = {
75    "1.7": 7,
76    "7": 7,
77    "1.8": 8,
78    "8": 8,
79    "1.9": 9,
80    "9": 9,
81    "11": 11,
82    "17": 17,
83}
84
85def _default_version(api_level):
86    """Returns the default java version for the input api level."""
87    return max(_compatible_versions_for_api_level(api_level))
88
89def _get_version(java_version = None, api_level = None):
90    """Returns the java version to use for a given target based on the java_version set by this target and the api_level_string extracted from sdk_version."""
91    if java_version:
92        return _NORMALIZED_VERSIONS[java_version]
93    elif api_level:
94        return _default_version(api_level)
95    return _DEFAULT_VERSION
96
97def _kt_jvm_version_from_normalized_java_version(java_version):
98    if java_version == 7:
99        return "1.6"
100    elif java_version == 8:
101        return "1.8"
102    else:
103        return str(java_version)
104
105java_versions = struct(
106    ALL_VERSIONS = _ALL_VERSIONS,
107    VERSION_TO_CONFIG_SETTING = _VERSION_TO_CONFIG_SETTING,
108    compatible_versions_for_api_level = _compatible_versions_for_api_level,
109    get_version = _get_version,
110    kt_jvm_version_from_normalized_java_version = _kt_jvm_version_from_normalized_java_version,
111    supports_pre_java_9 = _supports_pre_java_9,
112    supports_post_java_9 = _supports_post_java_9,
113)
114