• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15load("//build/bazel/rules/java:versions.bzl", "java_versions")
16load("//build/bazel/rules/common:sdk_version.bzl", "sdk_version")
17load("//build/bazel/rules/common:api.bzl", "api")
18
19def _sdk_transition_impl(settings, attr):
20    host_platform = settings["//command_line_option:host_platform"]
21    default_java_version = str(java_versions.get_version())
22
23    # TODO: this condition should really be "platform is not a device".
24    # More details on why we're treating java version for non-device platforms differently at the
25    # definition of the //build/bazel/rules/java:host_version build setting.
26    if all([host_platform == platform for platform in settings["//command_line_option:platforms"]]):
27        return {
28            "//build/bazel/rules/java:version": default_java_version,
29            "//build/bazel/rules/java:host_version": str(java_versions.get_version(attr.java_version)),
30            "//build/bazel/rules/java/sdk:kind": sdk_version.KIND_NONE,
31            "//build/bazel/rules/java/sdk:api_level": api.NONE_API_LEVEL,
32        }
33    sdk_spec = sdk_version.sdk_spec_from(attr.sdk_version)
34    java_version = str(java_versions.get_version(attr.java_version, sdk_spec.api_level))
35
36    return {
37        "//build/bazel/rules/java:host_version": default_java_version,
38        "//build/bazel/rules/java:version": java_version,
39        "//build/bazel/rules/java/sdk:kind": sdk_spec.kind,
40        "//build/bazel/rules/java/sdk:api_level": sdk_spec.api_level,
41    }
42
43sdk_transition = transition(
44    implementation = _sdk_transition_impl,
45    inputs = [
46        "//command_line_option:host_platform",
47        "//command_line_option:platforms",
48    ],
49    outputs = [
50        "//build/bazel/rules/java:version",
51        "//build/bazel/rules/java:host_version",
52        "//build/bazel/rules/java/sdk:kind",
53        "//build/bazel/rules/java/sdk:api_level",
54    ],
55)
56
57sdk_transition_attrs = {
58    # This attribute must have a specific name to let the DexArchiveAspect propagate
59    # through it.
60    "exports": attr.label(
61        cfg = sdk_transition,
62    ),
63    "java_version": attr.string(),
64    "sdk_version": attr.string(),
65    "_allowlist_function_transition": attr.label(
66        default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
67    ),
68}
69