aboutsummaryrefslogtreecommitdiff
path: root/toolchains/kotlin_jvm/kt_jvm_toolchains.bzl
blob: 41fb769f756eab9de887d3b9f446d4218f578eef (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# 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.

"""Kotlin toolchain."""

load("//bazel:stubs.bzl", "select_java_language_level")
load("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN")

# Work around to toolchains in Google3.
KtJvmToolchainInfo = provider()

KT_VERSION = "v1_8_10"

KT_LANG_VERSION = "1.8"

# Kotlin JVM toolchain type label
_TYPE = Label("//toolchains/kotlin_jvm:kt_jvm_toolchain_type")

def _kotlinc_common_flags(ctx, other_flags):
    """Returns kotlinc flags to use in all compilations."""
    args = [
        # We're supplying JDK in bootclasspath explicitly instead
        "-no-jdk",

        # stdlib included in merged_deps
        "-no-stdlib",

        # The bytecode format to emit
        "-jvm-target",
        ctx.attr.jvm_target,

        # Emit bytecode with parameter names
        "-java-parameters",

        # Allow default method declarations, akin to what javac emits (b/110378321).
        "-Xjvm-default=all",

        # Trust JSR305 nullness type qualifier nicknames the same as @Nonnull/@Nullable
        # (see https://kotlinlang.org/docs/reference/java-interop.html#jsr-305-support)
        "-Xjsr305=strict",

        # Trust go/JSpecify nullness annotations
        # (see https://kotlinlang.org/docs/whatsnew1520.html#support-for-jspecify-nullness-annotations)
        "-Xjspecify-annotations=strict",

        # Trust annotations on type arguments, etc.
        # (see https://kotlinlang.org/docs/java-interop.html#annotating-type-arguments-and-type-parameters)
        "-Xtype-enhancement-improvements-strict-mode",

        # TODO: Remove this as the default setting (probably Kotlin 1.7)
        "-Xenhance-type-parameter-types-to-def-not-null=true",

        # Explicitly set language version so we can update compiler separate from language version
        "-language-version",
        ctx.attr.kotlin_language_version,

        # Enable type annotations in the JVM bytecode (b/170647926)
        "-Xemit-jvm-type-annotations",

        # TODO: Temporarily disable 1.5's sam wrapper conversion
        "-Xsam-conversions=class",

        # We don't want people to use experimental APIs, but if they do, we want them to use @OptIn
        "-opt-in=kotlin.RequiresOptIn",

        # Don't complain when using old builds or release candidate builds
        "-Xskip-prerelease-check",

        # Allows a no source files to create an empty jar.
        "-Xallow-no-source-files",

        # TODO: Remove this flag
        "-Xuse-old-innerclasses-logic",

        # TODO: Remove this flag
        "-Xno-source-debug-extension",
    ] + other_flags

    # --define=extra_kt_jvm_opts is for overriding from command line.
    # (Last wins in repeated --define=foo= use, so use --define=bar= instead.)
    extra_kt_jvm_opts = ctx.var.get("extra_kt_jvm_opts", default = None)
    if extra_kt_jvm_opts:
        args.extend([o for o in extra_kt_jvm_opts.split(" ") if o])
    return args

def _kotlinc_ide_flags(ctx):
    return _kotlinc_common_flags(ctx, other_flags = [])

def _kotlinc_cli_flags(ctx):
    return _kotlinc_common_flags(ctx, other_flags = [
        # Silence all warning-level diagnostics
        "-nowarn",
    ])

def _kt_jvm_toolchain_impl(ctx):
    kt_jvm_toolchain = dict(
        android_java8_apis_desugared = ctx.attr.android_java8_apis_desugared,
        android_lint_config = ctx.file.android_lint_config,
        android_lint_runner = ctx.attr.android_lint_runner[DefaultInfo].files_to_run,
        build_marker = ctx.file.build_marker,
        coverage_instrumenter = ctx.attr.coverage_instrumenter[DefaultInfo].files_to_run,
        # Don't require JavaInfo provider for integration test convenience.
        coverage_runtime = ctx.attr.coverage_runtime[JavaInfo] if JavaInfo in ctx.attr.coverage_runtime else None,
        genclass = ctx.file.genclass,
        header_gen_tool = ctx.attr.header_gen_tool[DefaultInfo].files_to_run if ctx.attr.header_gen_tool else None,
        jar_tool = ctx.attr.jar_tool[DefaultInfo].files_to_run,
        java_language_version = ctx.attr.java_language_version,
        java_runtime = ctx.attr.java_runtime,
        jvm_abi_gen_plugin = ctx.file.jvm_abi_gen_plugin,
        jvm_target = ctx.attr.jvm_target,
        kotlin_annotation_processing = ctx.file.kotlin_annotation_processing,
        kotlin_compiler = ctx.attr.kotlin_compiler[DefaultInfo].files_to_run,
        kotlin_language_version = ctx.attr.kotlin_language_version,
        kotlin_libs = [JavaInfo(compile_jar = jar, output_jar = jar) for jar in ctx.files.kotlin_libs],
        kotlin_sdk_libraries = ctx.attr.kotlin_sdk_libraries,
        kotlinc_cli_flags = _kotlinc_cli_flags(ctx),
        kotlinc_ide_flags = _kotlinc_ide_flags(ctx),
        proguard_whitelister = ctx.attr.proguard_whitelister[DefaultInfo].files_to_run,
        source_jar_zipper = ctx.file.source_jar_zipper,
        turbine = ctx.file.turbine,
        turbine_direct = ctx.file.turbine_direct if ctx.attr.enable_turbine_direct else None,
        turbine_jsa = ctx.file.turbine_jsa,
        turbine_java_runtime = ctx.attr.turbine_java_runtime,
    )
    return [
        platform_common.ToolchainInfo(**kt_jvm_toolchain),
        KtJvmToolchainInfo(**kt_jvm_toolchain),
    ]

kt_jvm_toolchain = rule(
    attrs = dict(
        android_java8_apis_desugared = attr.bool(
            # Reflects a select in build rules.
            doc = "Whether Java 8 API desugaring is enabled",
            mandatory = True,
        ),
        android_lint_config = attr.label(
            cfg = "exec",
            allow_single_file = [".xml"],
        ),
        android_lint_runner = attr.label(
            default = "//bazel:stub_tool",
            executable = True,
            cfg = "exec",
        ),
        build_marker = attr.label(
            default = "//tools:build_marker",
            allow_single_file = [".jar"],
        ),
        coverage_instrumenter = attr.label(
            default = "//tools/coverage:offline_instrument",
            cfg = "exec",
            executable = True,
        ),
        coverage_runtime = attr.label(
            default = "@maven//:org_jacoco_org_jacoco_agent",
        ),
        enable_turbine_direct = attr.bool(
            # If disabled, the value of turbine_direct will be ignored.
            # Starlark doesn't allow None to override default-valued attributes:
            default = True,
        ),
        genclass = attr.label(
            default = "@bazel_tools//tools/jdk:GenClass_deploy.jar",
            cfg = "exec",
            allow_single_file = True,
        ),
        header_gen_tool = attr.label(
            executable = True,
            allow_single_file = True,
            cfg = "exec",
        ),
        jar_tool = attr.label(
            default = "@bazel_tools//tools/jdk:jar",
            executable = True,
            allow_files = True,
            cfg = "exec",
        ),
        java_language_version = attr.string(
            default = "11",
        ),
        java_runtime = attr.label(
            default = "@bazel_tools//tools/jdk:current_java_runtime",
            cfg = "exec",
            allow_files = True,
        ),
        jvm_abi_gen_plugin = attr.label(
            default = "@kotlinc//:jvm_abi_gen_plugin",
            cfg = "exec",
            allow_single_file = [".jar"],
        ),
        jvm_target = attr.string(
            doc = "The value to pass as -jvm-target, indicating the bytecode format to emit.",
        ),
        kotlin_annotation_processing = attr.label(
            default = "@kotlinc//:kotlin_annotation_processing",
            cfg = "exec",
            allow_single_file = True,
        ),
        kotlin_compiler = attr.label(
            default = "@kotlinc//:kotlin_compiler",
            cfg = "exec",
            executable = True,
        ),
        kotlin_language_version = attr.string(
            default = KT_LANG_VERSION,
        ),
        kotlin_libs = attr.label_list(
            doc = "The libraries required during all Kotlin builds.",
            default = [
                "@kotlinc//:kotlin_stdlib",
                "@kotlinc//:annotations",
            ],
            allow_files = [".jar"],
            cfg = "target",
        ),
        kotlin_sdk_libraries = attr.label_list(
            doc = "The libraries required to resolve Kotlin code in an IDE.",
            default = [
                "@kotlinc//:kotlin_reflect",
                "@kotlinc//:kotlin_stdlib",
                "@kotlinc//:kotlin_test_not_testonly",
            ],
            cfg = "target",
        ),
        proguard_whitelister = attr.label(
            default = "@bazel_tools//tools/jdk:proguard_whitelister",
            cfg = "exec",
        ),
        runtime = attr.label_list(
            # This attribute has a "magic" name recognized by the native DexArchiveAspect
            # (b/78647825). Must list all implicit runtime deps here, this is not limited
            # to Kotlin runtime libs.
            default = [
                "@kotlinc//:kotlin_stdlib",
                "@kotlinc//:annotations",
            ],
            cfg = "target",
            doc = "The Kotlin runtime libraries grouped into one attribute.",
        ),
        source_jar_zipper = attr.label(
            default = "//tools/bin:source_jar_zipper_binary",
            cfg = "exec",
            allow_single_file = [".jar"],
        ),
        turbine = attr.label(
            default = "@bazel_tools//tools/jdk:turbine_direct",
            cfg = "exec",
            allow_single_file = True,
        ),
        turbine_direct = attr.label(
            executable = True,
            cfg = "exec",
            allow_single_file = True,
        ),
        turbine_jsa = attr.label(
            cfg = "exec",
            allow_single_file = True,
        ),
        turbine_java_runtime = attr.label(
            cfg = "exec",
        ),
    ),
    provides = [platform_common.ToolchainInfo],
    implementation = _kt_jvm_toolchain_impl,
)

def _declare(**kwargs):
    kt_jvm_toolchain(
        android_java8_apis_desugared = select({
            "//conditions:default": False,
        }),
        # The JVM bytecode version to output
        # https://kotlinlang.org/docs/compiler-reference.html#jvm-target-version
        jvm_target = "11",
        **kwargs
    )

_ATTRS = dict(
    _toolchain = attr.label(
        # TODO: Delete this attr when fixed.
        doc = "Magic attribute name for DexArchiveAspect (b/78647825)",
        default = "//toolchains/kotlin_jvm:kt_jvm_toolchain_impl",
    ),
)

kt_jvm_toolchains = struct(
    name = _TYPE.name,
    get = lambda ctx: ctx.toolchains[_TYPE],
    type = str(_TYPE),
    declare = _declare,
    attrs = _ATTRS,
)