aboutsummaryrefslogtreecommitdiff
path: root/kotlin/jvm/internal_do_not_use/util/srcjars.bzl
blob: becf573b09a35b7c3cfc9d2b21347661133a1515 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright 2022 Google LLC. 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.

"""kt_srcjars"""

# go/keep-sorted start
load("//:visibility.bzl", "RULES_KOTLIN")
load(":run_deploy_jar.bzl", "kt_run_deploy_jar")
# go/keep-sorted end

def _zip(
        ctx,
        kt_jvm_toolchain,
        out_jar,
        srcs = [],
        common_srcs = [],
        ignore_not_allowed_files = False):
    """Creates a srcjar from a set of Kotlin and Java srcs

    Paths inside the srcjar are derived from the package name in the source file.
    """

    args = ctx.actions.args()
    args.add("zip")
    args.add(out_jar)
    args.add_joined("--kotlin_srcs", srcs, join_with = ",")
    args.add_joined("--common_srcs", common_srcs, join_with = ",")
    if ignore_not_allowed_files:
        args.add("-i")

    kt_run_deploy_jar(
        ctx = ctx,
        java_runtime = kt_jvm_toolchain.java_runtime,
        deploy_jar = kt_jvm_toolchain.source_jar_zipper,
        inputs = srcs + common_srcs,
        outputs = [out_jar],
        args = [args],
        mnemonic = "KtJar",
        progress_message = "Create Jar (kotlin/common.bzl): %{output}",
    )

    return out_jar

def _unzip(
        ctx,
        kt_jvm_toolchain,
        dir,
        input):
    args = ctx.actions.args()
    args.add("unzip", input)
    args.add(dir.path)

    kt_run_deploy_jar(
        ctx = ctx,
        java_runtime = kt_jvm_toolchain.java_runtime,
        deploy_jar = kt_jvm_toolchain.source_jar_zipper,
        inputs = [input],
        outputs = [dir],
        args = [args],
        mnemonic = "SrcJarUnzip",
    )

    return dir

def _zip_resources(ctx, kt_jvm_toolchain, output_jar, input_dirs):
    """Packs a sequence of tree artifacts into a single jar.

    Given the following file directory structure,
        /usr/home/a/x/1.txt
        /usr/home/b/y/1.txt
    with an input_dirs as [
        "/usr/home/a",
        "/usr/home/b",
    ],
    The tool produces a jar with in-archive structure of,
        x/1.txt
        y/1.txt

    The function fails on the duplicate jar entry case. e.g. if we pass an
    input_dirs as [
        "/usr/home/a/x",
        "/usr/home/b/y",
    ],
    then the blaze action would fail with an error message.
        "java.lang.IllegalStateException: 1.txt has the same path as 1.txt!
        If it is intended behavior rename one or both of them."

    Args:
        ctx: The build rule context.
        kt_jvm_toolchain: Toolchain containing the jar tool.
        output_jar: The jar to be produced by this action.
        input_dirs: A sequence of tree artifacts to be zipped.

    Returns:
        The generated output jar, i.e. output_jar
    """

    args = ctx.actions.args()
    args.add("zip_resources")
    args.add(output_jar)
    args.add_joined(
        "--input_dirs",
        input_dirs,
        join_with = ",",
        omit_if_empty = False,
        expand_directories = False,
    )

    kt_run_deploy_jar(
        ctx = ctx,
        java_runtime = kt_jvm_toolchain.java_runtime,
        deploy_jar = kt_jvm_toolchain.source_jar_zipper,
        inputs = input_dirs,
        outputs = [output_jar],
        args = [args],
        mnemonic = "KtJarActionFromTreeArtifacts",
        progress_message = "Create Jar %{output}",
    )

    return output_jar

def _DirSrcjarSyncer(
        ctx,
        kt_jvm_toolchain,
        file_factory):
    """Synchronizes the contents of a set of srcjar files and tree-artifacts"""

    _dirs = []
    _srcjars = []

    def add_dirs(dirs):
        if not dirs:
            return

        _dirs.extend(dirs)
        _srcjars.append(
            _zip_resources(
                ctx,
                kt_jvm_toolchain,
                file_factory.declare_file("%s-codegen.srcjar" % len(_srcjars)),
                dirs,
            ),
        )

    def add_srcjars(srcjars):
        if not srcjars:
            return

        for srcjar in srcjars:
            _dirs.append(
                _unzip(
                    ctx,
                    kt_jvm_toolchain,
                    file_factory.declare_directory("%s.expand" % len(_dirs)),
                    srcjar,
                ),
            )
        _srcjars.extend(srcjars)

    return struct(
        add_dirs = add_dirs,
        add_srcjars = add_srcjars,
        dirs = _dirs,
        srcjars = _srcjars,
    )

kt_srcjars = struct(
    zip = _zip,
    unzip = _unzip,
    zip_resources = _zip_resources,
    DirSrcjarSyncer = _DirSrcjarSyncer,
)