xref: /aosp_15_r20/external/bazelbuild-rules_android/rules/desugar.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
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"""Bazel Desugar Commands."""
16
17def _desugar(
18        ctx,
19        input,
20        output = None,
21        classpath = None,
22        bootclasspath = [],
23        min_sdk_version = 0,
24        library_desugaring = True,
25        desugar_exec = None,
26        toolchain_type = None):
27    """Desugars a JAR.
28
29    Args:
30        ctx: The context.
31        input: File. The jar to be desugared.
32        output: File. The desugared jar.
33        classpath: Depset of Files. The transitive classpath needed to resolve symbols in the input jar.
34        bootclasspath: List of Files. Bootclasspaths that was used to compile the input jar with.
35        min_sdk_version: Integer. The minimum targeted sdk version.
36        library_desugaring: Boolean. Whether to enable core library desugaring.
37        desugar_exec: File. The executable desugar file.
38    """
39
40    args = ctx.actions.args()
41    args.use_param_file("@%s", use_always = True)  # Required for workers.
42    args.set_param_file_format("multiline")
43    args.add("--input", input)
44    args.add("--output", output)
45    args.add_all(classpath, before_each = "--classpath_entry")
46    args.add_all(bootclasspath, before_each = "--bootclasspath_entry")
47
48    if library_desugaring:
49        if ctx.fragments.android.check_desugar_deps:
50            args.add("--emit_dependency_metadata_as_needed")
51
52        if ctx.fragments.android.desugar_java8_libs and library_desugaring:
53            args.add("--desugar_supported_core_libs")
54
55    if min_sdk_version > 0:
56        args.add("--min_sdk_version", str(min_sdk_version))
57
58    ctx.actions.run(
59        inputs = depset([input] + bootclasspath, transitive = [classpath]),
60        outputs = [output],
61        executable = desugar_exec,
62        arguments = [args],
63        mnemonic = "Desugar",
64        progress_message = "Desugaring " + input.short_path + " for Android",
65        execution_requirements = {"supports-workers": "1"},
66        toolchain = toolchain_type,
67    )
68
69desugar = struct(
70    desugar = _desugar,
71)
72