aboutsummaryrefslogtreecommitdiff
path: root/rules/desugar.bzl
blob: cc54450e6fd0420c1d82c5686e4994f42a8f7ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Bazel Desugar Commands."""

def _desugar(
        ctx,
        input,
        output = None,
        classpath = None,
        bootclasspath = [],
        min_sdk_version = 0,
        library_desugaring = True,
        desugar_exec = None,
        toolchain_type = None):
    """Desugars a JAR.

    Args:
        ctx: The context.
        input: File. The jar to be desugared.
        output: File. The desugared jar.
        classpath: Depset of Files. The transitive classpath needed to resolve symbols in the input jar.
        bootclasspath: List of Files. Bootclasspaths that was used to compile the input jar with.
        min_sdk_version: Integer. The minimum targeted sdk version.
        library_desugaring: Boolean. Whether to enable core library desugaring.
        desugar_exec: File. The executable desugar file.
    """

    args = ctx.actions.args()
    args.use_param_file("@%s", use_always = True)  # Required for workers.
    args.set_param_file_format("multiline")
    args.add("--input", input)
    args.add("--output", output)
    args.add_all(classpath, before_each = "--classpath_entry")
    args.add_all(bootclasspath, before_each = "--bootclasspath_entry")

    if library_desugaring:
        if ctx.fragments.android.check_desugar_deps:
            args.add("--emit_dependency_metadata_as_needed")

        if ctx.fragments.android.desugar_java8_libs and library_desugaring:
            args.add("--desugar_supported_core_libs")

    if min_sdk_version > 0:
        args.add("--min_sdk_version", str(min_sdk_version))

    ctx.actions.run(
        inputs = depset([input] + bootclasspath, transitive = [classpath]),
        outputs = [output],
        executable = desugar_exec,
        arguments = [args],
        mnemonic = "Desugar",
        progress_message = "Desugaring " + input.short_path + " for Android",
        execution_requirements = {"supports-workers": "1"},
        toolchain = toolchain_type,
    )

desugar = struct(
    desugar = _desugar,
)