xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/toolchains/kotlin_jvm/kotlinc_flags.bzl (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
1# Copyright 2022 Google LLC. 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"""Static flags for kotlinc."""
16
17load("//:visibility.bzl", "RULES_KOTLIN")
18
19_KT_LANG_VERSION = "1.9"
20
21_SHARED_FLAGS = [
22    # We're supplying JDK in bootclasspath explicitly instead
23    "-no-jdk",
24
25    # stdlib included in merged_deps
26    "-no-stdlib",
27
28    # The bytecode format to emit
29    "-jvm-target",
30    "11",
31
32    # Emit bytecode with parameter names
33    "-java-parameters",
34
35    # Allow default method declarations, akin to what javac emits (b/110378321).
36    "-Xjvm-default=all",
37
38    # Trust JSR305 nullness type qualifier nicknames the same as @Nonnull/@Nullable
39    # (see https://kotlinlang.org/docs/reference/java-interop.html#jsr-305-support)
40    "-Xjsr305=strict",
41
42    # Trust annotations on type arguments, etc.
43    # (see https://kotlinlang.org/docs/java-interop.html#annotating-type-arguments-and-type-parameters)
44    "-Xtype-enhancement-improvements-strict-mode",
45
46    # TODO: Remove this as the default setting (probably Kotlin 1.7)
47    "-Xenhance-type-parameter-types-to-def-not-null=true",
48
49    # Explicitly set language version so we can update compiler separate from language version
50    "-language-version",
51    _KT_LANG_VERSION,
52
53    # Enable type annotations in the JVM bytecode (b/170647926)
54    "-Xemit-jvm-type-annotations",
55
56    # TODO: Temporarily disable 1.5's sam wrapper conversion
57    "-Xsam-conversions=class",
58
59    # We don't want people to use experimental APIs, but if they do, we want them to use @OptIn
60    "-opt-in=kotlin.RequiresOptIn",
61
62    # Don't complain when using old builds or release candidate builds
63    "-Xskip-prerelease-check",
64
65    # Allows a no source files to create an empty jar.
66    "-Xallow-no-source-files",
67
68    # TODO: Remove this flag
69    "-Xuse-old-innerclasses-logic",
70
71    # TODO: Remove this flag
72    "-Xno-source-debug-extension",
73]
74
75_CLI_ADDITIONAL_FLAGS = [
76    # Silence all warning-level diagnostics
77    "-nowarn",
78]
79
80def _read_one_define_flags(ctx, name):
81    define = ctx.var.get(name, default = None)
82    return [f for f in define.split(" ") if f] if define else []
83
84def _read_define_flags(ctx):
85    return (
86        _read_one_define_flags(ctx, "extra_kt_jvm_opts")
87    )
88
89kotlinc_flags = struct(
90    # go/keep-sorted start
91    CLI_FLAGS = _SHARED_FLAGS + _CLI_ADDITIONAL_FLAGS,
92    IDE_FLAGS = _SHARED_FLAGS,
93    KT_LANG_VERSION = _KT_LANG_VERSION,
94    read_define_flags = _read_define_flags,
95    # go/keep-sorted end
96)
97